diff --git a/examples/nested-jsbeautifyrc/markdown/expected/test.md b/examples/nested-jsbeautifyrc/markdown/expected/test.md index 52dd9fc..6cda824 100644 --- a/examples/nested-jsbeautifyrc/markdown/expected/test.md +++ b/examples/nested-jsbeautifyrc/markdown/expected/test.md @@ -1,5 +1,4 @@ -- item - 1. one - 2. two - 3. three - +- item + 1. one + 2. two + 3. three diff --git a/examples/nested-jsbeautifyrc/sql/expected/test.sql b/examples/nested-jsbeautifyrc/sql/expected/test.sql index 2925130..9a4aab3 100644 --- a/examples/nested-jsbeautifyrc/sql/expected/test.sql +++ b/examples/nested-jsbeautifyrc/sql/expected/test.sql @@ -3,7 +3,7 @@ SELECT ca.proj_id AS proj_id, ca.ca_date_start AS proj_start, ca.ca_date_end AS proj_end, - (SELECT count(*) + (SELECT COUNT(*) FROM rotations r WHERE r.proj_id = proj_id AND r.r_status = 'R' @@ -20,6 +20,6 @@ WHERE ca.client_id = 12345 AND ca.client_id = c.client_id AND ca_type = 'zzz' AND c.agency_id = 0 - AND ca.client_id = nvl(caa.client_id, ca.client_id) - AND proj_id = nvl(caa.proj_id, proj_id) + AND ca.client_id = NVL(caa.client_id, ca.client_id) + AND proj_id = NVL(caa.proj_id, proj_id) AND caa.contact_id = 7890 \ No newline at end of file diff --git a/examples/simple-jsbeautifyrc/markdown/expected/test.md b/examples/simple-jsbeautifyrc/markdown/expected/test.md index d50ab99..9eb4905 100644 --- a/examples/simple-jsbeautifyrc/markdown/expected/test.md +++ b/examples/simple-jsbeautifyrc/markdown/expected/test.md @@ -1,8 +1,9 @@ -- item -- item -- item - -1. one -2. two -3. three +# heading 1 +- item +- item +- item +## heading 2 +1. one +2. two +3. three diff --git a/examples/simple-jsbeautifyrc/markdown/expected/yaml-front-matter-2.md b/examples/simple-jsbeautifyrc/markdown/expected/yaml-front-matter-2.md index 0f6a69a..ef73802 100644 --- a/examples/simple-jsbeautifyrc/markdown/expected/yaml-front-matter-2.md +++ b/examples/simple-jsbeautifyrc/markdown/expected/yaml-front-matter-2.md @@ -1,5 +1,5 @@ --- -title: "This is a title!" +title: 'This is a title!' --- stuff diff --git a/examples/simple-jsbeautifyrc/markdown/original/yaml-front-matter.md b/examples/simple-jsbeautifyrc/markdown/original/_yaml-front-matter.md similarity index 100% rename from examples/simple-jsbeautifyrc/markdown/original/yaml-front-matter.md rename to examples/simple-jsbeautifyrc/markdown/original/_yaml-front-matter.md diff --git a/examples/simple-jsbeautifyrc/markdown/original/test.md b/examples/simple-jsbeautifyrc/markdown/original/test.md index 11d1cdd..ac2669d 100644 --- a/examples/simple-jsbeautifyrc/markdown/original/test.md +++ b/examples/simple-jsbeautifyrc/markdown/original/test.md @@ -1,7 +1,11 @@ +# heading 1 + - item - item - item +## heading 2 + 1. one 2. two 2. three diff --git a/examples/simple-jsbeautifyrc/sql/expected/test.sql b/examples/simple-jsbeautifyrc/sql/expected/test.sql index 617439f..9a4aab3 100644 --- a/examples/simple-jsbeautifyrc/sql/expected/test.sql +++ b/examples/simple-jsbeautifyrc/sql/expected/test.sql @@ -1 +1,25 @@ -SELECT ca.proj_id AS proj_id, ca.ca_name AS proj_name, ca.ca_date_start AS proj_start, ca.ca_date_end AS proj_end,(SELECT COUNT(*) FROM rotations r WHERE r.proj_id = proj_id AND r.r_status = 'R' GROUP BY r.proj_id) r_count, (SELECT count(*) FROM rotations r WHERE r.proj_id = proj_id AND r.channel_id = 24 ) r_rtb_count FROM projs ca, clients c, proj_auth caa WHERE ca.client_id = 12345 AND ca.client_id = c.client_id AND ca_type = 'zzz' AND c.agency_id = 0 AND ca.client_id = NVL( caa.client_id, ca.client_id ) AND proj_id = NVL( caa.proj_id, proj_id ) AND caa.contact_id = 7890 +SELECT ca.proj_id AS proj_id, + ca.ca_name AS proj_name, + ca.ca_date_start AS proj_start, + ca.ca_date_end AS proj_end, + + (SELECT COUNT(*) + FROM rotations r + WHERE r.proj_id = proj_id + AND r.r_status = 'R' + GROUP BY r.proj_id) r_count, + + (SELECT count(*) + FROM rotations r + WHERE r.proj_id = proj_id + AND r.channel_id = 24) r_rtb_count +FROM projs ca, + clients c, + proj_auth caa +WHERE ca.client_id = 12345 + AND ca.client_id = c.client_id + AND ca_type = 'zzz' + AND c.agency_id = 0 + AND ca.client_id = NVL(caa.client_id, ca.client_id) + AND proj_id = NVL(caa.proj_id, proj_id) + AND caa.contact_id = 7890 \ No newline at end of file diff --git a/examples/simple-jsbeautifyrc/sql/expected/test2.sql b/examples/simple-jsbeautifyrc/sql/expected/test2.sql index 40f1942..284de85 100644 --- a/examples/simple-jsbeautifyrc/sql/expected/test2.sql +++ b/examples/simple-jsbeautifyrc/sql/expected/test2.sql @@ -1,5 +1,32 @@ -INSERT INTO client (host, description, created_at) VALUES('hallpclnx', 'My linux machine', CURRENT_TIMESTAMP); -INSERT INTO thread (thread, description, created_at, client_id) VALUES(1, 'Living room camera', CURRENT_TIMESTAMP, 1); -INSERT INTO thread (thread, description, created_at, client_id) VALUES(2, 'Porch camera', CURRENT_TIMESTAMP, 1); -INSERT INTO thread (thread, description, created_at, client_id) VALUES(2, 'Garden camera', CURRENT_TIMESTAMP, 1); -INSERT INTO client (host, description, created_at) VALUES('shedpclnx', 'My shed linux machine', CURRENT_TIMESTAMP); +INSERT INTO client (host, description, created_at) +VALUES('hallpclnx', + 'My linux machine', + CURRENT_TIMESTAMP); + + +INSERT INTO thread (thread, description, created_at, client_id) +VALUES(1, + 'Living room camera', + CURRENT_TIMESTAMP, + 1); + + +INSERT INTO thread (thread, description, created_at, client_id) +VALUES(2, + 'Porch camera', + CURRENT_TIMESTAMP, + 1); + + +INSERT INTO thread (thread, description, created_at, client_id) +VALUES(2, + 'Garden camera', + CURRENT_TIMESTAMP, + 1); + + +INSERT INTO client (host, description, created_at) +VALUES('shedpclnx', + 'My shed linux machine', + CURRENT_TIMESTAMP); + diff --git a/examples/simple-jsbeautifyrc/sql/original/_test.sql b/examples/simple-jsbeautifyrc/sql/original/test.sql similarity index 100% rename from examples/simple-jsbeautifyrc/sql/original/_test.sql rename to examples/simple-jsbeautifyrc/sql/original/test.sql diff --git a/examples/simple-jsbeautifyrc/sql/original/_test2.sql b/examples/simple-jsbeautifyrc/sql/original/test2.sql similarity index 100% rename from examples/simple-jsbeautifyrc/sql/original/_test2.sql rename to examples/simple-jsbeautifyrc/sql/original/test2.sql diff --git a/spec/beautify-languages-spec.coffee b/spec/beautify-languages-spec.coffee index 2bc5d78..f75cf6d 100644 --- a/spec/beautify-languages-spec.coffee +++ b/spec/beautify-languages-spec.coffee @@ -139,14 +139,13 @@ describe "BeautifyLanguages", -> # Check for beautification errors if text isnt expectedContents # console.warn(allOptions, text, expectedContents) - fileName = originalTestPath + fileName = expectedTestPath oldStr=text newStr=expectedContents - oldHeader="original" + oldHeader="beautified" newHeader="expected" diff = JsDiff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader) throw new Error("Beautifier output does not match expected output:\n"+diff) - #expect(text).toEqual expectedContents # All done! beautifyCompleted = true diff --git a/src/beautifiers/beautifier.coffee b/src/beautifiers/beautifier.coffee index d4eb90a..24a2236 100644 --- a/src/beautifiers/beautifier.coffee +++ b/src/beautifiers/beautifier.coffee @@ -84,14 +84,19 @@ module.exports = class Beautifier # Resolve executable Promise.resolve(executable) .then((exe) -> + console.log('exe', exe) # Flatten args first args = _.flatten(args) + console.log('flat args', args) # Resolve all args Promise.all(args) .then((args) -> return new Promise((resolve, reject) -> + console.log('resolved args', args) # Remove null values + args = _.without(args, undefined) args = _.without(args, null) + console.log('args without undefined/null', args) # Spawn command stdout = "" stderr = "" @@ -107,6 +112,7 @@ module.exports = class Beautifier cmd.stderr.on('data', (data) -> stderr += data ) # when the spawn child process exits, check if there were any errors and close the writeable stream cmd.on('exit', (code) -> + console.log('spawn done', code, stderr, stdout) # If return code is not 0 then error occured if code isnt 0 reject(stderr) diff --git a/src/beautifiers/index.coffee b/src/beautifiers/index.coffee index a35b6a5..1e6e43c 100644 --- a/src/beautifiers/index.coffee +++ b/src/beautifiers/index.coffee @@ -101,21 +101,27 @@ module.exports = class Beautifiers # Get language fileExtension = path.extname(filePath) - languages = @languages.getLanguages(grammar, fileExtension) + languages = @languages.getLanguages({grammar, fileExtension}) # TODO: select appropriate language language = languages[0] - # Options for Language - # TODO: support fallback for options - options = @getOptions(language.namespace, allOptions) || {} - console.log('options', options) - # Beautify! unsupportedGrammar = false - if atom.config.get("atom-beautify.disabledLanguages")?.indexOf(grammar) > - 1 + if atom.config.get("atom-beautify.disabledLanguages")?.indexOf(language) > - 1 return resolve(null) + # Options for Language + console.log('allOptions', allOptions) + options = @getOptions(language.namespace, allOptions) || {} + # Support fallback for options + if language.fallback? + for fallback in language.fallback + # Merge current options on top of fallback options + console.log(fallback) + options = _.merge(@getOptions(fallback, allOptions) || {}, options) + console.log('options', options) + # Get Beautifiers # console.log(grammar, language) beautifiers = @getBeautifiers(language.name, options) @@ -127,6 +133,32 @@ module.exports = class Beautifiers else # TODO: select beautifier beautifier = beautifiers[0] + + # Transform options, if applicable + beautifierOptions = beautifier.options[language.name] + console.log('beautifierOptions', beautifierOptions) + if typeof beautifierOptions is "boolean" + if beautifierOptions isnt true + # Disable options + options = {} + else if typeof beautifierOptions is "object" + # Transform the options + transformedOptions = {} + for field, op of beautifierOptions + if typeof op is "string" + # Rename + transformedOptions[field] = options[op] + else if typeof op is "function" + # Transform + transformedOptions[field] = op(options) + else if typeof op is "boolean" + # Enable/Disable + if op is true + transformedOptions[field] = options[field] + # Replace old options with new transformed options + options = transformedOptions + else + console.warn("Unsupported Language options: ",beautifierOptions) console.log('beautify!', beautifier, language, options) beautifier.beautify(text, language.name, options) .then(resolve) @@ -360,7 +392,7 @@ module.exports = class Beautifiers # 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) + if _.indexOf(self.languages.namespaces, 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); diff --git a/src/beautifiers/prettydiff.coffee b/src/beautifiers/prettydiff.coffee index da1221e..960020a 100644 --- a/src/beautifiers/prettydiff.coffee +++ b/src/beautifiers/prettydiff.coffee @@ -8,12 +8,47 @@ module.exports = class PrettyDiff extends Beautifier options: { CSV: true HTML: true - JavaScript: true - CSS: true - SCSS: true - Sass: true + JavaScript: + inchar: "indent_char" + insize: "indent_size" + alphasort: (options) -> + options.alphasort or false + preserve: (options) -> + if (options.preserve_newlines is true ) then \ + "all" else "none" + CSS: + inchar: "indent_char" + insize: "indent_size" + alphasort: (options) -> + options.alphasort or false + preserve: (options) -> + if (options.preserve_newlines is true ) then \ + "all" else "none" + SCSS: + inchar: "indent_char" + insize: "indent_size" + alphasort: (options) -> + options.alphasort or false + preserve: (options) -> + if (options.preserve_newlines is true ) then \ + "all" else "none" + Sass: + inchar: "indent_char" + insize: "indent_size" + alphasort: (options) -> + options.alphasort or false + preserve: (options) -> + if (options.preserve_newlines is true ) then \ + "all" else "none" JSON: true - TSS: true + TSS: + inchar: "indent_char" + insize: "indent_size" + alphasort: (options) -> + options.alphasort or false + preserve: (options) -> + if (options.preserve_newlines is true ) then \ + "all" else "none" LESS: { inchar: "indent_char" insize: "indent_size" @@ -40,7 +75,7 @@ module.exports = class PrettyDiff extends Beautifier lang = "html" when "JavaScript", "JSON", "JSX" lang = "javascript" - when "CSS", "LESS", "SCSS", "SASS" + when "CSS", "LESS", "SCSS", "Sass" lang = "css" when "TSS" lang = "tss" @@ -57,6 +92,7 @@ module.exports = class PrettyDiff extends Beautifier # Merge args intos options _.merge(options, args) + console.log('prettydiff args', args, options) # Beautify output = prettydiff.api(options) diff --git a/src/beautifiers/ruby-beautify.coffee b/src/beautifiers/ruby-beautify.coffee index 3222e70..8e0fdec 100644 --- a/src/beautifiers/ruby-beautify.coffee +++ b/src/beautifiers/ruby-beautify.coffee @@ -8,7 +8,7 @@ Beautifier = require('./beautifier') module.exports = class RubyBeautify extends Beautifier options: { - SQL: true + Ruby: true } cli: (options) -> diff --git a/src/languages/index.coffee b/src/languages/index.coffee index b36537e..4d81432 100644 --- a/src/languages/index.coffee +++ b/src/languages/index.coffee @@ -72,9 +72,11 @@ module.exports = class Languages ### Get language for grammar and extension ### - getLanguages: (grammar, extension) -> + getLanguages: ({name, namespace, grammar, extension}) -> # console.log(grammar, extension, @languages) _.union( + _.filter(@languages, (language) -> _.contains(language.name, name)) + _.filter(@languages, (language) -> _.contains(language.namespace, namespace)) _.filter(@languages, (language) -> _.contains(language.grammars, grammar)) _.filter(@languages, (language) -> _.contains(language.extensions, extension)) ) \ No newline at end of file