From 573a949b6f09a629d1ca42db4bcee0965be76c9b Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Tue, 19 May 2015 15:02:23 -0300 Subject: [PATCH] Closes #346. Merge options for language namespace, fallbacks, _default --- examples/nested-jsbeautifyrc/.jsbeautifyrc | 7 +- .../less/original/{test2.less => _test2.less} | 0 .../{operators.coffee => _operators.coffee} | 0 .../original/{test.coffee => _test.coffee} | 0 spec/beautify-languages-spec.coffee | 4 +- src/beautifiers/index.coffee | 106 +++++++++++------- src/beautifiers/prettydiff.coffee | 3 +- src/beautify.coffee | 1 - 8 files changed, 72 insertions(+), 49 deletions(-) rename examples/nested-jsbeautifyrc/less/original/{test2.less => _test2.less} (100%) rename examples/simple-jsbeautifyrc/coffeescript/original/{operators.coffee => _operators.coffee} (100%) rename examples/simple-jsbeautifyrc/coffeescript/original/{test.coffee => _test.coffee} (100%) diff --git a/examples/nested-jsbeautifyrc/.jsbeautifyrc b/examples/nested-jsbeautifyrc/.jsbeautifyrc index 7c173cb..73c4142 100644 --- a/examples/nested-jsbeautifyrc/.jsbeautifyrc +++ b/examples/nested-jsbeautifyrc/.jsbeautifyrc @@ -3,7 +3,7 @@ brace_style: "collapse" indent_char: " " indent_scripts: "normal" - indent_size: 6 + indent_size: 2 max_preserve_newlines: 1 preserve_newlines: true unformatted: @@ -16,7 +16,8 @@ wrap_line_length: 0 css: indent_char: " " - indent_size: 4 + indent_size: 2 + preserve_newlines: true js: indent_size: 2 indent_char: " " @@ -31,7 +32,7 @@ indent_level: 0 indent_with_tabs: false python: - indent_size: 4 + indent_size: 2 #max_line_length: 79 #ignore: # - "E24" diff --git a/examples/nested-jsbeautifyrc/less/original/test2.less b/examples/nested-jsbeautifyrc/less/original/_test2.less similarity index 100% rename from examples/nested-jsbeautifyrc/less/original/test2.less rename to examples/nested-jsbeautifyrc/less/original/_test2.less diff --git a/examples/simple-jsbeautifyrc/coffeescript/original/operators.coffee b/examples/simple-jsbeautifyrc/coffeescript/original/_operators.coffee similarity index 100% rename from examples/simple-jsbeautifyrc/coffeescript/original/operators.coffee rename to examples/simple-jsbeautifyrc/coffeescript/original/_operators.coffee diff --git a/examples/simple-jsbeautifyrc/coffeescript/original/test.coffee b/examples/simple-jsbeautifyrc/coffeescript/original/_test.coffee similarity index 100% rename from examples/simple-jsbeautifyrc/coffeescript/original/test.coffee rename to examples/simple-jsbeautifyrc/coffeescript/original/_test.coffee diff --git a/spec/beautify-languages-spec.coffee b/spec/beautify-languages-spec.coffee index 31793fb..6080866 100644 --- a/spec/beautify-languages-spec.coffee +++ b/spec/beautify-languages-spec.coffee @@ -37,6 +37,8 @@ describe "BeautifyLanguages", -> # Force activate package pack = atom.packages.getLoadedPackage("atom-beautify") pack.activateNow() + # Change logger level + # atom.config.set('atom-beautify._loggerLevel', 'verbose') # Return promise return activationPromise @@ -134,7 +136,7 @@ describe "BeautifyLanguages", -> beautifyCompleted = false completionFun = (text) -> - # console.log(expectedTestPath, text) if ext is ".swig" + # logger.verbose(expectedTestPath, text) if ext is ".less" expect(text instanceof Error).not.toEqual(true, text) # if text instanceof Error # return beautifyCompleted = text # text == Error diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index 9d92c6c..009e3e3 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -288,9 +288,9 @@ module.exports = class Beautifiers beautify : (text, allOptions, grammar, filePath, {onSave} = {}) -> - return new Promise((resolve, reject)=> + return new Promise((resolve, reject) => logger.info('beautify', text, allOptions, grammar, filePath) - console.log(allOptions) + logger.verbose(allOptions) # Get language fileExtension = path.extname(filePath) @@ -339,29 +339,21 @@ module.exports = class Beautifiers return resolve( null ) # Options for Language - options = @getOptions(language.namespace, allOptions) or {} - - console.log(options) - - # Support fallback for options - if language.fallback? - for fallback in language.fallback - - # Merge current options on top of fallback options - options = _.merge( @getOptions(fallback, allOptions) or {}, options) + options = @getOptions([language.namespace].concat(language.fallback or []), allOptions) or {} # Get Beautifier logger.verbose(grammar, language) beautifiers = @getBeautifiers(language.name, options) - console.log(options) - # logger.verbose('beautifiers', beautifiers) - # + logger.verbose('options', options) + logger.verbose('beautifiers', beautifiers) + + logger.verbose(language.name, filePath, options, allOptions) + # Check if unsupported language if beautifiers.length < 1 unsupportedGrammar = true else - # Select beautifier from language config preferences beautifier = _.find(beautifiers, (beautifier) -> beautifier.name is preferredBeautifierName @@ -420,7 +412,6 @@ module.exports = class Beautifiers # Apply language-specific option transformations options = transformOptions(beautifier, language.name, options) - # Beautify text with language options beautifier.beautify(text, language.name, options) .then(resolve) @@ -546,7 +537,7 @@ module.exports = class Beautifiers envs = @getUserHome() home = path.normalize(path.join(envs, config)) proj = @findFile(config, dir, upwards) - console.log(dir, proj, home) + logger.verbose(dir, proj, home) return proj if proj return home if @verifyExists(home) null @@ -650,6 +641,7 @@ module.exports = class Beautifiers editorconfig ?= require('editorconfig') editorConfigOptions = editorconfig.parse(editedFilePath) + logger.verbose('editorConfigOptions', editorConfigOptions) # Transform EditorConfig to Atom Beautify's config structure and naming if editorConfigOptions.indent_style is 'space' @@ -675,11 +667,15 @@ module.exports = class Beautifiers pf = path.join(p, "FAKEFILENAME") pc = @getConfig(pf, false) + isNested = @isNestedOptions(pc) + unless isNested + pc = { + _default: pc + } # Add config for p to project's config options projectOptions.push(pc) - # logger.verbose p, pc # Move upwards p = path.resolve(p, "../") @@ -689,48 +685,72 @@ module.exports = class Beautifiers # Combine all options together allOptions = [ - editorOptions - configOptions - homeOptions + { + _default: + editorOptions + }, + configOptions, + { + _default: + homeOptions + }, editorConfigOptions ] + # Reverse and add projectOptions to all options + projectOptions.reverse() allOptions = allOptions.concat(projectOptions) - # logger.verbose(allOptions) return allOptions - getOptions : (selection, allOptions) -> + + isNestedOptions : (currOptions) -> + containsNested = false + key = undefined + + # Check if already nested under _default + if currOptions._default + return true + + # Check to see if config file uses nested object format to split up js/css/html options + for key of currOptions + + # Check if is supported language + if _.indexOf(@languages.namespaces, key) >= 0 and typeof currOptions[key] is "object" # Check if nested object (more options in value) + containsNested = true + break # Found, break out of loop, no need to continue + + return containsNested + + getOptions : (selections, allOptions) => self = this _ ?= require("lodash") extend ?= require("extend") + logger.verbose(selections, allOptions) # logger.verbose(selection, allOptions); # Reduce all options into correctly merged options. - options = _.reduce(allOptions, (result, currOptions) -> - containsNested = false - collectedConfig = {} - key = undefined - - - # Check to see if config file uses nested object format to split up js/css/html options - for key of currOptions - - # Check if is supported language - if _.indexOf(self.languages.namespaces, key) >= 0 and typeof currOptions[key] is "object" # Check if nested object (more options in value) - containsNested = true - break # Found, break out of loop, no need to continue - + options = _.reduce(allOptions, (result, currOptions) => + collectedConfig = currOptions._default or {} + containsNested = @isNestedOptions(currOptions) + logger.verbose(containsNested, currOptions) # logger.verbose(containsNested, currOptions); + # Create a flat object of config options if nested format was used unless containsNested - _.merge collectedConfig, currOptions - else + # _.merge collectedConfig, currOptions + currOptions = { + _default: currOptions + } - # Merge with selected options - # where `selection` could be `html`, `js`, 'css', etc - # logger.verbose(selection, currOptions[selection]); + # Merge with selected options + # where `selection` could be `html`, `js`, 'css', etc + for selection in selections + # Merge current options on top of fallback options + logger.verbose('options', selection, currOptions[selection]); _.merge collectedConfig, currOptions[selection] + logger.verbose('options', selection, collectedConfig); + extend result, collectedConfig , {}) diff --git a/src/beautifiers/prettydiff.coffee b/src/beautifiers/prettydiff.coffee index 51c2308..67f47b8 100644 --- a/src/beautifiers/prettydiff.coffee +++ b/src/beautifiers/prettydiff.coffee @@ -34,7 +34,7 @@ module.exports = class PrettyDiff extends Beautifier beautify: (text, language, options) -> - return new @Promise((resolve, reject) -> + return new @Promise((resolve, reject) => prettydiff = require("prettydiff") _ = require('lodash') @@ -71,6 +71,7 @@ module.exports = class PrettyDiff extends Beautifier _.merge(options, args) # Beautify + @verbose('prettydiff', options) output = prettydiff.api(options) result = output[0] diff --git a/src/beautify.coffee b/src/beautify.coffee index 0050de1..70ade1d 100644 --- a/src/beautify.coffee +++ b/src/beautify.coffee @@ -43,7 +43,6 @@ setCursors = (editor, posArray) -> editor.addCursorAtBufferPosition bufferPosition return beautify = ({onSave}) -> - console.log('beautify!!!') # Deprecation warning for beautify on save if atom.config.get("atom-beautify.beautifyOnSave") is true detail = """See issue https://github.com/Glavin001/atom-beautify/issues/308