From 26ab3ec198e27de009e353cc1b5641d85847b605 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Thu, 30 Apr 2015 16:45:45 -0300 Subject: [PATCH] See #282. Add global language option transforms for beautifier Beautifier can have global language option transforms with "_" (underscore) key. --- src/beautifiers/beautifier.coffee | 1 - src/beautifiers/index.coffee | 119 +++++++++++++++++------------- src/beautifiers/prettydiff.coffee | 65 ++++------------ 3 files changed, 83 insertions(+), 102 deletions(-) diff --git a/src/beautifiers/beautifier.coffee b/src/beautifiers/beautifier.coffee index d39e51f..36df178 100644 --- a/src/beautifiers/beautifier.coffee +++ b/src/beautifiers/beautifier.coffee @@ -183,7 +183,6 @@ module.exports = class Beautifier Constructor to setup beautifer ### constructor: () -> - # Set supported languages @languages = _.keys(@options) # TODO: Remove default/catch all key, `_` diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index f9e1d61..42b8db0 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -100,61 +100,76 @@ module.exports = class Beautifiers fileExtension = path.extname(filePath) languages = @languages.getLanguages({grammar, fileExtension}) - # TODO: select appropriate language - language = languages[0] - - # Beautify! - unsupportedGrammar = false - if atom.config.get("atom-beautify.disabledLanguages")?.indexOf(language) > - 1 - return resolve(null) - - # Options for Language - options = @getOptions(language.namespace, allOptions) || {} - # 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) || {}, options) - - # Get Beautifiers - # console.log(grammar, language) - beautifiers = @getBeautifiers(language.name, options) - # console.log('beautifiers', beautifiers) - - # Check if unsupported grammar - if beautifiers.length < 1 + # Check if unsupported language + if languages.length < 1 unsupportedGrammar = true else - # TODO: select beautifier - beautifier = beautifiers[0] + # TODO: select appropriate language + language = languages[0] - # Transform options, if applicable - beautifierOptions = beautifier.options[language.name] - if typeof beautifierOptions is "boolean" - if beautifierOptions isnt true - # Disable options - options = {} - else if typeof beautifierOptions is "object" - # Transform the options - transformedOptions = {} - for field, op of beautifierOptions - if typeof op is "string" - # Rename - transformedOptions[field] = options[op] - else if typeof op is "function" - # Transform - transformedOptions[field] = op(options) - else if typeof op is "boolean" - # Enable/Disable - if op is true - transformedOptions[field] = options[field] - # Replace old options with new transformed options - options = transformedOptions + # Beautify! + unsupportedGrammar = false + if atom.config.get("atom-beautify.disabledLanguages")?.indexOf(language) > - 1 + return resolve(null) + + # Options for Language + options = @getOptions(language.namespace, allOptions) || {} + # 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) || {}, options) + + # Get Beautifiers + # console.log(grammar, language) + beautifiers = @getBeautifiers(language.name, options) + # console.log('beautifiers', beautifiers) + + # Check if unsupported language + if beautifiers.length < 1 + unsupportedGrammar = true else - console.warn("Unsupported Language options: ", beautifierOptions) - beautifier.beautify(text, language.name, options) - .then(resolve) - .catch(reject) + # TODO: select beautifier + beautifier = beautifiers[0] + + transformOptions = (beautifier, languageName, options) -> + # Transform options, if applicable + beautifierOptions = beautifier.options[languageName] + if typeof beautifierOptions is "boolean" + if beautifierOptions isnt true + # Disable options + options = {} + else if typeof beautifierOptions is "object" + # Transform the options + transformedOptions = {} + # Transform for fields + for field, op of beautifierOptions + if typeof op is "string" + # Rename + transformedOptions[field] = options[op] + else if typeof op is "function" + # Transform + transformedOptions[field] = op(options) + else if typeof op is "boolean" + # Enable/Disable + if op is true + transformedOptions[field] = options[field] + # Replace old options with new transformed options + options = transformedOptions + else + console.warn("Unsupported Language options: ", beautifierOptions) + return options + + # Apply Beautifier / global option transformations + if beautifier.options._? + options = transformOptions(beautifier, "_", options) + # Apply language-specific option transformations + options = transformOptions(beautifier, language.name, options) + + # Beautify text with language options + beautifier.beautify(text, language.name, options) + .then(resolve) + .catch(reject) # Check if Analytics is enabled if atom.config.get("atom-beautify.analytics") @@ -181,7 +196,7 @@ module.exports = class Beautifiers if atom.config.get("atom-beautify.muteUnsupportedLanguageErrors") return resolve(null) else - reject(new Error("Unsupported language for grammar '#{grammar}'.")) + reject(new Error("Unsupported language for grammar '#{grammar}' with extension '#{fileExtension}'.")) ) diff --git a/src/beautifiers/prettydiff.coffee b/src/beautifiers/prettydiff.coffee index d5b40e5..7d911d4 100644 --- a/src/beautifiers/prettydiff.coffee +++ b/src/beautifiers/prettydiff.coffee @@ -6,58 +6,25 @@ _ = require('lodash') module.exports = class PrettyDiff extends Beautifier options: { + # Apply these options first / globally, for all languages + _: + inchar: "indent_char" + insize: "indent_size" + alphasort: (options) -> + options.alphasort or false + preserve: (options) -> + if (options.preserve_newlines is true ) then \ + "all" else "none" + # Apply language-specific options CSV: true HTML: true - JavaScript: - inchar: "indent_char" - insize: "indent_size" - alphasort: (options) -> - options.alphasort or false - preserve: (options) -> - if (options.preserve_newlines is true ) then \ - "all" else "none" - CSS: - inchar: "indent_char" - insize: "indent_size" - alphasort: (options) -> - options.alphasort or false - preserve: (options) -> - if (options.preserve_newlines is true ) then \ - "all" else "none" - SCSS: - inchar: "indent_char" - insize: "indent_size" - alphasort: (options) -> - options.alphasort or false - preserve: (options) -> - if (options.preserve_newlines is true ) then \ - "all" else "none" - Sass: - inchar: "indent_char" - insize: "indent_size" - alphasort: (options) -> - options.alphasort or false - preserve: (options) -> - if (options.preserve_newlines is true ) then \ - "all" else "none" + JavaScript: true + CSS: true + SCSS: true + Sass: true JSON: true - TSS: - inchar: "indent_char" - insize: "indent_size" - alphasort: (options) -> - options.alphasort or false - preserve: (options) -> - if (options.preserve_newlines is true ) then \ - "all" else "none" - LESS: { - inchar: "indent_char" - insize: "indent_size" - alphasort: (options) -> - options.alphasort or false - preserve: (options) -> - if (options.preserve_newlines is true ) then \ - "all" else "none" - } + TSS: true + LESS: true } beautify: (text, language, options) ->