-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
59 lines (45 loc) · 1.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use babel';
import { CompositeDisposable } from 'atom';
import {existsSync, readFileSync} from 'fs';
import {join, relative, dirname, extname} from 'path';
import {exec} from 'child_process';
const EXEC_TIMEOUT = 60 * 1000; // 1 minute
console.log("Loaded");
export default {
activate(state) {
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(atom.workspace.observeTextEditors(textEditor => {
this.subscriptions.add(textEditor.onDidSave(this.handleDidSave.bind(this)));
}));
},
deactivate() {
this.subscriptions.dispose();
},
handleDidSave(event) {
let savedFile = event.path;
this.run({savedFile});
},
run({savedFile}) {
if (extname(savedFile) !== ".java") {
return;
}
const command = "java -jar google-java-format-1.5-all-deps.jar --replace " + savedFile;
const options = {cwd: __dirname, timeout: EXEC_TIMEOUT};
exec(command, options, (err, stdout, stderr) => {
const message = 'google-java-format';
const output = stdout.trim();
if (output) {
atom.notifications.addSuccess(message, {detail: output, dismissable: true});
}
const error = stderr.trim() || (err && err.message);
if (error) {
atom.notifications.addError(message, {detail: error, dismissable: true});
}
});
atom.workspace.getActiveEditor().buffer.file.readSync(true);
atom.workspace.getActiveEditor().buffer.load();
},
about() {
console.log('GoogleJavaFormat!');
}
};