Fix issue #707 by adding Atom-based EOL detection

Determines 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.
This commit is contained in:
Garret Wilson 2016-04-22 19:16:55 -07:00
parent 09c3d6a539
commit 268e1f5d80
1 changed files with 23 additions and 0 deletions

View File

@ -24,6 +24,9 @@ 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
return new @Promise((resolve, reject) =>
try
switch language
@ -52,3 +55,23 @@ module.exports = class JSBeautify extends Beautifier
reject(err)
)
# 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= ->
switch atom.config.get('line-ending-selector.defaultLineEnding')
when 'LF'
return '\n'
when 'CRLF'
return '\r\n'
when 'OS Default'
return if process.platform is 'win32' then '\r\n' else '\n'
else
return null