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
This commit is contained in:
parent
4d486794b4
commit
58a375f8b6
|
@ -148,15 +148,13 @@ function findConfig(config, file) {
|
|||
|
||||
function beautify() {
|
||||
|
||||
console.log('Beautify!!!');
|
||||
|
||||
var text;
|
||||
var editor = atom.workspace.getActiveEditor();
|
||||
var isSelection = !! editor.getSelectedText();
|
||||
var softTabs = editor.softTabs;
|
||||
var tabLength = editor.getTabLength();
|
||||
|
||||
var beautifyOptions = {
|
||||
var defaultOptions = {
|
||||
'indent_size': softTabs ? tabLength : 1,
|
||||
'indent_char': softTabs ? ' ' : '\t',
|
||||
'indent_with_tabs': !softTabs
|
||||
|
@ -165,45 +163,29 @@ function beautify() {
|
|||
// Look for .jsbeautifierrc in file and home path, check env variables
|
||||
var editedFilePath = editor.getPath();
|
||||
|
||||
// Get the path to the config file
|
||||
var configPath = findConfig('.jsbeautifyrc', editedFilePath);
|
||||
function getConfig(startPath) {
|
||||
// Get the path to the config file
|
||||
var configPath = findConfig('.jsbeautifyrc', startPath);
|
||||
|
||||
var externalOptions;
|
||||
if (configPath) {
|
||||
var strip = require('strip-json-comments');
|
||||
try {
|
||||
externalOptions = JSON.parse(strip(fs.readFileSync(configPath, {
|
||||
encoding: 'utf8'
|
||||
})));
|
||||
} catch (e) {
|
||||
var externalOptions;
|
||||
if (configPath) {
|
||||
var strip = require('strip-json-comments');
|
||||
try {
|
||||
externalOptions = JSON.parse(strip(fs.readFileSync(configPath, {
|
||||
encoding: 'utf8'
|
||||
})));
|
||||
} catch (e) {
|
||||
externalOptions = {};
|
||||
}
|
||||
} else {
|
||||
externalOptions = {};
|
||||
}
|
||||
} else {
|
||||
externalOptions = {};
|
||||
return externalOptions;
|
||||
}
|
||||
|
||||
var containsNested = false;
|
||||
var collectedConfig = {};
|
||||
var key;
|
||||
|
||||
// Check to see if config file uses nested object format to split up js/css/html options
|
||||
for (key in externalOptions) {
|
||||
if (typeof externalOptions[key] === 'object') {
|
||||
containsNested = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a flat object of config options if nested format was used
|
||||
if (!containsNested) {
|
||||
collectedConfig = externalOptions;
|
||||
} else {
|
||||
for (key in externalOptions) {
|
||||
_.merge(collectedConfig, externalOptions[key]);
|
||||
}
|
||||
}
|
||||
|
||||
beautifyOptions = extend(collectedConfig, beautifyOptions);
|
||||
beautifyOptions = cleanOptions(beautifyOptions, knownOpts);
|
||||
// Get the path to the config file
|
||||
var projectOptions = getConfig(editedFilePath);
|
||||
var homeOptions = getConfig(getUserHome());
|
||||
|
||||
if (isSelection) {
|
||||
text = editor.getSelectedText();
|
||||
|
@ -212,17 +194,57 @@ function beautify() {
|
|||
}
|
||||
var oldText = text;
|
||||
|
||||
// All of the options
|
||||
// Listed in order from default (base) to the one with the highest priority
|
||||
// Left = Default, Right = Will override the left.
|
||||
var allOptions = [defaultOptions, homeOptions, projectOptions];
|
||||
|
||||
function getOptions(selection, allOptions) {
|
||||
|
||||
// Reduce all options into correctly merged options.
|
||||
var options = _.reduce(allOptions, function(result, currOptions) {
|
||||
|
||||
var containsNested = false;
|
||||
var collectedConfig = {};
|
||||
var key;
|
||||
|
||||
// Check to see if config file uses nested object format to split up js/css/html options
|
||||
for (key in currOptions) {
|
||||
if (typeof currOptions[key] === 'object') {
|
||||
containsNested = true;
|
||||
break; // Found, break out of loop, no need to continue
|
||||
}
|
||||
}
|
||||
|
||||
// Create a flat object of config options if nested format was used
|
||||
if (!containsNested) {
|
||||
collectedConfig = currOptions;
|
||||
} else {
|
||||
// Merge with selected options
|
||||
// this == `selected`, where `selected` could be `html`, `js`, 'css', etc
|
||||
_.merge(collectedConfig, currOptions[this]);
|
||||
}
|
||||
|
||||
return extend(result, collectedConfig);
|
||||
|
||||
}, {}, selection);
|
||||
|
||||
// Clean
|
||||
options = cleanOptions(options, knownOpts);
|
||||
return options;
|
||||
}
|
||||
|
||||
switch (editor.getGrammar().name) {
|
||||
case 'JavaScript':
|
||||
text = beautifyJS(text, beautifyOptions);
|
||||
text = beautifyJS(text, getOptions('js', allOptions));
|
||||
break;
|
||||
case 'HTML (Liquid)':
|
||||
case 'HTML':
|
||||
case 'XML':
|
||||
text = beautifyHTML(text, beautifyOptions);
|
||||
text = beautifyHTML(text, getOptions('html', allOptions));
|
||||
break;
|
||||
case 'CSS':
|
||||
text = beautifyCSS(text, beautifyOptions);
|
||||
text = beautifyCSS(text, getOptions('css', allOptions));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
|
@ -248,7 +270,7 @@ function beautify() {
|
|||
}
|
||||
}
|
||||
|
||||
function handleSafeEvent() {
|
||||
function handleSaveEvent() {
|
||||
atom.workspace.eachEditor(function (editor) {
|
||||
var buffer = editor.getBuffer();
|
||||
plugin.unsubscribe(buffer);
|
||||
|
@ -265,9 +287,9 @@ plugin.configDefaults = {
|
|||
};
|
||||
|
||||
plugin.activate = function () {
|
||||
handleSafeEvent();
|
||||
handleSaveEvent();
|
||||
plugin.subscribe(atom.config.observe(
|
||||
'atom-beautify.beautifyOnSave',
|
||||
handleSafeEvent));
|
||||
handleSaveEvent));
|
||||
return atom.workspaceView.command('beautify', beautify);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue