See #282. Add global language option transforms for beautifier

Beautifier can have global language option transforms
with "_" (underscore) key.
This commit is contained in:
Glavin Wiechert 2015-04-30 16:45:45 -03:00
parent 1a92e608d5
commit 26ab3ec198
3 changed files with 83 additions and 102 deletions

View File

@ -183,7 +183,6 @@ module.exports = class Beautifier
Constructor to setup beautifer Constructor to setup beautifer
### ###
constructor: () -> constructor: () ->
# Set supported languages # Set supported languages
@languages = _.keys(@options) @languages = _.keys(@options)
# TODO: Remove default/catch all key, `_` # TODO: Remove default/catch all key, `_`

View File

@ -100,61 +100,76 @@ module.exports = class Beautifiers
fileExtension = path.extname(filePath) fileExtension = path.extname(filePath)
languages = @languages.getLanguages({grammar, fileExtension}) languages = @languages.getLanguages({grammar, fileExtension})
# TODO: select appropriate language # Check if unsupported language
language = languages[0] if languages.length < 1
# 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
unsupportedGrammar = true unsupportedGrammar = true
else else
# TODO: select beautifier # TODO: select appropriate language
beautifier = beautifiers[0] language = languages[0]
# Transform options, if applicable # Beautify!
beautifierOptions = beautifier.options[language.name] unsupportedGrammar = false
if typeof beautifierOptions is "boolean" if atom.config.get("atom-beautify.disabledLanguages")?.indexOf(language) > - 1
if beautifierOptions isnt true return resolve(null)
# Disable options
options = {} # Options for Language
else if typeof beautifierOptions is "object" options = @getOptions(language.namespace, allOptions) || {}
# Transform the options # Support fallback for options
transformedOptions = {} if language.fallback?
for field, op of beautifierOptions for fallback in language.fallback
if typeof op is "string" # Merge current options on top of fallback options
# Rename options = _.merge(@getOptions(fallback, allOptions) || {}, options)
transformedOptions[field] = options[op]
else if typeof op is "function" # Get Beautifiers
# Transform # console.log(grammar, language)
transformedOptions[field] = op(options) beautifiers = @getBeautifiers(language.name, options)
else if typeof op is "boolean" # console.log('beautifiers', beautifiers)
# Enable/Disable
if op is true # Check if unsupported language
transformedOptions[field] = options[field] if beautifiers.length < 1
# Replace old options with new transformed options unsupportedGrammar = true
options = transformedOptions
else else
console.warn("Unsupported Language options: ", beautifierOptions) # TODO: select beautifier
beautifier.beautify(text, language.name, options) beautifier = beautifiers[0]
.then(resolve)
.catch(reject) 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 # Check if Analytics is enabled
if atom.config.get("atom-beautify.analytics") if atom.config.get("atom-beautify.analytics")
@ -181,7 +196,7 @@ module.exports = class Beautifiers
if atom.config.get("atom-beautify.muteUnsupportedLanguageErrors") if atom.config.get("atom-beautify.muteUnsupportedLanguageErrors")
return resolve(null) return resolve(null)
else else
reject(new Error("Unsupported language for grammar '#{grammar}'.")) reject(new Error("Unsupported language for grammar '#{grammar}' with extension '#{fileExtension}'."))
) )

View File

@ -6,58 +6,25 @@ _ = require('lodash')
module.exports = class PrettyDiff extends Beautifier module.exports = class PrettyDiff extends Beautifier
options: { 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 CSV: true
HTML: true HTML: true
JavaScript: JavaScript: true
inchar: "indent_char" CSS: true
insize: "indent_size" SCSS: true
alphasort: (options) -> Sass: true
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"
JSON: true JSON: true
TSS: TSS: true
inchar: "indent_char" LESS: true
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"
}
} }
beautify: (text, language, options) -> beautify: (text, language, options) ->