From 58a375f8b6823fda7632f53d145e3e2d21e16dcd Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Thu, 12 Jun 2014 22:47:03 -0300 Subject: [PATCH] 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 --- lib/atom-beautify.js | 108 ++++++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 43 deletions(-) diff --git a/lib/atom-beautify.js b/lib/atom-beautify.js index f1fdcb5..3438c58 100644 --- a/lib/atom-beautify.js +++ b/lib/atom-beautify.js @@ -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); };