Skip to content

Commit 6d5458d

Browse files
Implement format on save
Unfortunately there doesn't seem to be a unified way to see if we're dealing with a Ruby file. Package activation is contingent on the Ruby or RoR package being loaded. The keyboard shortcut is done by CSS selectors on the file grammar. And finally the on save check is a plain JS conditional.
1 parent d3e0fc5 commit 6d5458d

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

keymaps/rubyfmt.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
".platform-darwin atom-text-editor[data-grammar='source ruby']": {
2+
".platform-darwin atom-text-editor[data-grammar^='source ruby']": {
33
"cmd-;": "rubyfmt:formatBuffer"
44
},
5-
".platform-win32 atom-text-editor[data-grammar='source ruby'], .platform-linux atom-text-editor[data-grammar='source ruby']": {
5+
".platform-win32 atom-text-editor[data-grammar^='source ruby'], .platform-linux atom-text-editor[data-grammar^='source ruby']": {
66
"alt-;": "rubyfmt:formatBuffer"
77
}
88
}

lib/rubyfmt.js

+56-11
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,73 @@ import { spawn } from 'child_process';
66
export default {
77

88
subscriptions: null,
9+
config: {
10+
formatOnSave: {
11+
order: 1,
12+
title: 'Format on save',
13+
description: "Automatically format when saving",
14+
type: 'boolean',
15+
default: false
16+
},
17+
rubyExecutable: {
18+
order: 2,
19+
type: 'string',
20+
default: 'ruby',
21+
title: "Ruby executable",
22+
description: "Can be overriden with an absolute path"
23+
},
24+
rubyfmtExecutable: {
25+
order: 3,
26+
type: 'string',
27+
default: 'rubyfmt',
28+
title: "Rubyfmt executable",
29+
description: "Can be overriden with an absolute path"
30+
},
31+
},
932

1033
activate(state) {
1134
this.subscriptions = new CompositeDisposable();
1235
this.subscriptions.add(atom.commands.add('atom-text-editor', {
13-
'rubyfmt:formatBuffer': () => this.formatBuffer()
36+
'rubyfmt:formatBuffer': () => this.handleManualFormat()
1437
}));
38+
this.subscriptions.add(atom.workspace.observeTextEditors(textEditor => {
39+
buffer = textEditor.getBuffer()
40+
this.subscriptions.add(buffer.onWillSave(() => this.handleSave(textEditor)))
41+
}))
1542
},
1643

1744
deactivate() {
1845
this.subscriptions.dispose();
1946
},
2047

21-
formatBuffer() {
22-
activePaneItem = atom.workspace.getActivePaneItem();
23-
if(activePaneItem.getText) {
24-
text = activePaneItem.getText()
25-
this.format(text).then((formattedText) => {
26-
activePaneItem.setText(formattedText);
27-
});
48+
handleManualFormat() {
49+
item = atom.workspace.getActivePaneItem()
50+
if(item.getBuffer) {
51+
this.formatEditor(item);
2852
}
2953
},
3054

55+
handleSave(textEditor) {
56+
if(this.isRuby(textEditor) && atom.config.get('rubyfmt.formatOnSave')) { this.formatEditor(textEditor) }
57+
},
58+
59+
isRuby(textEditor) {
60+
return textEditor.getRootScopeDescriptor().getScopesArray().some(scope => {
61+
return scope.startsWith("source.ruby")
62+
})
63+
},
64+
65+
formatEditor(textEditor) {
66+
return this.format(textEditor.getText()).then((formattedText) => {
67+
textEditor.setText(formattedText);
68+
});
69+
},
70+
3171
format(text) {
3272
var promise = new Promise(function(resolve, reject) {
3373
var toReturn
34-
rubyfmt = spawn('rubyfmt', [], {windowsHide: true})
74+
executable = atom.config.get('rubyfmt.rubyfmtExecutable')
75+
rubyfmt = spawn(executable, [], {windowsHide: true})
3576
rubyfmt.stdout.on('data', (data) => {
3677
toReturn = data.toString('utf8');
3778
});
@@ -42,8 +83,12 @@ export default {
4283

4384
rubyfmt.stdin.write(text);
4485
rubyfmt.stdin.end();
45-
rubyfmt.on('close', () => {
46-
resolve(toReturn);
86+
rubyfmt.on('close', (status) => {
87+
if(status == 0) {
88+
resolve(toReturn);
89+
} else {
90+
reject(status)
91+
}
4792
});
4893
});
4994
return promise;

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"name": "rubyfmt",
33
"main": "./lib/rubyfmt",
4-
"version": "0.0.1",
4+
"version": "0.0.3",
55
"description": "Atom package to autoformat Ruby code with Rubyfmt",
66
"keywords": [
77
"formatter",
8+
9+
810
"format",
911
"ruby",
1012
"rubyfmt"
1113
],
12-
"activationCommands": {
13-
"atom-workspace": "rubyfmt:formatBuffer"
14-
},
14+
"activationHooks": ["language-ruby:grammar-used", "language-ruby-on-rails:grammar-used"],
1515
"repository": "https://github.com/toreriklinnerud/atom-rubyfmt",
1616
"license": "MIT",
1717
"engines": {

0 commit comments

Comments
 (0)