From 893e116fc4f9a905596e906a8b79fca3241cbcf1 Mon Sep 17 00:00:00 2001 From: hxsf Date: Tue, 25 Apr 2017 14:23:03 +0800 Subject: [PATCH 1/4] fix bug #1588 --- examples/simple-jsbeautifyrc/lua/expected/test.lua | 1 + examples/simple-jsbeautifyrc/lua/original/test.lua | 1 + src/beautifiers/lua-beautifier/beautifier.coffee | 10 ++++++---- src/beautifiers/lua-beautifier/index.coffee | 2 +- src/languages/lua.coffee | 5 +++++ 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/examples/simple-jsbeautifyrc/lua/expected/test.lua b/examples/simple-jsbeautifyrc/lua/expected/test.lua index 878d1c1..82141da 100644 --- a/examples/simple-jsbeautifyrc/lua/expected/test.lua +++ b/examples/simple-jsbeautifyrc/lua/expected/test.lua @@ -2,6 +2,7 @@ -- and return a closure which can be used for continuing the sort. local a = 'a b c' local b = '12345678' +local x = 1.99e-07 local c = 'a b c' + 'a b c' local t = { a = 1, diff --git a/examples/simple-jsbeautifyrc/lua/original/test.lua b/examples/simple-jsbeautifyrc/lua/original/test.lua index fed0705..edcd1c7 100644 --- a/examples/simple-jsbeautifyrc/lua/original/test.lua +++ b/examples/simple-jsbeautifyrc/lua/original/test.lua @@ -2,6 +2,7 @@ -- and return a closure which can be used for continuing the sort. local a= 'a b c' local b ='12345678' +local x = 1.99e-07 local c = 'a b c' +'a b c' local t = { a = 1, diff --git a/src/beautifiers/lua-beautifier/beautifier.coffee b/src/beautifiers/lua-beautifier/beautifier.coffee index e7aba13..2cac5f1 100644 --- a/src/beautifiers/lua-beautifier/beautifier.coffee +++ b/src/beautifiers/lua-beautifier/beautifier.coffee @@ -8,7 +8,8 @@ adjust_space = (line) -> # replace all whitespaces inside the string with one space, WARNING: the whitespaces in string will be replace too! line = line.replace /\s?(==|>=|<=|~=|[=><\+\*\/])\s?/g, ' $1 ' # add whitespace around the operator - line = line.replace /([^=|\-|(|\s])\s?\-\s?([^\-|\[])/g, '$1 - $2' + line = line.replace /([^=e\-\(\s])\s?\-\s?([^\-\[])/g, '$1 - $2' + line = line.replace /([^\d])e\s?\-\s?([^\-\[])/g, '$1e - $2' # just format minus, not for -- or negative number or commentary. line = line.replace /,([^\s])/g, ', $1' # adjust ',' @@ -25,7 +26,8 @@ adjust_space = (line) -> DEFAULT_WARN_FN = (msg) -> console.log('WARNING:', msg) -module.exports = (str, indent, warn_fn) -> +module.exports = (str, indent, warn_fn, opts = {}) -> + do_not_change_whitespace = !!opts?.do_not_change_whitespace indent = indent or DEFAULT_INDENT warn_fn = if typeof warn_fn == 'function' then warn_fn else DEFAULT_WARN_FN indent = ' '.repeat(indent) if Number.isInteger(indent) @@ -45,7 +47,7 @@ module.exports = (str, indent, warn_fn) -> arr = line.split(/\]=*\]/, 2) comment = arr[0] code = arr[1] - line = comment + ']' + '='.repeat($template - 1) + ']' + adjust_space(code) + line = comment + ']' + '='.repeat($template - 1) + ']' + if do_not_change_whitespace then code else adjust_space(code) $template = 0 $template = 0 else @@ -56,7 +58,7 @@ module.exports = (str, indent, warn_fn) -> if !$template_flag line = line.trim() # remote all spaces on both ends - line = adjust_space(line) + line = if do_not_change_whitespace then line else adjust_space(line) if !line.length return '' raw_line = line diff --git a/src/beautifiers/lua-beautifier/index.coffee b/src/beautifiers/lua-beautifier/index.coffee index 24f9376..7108863 100644 --- a/src/beautifiers/lua-beautifier/index.coffee +++ b/src/beautifiers/lua-beautifier/index.coffee @@ -18,6 +18,6 @@ module.exports = class Lua extends Beautifier beautify: (text, language, options) -> new @Promise (resolve, reject) -> try - resolve format text, options.indent_char.repeat options.indent_size + resolve format text, options.indent_char.repeat(options.indent_size), @warn, options catch error reject error diff --git a/src/languages/lua.coffee b/src/languages/lua.coffee index 236575d..f95f2e0 100644 --- a/src/languages/lua.coffee +++ b/src/languages/lua.coffee @@ -19,4 +19,9 @@ module.exports = { defaultBeautifier: "Lua beautifier" + options: + do_not_change_whitespace: + type: 'boolean' + default: false + description: "do not adjust whitespace" } From 1e7b5cbe9f2b88f90dee1389dbcae526bca84d49 Mon Sep 17 00:00:00 2001 From: hxsf Date: Tue, 25 Apr 2017 14:34:59 +0800 Subject: [PATCH 2/4] add eol support --- src/beautifiers/lua-beautifier/beautifier.coffee | 3 ++- src/beautifiers/lua-beautifier/index.coffee | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/beautifiers/lua-beautifier/beautifier.coffee b/src/beautifiers/lua-beautifier/beautifier.coffee index 2cac5f1..2d413ab 100644 --- a/src/beautifiers/lua-beautifier/beautifier.coffee +++ b/src/beautifiers/lua-beautifier/beautifier.coffee @@ -28,6 +28,7 @@ DEFAULT_WARN_FN = (msg) -> module.exports = (str, indent, warn_fn, opts = {}) -> do_not_change_whitespace = !!opts?.do_not_change_whitespace + eol = opts?.eol or '\n' indent = indent or DEFAULT_INDENT warn_fn = if typeof warn_fn == 'function' then warn_fn else DEFAULT_WARN_FN indent = ' '.repeat(indent) if Number.isInteger(indent) @@ -102,4 +103,4 @@ module.exports = (str, indent, warn_fn, opts = {}) -> new_line or undefined warn_fn 'positive indentation at the end' if $currIndent > 0 - new_code.join '\n' + new_code.join eol diff --git a/src/beautifiers/lua-beautifier/index.coffee b/src/beautifiers/lua-beautifier/index.coffee index 7108863..e9d9445 100644 --- a/src/beautifiers/lua-beautifier/index.coffee +++ b/src/beautifiers/lua-beautifier/index.coffee @@ -16,6 +16,7 @@ module.exports = class Lua extends Beautifier } beautify: (text, language, options) -> + options.eol = @getDefaultLineEnding('\r\n','\n',options.end_of_line) new @Promise (resolve, reject) -> try resolve format text, options.indent_char.repeat(options.indent_size), @warn, options From c53b92e3fb8b8470971926892278795ae65502af Mon Sep 17 00:00:00 2001 From: hxsf Date: Tue, 25 Apr 2017 14:40:27 +0800 Subject: [PATCH 3/4] gen docs and options --- docs/options.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ src/options.json | 14 +++++++++++++ 2 files changed, 66 insertions(+) diff --git a/docs/options.md b/docs/options.md index c4e0a38..8c55d36 100644 --- a/docs/options.md +++ b/docs/options.md @@ -7633,6 +7633,7 @@ Maximum amount of characters per line (0 = disable) (Supported by Pretty Diff) | `disabled` | :white_check_mark: | | `default_beautifier` | :white_check_mark: | | `beautify_on_save` | :white_check_mark: | +| `do_not_change_whitespace` | :white_check_mark: | **Description**: @@ -7693,6 +7694,30 @@ Automatically beautify Lua files on save 2. Go into *Packages* and search for "*Atom Beautify*" package. 3. Find the option "*Beautify On Save*" and change it to your desired configuration. +##### [Do not change whitespace](#do-not-change-whitespace) + +**Namespace**: `lua` + +**Key**: `do_not_change_whitespace` + +**Type**: `boolean` + +**Supported Beautifiers**: [`Lua beautifier`](#lua-beautifier) + +**Description**: + +do not adjust whitespace (Supported by Lua beautifier) + +**Example `.jsbeautifyrc` Configuration** + +```json +{ + "lua": { + "do_not_change_whitespace": false + } +} +``` + #### [Markdown](#markdown) **Supported Beautifiers**: [`Remark`](#remark) [`Tidy Markdown`](#tidy-markdown) @@ -16775,6 +16800,33 @@ undefined (Supported by Latex Beautify) ``` +### Lua beautifier + +##### [Do not change whitespace](#do-not-change-whitespace) + +**Namespace**: `lua` + +**Key**: `do_not_change_whitespace` + +**Type**: `boolean` + +**Supported Beautifiers**: [`Lua beautifier`](#lua-beautifier) + +**Description**: + +do not adjust whitespace (Supported by Lua beautifier) + +**Example `.jsbeautifyrc` Configuration** + +```json +{ + "lua": { + "do_not_change_whitespace": false + } +} +``` + + ### Marko Beautifier ##### [Indent size](#indent-size) diff --git a/src/options.json b/src/options.json index e7894a9..a9a475f 100644 --- a/src/options.json +++ b/src/options.json @@ -4567,6 +4567,20 @@ "lua" ], "properties": { + "do_not_change_whitespace": { + "type": "boolean", + "default": false, + "description": "do not adjust whitespace (Supported by Lua beautifier)", + "title": "Do not change whitespace", + "beautifiers": [ + "Lua beautifier" + ], + "key": "do_not_change_whitespace", + "language": { + "name": "Lua", + "namespace": "lua" + } + }, "disabled": { "title": "Disable Beautifying Language", "order": -3, From ce287977c81c843f489eac3b706ad04cf15974a5 Mon Sep 17 00:00:00 2001 From: hxsf Date: Sun, 28 May 2017 15:07:51 +0800 Subject: [PATCH 4/4] remove do not change whitespace --- docs/options.md | 30 ++++++++++++------- .../lua-beautifier/beautifier.coffee | 5 ++-- src/languages/lua.coffee | 9 +++--- src/options.json | 17 +++++++---- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/docs/options.md b/docs/options.md index 8c55d36..d69baa1 100644 --- a/docs/options.md +++ b/docs/options.md @@ -7633,7 +7633,7 @@ Maximum amount of characters per line (0 = disable) (Supported by Pretty Diff) | `disabled` | :white_check_mark: | | `default_beautifier` | :white_check_mark: | | `beautify_on_save` | :white_check_mark: | -| `do_not_change_whitespace` | :white_check_mark: | +| `end_of_line` | :white_check_mark: | **Description**: @@ -7694,26 +7694,30 @@ Automatically beautify Lua files on save 2. Go into *Packages* and search for "*Atom Beautify*" package. 3. Find the option "*Beautify On Save*" and change it to your desired configuration. -##### [Do not change whitespace](#do-not-change-whitespace) +##### [End of line](#end-of-line) **Namespace**: `lua` -**Key**: `do_not_change_whitespace` +**Key**: `end_of_line` -**Type**: `boolean` +**Default**: `System Default` + +**Type**: `string` + +**Enum**: `CRLF` `LF` `System Default` **Supported Beautifiers**: [`Lua beautifier`](#lua-beautifier) **Description**: -do not adjust whitespace (Supported by Lua beautifier) +Override EOL from line-ending-selector (Supported by Lua beautifier) **Example `.jsbeautifyrc` Configuration** ```json { "lua": { - "do_not_change_whitespace": false + "end_of_line": "System Default" } } ``` @@ -16802,26 +16806,30 @@ undefined (Supported by Latex Beautify) ### Lua beautifier -##### [Do not change whitespace](#do-not-change-whitespace) +##### [End of line](#end-of-line) **Namespace**: `lua` -**Key**: `do_not_change_whitespace` +**Key**: `end_of_line` -**Type**: `boolean` +**Default**: `System Default` + +**Type**: `string` + +**Enum**: `CRLF` `LF` `System Default` **Supported Beautifiers**: [`Lua beautifier`](#lua-beautifier) **Description**: -do not adjust whitespace (Supported by Lua beautifier) +Override EOL from line-ending-selector (Supported by Lua beautifier) **Example `.jsbeautifyrc` Configuration** ```json { "lua": { - "do_not_change_whitespace": false + "end_of_line": "System Default" } } ``` diff --git a/src/beautifiers/lua-beautifier/beautifier.coffee b/src/beautifiers/lua-beautifier/beautifier.coffee index 2d413ab..35398ae 100644 --- a/src/beautifiers/lua-beautifier/beautifier.coffee +++ b/src/beautifiers/lua-beautifier/beautifier.coffee @@ -27,7 +27,6 @@ DEFAULT_WARN_FN = (msg) -> console.log('WARNING:', msg) module.exports = (str, indent, warn_fn, opts = {}) -> - do_not_change_whitespace = !!opts?.do_not_change_whitespace eol = opts?.eol or '\n' indent = indent or DEFAULT_INDENT warn_fn = if typeof warn_fn == 'function' then warn_fn else DEFAULT_WARN_FN @@ -48,7 +47,7 @@ module.exports = (str, indent, warn_fn, opts = {}) -> arr = line.split(/\]=*\]/, 2) comment = arr[0] code = arr[1] - line = comment + ']' + '='.repeat($template - 1) + ']' + if do_not_change_whitespace then code else adjust_space(code) + line = comment + ']' + '='.repeat($template - 1) + ']' + adjust_space(code) $template = 0 $template = 0 else @@ -59,7 +58,7 @@ module.exports = (str, indent, warn_fn, opts = {}) -> if !$template_flag line = line.trim() # remote all spaces on both ends - line = if do_not_change_whitespace then line else adjust_space(line) + line = adjust_space(line) if !line.length return '' raw_line = line diff --git a/src/languages/lua.coffee b/src/languages/lua.coffee index f95f2e0..9f5d49b 100644 --- a/src/languages/lua.coffee +++ b/src/languages/lua.coffee @@ -20,8 +20,9 @@ module.exports = { defaultBeautifier: "Lua beautifier" options: - do_not_change_whitespace: - type: 'boolean' - default: false - description: "do not adjust whitespace" + end_of_line: + type: 'string' + default: "System Default" + enum: ["CRLF","LF","System Default"] + description: "Override EOL from line-ending-selector" } diff --git a/src/options.json b/src/options.json index a9a475f..dc05659 100644 --- a/src/options.json +++ b/src/options.json @@ -4567,15 +4567,20 @@ "lua" ], "properties": { - "do_not_change_whitespace": { - "type": "boolean", - "default": false, - "description": "do not adjust whitespace (Supported by Lua beautifier)", - "title": "Do not change whitespace", + "end_of_line": { + "type": "string", + "default": "System Default", + "enum": [ + "CRLF", + "LF", + "System Default" + ], + "description": "Override EOL from line-ending-selector (Supported by Lua beautifier)", + "title": "End of line", "beautifiers": [ "Lua beautifier" ], - "key": "do_not_change_whitespace", + "key": "end_of_line", "language": { "name": "Lua", "namespace": "lua"