Skip to content

Commit

Permalink
Fixes #16. Custom configuration options are now working correctly.
Browse files Browse the repository at this point in the history
Applied in order of precedence:
- Default
- User Home .jsbeautifyrc
- Closest .jsbeautify to the current file, see #15
  • Loading branch information
Glavin001 committed Jun 13, 2014
1 parent 4d48679 commit 58a375f
Showing 1 changed file with 66 additions and 44 deletions.
110 changes: 66 additions & 44 deletions lib/atom-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

var externalOptions;
if (configPath) {
var strip = require('strip-json-comments');
try {
externalOptions = JSON.parse(strip(fs.readFileSync(configPath, {
encoding: 'utf8'
})));
} catch (e) {
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) {
externalOptions = {};
}
} else {
externalOptions = {};
}
} else {
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]);
}
return externalOptions;
}

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();
Expand All @@ -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;
Expand All @@ -248,7 +270,7 @@ function beautify() {
}
}

function handleSafeEvent() {
function handleSaveEvent() {
atom.workspace.eachEditor(function (editor) {
var buffer = editor.getBuffer();
plugin.unsubscribe(buffer);
Expand All @@ -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);
};

0 comments on commit 58a375f

Please sign in to comment.