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() {
|
function beautify() {
|
||||||
|
|
||||||
console.log('Beautify!!!');
|
|
||||||
|
|
||||||
var text;
|
var text;
|
||||||
var editor = atom.workspace.getActiveEditor();
|
var editor = atom.workspace.getActiveEditor();
|
||||||
var isSelection = !! editor.getSelectedText();
|
var isSelection = !! editor.getSelectedText();
|
||||||
var softTabs = editor.softTabs;
|
var softTabs = editor.softTabs;
|
||||||
var tabLength = editor.getTabLength();
|
var tabLength = editor.getTabLength();
|
||||||
|
|
||||||
var beautifyOptions = {
|
var defaultOptions = {
|
||||||
'indent_size': softTabs ? tabLength : 1,
|
'indent_size': softTabs ? tabLength : 1,
|
||||||
'indent_char': softTabs ? ' ' : '\t',
|
'indent_char': softTabs ? ' ' : '\t',
|
||||||
'indent_with_tabs': !softTabs
|
'indent_with_tabs': !softTabs
|
||||||
|
@ -165,8 +163,9 @@ function beautify() {
|
||||||
// Look for .jsbeautifierrc in file and home path, check env variables
|
// Look for .jsbeautifierrc in file and home path, check env variables
|
||||||
var editedFilePath = editor.getPath();
|
var editedFilePath = editor.getPath();
|
||||||
|
|
||||||
|
function getConfig(startPath) {
|
||||||
// Get the path to the config file
|
// Get the path to the config file
|
||||||
var configPath = findConfig('.jsbeautifyrc', editedFilePath);
|
var configPath = findConfig('.jsbeautifyrc', startPath);
|
||||||
|
|
||||||
var externalOptions;
|
var externalOptions;
|
||||||
if (configPath) {
|
if (configPath) {
|
||||||
|
@ -181,29 +180,12 @@ function beautify() {
|
||||||
} else {
|
} else {
|
||||||
externalOptions = {};
|
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
|
// Get the path to the config file
|
||||||
if (!containsNested) {
|
var projectOptions = getConfig(editedFilePath);
|
||||||
collectedConfig = externalOptions;
|
var homeOptions = getConfig(getUserHome());
|
||||||
} else {
|
|
||||||
for (key in externalOptions) {
|
|
||||||
_.merge(collectedConfig, externalOptions[key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
beautifyOptions = extend(collectedConfig, beautifyOptions);
|
|
||||||
beautifyOptions = cleanOptions(beautifyOptions, knownOpts);
|
|
||||||
|
|
||||||
if (isSelection) {
|
if (isSelection) {
|
||||||
text = editor.getSelectedText();
|
text = editor.getSelectedText();
|
||||||
|
@ -212,17 +194,57 @@ function beautify() {
|
||||||
}
|
}
|
||||||
var oldText = text;
|
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) {
|
switch (editor.getGrammar().name) {
|
||||||
case 'JavaScript':
|
case 'JavaScript':
|
||||||
text = beautifyJS(text, beautifyOptions);
|
text = beautifyJS(text, getOptions('js', allOptions));
|
||||||
break;
|
break;
|
||||||
case 'HTML (Liquid)':
|
case 'HTML (Liquid)':
|
||||||
case 'HTML':
|
case 'HTML':
|
||||||
case 'XML':
|
case 'XML':
|
||||||
text = beautifyHTML(text, beautifyOptions);
|
text = beautifyHTML(text, getOptions('html', allOptions));
|
||||||
break;
|
break;
|
||||||
case 'CSS':
|
case 'CSS':
|
||||||
text = beautifyCSS(text, beautifyOptions);
|
text = beautifyCSS(text, getOptions('css', allOptions));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
|
@ -248,7 +270,7 @@ function beautify() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleSafeEvent() {
|
function handleSaveEvent() {
|
||||||
atom.workspace.eachEditor(function (editor) {
|
atom.workspace.eachEditor(function (editor) {
|
||||||
var buffer = editor.getBuffer();
|
var buffer = editor.getBuffer();
|
||||||
plugin.unsubscribe(buffer);
|
plugin.unsubscribe(buffer);
|
||||||
|
@ -265,9 +287,9 @@ plugin.configDefaults = {
|
||||||
};
|
};
|
||||||
|
|
||||||
plugin.activate = function () {
|
plugin.activate = function () {
|
||||||
handleSafeEvent();
|
handleSaveEvent();
|
||||||
plugin.subscribe(atom.config.observe(
|
plugin.subscribe(atom.config.observe(
|
||||||
'atom-beautify.beautifyOnSave',
|
'atom-beautify.beautifyOnSave',
|
||||||
handleSafeEvent));
|
handleSaveEvent));
|
||||||
return atom.workspaceView.command('beautify', beautify);
|
return atom.workspaceView.command('beautify', beautify);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue