Skip to content

Commit

Permalink
Closes #284, #282. Language config options in Package settings
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Glavin001 committed May 2, 2015
1 parent a30dc62 commit a226261
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
60 changes: 50 additions & 10 deletions src/beautifiers/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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('.')
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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

###
Expand All @@ -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) =>

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/beautifiers/prettydiff.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = class PrettyDiff extends Beautifier
HTML: true
XML: true
Spacebars: true
JSX: true

This comment has been minimized.

Copy link
@Glavin001

Glavin001 May 3, 2015

Author Owner

Closes #144

JavaScript: true
CSS: true
SCSS: true
Expand Down
6 changes: 0 additions & 6 deletions src/beautify.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a226261

Please sign in to comment.