From 4d59f6dfdca3ec6c7f9a450093e2ca9a753c93ba Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Tue, 12 Jan 2016 13:03:40 -0400 Subject: [PATCH] Improve Help Debug Editor with options for language & beautifier What's new: - Add reusable function transformOptions to beautifiers/index - Improve Help Debug Editor - Fix bug causing optionsForLanguage to be incorrect - Add support for transformOptions to determine final options --- src/beautifiers/index.coffee | 108 ++++++++++++++--------------- src/beautifiers/js-beautify.coffee | 1 + src/beautify.coffee | 39 ++++++++--- 3 files changed, 85 insertions(+), 63 deletions(-) diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index 6569f74..5aa37cd 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -292,7 +292,7 @@ module.exports = class Beautifiers extends EventEmitter str.replace(/#/g, '%23').replace(/;/g, '%3B') - getBeautifiers : (language, options) -> + getBeautifiers : (language) -> # logger.verbose(@beautifiers) _.filter( @beautifiers, (beautifier) -> @@ -320,6 +320,56 @@ module.exports = class Beautifiers extends EventEmitter selections = (language.fallback or []).concat([language.namespace]) options = @getOptions(selections, allOptions) or {} + transformOptions : (beautifier, languageName, options) -> + + # Transform options, if applicable + beautifierOptions = beautifier.options[languageName] + if typeof beautifierOptions is "boolean" + + # Language is supported by beautifier + # If true then all options are directly supported + # If falsy then pass all options to beautifier, + # although no options are directly supported. + return 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[field]) + else if typeof op is "boolean" + + # Enable/Disable + if op is true + transformedOptions[field] = options[field] + else if _.isArray(op) + + # Complex function + [fields..., fn] = op + vals = _.map(fields, (f) -> + return options[f] + ) + + # Apply function + transformedOptions[field] = fn.apply( null , vals) + + # Replace old options with new transformed options + return transformedOptions + else + logger.warn("Unsupported Language options: ", beautifierOptions) + return options + + beautify : (text, allOptions, grammar, filePath, {onSave} = {}) -> return Promise.all(allOptions) .then((allOptions) => @@ -369,7 +419,6 @@ module.exports = class Beautifiers extends EventEmitter beautifyOnSave = atom.config.get("atom-beautify.language_#{language.namespace}_beautify_on_save") legacyBeautifyOnSave = atom.config.get("atom-beautify.beautifyOnSave") - # Verify if beautifying on save if onSave and not (beautifyOnSave or legacyBeautifyOnSave) logger.verbose("Beautify on save is disabled for language #{language.name}") @@ -381,9 +430,9 @@ module.exports = class Beautifiers extends EventEmitter # Get Beautifier logger.verbose(grammar, language) - beautifiers = @getBeautifiers(language.name, options) + beautifiers = @getBeautifiers(language.name) - logger.verbose('options', options) + logger.verbose("language options: #{JSON.stringify(options, null, 4)}") logger.verbose('beautifiers', _.map(beautifiers, 'name')) logger.verbose(language.name, filePath, options, allOptions) @@ -398,58 +447,9 @@ module.exports = class Beautifiers extends EventEmitter beautifier.name is preferredBeautifierName ) or beautifiers[0] logger.verbose('beautifier', beautifier.name) - transformOptions = (beautifier, languageName, options) -> - - # Transform options, if applicable - beautifierOptions = beautifier.options[languageName] - if typeof beautifierOptions is "boolean" - - # Language is supported by beautifier - # If true then all options are directly supported - # If falsy then pass all options to beautifier, - # although no options are directly supported. - return 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[field]) - else if typeof op is "boolean" - - # Enable/Disable - if op is true - transformedOptions[field] = options[field] - else if _.isArray(op) - - # Complex function - [fields..., fn] = op - vals = _.map(fields, (f) -> - return options[f] - ) - - - # Apply function - transformedOptions[field] = fn.apply( null , vals) - - # Replace old options with new transformed options - return transformedOptions - else - logger.warn("Unsupported Language options: ", beautifierOptions) - return options # Apply language-specific option transformations - options = transformOptions(beautifier, language.name, options) + options = @transformOptions(beautifier, language.name, options) # Beautify text with language options @emit "beautify::start" diff --git a/src/beautifiers/js-beautify.coffee b/src/beautifiers/js-beautify.coffee index 202533c..3687fe5 100644 --- a/src/beautifiers/js-beautify.coffee +++ b/src/beautifiers/js-beautify.coffee @@ -23,6 +23,7 @@ module.exports = class JSBeautify extends Beautifier beautify: (text, language, options) -> @verbose("JS Beautify language #{language}") + @info("JS Beautify Options: #{JSON.stringify(options, null, 4)}") return new @Promise((resolve, reject) => try switch language diff --git a/src/beautify.coffee b/src/beautify.coffee index c16287a..b537071 100644 --- a/src/beautify.coffee +++ b/src/beautify.coffee @@ -324,6 +324,16 @@ debug = () -> # Language language = beautifier.getLanguage(grammarName, filePath) addInfo('Original File Language', language?.name) + addInfo('Language namespace', language?.namespace) + + # Beautifier + beautifiers = beautifier.getBeautifiers(language.name) + preferredBeautifierName = atom.config.get("atom-beautify.language_#{language.namespace}_default_beautifier") + addInfo('Supported Beautifiers', _.map(beautifiers, 'name').join(', ')) + selectedBeautifier = _.find(beautifiers, (beautifier) -> + beautifier.name is preferredBeautifierName + ) or beautifiers[0] + addInfo('Selected Beautifier', selectedBeautifier.name) # Get current editor's text text = editor.getText() @@ -331,9 +341,13 @@ debug = () -> # Contents codeBlockSyntax = (language?.name ? grammarName).toLowerCase().split(' ')[0] addInfo('Original File Contents', "\n```#{codeBlockSyntax}\n#{text}\n```") - addHeader(2, "Beautification options") + + addInfo('Package Settings', "\n" + + "The raw package settings options\n" + + "```json\n#{JSON.stringify(atom.config.get('atom-beautify'), undefined, 4)}\n```") # Beautification Options + addHeader(2, "Beautification options") # Get all options allOptions = beautifier.getOptionsForPath(filePath, editor) # Resolve options with promises @@ -348,9 +362,15 @@ debug = () -> ] = allOptions projectOptions = allOptions[4..] - finalOptions = beautifier.getOptionsForLanguage(allOptions, language?) + preTransformedOptions = beautifier.getOptionsForLanguage(allOptions, language) + + if selectedBeautifier + finalOptions = beautifier.transformOptions(selectedBeautifier, language.name, preTransformedOptions) # Show options + # addInfo('All Options', "\n" + + # "All options extracted for file\n" + + # "```json\n#{JSON.stringify(allOptions, undefined, 4)}\n```") addInfo('Editor Options', "\n" + "Options from Atom Editor settings\n" + "```json\n#{JSON.stringify(editorOptions, undefined, 4)}\n```") @@ -366,13 +386,14 @@ debug = () -> addInfo('Project Options', "\n" + "Options from `.jsbeautifyrc` files starting from directory `#{path.dirname(filePath)}` and going up to root\n" + "```json\n#{JSON.stringify(projectOptions, undefined, 4)}\n```") - addInfo('Final Options', "\n" + - "Final combined options that are used\n" + - "```json\n#{JSON.stringify(finalOptions, undefined, 4)}\n```") - - addInfo('Package Settings', "\n" + - "The raw package settings options\n" + - "```json\n#{JSON.stringify(atom.config.get('atom-beautify'), undefined, 4)}\n```") + addInfo('Pre-Transformed Options', "\n" + + "Combined options before transforming them given a beautifier's specifications\n" + + "```json\n#{JSON.stringify(preTransformedOptions, undefined, 4)}\n```") + if selectedBeautifier + addHeader(3, 'Final Options') + addInfo('Final Options', "\n" + + "Final combined and transformed options that are used\n" + + "```json\n#{JSON.stringify(finalOptions, undefined, 4)}\n```") # logs = ""