diff --git a/src/beautifiers/beautifier.coffee b/src/beautifiers/beautifier.coffee index e369ac6..187bd69 100644 --- a/src/beautifiers/beautifier.coffee +++ b/src/beautifiers/beautifier.coffee @@ -97,6 +97,25 @@ module.exports = class Beautifier startDir.pop() return null + # Retrieves the default line ending based upon the Atom configuration + # `line-ending-selector.defaultLineEnding`. If the Atom configuration + # indicates "OS Default", the `process.platform` is queried, returning + # CRLF for Windows systems and LF for all other systems. + # Code modified from atom/line-ending-selector + # returns: The correct line-ending character sequence based upon the Atom + # configuration, or `null` if the Atom line ending configuration was not + # recognized. + # see: https://github.com/atom/line-ending-selector/blob/master/lib/main.js + getDefaultLineEnding: (crlf,lf) -> + switch atom.config.get('line-ending-selector.defaultLineEnding') + when 'LF' + return lf + when 'CRLF' + return crlf + when 'OS Default' + return if process.platform is 'win32' then crlf else lf + else + return lf ### If platform is Windows ### diff --git a/src/beautifiers/js-beautify.coffee b/src/beautifiers/js-beautify.coffee index 7092e23..fe9c464 100644 --- a/src/beautifiers/js-beautify.coffee +++ b/src/beautifiers/js-beautify.coffee @@ -6,6 +6,15 @@ module.exports = class JSBeautify extends Beautifier link: "https://github.com/beautify-web/js-beautify" options: { + _: + eol: ['end_of_line', (end_of_line) -> + if (end_of_line == 'CRLF') + '\r\n' + else if (end_of_line == 'LF') + '\n' + else + null + ] HTML: true XML: true Handlebars: true @@ -27,9 +36,7 @@ module.exports = class JSBeautify extends Beautifier beautify: (text, language, options) -> @verbose("JS Beautify language #{language}") @info("JS Beautify Options: #{JSON.stringify(options, null, 4)}") - #TODO reconsider handling of EOL once js-beautify adds EOL detection - #see https://github.com/beautify-web/js-beautify/issues/899 - options.eol = getDefaultLineEnding() ? options.eol #fixes issue #707 + options.eol = options.eol ? @getDefaultLineEnding('\r\n','\n') return new @Promise((resolve, reject) => try switch language diff --git a/src/beautifiers/prettydiff.coffee b/src/beautifiers/prettydiff.coffee index 2be957e..37c040a 100644 --- a/src/beautifiers/prettydiff.coffee +++ b/src/beautifiers/prettydiff.coffee @@ -42,6 +42,14 @@ module.exports = class PrettyDiff extends Beautifier ] ternaryline: "preserve_ternary_lines" bracepadding: "space_in_paren" + crlf: ['end_of_line', (end_of_line) -> + if (end_of_line == 'CRLF') + true + else if (end_of_line == 'LF') + false + else + null + ] # Apply language-specific options CSV: true Coldfusion: true @@ -70,6 +78,7 @@ module.exports = class PrettyDiff extends Beautifier beautify: (text, language, options) -> + options.crlf = options.crlf ? @getDefaultLineEnding(true,false) return new @Promise((resolve, reject) => prettydiff = require("prettydiff") _ = require('lodash') diff --git a/src/languages/javascript.coffee b/src/languages/javascript.coffee index 49c7a34..a8b2e7b 100644 --- a/src/languages/javascript.coffee +++ b/src/languages/javascript.coffee @@ -28,7 +28,6 @@ module.exports = { defaultBeautifier: "JS Beautify" ### - ### options: # JavaScript @@ -111,5 +110,10 @@ module.exports = { default: false description: "If a terminating comma should be inserted into \ arrays, object literals, and destructured objects." + end_of_line: + type: 'string' + default: "System Default" + enum: ["CRLF","LF","System Default"] + description: "Override EOL from line-ending-selector" }