Closes #346. Merge options for language namespace, fallbacks, _default

This commit is contained in:
Glavin Wiechert 2015-05-19 15:02:23 -03:00
parent 5fd07cff9c
commit 573a949b6f
8 changed files with 72 additions and 49 deletions

View File

@ -3,7 +3,7 @@
brace_style: "collapse" brace_style: "collapse"
indent_char: " " indent_char: " "
indent_scripts: "normal" indent_scripts: "normal"
indent_size: 6 indent_size: 2
max_preserve_newlines: 1 max_preserve_newlines: 1
preserve_newlines: true preserve_newlines: true
unformatted: unformatted:
@ -16,7 +16,8 @@
wrap_line_length: 0 wrap_line_length: 0
css: css:
indent_char: " " indent_char: " "
indent_size: 4 indent_size: 2
preserve_newlines: true
js: js:
indent_size: 2 indent_size: 2
indent_char: " " indent_char: " "
@ -31,7 +32,7 @@
indent_level: 0 indent_level: 0
indent_with_tabs: false indent_with_tabs: false
python: python:
indent_size: 4 indent_size: 2
#max_line_length: 79 #max_line_length: 79
#ignore: #ignore:
# - "E24" # - "E24"

View File

@ -37,6 +37,8 @@ describe "BeautifyLanguages", ->
# Force activate package # Force activate package
pack = atom.packages.getLoadedPackage("atom-beautify") pack = atom.packages.getLoadedPackage("atom-beautify")
pack.activateNow() pack.activateNow()
# Change logger level
# atom.config.set('atom-beautify._loggerLevel', 'verbose')
# Return promise # Return promise
return activationPromise return activationPromise
@ -134,7 +136,7 @@ describe "BeautifyLanguages", ->
beautifyCompleted = false beautifyCompleted = false
completionFun = (text) -> 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) expect(text instanceof Error).not.toEqual(true, text)
# if text instanceof Error # if text instanceof Error
# return beautifyCompleted = text # text == Error # return beautifyCompleted = text # text == Error

View File

