diff --git a/docs/options.md b/docs/options.md index e315e7e..9679b3a 100644 --- a/docs/options.md +++ b/docs/options.md @@ -3579,6 +3579,121 @@ Maximum characters per line (0 disables) (Supported by JS Beautify, Pretty Diff) } ``` +#### [Jade](#jade) + +**Supported Beautifiers**: [`Pug Beautify`](#pug-beautify) [`Pug Beautify`](#pug-beautify) + +**Description**: + +Options for language Jade + +##### [Disable Beautifying Language](#disable-beautifying-language) + +**Important**: This option is only configurable from within Atom Beautify's setting panel. + +**Type**: `boolean` + +**Description**: + +Disable Jade Beautification + +**How to Configure** + +1. You can open the [Settings View](https://github.com/atom/settings-view) by navigating to +*Edit > Preferences (Linux)*, *Atom > Preferences (OS X)*, or *File > Preferences (Windows)*. +2. Go into *Packages* and search for "*Atom Beautify*" package. +3. Find the option "*Disable Beautifying Language*" and change it to your desired configuration. + +##### [Default Beautifier](#default-beautifier) + +**Important**: This option is only configurable from within Atom Beautify's setting panel. + +**Default**: `Pug Beautify` + +**Type**: `string` + +**Enum**: `Pug Beautify` + +**Description**: + +Default Beautifier to be used for Jade + +**How to Configure** + +1. You can open the [Settings View](https://github.com/atom/settings-view) by navigating to +*Edit > Preferences (Linux)*, *Atom > Preferences (OS X)*, or *File > Preferences (Windows)*. +2. Go into *Packages* and search for "*Atom Beautify*" package. +3. Find the option "*Default Beautifier*" and change it to your desired configuration. + +##### [Beautify On Save](#beautify-on-save) + +**Important**: This option is only configurable from within Atom Beautify's setting panel. + +**Type**: `boolean` + +**Description**: + +Automatically beautify Jade files on save + +**How to Configure** + +1. You can open the [Settings View](https://github.com/atom/settings-view) by navigating to +*Edit > Preferences (Linux)*, *Atom > Preferences (OS X)*, or *File > Preferences (Windows)*. +2. Go into *Packages* and search for "*Atom Beautify*" package. +3. Find the option "*Beautify On Save*" and change it to your desired configuration. + +##### [Indent char](#indent-char) + +**Namespace**: `html` + +**Key**: `indent_char` + +**Default**: ` ` + +**Type**: `string` + +**Supported Beautifiers**: [`Pug Beautify`](#pug-beautify) + +**Description**: + +Indentation character (Supported by Pug Beautify) + +**Example `.jsbeautifyrc` Configuration** + +```json +{ + "html": { + "indent_char": " " + } +} +``` + +##### [Indent size](#indent-size) + +**Namespace**: `html` + +**Key**: `indent_size` + +**Default**: `4` + +**Type**: `integer` + +**Supported Beautifiers**: [`Pug Beautify`](#pug-beautify) + +**Description**: + +Indentation size/length (Supported by Pug Beautify) + +**Example `.jsbeautifyrc` Configuration** + +```json +{ + "html": { + "indent_size": 4 + } +} +``` + #### [Java](#java) **Supported Beautifiers**: [`Uncrustify`](#uncrustify) @@ -13086,6 +13201,61 @@ If a terminating comma should be inserted into arrays, object literals, and dest ``` +### Pug Beautify + +##### [Indent size](#indent-size) + +**Namespace**: `html` + +**Key**: `indent_size` + +**Default**: `4` + +**Type**: `integer` + +**Supported Beautifiers**: [`Pug Beautify`](#pug-beautify) + +**Description**: + +Indentation size/length (Supported by Pug Beautify) + +**Example `.jsbeautifyrc` Configuration** + +```json +{ + "html": { + "indent_size": 4 + } +} +``` + +##### [Indent char](#indent-char) + +**Namespace**: `html` + +**Key**: `indent_char` + +**Default**: ` ` + +**Type**: `string` + +**Supported Beautifiers**: [`Pug Beautify`](#pug-beautify) + +**Description**: + +Indentation character (Supported by Pug Beautify) + +**Example `.jsbeautifyrc` Configuration** + +```json +{ + "html": { + "indent_char": " " + } +} +``` + + ### Remark ##### [Gfm](#gfm) diff --git a/examples/nested-jsbeautifyrc/.jsbeautifyrc b/examples/nested-jsbeautifyrc/.jsbeautifyrc index 98eac37..57240ac 100644 --- a/examples/nested-jsbeautifyrc/.jsbeautifyrc +++ b/examples/nested-jsbeautifyrc/.jsbeautifyrc @@ -55,3 +55,7 @@ indent_size: 4 markdown: listItemIndent: 1 + jade: + indent_size: 4 + indent_char: " " + omit_div: true diff --git a/examples/nested-jsbeautifyrc/jade/expected/test.jade b/examples/nested-jsbeautifyrc/jade/expected/test.jade new file mode 100644 index 0000000..d9fd833 --- /dev/null +++ b/examples/nested-jsbeautifyrc/jade/expected/test.jade @@ -0,0 +1,30 @@ +extends ../layout/basiclayout + +block append stylesheets + link(rel='stylesheet', href='/bower_components/bootstrap-table/dist/bootstrap-table.css') + style. + .sample_id { + color: #337AB7; + cursor: pointer; + } + +block content + .container + .col-lg-10 + // Comment + .page-header + h1 Sample Anaysis + small Find a sample + div + #toolbar + table#table + + //- div.well + //- | Lorem ipsum dolor sit amet, natum bonorum expetendis usu ut. Eum impetus offendit disputationi eu, at vim aliquip lucilius praesent. Alia laudem antiopam te ius, sed ad munere integre, ubique facete sapientem nam ut. + //- br + //- br + //- ul + //- li Lorem ipsum dolor sit amet, natum bonorum expetendis usu ut. + //- li Lorem ipsum dolor sit amet, natum bonorum expetendis usu ut. +block append scripts + script(src='/bower_components/bootstrap-table/dist/bootstrap-table.js') \ No newline at end of file diff --git a/examples/nested-jsbeautifyrc/jade/original/test.jade b/examples/nested-jsbeautifyrc/jade/original/test.jade new file mode 100644 index 0000000..f860d54 --- /dev/null +++ b/examples/nested-jsbeautifyrc/jade/original/test.jade @@ -0,0 +1,30 @@ +extends ../layout/basiclayout + +block append stylesheets + link(rel='stylesheet', href='/bower_components/bootstrap-table/dist/bootstrap-table.css') + style. + .sample_id { + color: #337AB7; + cursor: pointer; + } + +block content + div.container + div.col-lg-10 + // Comment + div.page-header + h1 Sample Anaysis + small Find a sample + div + div#toolbar + table#table + + //- div.well + //- | Lorem ipsum dolor sit amet, natum bonorum expetendis usu ut. Eum impetus offendit disputationi eu, at vim aliquip lucilius praesent. Alia laudem antiopam te ius, sed ad munere integre, ubique facete sapientem nam ut. + //- br + //- br + //- ul + //- li Lorem ipsum dolor sit amet, natum bonorum expetendis usu ut. + //- li Lorem ipsum dolor sit amet, natum bonorum expetendis usu ut. +block append scripts + script(src='/bower_components/bootstrap-table/dist/bootstrap-table.js') \ No newline at end of file diff --git a/package.json b/package.json index 6cb34de..ee32092 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "atom-beautify", "main": "./src/beautify", - "version": "0.28.27", + "version": "0.28.28", "private": true, "description": "Beautify HTML, CSS, JavaScript, PHP, Python, Ruby, Java, C, C++, C#, Objective-C, CoffeeScript, TypeScript, Coldfusion, and SQL in Atom", "repository": { @@ -104,6 +104,7 @@ "node-dir": "^0.1.8", "node-uuid": "^1.4.3", "prettydiff": "^1.16.27", + "pug-beautify": "^0.1.1", "remark": "^3.2.2", "season": "^5.3.0", "space-pen": "^5.1.1", diff --git a/script/build-options.coffee b/script/build-options.coffee index 9578e0b..590d57f 100644 --- a/script/build-options.coffee +++ b/script/build-options.coffee @@ -4,6 +4,198 @@ _ = require('lodash') _plus = require('underscore-plus') Beautifiers = require("../src/beautifiers") + +buildOptionsForBeautifiers = (beautifiers, allLanguages) -> + + # Get all Options for Languages + langOptions = {} + languages = {} # Hash map of languages with their names + for lang in allLanguages + 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('.') + .map(_plus.capitalize).join(' ') + op.title = "#{lang.name} - #{op.title}" + + + # Init field for supported beautifiers + op.beautifiers = [] + + # Remember Option's Key + op.key = field + + # Remember Option's Language + op.language = { + name: lang.name, + namespace: lang.namespace + } + + # Add option + options[field] = op + + # Find supported beautifiers for each language + for beautifier in beautifiers + beautifierName = beautifier.name + + + # Iterate over supported languages + for languageName, options of beautifier.options + laOp = langOptions[languageName] + + + # Is a valid Language name + if typeof options is "boolean" + + # Enable / disable all options + # Add Beautifier support to Language + languages[languageName]?.beautifiers.push(beautifierName) + + + # Check for beautifier's options support + if options is true + + # Beautifier supports all options for this language + if laOp + + # logger.verbose('add supported beautifier', languageName, beautifierName) + for field, op of laOp + op.beautifiers.push(beautifierName) + else + + # Supports language but no options specifically + logger.warn("Could not find options for language: #{languageName}") + else if typeof options is "object" + + # Iterate over beautifier's options for this language + for field, op of options + 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 + # logger.verbose('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) + else + + # Unsupported + logger.warn("Unsupported option:", beautifierName, languageName, field, op, langOptions) + + # Prefix language's options with namespace + for langName, ops of langOptions + + # Get language with name + lang = languages[langName] + + + # Use the namespace from language as key prefix + prefix = lang.namespace + + + # logger.verbose(langName, lang, prefix, ops) + # Iterate over all language options and rename fields + for field, op of ops + + # Rename field + delete ops[field] + ops["#{prefix}_#{field}"] = op + + # Flatten Options per language to array of all options + allOptions = _.values(langOptions) + + + # logger.verbose('allOptions', allOptions) + # Flatten array of objects to single object for options + flatOptions = _.reduce(allOptions, ((result, languageOptions, language) -> + + # Iterate over fields (keys) in Language's Options + # and merge them into single result + # logger.verbose('language options', language, languageOptions, result) + return _.reduce(languageOptions, ((result, optionDef, optionName) -> + + # TODO: Add supported beautifiers to option description + # logger.verbose('optionDef', optionDef, optionName) + if optionDef.beautifiers.length > 0 + + # optionDef.title = " + optionDef.description = "#{optionDef.description} (Supported by #{optionDef.beautifiers.join(', ')})" + else + + # optionDef.title = "(DEPRECATED) + optionDef.description = "#{optionDef.description} (Not supported by any beautifiers)" + if result[optionName]? + logger.warn("Duplicate option detected: ", optionName, optionDef) + result[optionName] = optionDef + return result + ), result) + ), {}) + + + # Generate Language configurations + # logger.verbose('languages', languages) + for langName, lang of languages + + # logger.verbose(langName, lang) + name = lang.name + beautifiers = lang.beautifiers + optionName = "language_#{lang.namespace}" + + # Add Language configurations + flatOptions["#{optionName}_disabled"] = { + title : "Language Config - #{name} - Disable Beautifying Language" + type : 'boolean' + default : false + description : "Disable #{name} Beautification" + } + flatOptions["#{optionName}_default_beautifier"] = { + title : "Language Config - #{name} - Default Beautifier" + type : 'string' + default : lang.defaultBeautifier ? beautifiers[0] + description : "Default Beautifier to be used for #{name}" + enum : _.uniq(beautifiers) + } + flatOptions["#{optionName}_beautify_on_save"] = { + title : "Language Config - #{name} - Beautify On Save" + type : 'boolean' + default : false + description : "Automatically beautify #{name} files on save" + } + + # logger.verbose('flatOptions', flatOptions) + return flatOptions + + buildOptionsForBeautifiers = (beautifiers, allLanguages) -> # Get all Options for Languages diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index 73b2c8e..23a680e 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -52,6 +52,7 @@ module.exports = class Beautifiers extends EventEmitter 'php-cs-fixer' 'phpcbf' 'prettydiff' + 'pug-beautify' 'puppet-fix' 'remark' 'rubocop' diff --git a/src/beautifiers/pug-beautify.coffee b/src/beautifiers/pug-beautify.coffee new file mode 100644 index 0000000..df07892 --- /dev/null +++ b/src/beautifiers/pug-beautify.coffee @@ -0,0 +1,26 @@ +"use strict" +Beautifier = require('./beautifier') + +module.exports = class PugBeautify extends Beautifier + name: "Pug Beautify" + options: { + # Apply these options first / globally, for all languages + Jade: + fill_tab: ['indent_char', (indent_char) -> + # Should use tabs? + return (indent_char is "\t") + ] + omit_div: true + tab_size: "indent_size" + } + + beautify: (text, language, options) -> + + return new @Promise((resolve, reject) -> + pugBeautify = require("pug-beautify") + try + resolve(pugBeautify(text, options)) + catch error + # Error occurred + reject(error) + ) diff --git a/src/languages/html.coffee b/src/languages/html.coffee index a131be6..7043bf7 100644 --- a/src/languages/html.coffee +++ b/src/languages/html.coffee @@ -38,7 +38,6 @@ module.exports = { indent_char: type: 'string' default: defaultIndentChar - minimum: 0 description: "Indentation character" brace_style: type: 'string' diff --git a/src/languages/index.coffee b/src/languages/index.coffee index 8ad80e2..b6980eb 100644 --- a/src/languages/index.coffee +++ b/src/languages/index.coffee @@ -32,6 +32,7 @@ module.exports = class Languages "handlebars" "haskell" "html" + "jade" "java" "javascript" "json" diff --git a/src/languages/jade.coffee b/src/languages/jade.coffee new file mode 100644 index 0000000..2f2eb82 --- /dev/null +++ b/src/languages/jade.coffee @@ -0,0 +1,47 @@ +# Get Atom defaults +scope = ['text.jade'] +tabLength = atom?.config.get('editor.tabLength', scope: scope) ? 4 +softTabs = atom?.config.get('editor.softTabs', scope: scope) ? true +defaultIndentSize = (if softTabs then tabLength else 1) +defaultIndentChar = (if softTabs then " " else "\t") +defaultIndentWithTabs = not softTabs + +module.exports = { + + name: "Jade" + namespace: "jade" + fallback: ['html'] + + ### + Supported Grammars + ### + grammars: [ + "Jade" + ] + + ### + Supported extensions + ### + extensions: [ + "jade" + ] + + options: [ + indent_size: + type: 'integer' + default: defaultIndentSize + minimum: 0 + description: "Indentation size/length" + indent_char: + type: 'string' + default: defaultIndentChar + description: "Indentation character" + omit_div: + type: 'boolean' + default: false + description: "Whether to omit/remove the 'div' tags." + ] + + defaultBeautifier: "Pug Beautify" + +}