From a2262619ad3b1a97040b8df1f6bd41413fcbf6c4 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Sat, 2 May 2015 12:55:37 -0300 Subject: [PATCH] Closes #284, #282. Language config options in Package settings Language config options for: - disabled - Disable beautifying a language - `disabled_languages` no longer works - use the options for each language to disable them - default_beautifier - Preferred beautifier to use for language - if there are multiple beautifiers for a language it will choose the beautifier the user has set as default. Initially the default beautifier is just the first beautifier to register support for that language --- src/beautifiers/index.coffee | 60 +++++++++++++++++++++++++------ src/beautifiers/prettydiff.coffee | 1 + src/beautify.coffee | 6 ---- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index df41e6b..7a39d63 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -82,9 +82,14 @@ module.exports = class Beautifiers buildOptionsForBeautifiers: (beautifiers) -> # Get all Options for Languages langOptions = {} + languages = {} # Hash map of languages with their names for lang in @languages.languages langOptions[lang.name] ?= {} + languages[lang.name] ?= lang options = langOptions[lang.name] + # Init field for supported beautifiers + lang.beautifiers = [] + # Process all language options for field, op of lang.options if not op.title? op.title = _plus.uncamelcase(field).split('.') @@ -108,6 +113,7 @@ module.exports = class Beautifiers # Beautifier supports all options for this language if laOp # console.log('add supported beautifier', languageName, beautifierName) + languages[languageName]?.beautifiers.push(beautifierName) for field, op of laOp op.beautifiers.push(beautifierName) else @@ -118,18 +124,22 @@ module.exports = class Beautifiers if typeof op is "boolean" # Transformation if op is true + languages[languageName]?.beautifiers.push(beautifierName) laOp?[field]?.beautifiers.push(beautifierName) else if typeof op is "string" # Rename # console.log('support option with rename:', field, op, languageName, beautifierName, langOptions) + languages[languageName]?.beautifiers.push(beautifierName) laOp?[op]?.beautifiers.push(beautifierName) else if typeof op is "function" # Transformation + languages[languageName]?.beautifiers.push(beautifierName) laOp?[field]?.beautifiers.push(beautifierName) else if _.isArray(op) # Complex Function [fields..., fn] = op # Add beautifier support to all required fields + languages[languageName]?.beautifiers.push(beautifierName) for f in fields # Add beautifier to required field laOp?[f]?.beautifiers.push(beautifierName) @@ -140,7 +150,7 @@ module.exports = class Beautifiers # Prefix language's options with namespace for langName, ops of langOptions # Get language with name - lang = @languages.getLanguages(name:langName)?[0] + lang = languages[langName] # Use the namespace from language as key prefix prefix = lang.namespace # console.log(langName, lang, prefix, ops) @@ -174,7 +184,33 @@ module.exports = class Beautifiers ), result) ), {}) - # console.log('flatOptions', flatOptions) + # Generate Language configurations + langConfigs = {} + # Process all languages + for langName, lang of languages + name = lang.name + beautifiers = lang.beautifiers + langConfigs[name] = { + type: 'object' + properties: + disabled: + title: "Language Config - #{name} - Disable Beautifying Language" + type: 'boolean' + default: false + description: "Disable #{name} Beautification" + default_beautifier: + title: "Language Config - #{name} - Default Beautifier" + type: 'string' + default: beautifiers[0] + description: "Default Beautifier to be used for #{name}" + enum: _.uniq(beautifiers) + } + # Add Language configurations + flatOptions.languages = { + type: 'object' + properties: langConfigs + } + return flatOptions ### @@ -187,10 +223,6 @@ module.exports = class Beautifiers _.contains(beautifier.languages, language) ) - # getBeautifiersForGrammar: (grammar) -> - # - # getBeautifiersForExtension: (extension) -> - beautify: (text, allOptions, grammar, filePath) -> return new Promise((resolve, reject) => @@ -205,9 +237,13 @@ module.exports = class Beautifiers # TODO: select appropriate language language = languages[0] + # Get language config + langConfig = atom.config.get("atom-beautify.languages")?[language.name] + # Beautify! unsupportedGrammar = false - if atom.config.get("atom-beautify.disabledLanguages")?.indexOf(language) > - 1 + # Check if Language is disabled + if langConfig?.disabled return resolve(null) # Options for Language @@ -218,7 +254,7 @@ module.exports = class Beautifiers # Merge current options on top of fallback options options = _.merge(@getOptions(fallback, allOptions) || {}, options) - # Get Beautifiers + # Get Beautifier # console.log(grammar, language) beautifiers = @getBeautifiers(language.name, options) # console.log('beautifiers', beautifiers) @@ -227,8 +263,12 @@ module.exports = class Beautifiers if beautifiers.length < 1 unsupportedGrammar = true else - # TODO: select beautifier - beautifier = beautifiers[0] + # Select beautifier from language config preferences + preferredBeautifierName = langConfig.default_beautifier + beautifier = _.find(beautifiers, (beautifier) -> + beautifier.name is preferredBeautifierName + ) or beautifiers[0] + # console.log('beautifier', beautifier.name, beautifiers) transformOptions = (beautifier, languageName, options) -> # Transform options, if applicable diff --git a/src/beautifiers/prettydiff.coffee b/src/beautifiers/prettydiff.coffee index 74dae6e..eb0978f 100644 --- a/src/beautifiers/prettydiff.coffee +++ b/src/beautifiers/prettydiff.coffee @@ -23,6 +23,7 @@ module.exports = class PrettyDiff extends Beautifier HTML: true XML: true Spacebars: true + JSX: true JavaScript: true CSS: true SCSS: true diff --git a/src/beautify.coffee b/src/beautify.coffee index d641042..518b84e 100644 --- a/src/beautify.coffee +++ b/src/beautify.coffee @@ -350,12 +350,6 @@ plugin.config = _.merge( type: 'boolean' default: false description: "Do not show any/all errors when they occur" - disabledLanguages: - type: 'array' - default: [] - items: - type: 'string' - description: "An array of languages/grammars to disable Beautification for" , defaultLanguageOptions) plugin.activate = -> handleSaveEvent()