@ -288,9 +288,9 @@ module.exports = class Beautifiers
beautify : (text, allOptions, grammar, filePath, {onSave} = {}) -> beautify : (text, allOptions, grammar, filePath, {onSave} = {}) ->
return new Promise((resolve, reject)=> return new Promise((resolve, reject) =>
logger.info('beautify', text, allOptions, grammar, filePath) logger.info('beautify', text, allOptions, grammar, filePath)
console.log(allOptions) logger.verbose(allOptions)
# Get language # Get language
fileExtension = path.extname(filePath) fileExtension = path.extname(filePath)
@ -339,29 +339,21 @@ module.exports = class Beautifiers
return resolve( null ) return resolve( null )
# Options for Language # Options for Language
options = @getOptions(language.namespace, allOptions) or {} options = @getOptions([language.namespace].concat(language.fallback or []), 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)
# Get Beautifier # Get Beautifier
logger.verbose(grammar, language) logger.verbose(grammar, language)
beautifiers = @getBeautifiers(language.name, options) beautifiers = @getBeautifiers(language.name, options)
console.log(options) logger.verbose('options', options)
# logger.verbose('beautifiers', beautifiers) logger.verbose('beautifiers', beautifiers)
#
logger.verbose(language.name, filePath, options, allOptions)
# Check if unsupported language # Check if unsupported language
if beautifiers.length < 1 if beautifiers.length < 1
unsupportedGrammar = true unsupportedGrammar = true
else else
# Select beautifier from language config preferences # Select beautifier from language config preferences
beautifier = _.find(beautifiers, (beautifier) -> beautifier = _.find(beautifiers, (beautifier) ->
beautifier.name is preferredBeautifierName beautifier.name is preferredBeautifierName
@ -420,7 +412,6 @@ module.exports = class Beautifiers
# Apply language-specific option transformations # Apply language-specific option transformations
options = transformOptions(beautifier, language.name, options) options = transformOptions(beautifier, language.name, options)
# Beautify text with language options # Beautify text with language options
beautifier.beautify(text, language.name, options) beautifier.beautify(text, language.name, options)
.then(resolve) .then(resolve)
@ -546,7 +537,7 @@ module.exports = class Beautifiers
envs = @getUserHome() envs = @getUserHome()
home = path.normalize(path.join(envs, config)) home = path.normalize(path.join(envs, config))
proj = @findFile(config, dir, upwards) proj = @findFile(config, dir, upwards)
console.log(dir, proj, home) logger.verbose(dir, proj, home)
return proj if proj return proj if proj
return home if @verifyExists(home) return home if @verifyExists(home)
null null
@ -650,6 +641,7 @@ module.exports = class Beautifiers
editorconfig ?= require('editorconfig') editorconfig ?= require('editorconfig')
editorConfigOptions = editorconfig.parse(editedFilePath) editorConfigOptions = editorconfig.parse(editedFilePath)
logger.verbose('editorConfigOptions', editorConfigOptions)
# Transform EditorConfig to Atom Beautify's config structure and naming # Transform EditorConfig to Atom Beautify's config structure and naming
if editorConfigOptions.indent_style is 'space' if editorConfigOptions.indent_style is 'space'
@ -675,11 +667,15 @@ module.exports = class Beautifiers
pf = path.join(p, "FAKEFILENAME") pf = path.join(p, "FAKEFILENAME")
pc = @getConfig(pf, false) pc = @getConfig(pf, false)
isNested = @isNestedOptions(pc)
unless isNested
pc = {
_default: pc
}
# Add config for p to project's config options # Add config for p to project's config options
projectOptions.push(pc) projectOptions.push(pc)
# logger.verbose p, pc # logger.verbose p, pc
# Move upwards # Move upwards
p = path.resolve(p, "../") p = path.resolve(p, "../")
@ -689,48 +685,72 @@ module.exports = class Beautifiers
# Combine all options together # Combine all options together
allOptions = [ allOptions = [
editorOptions {
configOptions _default:
homeOptions editorOptions
},
configOptions,
{
_default:
homeOptions
},
editorConfigOptions editorConfigOptions
] ]
# Reverse and add projectOptions to all options
projectOptions.reverse()
allOptions = allOptions.concat(projectOptions) allOptions = allOptions.concat(projectOptions)
# logger.verbose(allOptions) # logger.verbose(allOptions)
return 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 self = this
_ ?= require("lodash") _ ?= require("lodash")
extend ?= require("extend") extend ?= require("extend")
logger.verbose(selections, allOptions)
# logger.verbose(selection, allOptions); # logger.verbose(selection, allOptions);
# Reduce all options into correctly merged options. # Reduce all options into correctly merged options.
options = _.reduce(allOptions, (result, currOptions) -> options = _.reduce(allOptions, (result, currOptions) =>
containsNested = false collectedConfig = currOptions._default or {}
collectedConfig = {} containsNested = @isNestedOptions(currOptions)
key = undefined logger.verbose(containsNested, currOptions)
# 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
# logger.verbose(containsNested, currOptions); # logger.verbose(containsNested, currOptions);
# Create a flat object of config options if nested format was used # Create a flat object of config options if nested format was used
unless containsNested unless containsNested
_.merge collectedConfig, currOptions # _.merge collectedConfig, currOptions
else currOptions = {
_default: currOptions
}
# Merge with selected options # Merge with selected options
# where `selection` could be `html`, `js`, 'css', etc # where `selection` could be `html`, `js`, 'css', etc
# logger.verbose(selection, currOptions[selection]); for selection in selections
# Merge current options on top of fallback options
logger.verbose('options', selection, currOptions[selection]);
_.merge collectedConfig, currOptions[selection] _.merge collectedConfig, currOptions[selection]
logger.verbose('options', selection, collectedConfig);
extend result, collectedConfig extend result, collectedConfig
, {}) , {})

View File

@ -34,7 +34,7 @@ module.exports = class PrettyDiff extends Beautifier
beautify: (text, language, options) -> beautify: (text, language, options) ->
return new @Promise((resolve, reject) -> return new @Promise((resolve, reject) =>
prettydiff = require("prettydiff") prettydiff = require("prettydiff")
_ = require('lodash') _ = require('lodash')
@ -71,6 +71,7 @@ module.exports = class PrettyDiff extends Beautifier
_.merge(options, args) _.merge(options, args)
# Beautify # Beautify
@verbose('prettydiff', options)
output = prettydiff.api(options) output = prettydiff.api(options)
result = output[0] result = output[0]

View File

@ -43,7 +43,6 @@ setCursors = (editor, posArray) ->
editor.addCursorAtBufferPosition bufferPosition editor.addCursorAtBufferPosition bufferPosition
return return
beautify = ({onSave}) -> beautify = ({onSave}) ->
console.log('beautify!!!')
# Deprecation warning for beautify on save # Deprecation warning for beautify on save
if atom.config.get("atom-beautify.beautifyOnSave") is true if atom.config.get("atom-beautify.beautifyOnSave") is true
detail = """See issue https://github.com/Glavin001/atom-beautify/issues/308 detail = """See issue https://github.com/Glavin001/atom-beautify/issues/308