From af9cc40decaefc9dd6b0bc4beec5bd63ce400277 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Thu, 28 Apr 2016 23:09:55 -0600 Subject: [PATCH] Improved Marko v3 support --- README.md | 1 - docs/add-languages-and-beautifiers.md | 7 +++++- package.json | 1 + src/beautifiers/index.coffee | 7 +++++- src/beautifiers/js-beautify.coffee | 3 +-- src/beautifiers/marko-beautifier.coffee | 32 +++++++++++++++++++++++++ src/languages/marko.coffee | 27 +++++++++++++++++++-- 7 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 src/beautifiers/marko-beautifier.coffee diff --git a/README.md b/README.md index f75b919..81052ee 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,6 @@ Or Settings/Preferences ➔ Packages ➔ Search for `atom-beautify` - [x] XML - [x] SVG - [x] [Marko](https://github.com/marko-js/marko) - - Requires [language-marko](https://github.com/marko-js/atom-language-marko) - [x] CSS, including - [Sass](http://sass-lang.com/) - [Less](http://lesscss.org/) diff --git a/docs/add-languages-and-beautifiers.md b/docs/add-languages-and-beautifiers.md index 63de982..e10a062 100644 --- a/docs/add-languages-and-beautifiers.md +++ b/docs/add-languages-and-beautifiers.md @@ -26,7 +26,12 @@ Now your Language is available and can be detected and beautifiers can support i - Prettydiff is a good example of complex options: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/prettydiff.coffee - PHP-CS-Fixer is a good example of a CLI beautifier with arguments: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/php-cs-fixer.coffee#L15-L39 - `options` - the key represents the Language's name. The value could be `true` (supports all options), `false` (supports language, with no options), or an `object` whose keys are option keys and values are complex mappings. If you need to use these, let me know. `true` is probably what you want. - - The `beautify` function should return a `Promise` (use `@Promise` as shown). The arguments passed are: `text`, the source code from Atom's Text Editor, `language` is a string of the language's name (`JavaScript`), and `options` is an object of all of the options in their form as described by your Language definition (see `Configure the new language` above). + - The `beautify` function should return a `Promise` (use `@Promise` as shown). The arguments passed are: + - __`text`__ - the source code from Atom's Text Editor + - __`language`__ - the language's name (`JavaScript`) + - __`options`__ - an object of all of the options in their form as described by your Language definition (see `Configure the new language` above). + - __`context`__ - an object with extra information: + - __`filePath`__ - The file path associated with the text being beautified (may be null) 3. Add beautifier to list of `beautifierNames`: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/index.coffee#L34 4. Add test example files in https://github.com/Glavin001/atom-beautify/tree/master/examples - You can put your test in `nested-jsbeautifyrc` directory, https://github.com/Glavin001/atom-beautify/tree/master/examples/nested-jsbeautifyrc diff --git a/package.json b/package.json index 96b5fc6..107aed5 100644 --- a/package.json +++ b/package.json @@ -108,6 +108,7 @@ "jscs": "^3.0.3", "lodash": "^4.8.2", "loophole": "^1.0.0", + "marko-prettyprint": "^1.1.0", "node-dir": "^0.1.8", "node-uuid": "^1.4.3", "prettydiff": "^1.16.27", diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index 3c15caf..5c97d32 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -66,6 +66,7 @@ module.exports = class Beautifiers extends EventEmitter 'typescript-formatter' 'yapf' 'erl_tidy' + 'marko-beautifier' ] ### @@ -276,7 +277,11 @@ module.exports = class Beautifiers extends EventEmitter # Beautify text with language options @emit "beautify::start" - beautifier.beautify(text, language.name, options) + + context = + filePath: filePath + + beautifier.beautify(text, language.name, options, context) .then(resolve) .catch(reject) .finally(=> diff --git a/src/beautifiers/js-beautify.coffee b/src/beautifiers/js-beautify.coffee index efc1e3b..17e323c 100644 --- a/src/beautifiers/js-beautify.coffee +++ b/src/beautifiers/js-beautify.coffee @@ -9,7 +9,6 @@ module.exports = class JSBeautify extends Beautifier XML: true Handlebars: true Mustache: true - Marko: true JavaScript: true JSON: true CSS: @@ -41,7 +40,7 @@ module.exports = class JSBeautify extends Beautifier beautifyHTML = require("js-beautify").html text = beautifyHTML(text, options) resolve text - when "HTML (Liquid)", "HTML", "XML", "Marko", "Web Form/Control (C#)", "Web Handler (C#)" + when "HTML (Liquid)", "HTML", "XML", "Web Form/Control (C#)", "Web Handler (C#)" beautifyHTML = require("js-beautify").html text = beautifyHTML(text, options) @debug("Beautified HTML: #{text}") diff --git a/src/beautifiers/marko-beautifier.coffee b/src/beautifiers/marko-beautifier.coffee new file mode 100644 index 0000000..3449ca5 --- /dev/null +++ b/src/beautifiers/marko-beautifier.coffee @@ -0,0 +1,32 @@ +"use strict" +Beautifier = require('./beautifier') + +module.exports = class MarkoBeautifier extends Beautifier + name: 'Marko Beautifier' + + options: + Marko: true + + beautify: (text, language, options, context) -> + + return new @Promise((resolve, reject) -> + markoPrettyprint = require('marko-prettyprint') + + indent_char = options.indent_char || ' ' + indent_size = options.indent_size || 4 + + indent = '' + + for i in [0...indent_size - 1] by 1 + indent += indent_char + + prettyprintOptions = + syntax : options.syntax + filename: if context.filePath then context.filePath else require.resolve('marko-prettyprint') + + try + resolve(markoPrettyprint(text, prettyprintOptions)) + catch error + # Error occurred + reject(error) + ) diff --git a/src/languages/marko.coffee b/src/languages/marko.coffee index f8df162..c1725b3 100644 --- a/src/languages/marko.coffee +++ b/src/languages/marko.coffee @@ -1,8 +1,15 @@ +# Get Atom defaults +scope = ['text.marko'] +tabLength = atom?.config.get('editor.tabLength', scope: scope) ? 4 +softTabs = atom?.config.get('editor.softTabs', scope: scope) ? true +defaultIndentSize = (if softTabs then tabLength else 4) +defaultIndentChar = (if softTabs then " " else "\t") +defaultIndentWithTabs = not softTabs + module.exports = { name: "Marko" namespace: "marko" - fallback: ['html'] ### Supported Grammars @@ -18,6 +25,22 @@ module.exports = { "marko" ] - options: [] + options: + indent_size: + type: 'integer' + default: defaultIndentSize + minimum: 0 + description: "Indentation size/length" + indent_char: + type: 'string' + default: defaultIndentChar + description: "Indentation character" + syntax: + type: 'string' + default: "html" + enum: ["html", "concise"] + description: "[html|concise]" + + defaultBeautifier: "Marko Beautifier" }