Skip to content

Commit 58a375f

Browse files
committed
Fixes #16. Custom configuration options are now working correctly.
Applied in order of precedence: - Default - User Home .jsbeautifyrc - Closest .jsbeautify to the current file, see #15
1 parent 4d48679 commit 58a375f

File tree

1 file changed

+66
-44
lines changed

1 file changed

+66
-44
lines changed

lib/atom-beautify.js

+66-44
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,13 @@ function findConfig(config, file) {
148148

149149
function beautify() {
150150

151-
console.log('Beautify!!!');
152-
153151
var text;
154152
var editor = atom.workspace.getActiveEditor();
155153
var isSelection = !! editor.getSelectedText();
156154
var softTabs = editor.softTabs;
157155
var tabLength = editor.getTabLength();
158156

159-
var beautifyOptions = {
157+
var defaultOptions = {
160158
'indent_size': softTabs ? tabLength : 1,
161159
'indent_char': softTabs ? ' ' : '\t',
162160
'indent_with_tabs': !softTabs
@@ -165,45 +163,29 @@ function beautify() {
165163
// Look for .jsbeautifierrc in file and home path, check env variables
166164
var editedFilePath = editor.getPath();
167165

168-
// Get the path to the config file
169-
var configPath = findConfig('.jsbeautifyrc', editedFilePath);
170-
171-
var externalOptions;
172-
if (configPath) {
173-
var strip = require('strip-json-comments');
174-
try {
175-
externalOptions = JSON.parse(strip(fs.readFileSync(configPath, {
176-
encoding: 'utf8'
177-
})));
178-
} catch (e) {
166+
function getConfig(startPath) {
167+
// Get the path to the config file
168+
var configPath = findConfig('.jsbeautifyrc', startPath);
169+
170+
var externalOptions;
171+
if (configPath) {
172+
var strip = require('strip-json-comments');
173+
try {
174+
externalOptions = JSON.parse(strip(fs.readFileSync(configPath, {
175+
encoding: 'utf8'
176+
})));
177+
} catch (e) {
178+
externalOptions = {};
179+
}
180+
} else {
179181
externalOptions = {};
180182
}
181-
} else {
182-
externalOptions = {};
183-
}
184-
185-
var containsNested = false;
186-
var collectedConfig = {};
187-
var key;
188-
189-
// Check to see if config file uses nested object format to split up js/css/html options
190-
for (key in externalOptions) {
191-
if (typeof externalOptions[key] === 'object') {
192-
containsNested = true;
193-
}
194-
}
195-
196-
// Create a flat object of config options if nested format was used
197-
if (!containsNested) {
198-
collectedConfig = externalOptions;
199-
} else {
200-
for (key in externalOptions) {
201-
_.merge(collectedConfig, externalOptions[key]);
202-
}
183+
return externalOptions;
203184
}
204185

205-
beautifyOptions = extend(collectedConfig, beautifyOptions);
206-
beautifyOptions = cleanOptions(beautifyOptions, knownOpts);
186+
// Get the path to the config file
187+
var projectOptions = getConfig(editedFilePath);
188+
var homeOptions = getConfig(getUserHome());
207189

208190
if (isSelection) {
209191
text = editor.getSelectedText();
@@ -212,17 +194,57 @@ function beautify() {
212194
}
213195
var oldText = text;
214196

197+
// All of the options
198+
// Listed in order from default (base) to the one with the highest priority
199+
// Left = Default, Right = Will override the left.
200+
var allOptions = [defaultOptions, homeOptions, projectOptions];
201+
202+
function getOptions(selection, allOptions) {
203+
204+
// Reduce all options into correctly merged options.
205+
var options = _.reduce(allOptions, function(result, currOptions) {
206+
207+
var containsNested = false;
208+
var collectedConfig = {};
209+
var key;
210+
211+
// Check to see if config file uses nested object format to split up js/css/html options
212+
for (key in currOptions) {
213+
if (typeof currOptions[key] === 'object') {
214+
containsNested = true;
215+
break; // Found, break out of loop, no need to continue
216+
}
217+
}
218+
219+
// Create a flat object of config options if nested format was used
220+
if (!containsNested) {
221+
collectedConfig = currOptions;
222+
} else {
223+
// Merge with selected options
224+
// this == `selected`, where `selected` could be `html`, `js`, 'css', etc
225+
_.merge(collectedConfig, currOptions[this]);
226+
}
227+
228+
return extend(result, collectedConfig);
229+
230+
}, {}, selection);
231+
232+
// Clean
233+
options = cleanOptions(options, knownOpts);
234+
return options;
235+
}
236+
215237
switch (editor.getGrammar().name) {
216238
case 'JavaScript':
217-
text = beautifyJS(text, beautifyOptions);
239+
text = beautifyJS(text, getOptions('js', allOptions));
218240
break;
219241
case 'HTML (Liquid)':
220242
case 'HTML':
221243
case 'XML':
222-
text = beautifyHTML(text, beautifyOptions);
244+
text = beautifyHTML(text, getOptions('html', allOptions));
223245
break;
224246
case 'CSS':
225-
text = beautifyCSS(text, beautifyOptions);
247+
text = beautifyCSS(text, getOptions('css', allOptions));
226248
break;
227249
default:
228250
return;
@@ -248,7 +270,7 @@ function beautify() {
248270
}
249271
}
250272

251-
function handleSafeEvent() {
273+
function handleSaveEvent() {
252274
atom.workspace.eachEditor(function (editor) {
253275
var buffer = editor.getBuffer();
254276
plugin.unsubscribe(buffer);
@@ -265,9 +287,9 @@ plugin.configDefaults = {
265287
};
266288

267289
plugin.activate = function () {
268-
handleSafeEvent();
290+
handleSaveEvent();
269291
plugin.subscribe(atom.config.observe(
270292
'atom-beautify.beautifyOnSave',
271-
handleSafeEvent));
293+
handleSaveEvent));
272294
return atom.workspaceView.command('beautify', beautify);
273295
};

0 commit comments

Comments
 (0)