2014-08-10 15:51:44 -06:00
|
|
|
###
|
|
|
|
Language Support and default options.
|
|
|
|
###
|
|
|
|
"use strict"
|
2014-09-07 16:32:08 -06:00
|
|
|
# Lazy loaded dependencies
|
|
|
|
_ = null
|
|
|
|
extend = null
|
2014-08-10 15:51:44 -06:00
|
|
|
# Language Beautifiers
|
2014-09-07 16:32:08 -06:00
|
|
|
beautifyJS = null
|
|
|
|
beautifyHTML = null
|
|
|
|
beautifyCSS = null
|
|
|
|
beautifySQL = null
|
|
|
|
beautifyPHP = null
|
|
|
|
beautifyPython = null
|
|
|
|
beautifyRuby = null
|
|
|
|
beautifyLESS = null
|
|
|
|
beautifyCoffeeScript = null
|
|
|
|
uncrustifyBeautifier = null
|
2014-09-08 15:50:54 -06:00
|
|
|
beautifyHTMLERB = null
|
2014-08-10 15:51:44 -06:00
|
|
|
|
|
|
|
# Misc
|
|
|
|
Analytics = require("analytics-node")
|
|
|
|
pkg = require("../package.json")
|
|
|
|
|
|
|
|
# Analytics
|
|
|
|
analyticsWriteKey = "u3c26xkae8"
|
|
|
|
module.exports =
|
|
|
|
|
|
|
|
# Supported unique configuration keys
|
|
|
|
# Used for detecting nested configurations in .jsbeautifyrc
|
|
|
|
languages: [
|
|
|
|
"js"
|
|
|
|
"html"
|
|
|
|
"css"
|
|
|
|
"sql"
|
|
|
|
"php"
|
|
|
|
"python"
|
|
|
|
"ruby"
|
|
|
|
"coffeescript"
|
|
|
|
"c"
|
|
|
|
"cpp"
|
|
|
|
"cs"
|
|
|
|
"objectivec"
|
|
|
|
"java"
|
|
|
|
"d"
|
|
|
|
"pawn"
|
|
|
|
"vala"
|
|
|
|
]
|
|
|
|
|
|
|
|
# Default options per language
|
|
|
|
defaultLanguageOptions:
|
|
|
|
|
|
|
|
# jshint ignore: start
|
|
|
|
|
|
|
|
# JavaScript
|
|
|
|
js_indent_size: 2
|
|
|
|
js_indent_char: " "
|
|
|
|
js_indent_level: 0
|
|
|
|
js_indent_with_tabs: false
|
|
|
|
js_preserve_newlines: true
|
|
|
|
js_max_preserve_newlines: 10
|
|
|
|
js_jslint_happy: false
|
|
|
|
js_brace_style: "collapse"
|
|
|
|
js_keep_array_indentation: false
|
|
|
|
js_keep_function_indentation: false
|
|
|
|
js_space_before_conditional: true
|
|
|
|
js_break_chained_methods: false
|
|
|
|
js_eval_code: false
|
|
|
|
js_unescape_strings: false
|
|
|
|
js_wrap_line_length: 0
|
|
|
|
|
|
|
|
# CSS
|
|
|
|
css_indent_size: 2
|
|
|
|
css_indent_Char: " "
|
|
|
|
|
|
|
|
# HTML
|
|
|
|
html_indent_inner_html: false
|
|
|
|
html_indent_size: 2
|
|
|
|
html_indent_char: " "
|
|
|
|
html_brace_style: "collapse"
|
|
|
|
html_indent_scripts: "normal"
|
|
|
|
html_wrap_line_length: 250
|
|
|
|
|
|
|
|
# SQL
|
|
|
|
sql_indent_size: 2
|
2014-08-17 08:01:53 -06:00
|
|
|
sql_keywords: "upper"
|
|
|
|
sql_identifiers: "lower"
|
|
|
|
sql_sqlformat_path: ""
|
2014-08-10 15:51:44 -06:00
|
|
|
|
|
|
|
# PHP
|
|
|
|
php_beautifier_path: ""
|
|
|
|
|
|
|
|
# Python
|
|
|
|
python_autopep8_path: ""
|
|
|
|
python_max_line_length: 79
|
|
|
|
python_indent_size: 4
|
|
|
|
python_ignore: ["E24"]
|
|
|
|
|
|
|
|
# Ruby
|
|
|
|
ruby_rbeautify_path: ""
|
|
|
|
|
|
|
|
# C
|
|
|
|
c_configPath: ""
|
|
|
|
|
|
|
|
# C++
|
|
|
|
cpp_configPath: ""
|
|
|
|
|
|
|
|
# Objective-C
|
|
|
|
objectivec_configPath: ""
|
|
|
|
|
|
|
|
# C#
|
|
|
|
cs_configPath: ""
|
|
|
|
|
|
|
|
# Objective-C
|
|
|
|
d_configPath: ""
|
|
|
|
|
|
|
|
# Java
|
|
|
|
java_configPath: ""
|
|
|
|
|
|
|
|
# Pawn
|
|
|
|
pawn_configPath: ""
|
|
|
|
|
|
|
|
# VALA
|
|
|
|
vala_configPath: ""
|
|
|
|
|
2014-09-14 18:31:37 -06:00
|
|
|
# Uncrustify
|
2014-09-14 19:15:48 -06:00
|
|
|
uncrustifyPath: ""
|
2014-09-14 18:31:37 -06:00
|
|
|
|
2014-08-10 15:51:44 -06:00
|
|
|
|
|
|
|
# jshint ignore: end
|
|
|
|
|
|
|
|
# Process each language
|
|
|
|
beautify: (text, grammar, allOptions, beautifyCompleted) ->
|
|
|
|
self = this
|
|
|
|
# Beautify!
|
|
|
|
unsupportedGrammar = false
|
|
|
|
options = undefined
|
|
|
|
switch grammar
|
|
|
|
# Treat JSON as JavaScript, because it will support comments.
|
|
|
|
# And Glavin001 has tested JSON beauifying with beautifyJS.
|
|
|
|
when "JSON", "JavaScript"
|
2014-09-07 16:32:08 -06:00
|
|
|
beautifyJS ?= require("js-beautify")
|
2014-08-10 15:51:44 -06:00
|
|
|
text = beautifyJS(text, self.getOptions("js", allOptions))
|
|
|
|
beautifyCompleted text
|
|
|
|
when "CoffeeScript"
|
2014-09-07 16:32:08 -06:00
|
|
|
beautifyCoffeeScript ?= require("./langs/coffeescript-beautify")
|
2014-08-10 15:51:44 -06:00
|
|
|
beautifyCoffeeScript text, self.getOptions("js", allOptions), beautifyCompleted
|
|
|
|
when "Handlebars"
|
|
|
|
# jshint ignore: start
|
|
|
|
allOptions.push indent_handlebars: true # Force jsbeautify to indent_handlebars
|
|
|
|
# jshint ignore: end
|
|
|
|
when "HTML (Liquid)", "HTML", "XML"
|
2014-09-07 16:32:08 -06:00
|
|
|
beautifyHTML ?= require("js-beautify").html
|
2014-08-10 15:51:44 -06:00
|
|
|
text = beautifyHTML(text, self.getOptions("html", allOptions))
|
|
|
|
beautifyCompleted text
|
2014-09-08 15:50:54 -06:00
|
|
|
when "HTML (Ruby - ERB)"
|
|
|
|
beautifyHTMLERB ?= require("./langs/html-erb-beautify")
|
|
|
|
beautifyHTMLERB text, self.getOptions("html", allOptions), beautifyCompleted
|
2014-08-10 15:51:44 -06:00
|
|
|
when "CSS"
|
2014-09-07 16:32:08 -06:00
|
|
|
beautifyCSS ?= require("js-beautify").css
|
2014-08-10 15:51:44 -06:00
|
|
|
text = beautifyCSS(text, self.getOptions("css", allOptions))
|
|
|
|
beautifyCompleted text
|
|
|
|
when "Sass", "SCSS", "LESS"
|
2014-09-07 16:32:08 -06:00
|
|
|
beautifyLESS ?= require("./langs/less-beautify")
|
2014-08-10 15:51:44 -06:00
|
|
|
beautifyLESS text, self.getOptions("css", allOptions), beautifyCompleted
|
|
|
|
when "SQL (Rails)", "SQL"
|
2014-09-07 16:32:08 -06:00
|
|
|
beautifySQL ?= require("./langs/sql-beautify")
|
2014-08-17 08:01:53 -06:00
|
|
|
beautifySQL text, self.getOptions("sql", allOptions), beautifyCompleted
|
2014-08-10 15:51:44 -06:00
|
|
|
when "PHP"
|
2014-09-07 16:32:08 -06:00
|
|
|
beautifyPHP ?= require("./langs/php-beautify")
|
2014-08-10 15:51:44 -06:00
|
|
|
beautifyPHP text, self.getOptions("php", allOptions), beautifyCompleted
|
|
|
|
when "Python"
|
2014-09-07 16:32:08 -06:00
|
|
|
beautifyPython ?= require("./langs/python-beautify")
|
2014-08-10 15:51:44 -06:00
|
|
|
beautifyPython text, self.getOptions("python", allOptions), beautifyCompleted
|
|
|
|
when "Ruby"
|
2014-09-07 16:32:08 -06:00
|
|
|
beautifyRuby ?= require("./langs/ruby-beautify")
|
2014-08-10 15:51:44 -06:00
|
|
|
beautifyRuby text, self.getOptions("ruby", allOptions), beautifyCompleted
|
|
|
|
when "C"
|
|
|
|
options = self.getOptions("c", allOptions)
|
|
|
|
options.languageOverride = "C"
|
2014-09-07 16:32:08 -06:00
|
|
|
uncrustifyBeautifier ?= require("./langs/uncrustify/")
|
2014-08-10 15:51:44 -06:00
|
|
|
uncrustifyBeautifier text, options, beautifyCompleted
|
|
|
|
when "C++"
|
|
|
|
options = self.getOptions("cpp", allOptions)
|
|
|
|
options.languageOverride = "CPP"
|
2014-09-07 16:32:08 -06:00
|
|
|
uncrustifyBeautifier ?= require("./langs/uncrustify/")
|
2014-08-10 15:51:44 -06:00
|
|
|
uncrustifyBeautifier text, options, beautifyCompleted
|
|
|
|
when "C#"
|
|
|
|
options = self.getOptions("cs", allOptions)
|
|
|
|
options.languageOverride = "CS"
|
2014-09-07 16:32:08 -06:00
|
|
|
uncrustifyBeautifier ?= require("./langs/uncrustify/")
|
2014-08-10 15:51:44 -06:00
|
|
|
uncrustifyBeautifier text, options, beautifyCompleted
|
|
|
|
when "Objective-C", "Objective-C++"
|
|
|
|
options = self.getOptions("objectivec", allOptions)
|
|
|
|
options.languageOverride = "OC+"
|
2014-09-07 16:32:08 -06:00
|
|
|
uncrustifyBeautifier ?= require("./langs/uncrustify/")
|
2014-08-10 15:51:44 -06:00
|
|
|
uncrustifyBeautifier text, options, beautifyCompleted
|
|
|
|
when "D"
|
|
|
|
options = self.getOptions("d", allOptions)
|
|
|
|
options.languageOverride = "D"
|
2014-09-07 16:32:08 -06:00
|
|
|
uncrustifyBeautifier ?= require("./langs/uncrustify/")
|
2014-08-10 15:51:44 -06:00
|
|
|
uncrustifyBeautifier text, options, beautifyCompleted
|
|
|
|
when "Pawn"
|
|
|
|
options = self.getOptions("pawn", allOptions)
|
|
|
|
options.languageOverride = "PAWN"
|
2014-09-07 16:32:08 -06:00
|
|
|
uncrustifyBeautifier ?= require("./langs/uncrustify/")
|
2014-08-10 15:51:44 -06:00
|
|
|
uncrustifyBeautifier text, options, beautifyCompleted
|
|
|
|
when "Vala"
|
|
|
|
options = self.getOptions("vala", allOptions)
|
|
|
|
options.languageOverride = "VALA"
|
2014-09-07 16:32:08 -06:00
|
|
|
uncrustifyBeautifier ?= require("./langs/uncrustify/")
|
2014-08-10 15:51:44 -06:00
|
|
|
uncrustifyBeautifier text, options, beautifyCompleted
|
|
|
|
when "Java"
|
|
|
|
options = self.getOptions("java", allOptions)
|
|
|
|
options.languageOverride = "JAVA"
|
2014-09-07 16:32:08 -06:00
|
|
|
uncrustifyBeautifier ?= require("./langs/uncrustify/")
|
2014-08-10 15:51:44 -06:00
|
|
|
uncrustifyBeautifier text, options, beautifyCompleted
|
|
|
|
else
|
|
|
|
unsupportedGrammar = true
|
|
|
|
|
|
|
|
# Check if Analytics is enabled
|
|
|
|
if atom.config.get("atom-beautify.analytics")
|
|
|
|
# Setup Analytics
|
|
|
|
analytics = new Analytics(analyticsWriteKey)
|
|
|
|
unless atom.config.get("atom-beautify._analyticsUserId")
|
|
|
|
uuid = require("node-uuid")
|
|
|
|
atom.config.set "atom-beautify._analyticsUserId", uuid.v4()
|
|
|
|
# Setup Analytics User Id
|
|
|
|
userId = atom.config.get("atom-beautify._analyticsUserId")
|
|
|
|
analytics.identify userId: userId
|
|
|
|
version = pkg.version
|
|
|
|
analytics.track
|
|
|
|
userId: atom.config.get("atom-beautify._analyticsUserId")
|
|
|
|
event: "Beautify"
|
|
|
|
properties:
|
|
|
|
grammar: grammar
|
|
|
|
version: version
|
|
|
|
options: allOptions
|
|
|
|
label: grammar
|
|
|
|
category: version
|
2014-09-07 22:02:23 -06:00
|
|
|
#
|
|
|
|
if unsupportedGrammar
|
|
|
|
throw new Error("Unsupported language for grammar '#{grammar}'.")
|
2014-08-10 15:51:44 -06:00
|
|
|
return
|
|
|
|
|
|
|
|
getOptions: (selection, allOptions) ->
|
|
|
|
self = this
|
2014-09-07 16:32:08 -06:00
|
|
|
_ ?= require("lodash")
|
|
|
|
extend ?= require("extend")
|
2014-08-10 15:51:44 -06:00
|
|
|
# console.log(selection, allOptions);
|
|
|
|
# Reduce all options into correctly merged options.
|
|
|
|
options = _.reduce(allOptions, (result, currOptions) ->
|
|
|
|
containsNested = false
|
|
|
|
collectedConfig = {}
|
|
|
|
key = undefined
|
|
|
|
# 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, 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
|
|
|
|
# console.log(containsNested, currOptions);
|
|
|
|
# Create a flat object of config options if nested format was used
|
|
|
|
unless containsNested
|
|
|
|
_.merge collectedConfig, currOptions
|
|
|
|
else
|
|
|
|
# Merge with selected options
|
|
|
|
# where `selection` could be `html`, `js`, 'css', etc
|
|
|
|
# console.log(selection, currOptions[selection]);
|
|
|
|
_.merge collectedConfig, currOptions[selection]
|
|
|
|
extend result, collectedConfig
|
|
|
|
, {})
|
|
|
|
# TODO: Clean.
|
|
|
|
# There is a bug in nopt
|
|
|
|
# See https://github.com/npm/nopt/issues/38#issuecomment-45971505
|
|
|
|
# console.log('pre-clean', JSON.stringify(options));
|
|
|
|
#options = cleanOptions(options, knownOpts);
|
|
|
|
#console.log('post-clean', JSON.stringify(options));
|
|
|
|
options
|