From af9cc40decaefc9dd6b0bc4beec5bd63ce400277 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Thu, 28 Apr 2016 23:09:55 -0600 Subject: [PATCH 1/6] 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" } From 2872e581ce6d431d8a08547454f2dce844185c1c Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Thu, 28 Apr 2016 23:12:24 -0600 Subject: [PATCH 2/6] Added psteeleidem as a contributor in package.json --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index 107aed5..65574b9 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,10 @@ { "name": "Frederic Delbos", "url": "https://github.com/fdelbos" + }, + { + "name": "Patrick Steele-Idem", + "url": "https://github.com/psteeleidem" } ], "engines": { From a17b73cf4058d722da1558b5caa44585674efe5b Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Thu, 28 Apr 2016 23:23:41 -0600 Subject: [PATCH 3/6] Updated test for marko --- .../nested-jsbeautifyrc/marko/expected/test.marko | 12 +++++------- .../nested-jsbeautifyrc/marko/original/test.marko | 10 ++++------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/examples/nested-jsbeautifyrc/marko/expected/test.marko b/examples/nested-jsbeautifyrc/marko/expected/test.marko index 27e2187..82fc16a 100644 --- a/examples/nested-jsbeautifyrc/marko/expected/test.marko +++ b/examples/nested-jsbeautifyrc/marko/expected/test.marko @@ -1,8 +1,6 @@ - - -
${item.name}
-
+ + +
${item.name}
+
- - content - \ No newline at end of file +content diff --git a/examples/nested-jsbeautifyrc/marko/original/test.marko b/examples/nested-jsbeautifyrc/marko/original/test.marko index 8bf706a..d9154ae 100644 --- a/examples/nested-jsbeautifyrc/marko/original/test.marko +++ b/examples/nested-jsbeautifyrc/marko/original/test.marko @@ -1,8 +1,6 @@ - - -
${item.name}
+ + +
${item.name}
- - content - +content \ No newline at end of file From 9ff770c3f006546a812889d793e030426d4aa36c Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Fri, 29 Apr 2016 00:06:36 -0600 Subject: [PATCH 4/6] Fixed marko indent --- src/beautifiers/marko-beautifier.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/beautifiers/marko-beautifier.coffee b/src/beautifiers/marko-beautifier.coffee index 3449ca5..6c29ee6 100644 --- a/src/beautifiers/marko-beautifier.coffee +++ b/src/beautifiers/marko-beautifier.coffee @@ -17,12 +17,13 @@ module.exports = class MarkoBeautifier extends Beautifier indent = '' - for i in [0...indent_size - 1] by 1 + for i in [0..indent_size - 1] indent += indent_char prettyprintOptions = syntax : options.syntax filename: if context.filePath then context.filePath else require.resolve('marko-prettyprint') + indent: indent try resolve(markoPrettyprint(text, prettyprintOptions)) From e4168688d6215fd937cf6306489b2ee596929421 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Fri, 29 Apr 2016 00:26:07 -0600 Subject: [PATCH 5/6] Updated marko tests --- examples/nested-jsbeautifyrc/marko/expected/test.marko | 6 +++--- examples/simple-jsbeautifyrc/marko/expected/test.marko | 8 ++------ examples/simple-jsbeautifyrc/marko/original/test.marko | 10 ++++------ 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/examples/nested-jsbeautifyrc/marko/expected/test.marko b/examples/nested-jsbeautifyrc/marko/expected/test.marko index 82fc16a..89301a8 100644 --- a/examples/nested-jsbeautifyrc/marko/expected/test.marko +++ b/examples/nested-jsbeautifyrc/marko/expected/test.marko @@ -1,6 +1,6 @@ - -
${item.name}
- + +
${item.name}
+ content diff --git a/examples/simple-jsbeautifyrc/marko/expected/test.marko b/examples/simple-jsbeautifyrc/marko/expected/test.marko index 20e1097..9a6db41 100644 --- a/examples/simple-jsbeautifyrc/marko/expected/test.marko +++ b/examples/simple-jsbeautifyrc/marko/expected/test.marko @@ -1,6 +1,2 @@ - - content - - - content - \ No newline at end of file +content +content diff --git a/examples/simple-jsbeautifyrc/marko/original/test.marko b/examples/simple-jsbeautifyrc/marko/original/test.marko index 43459e8..528cbe3 100644 --- a/examples/simple-jsbeautifyrc/marko/original/test.marko +++ b/examples/simple-jsbeautifyrc/marko/original/test.marko @@ -1,6 +1,4 @@ - -content - - -content - +if(data.items) + - content +else + - content \ No newline at end of file From b5f130db0fde08ca3c6f61bb6e23a867588ad913 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Wed, 1 Jun 2016 13:10:22 -0600 Subject: [PATCH 6/6] Added `html` as a fallback so that settings would be inherited if not set --- src/languages/marko.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/src/languages/marko.coffee b/src/languages/marko.coffee index c1725b3..e1db019 100644 --- a/src/languages/marko.coffee +++ b/src/languages/marko.coffee @@ -10,6 +10,7 @@ module.exports = { name: "Marko" namespace: "marko" + fallback: ['html'] ### Supported Grammars