From 8c8afc407dc11b309e9db0d4caad820951820f18 Mon Sep 17 00:00:00 2001 From: hxsf Date: Tue, 28 Mar 2017 15:59:28 +0800 Subject: [PATCH 1/4] add a simple Lua format base on js --- .../lua-beautifier/beautifier.coffee | 79 +++++++++++++++++++ src/beautifiers/lua-beautifier/beautifier.pl | 57 ------------- src/beautifiers/lua-beautifier/index.coffee | 12 +-- 3 files changed, 85 insertions(+), 63 deletions(-) create mode 100644 src/beautifiers/lua-beautifier/beautifier.coffee delete mode 100644 src/beautifiers/lua-beautifier/beautifier.pl diff --git a/src/beautifiers/lua-beautifier/beautifier.coffee b/src/beautifiers/lua-beautifier/beautifier.coffee new file mode 100644 index 0000000..ab3833b --- /dev/null +++ b/src/beautifiers/lua-beautifier/beautifier.coffee @@ -0,0 +1,79 @@ +DEFAULT_INDENT = ' ' + +module.exports = (str, indent) -> + indent = indent or DEFAULT_INDENT + indent = ' '.repeat indent if Number.isInteger indent + $currIndent = 0 + $nextIndent = 0 + $prevLength = 0 + $extIndent = 0 + $lastIndent = 0 + $template = 0 + new_code = str.split(/\r?\n/g).map (line, line_number) -> + $template_flag = false + if $template + res2 = line.match /\](=*)\]/ + if res2 and $template == res2[1].length + 1 + $template = 0 + $template_flag = true + else + return line + res1 = line.match /\[(=*)\[/ + if res1 + $template = res1[1].length + 1 + if !$template_flag + line = line.trim() + # remote all spaces on both ends + string_list = line.match /(['"])[^\1]*?\1/g + line = line.replace /\s+/g, ' ' + # 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 $2 $3' + # add whitespace around the operator + line = line.replace /,([^\s])/g, ', $1' + line = line.replace /\s,/g, ',' + # recover the whitespaces in string. + line = line.replace /(['"])[^\1]*?\1/g, -> + string_list.shift() + + return '' if !line.length + raw_line = line + line = line.replace /(['"])[^\1]*?\1/, '' + # remove all quoted fragments for proper bracket processing + line = line.replace /\s*--.+/, '' + # remove all comments; this ignores long bracket style comments + if /^((local )?function|repeat|while)\b/.test(line) and !/\bend\s*[\),;]*$/.test(line) or /\b(then|do)$/.test(line) and !/^elseif\b/.test(line) or /^if\b/.test(line) and /\bthen\b/.test(line) and !/\bend$/.test(line) or /\bfunction ?(?:\w+ )?\([^\)]*\)$/.test(line) and !/\bend$/.test(line) + $nextIndent = $currIndent + 1 + else if /^until\b/.test(line) or /^end\s*[\),;]*$/.test(line) or /^end\s*\)\s*\.\./.test(line) or /^else(if)?\b/.test(line) and /\bend$/.test(line) + $nextIndent = --$currIndent + else if /^else\b/.test(line) or /^elseif\b/.test(line) + $nextIndent = $currIndent + $currIndent = $currIndent - 1 + $brackets = (line.match(/\(/g) or []).length - ((line.match(/\)/g) or []).length) + # capture unbalanced brackets + $curly = (line.match(/\{/g) or []).length - ((line.match(/\}/g) or []).length) + # capture unbalanced curly brackets + # close (curly) brackets if needed + $currIndent += $curly if $curly < 0 + $currIndent += $brackets if $brackets < 0 + $nextIndent += $brackets + $curly + + if $currIndent - $lastIndent > 1 + $extIndent += $nextIndent - $lastIndent - 1 + $nextIndent = $currIndent = 1 + $lastIndent + if $currIndent - $lastIndent < -1 and $extIndent > 0 + $extIndent += $currIndent - $lastIndent + 1 + $currIndent = -1 + $lastIndent + if $nextIndent < $currIndent + $nextIndent = $currIndent + + if $currIndent < 0 + console.warn 'WARNING: negative indentation at line ${line_number}: ${raw_line}' + new_line = (if raw_line.length and $currIndent > 0 and !$template_flag then indent.repeat($currIndent) else '') + raw_line + + $useful = $prevLength > 0 or raw_line.length > 0 + $lastIndent = $currIndent + $currIndent = $nextIndent + $prevLength = raw_line.length + return new_line if $useful + console.warn 'WARNING: positive indentation at the end' if $currIndent > 0 + new_code.join '\n' diff --git a/src/beautifiers/lua-beautifier/beautifier.pl b/src/beautifiers/lua-beautifier/beautifier.pl deleted file mode 100644 index 349831a..0000000 --- a/src/beautifiers/lua-beautifier/beautifier.pl +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright 2011 Paul Kulchenko -# Credits: http://notebook.kulchenko.com/programming/lua-beautifier-in-55-lines-of-perl -use strict; -use warnings; - -use constant INDENT => ' '; -my($currIndent, $nextIndent, $prevLength) = (0, 0, 0); - -while (<>) { - chomp; - s/^\s+|\s+$//g; # remote all spaces on both ends - s/\s+/ /g; # replace all whitespaces inside the string with one space - - my $orig = $_; - - s/(['"])[^\1]*?\1//g; # remove all quoted fragments for proper bracket processing - s/\s*--.+//; # remove all comments; this ignores long bracket style comments - - # open a level; increase next indentation; don't change current one - if (/^((local )?function|repeat|while)\b/ && !/\bend\s*[\),;]*$/ - || /\b(then|do)$/ && !/^elseif\b/ # only open on 'then' if there is no 'elseif' - || /^if\b/ && /\bthen\b/ && !/\bend$/ # only open on 'if' if there is no 'end' at the end - || /\bfunction\s*\([^\)]*\)$/) { - $nextIndent = $currIndent + 1; - } - # close the level; change both current and next indentation - elsif (/^until\b/ - || /^end\s*[\),;]*$/ - || /^end\s*\)\s*\.\./ # this is a special case of 'end).."some string"' - || /^else(if)?\b/ && /\bend$/) { - $nextIndent = $currIndent = $currIndent - 1; - } - # keep the level; decrease the current indentation; keep the next one - elsif (/^else\b/ - || /^elseif\b/) { - ($nextIndent, $currIndent) = ($currIndent, $currIndent-1); - } - - my $brackets = y/(// - y/)//; # capture unbalanced brackets - my $curly = y/{// - y/}//; # capture unbalanced curly brackets - - # close (curly) brackets if needed - $currIndent += $curly if $curly < 0 && /^\}/; - $currIndent += $brackets if $brackets < 0 && /^\)/; - - warn "WARNING: negative indentation at line $.: $orig\n" if $currIndent < 0; - - print((length($orig) ? (INDENT x $currIndent) : ''), $orig, "\n") - if $prevLength > 0 || length($orig) > 0; # this is to collapse empty lines - - $nextIndent += $brackets + $curly; - - $currIndent = $nextIndent; - $prevLength = length($orig); -} - -warn "WARNING: positive indentation at the end\n" if $nextIndent > 0; diff --git a/src/beautifiers/lua-beautifier/index.coffee b/src/beautifiers/lua-beautifier/index.coffee index c96c743..35a7bb5 100644 --- a/src/beautifiers/lua-beautifier/index.coffee +++ b/src/beautifiers/lua-beautifier/index.coffee @@ -4,6 +4,7 @@ path = require("path") "use strict" Beautifier = require('../beautifier') +format = require './beautifier' module.exports = class Lua extends Beautifier name: "Lua beautifier" @@ -14,9 +15,8 @@ module.exports = class Lua extends Beautifier } beautify: (text, language, options) -> - lua_beautifier = path.resolve(__dirname, "beautifier.pl") - @run("perl", [ - lua_beautifier, - '<', - @tempFile("input", text) - ]) + new @Promise (resolve, reject) -> + try + resolve format text, options.indent_char.repeat options.indent_size + catch error + reject error From 25b3ef04176d1a3a40f71a5f75de26f4c7a5dcb3 Mon Sep 17 00:00:00 2001 From: hxsf Date: Tue, 28 Mar 2017 16:25:52 +0800 Subject: [PATCH 2/4] add examples for lua --- .../simple-jsbeautifyrc/lua/expected/test.lua | 18 ++++++++++++++++-- .../simple-jsbeautifyrc/lua/original/test.lua | 16 +++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/examples/simple-jsbeautifyrc/lua/expected/test.lua b/examples/simple-jsbeautifyrc/lua/expected/test.lua index 5c00e55..e2f200c 100644 --- a/examples/simple-jsbeautifyrc/lua/expected/test.lua +++ b/examples/simple-jsbeautifyrc/lua/expected/test.lua @@ -1,5 +1,19 @@ -- Ensure that that the element at i is in the right position, -- and return a closure which can be used for continuing the sort. +local a = 'a b c' +local b = '12345678' +local c = 'a b c' + 'a b c' +local t = { + a = 1, + b = 2, + c = 3, +} +local e = {a = 1, b = 2} + +function aaa(a, b, c) + return a + b +end + function quicksorter(i, vec, low, high) if low >= high then return quicksorter @@ -11,10 +25,10 @@ function quicksorter(i, vec, low, high) -- Create the promise local function self(i, vec, low, high) if i < middle then - left = left(i, vec, low, middle-1) + left = left(i, vec, low, middle - 1) return self elseif i > middle then - right = right(i, vec, middle+1, high) + right = right(i, vec, middle + 1, high) return self end end diff --git a/examples/simple-jsbeautifyrc/lua/original/test.lua b/examples/simple-jsbeautifyrc/lua/original/test.lua index 753deb3..e3692f5 100644 --- a/examples/simple-jsbeautifyrc/lua/original/test.lua +++ b/examples/simple-jsbeautifyrc/lua/original/test.lua @@ -1,5 +1,19 @@ -- Ensure that that the element at i is in the right position, -- and return a closure which can be used for continuing the sort. +local a= 'a b c' +local b ='12345678' +local c = 'a b c' +'a b c' +local t = { +a = 1, + b =2 , + c= 3, +} +local e={a=1,b=2} + +function aaa(a,b,c) +return a+b +end + function quicksorter(i, vec, low, high) if low >= high then return quicksorter @@ -18,7 +32,7 @@ right = right(i, vec, middle+1, high) return self end end - + -- Force the promise until i is in the right position return self(i, vec, low, high) end From 3c6479680b1b7034c950fefd36945d3dc3f6b4ba Mon Sep 17 00:00:00 2001 From: hxsf Date: Thu, 30 Mar 2017 12:08:51 +0800 Subject: [PATCH 3/4] fixed bugs and add feature + bugs: - negative number: 'local a = -1' => 'local a = - 1' - unequal: 'a ~= b' => 'a ~ = b' - change the comment: '-- a b' => '-- a b' + feature: - remove space before comma, add add space after comma: '{a = 1 ,b = 2}' => '{a = 1, b = 2}' --- .../simple-jsbeautifyrc/lua/expected/test.lua | 23 ++- .../simple-jsbeautifyrc/lua/original/test.lua | 23 ++- .../lua-beautifier/beautifier.coffee | 170 ++++++++++-------- 3 files changed, 135 insertions(+), 81 deletions(-) diff --git a/examples/simple-jsbeautifyrc/lua/expected/test.lua b/examples/simple-jsbeautifyrc/lua/expected/test.lua index e2f200c..878d1c1 100644 --- a/examples/simple-jsbeautifyrc/lua/expected/test.lua +++ b/examples/simple-jsbeautifyrc/lua/expected/test.lua @@ -8,11 +8,26 @@ local t = { b = 2, c = 3, } -local e = {a = 1, b = 2} - -function aaa(a, b, c) - return a + b +if a ~= 'a' then + local b = a end +local e = {a = 1, b = 2} +function aaa(a, b, c) + + -- comment 1 + -- comment 2 1231 + -- comment 1 123 123 123123 12 + -- [[ comment 1 ]] + --[[ + muli comments + ssss + @asdasd sad + ]] + local a = -1 + return a + b - c +end +local b = {a = 1, b = [[this is two space ; + ]], c = 2} function quicksorter(i, vec, low, high) if low >= high then diff --git a/examples/simple-jsbeautifyrc/lua/original/test.lua b/examples/simple-jsbeautifyrc/lua/original/test.lua index e3692f5..fed0705 100644 --- a/examples/simple-jsbeautifyrc/lua/original/test.lua +++ b/examples/simple-jsbeautifyrc/lua/original/test.lua @@ -8,11 +8,26 @@ a = 1, b =2 , c= 3, } -local e={a=1,b=2} - -function aaa(a,b,c) -return a+b +if a~='a' then +local b=a end +local e={a=1,b=2} +function aaa(a,b,c) + +-- comment 1 + -- comment 2 1231 + -- comment 1 123 123 123123 12 +-- [[ comment 1 ]] + --[[ + muli comments + ssss + @asdasd sad + ]] +local a = -1 +return a+b-c +end +local b = {a=1,b=[[this is two space ; + ]],c=2} function quicksorter(i, vec, low, high) if low >= high then diff --git a/src/beautifiers/lua-beautifier/beautifier.coffee b/src/beautifiers/lua-beautifier/beautifier.coffee index ab3833b..c24e3ba 100644 --- a/src/beautifiers/lua-beautifier/beautifier.coffee +++ b/src/beautifiers/lua-beautifier/beautifier.coffee @@ -1,79 +1,103 @@ DEFAULT_INDENT = ' ' -module.exports = (str, indent) -> - indent = indent or DEFAULT_INDENT - indent = ' '.repeat indent if Number.isInteger indent - $currIndent = 0 - $nextIndent = 0 - $prevLength = 0 - $extIndent = 0 - $lastIndent = 0 - $template = 0 - new_code = str.split(/\r?\n/g).map (line, line_number) -> - $template_flag = false - if $template - res2 = line.match /\](=*)\]/ - if res2 and $template == res2[1].length + 1 - $template = 0 - $template_flag = true - else - return line - res1 = line.match /\[(=*)\[/ - if res1 - $template = res1[1].length + 1 - if !$template_flag - line = line.trim() - # remote all spaces on both ends - string_list = line.match /(['"])[^\1]*?\1/g - line = line.replace /\s+/g, ' ' - # 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 $2 $3' - # add whitespace around the operator - line = line.replace /,([^\s])/g, ', $1' - line = line.replace /\s,/g, ',' - # recover the whitespaces in string. - line = line.replace /(['"])[^\1]*?\1/g, -> +adjust_space = (line) -> + string_list = line.match /(['"])[^\1]*?\1/g + muli_string = line.match /\[(=*)\[([^\]\1\]]*)/ + comment = line.match /\-{2}[^\[].*$/ + line = line.replace /\s+/g, ' ' + # 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' + # just format minus, not for -- or negative number or commentary. + line = line.replace /,([^\s])/g, ', $1' + # adjust ',' + line = line.replace /\s+,/g, ',' + # recover the whitespaces in string. + line = line.replace /(['"])[^\1]*?\1/g, -> string_list.shift() + if muli_string and muli_string[0] + line = line.replace /\[(=*)\[([^\]\1\]]*)/, muli_string[0] + if comment and comment[0] + line = line.replace /\-{2}[^\[].*$/, comment[0] + line - return '' if !line.length - raw_line = line - line = line.replace /(['"])[^\1]*?\1/, '' - # remove all quoted fragments for proper bracket processing - line = line.replace /\s*--.+/, '' - # remove all comments; this ignores long bracket style comments - if /^((local )?function|repeat|while)\b/.test(line) and !/\bend\s*[\),;]*$/.test(line) or /\b(then|do)$/.test(line) and !/^elseif\b/.test(line) or /^if\b/.test(line) and /\bthen\b/.test(line) and !/\bend$/.test(line) or /\bfunction ?(?:\w+ )?\([^\)]*\)$/.test(line) and !/\bend$/.test(line) - $nextIndent = $currIndent + 1 - else if /^until\b/.test(line) or /^end\s*[\),;]*$/.test(line) or /^end\s*\)\s*\.\./.test(line) or /^else(if)?\b/.test(line) and /\bend$/.test(line) - $nextIndent = --$currIndent - else if /^else\b/.test(line) or /^elseif\b/.test(line) - $nextIndent = $currIndent - $currIndent = $currIndent - 1 - $brackets = (line.match(/\(/g) or []).length - ((line.match(/\)/g) or []).length) - # capture unbalanced brackets - $curly = (line.match(/\{/g) or []).length - ((line.match(/\}/g) or []).length) - # capture unbalanced curly brackets - # close (curly) brackets if needed - $currIndent += $curly if $curly < 0 - $currIndent += $brackets if $brackets < 0 - $nextIndent += $brackets + $curly +DEFAULT_WARN_FN = (msg) -> + console.log('WARNING:', msg) - if $currIndent - $lastIndent > 1 - $extIndent += $nextIndent - $lastIndent - 1 - $nextIndent = $currIndent = 1 + $lastIndent - if $currIndent - $lastIndent < -1 and $extIndent > 0 - $extIndent += $currIndent - $lastIndent + 1 - $currIndent = -1 + $lastIndent - if $nextIndent < $currIndent - $nextIndent = $currIndent +module.exports = (str, indent, warn_fn) -> + 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) + $currIndent = 0 + $nextIndent = 0 + $prevLength = 0 + $extIndent = 0 + $lastIndent = 0 + $template = 0 + new_code = str.split(/\r?\n/g).map (line, line_number) -> + $template_flag = false + if $template + res2 = line.match(/\](=*)\]/) + if res2 and $template == res2[1].length + 1 + $template_flag = true + if $template and !/]=*]$/.test(line) + arr = line.split(/\]=*\]/, 2) + comment = arr[0] + code = arr[1] + line = comment + ']' + '='.repeat($template - 1) + ']' + adjust_space(code) + $template = 0 + $template = 0 + else + return line + res1 = line.match(/\[(=*)\[/) + if res1 + $template = res1[1].length + 1 + if !$template_flag + line = line.trim() + # remote all spaces on both ends + line = adjust_space(line) + if !line.length + return '' + raw_line = line + line = line.replace(/(['"])[^\1]*?\1/, '') + # remove all quoted fragments for proper bracket processing + line = line.replace(/\s*--.+/, '') + # remove all comments; this ignores long bracket style comments + if /^((local )?function|repeat|while)\b/.test(line) and !/\bend\s*[\),;]*$/.test(line) or /\b(then|do)$/.test(line) and !/^elseif\b/.test(line) or /^if\b/.test(line) and /\bthen\b/.test(line) and !/\bend$/.test(line) or /\bfunction ?(?:\w+ )?\([^\)]*\)$/.test(line) and !/\bend$/.test(line) + $nextIndent = $currIndent + 1 + else if /^until\b/.test(line) or /^end\s*[\),;]*$/.test(line) or /^end\s*\)\s*\.\./.test(line) or /^else(if)?\b/.test(line) and /\bend$/.test(line) + $nextIndent = --$currIndent + else if /^else\b/.test(line) or /^elseif\b/.test(line) + $nextIndent = $currIndent + $currIndent = $currIndent - 1 + $brackets = (line.match(/\(/g) or []).length - ((line.match(/\)/g) or []).length) + # capture unbalanced brackets + $curly = (line.match(/\{/g) or []).length - ((line.match(/\}/g) or []).length) + # capture unbalanced curly brackets + # close (curly) brackets if needed + if $curly < 0 + $currIndent += $curly + if $brackets < 0 + $currIndent += $brackets + $nextIndent += $brackets + $curly + # console.log({last: $lastIndent, curr: $currIndent, next: $nextIndent, ext: $extIndent}) + if $currIndent - $lastIndent > 1 + $extIndent += $nextIndent - $lastIndent - 1 + $nextIndent = $currIndent = 1 + $lastIndent + if $currIndent - $lastIndent < -1 and $extIndent > 0 + $extIndent += $currIndent - $lastIndent + 1 + $currIndent = -1 + $lastIndent + if $nextIndent < $currIndent + $nextIndent = $currIndent + # console.log({last: $lastIndent, curr: $currIndent, next: $nextIndent, ext: $extIndent}) + warn_fn """negative indentation at line #{line_number}: #{raw_line}""" if $currIndent < 0 + new_line = (if raw_line.length and $currIndent > 0 and !$template_flag then indent.repeat($currIndent) else '') + raw_line + $useful = $prevLength > 0 or raw_line.length > 0 + $lastIndent = $currIndent + $currIndent = $nextIndent + $prevLength = raw_line.length + new_line or undefined - if $currIndent < 0 - console.warn 'WARNING: negative indentation at line ${line_number}: ${raw_line}' - new_line = (if raw_line.length and $currIndent > 0 and !$template_flag then indent.repeat($currIndent) else '') + raw_line - - $useful = $prevLength > 0 or raw_line.length > 0 - $lastIndent = $currIndent - $currIndent = $nextIndent - $prevLength = raw_line.length - return new_line if $useful - console.warn 'WARNING: positive indentation at the end' if $currIndent > 0 - new_code.join '\n' + warn_fn 'positive indentation at the end' if $currIndent > 0 + new_code.join '\n' From 51af21106303dd97d8b510cd20ad1fdbebee83e4 Mon Sep 17 00:00:00 2001 From: hxsf Date: Thu, 30 Mar 2017 13:15:32 +0800 Subject: [PATCH 4/4] 'fix_indent' --- .../lua-beautifier/beautifier.coffee | 190 +++++++++--------- 1 file changed, 95 insertions(+), 95 deletions(-) diff --git a/src/beautifiers/lua-beautifier/beautifier.coffee b/src/beautifiers/lua-beautifier/beautifier.coffee index c24e3ba..e7aba13 100644 --- a/src/beautifiers/lua-beautifier/beautifier.coffee +++ b/src/beautifiers/lua-beautifier/beautifier.coffee @@ -1,103 +1,103 @@ DEFAULT_INDENT = ' ' adjust_space = (line) -> - string_list = line.match /(['"])[^\1]*?\1/g - muli_string = line.match /\[(=*)\[([^\]\1\]]*)/ - comment = line.match /\-{2}[^\[].*$/ - line = line.replace /\s+/g, ' ' - # 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' - # just format minus, not for -- or negative number or commentary. - line = line.replace /,([^\s])/g, ', $1' - # adjust ',' - line = line.replace /\s+,/g, ',' - # recover the whitespaces in string. - line = line.replace /(['"])[^\1]*?\1/g, -> - string_list.shift() - if muli_string and muli_string[0] - line = line.replace /\[(=*)\[([^\]\1\]]*)/, muli_string[0] - if comment and comment[0] - line = line.replace /\-{2}[^\[].*$/, comment[0] - line + string_list = line.match /(['"])[^\1]*?\1/g + muli_string = line.match /\[(=*)\[([^\]\1\]]*)/ + comment = line.match /\-{2}[^\[].*$/ + line = line.replace /\s+/g, ' ' + # 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' + # just format minus, not for -- or negative number or commentary. + line = line.replace /,([^\s])/g, ', $1' + # adjust ',' + line = line.replace /\s+,/g, ',' + # recover the whitespaces in string. + line = line.replace /(['"])[^\1]*?\1/g, -> + string_list.shift() + if muli_string and muli_string[0] + line = line.replace /\[(=*)\[([^\]\1\]]*)/, muli_string[0] + if comment and comment[0] + line = line.replace /\-{2}[^\[].*$/, comment[0] + line DEFAULT_WARN_FN = (msg) -> - console.log('WARNING:', msg) + console.log('WARNING:', msg) module.exports = (str, indent, warn_fn) -> - 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) - $currIndent = 0 - $nextIndent = 0 - $prevLength = 0 - $extIndent = 0 - $lastIndent = 0 - $template = 0 - new_code = str.split(/\r?\n/g).map (line, line_number) -> - $template_flag = false - if $template - res2 = line.match(/\](=*)\]/) - if res2 and $template == res2[1].length + 1 - $template_flag = true - if $template and !/]=*]$/.test(line) - arr = line.split(/\]=*\]/, 2) - comment = arr[0] - code = arr[1] - line = comment + ']' + '='.repeat($template - 1) + ']' + adjust_space(code) - $template = 0 - $template = 0 - else - return line - res1 = line.match(/\[(=*)\[/) - if res1 - $template = res1[1].length + 1 - if !$template_flag - line = line.trim() - # remote all spaces on both ends - line = adjust_space(line) - if !line.length - return '' - raw_line = line - line = line.replace(/(['"])[^\1]*?\1/, '') - # remove all quoted fragments for proper bracket processing - line = line.replace(/\s*--.+/, '') - # remove all comments; this ignores long bracket style comments - if /^((local )?function|repeat|while)\b/.test(line) and !/\bend\s*[\),;]*$/.test(line) or /\b(then|do)$/.test(line) and !/^elseif\b/.test(line) or /^if\b/.test(line) and /\bthen\b/.test(line) and !/\bend$/.test(line) or /\bfunction ?(?:\w+ )?\([^\)]*\)$/.test(line) and !/\bend$/.test(line) - $nextIndent = $currIndent + 1 - else if /^until\b/.test(line) or /^end\s*[\),;]*$/.test(line) or /^end\s*\)\s*\.\./.test(line) or /^else(if)?\b/.test(line) and /\bend$/.test(line) - $nextIndent = --$currIndent - else if /^else\b/.test(line) or /^elseif\b/.test(line) - $nextIndent = $currIndent - $currIndent = $currIndent - 1 - $brackets = (line.match(/\(/g) or []).length - ((line.match(/\)/g) or []).length) - # capture unbalanced brackets - $curly = (line.match(/\{/g) or []).length - ((line.match(/\}/g) or []).length) - # capture unbalanced curly brackets - # close (curly) brackets if needed - if $curly < 0 - $currIndent += $curly - if $brackets < 0 - $currIndent += $brackets - $nextIndent += $brackets + $curly - # console.log({last: $lastIndent, curr: $currIndent, next: $nextIndent, ext: $extIndent}) - if $currIndent - $lastIndent > 1 - $extIndent += $nextIndent - $lastIndent - 1 - $nextIndent = $currIndent = 1 + $lastIndent - if $currIndent - $lastIndent < -1 and $extIndent > 0 - $extIndent += $currIndent - $lastIndent + 1 - $currIndent = -1 + $lastIndent - if $nextIndent < $currIndent - $nextIndent = $currIndent - # console.log({last: $lastIndent, curr: $currIndent, next: $nextIndent, ext: $extIndent}) - warn_fn """negative indentation at line #{line_number}: #{raw_line}""" if $currIndent < 0 - new_line = (if raw_line.length and $currIndent > 0 and !$template_flag then indent.repeat($currIndent) else '') + raw_line - $useful = $prevLength > 0 or raw_line.length > 0 - $lastIndent = $currIndent - $currIndent = $nextIndent - $prevLength = raw_line.length - new_line or undefined + 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) + $currIndent = 0 + $nextIndent = 0 + $prevLength = 0 + $extIndent = 0 + $lastIndent = 0 + $template = 0 + new_code = str.split(/\r?\n/g).map (line, line_number) -> + $template_flag = false + if $template + res2 = line.match(/\](=*)\]/) + if res2 and $template == res2[1].length + 1 + $template_flag = true + if $template and !/]=*]$/.test(line) + arr = line.split(/\]=*\]/, 2) + comment = arr[0] + code = arr[1] + line = comment + ']' + '='.repeat($template - 1) + ']' + adjust_space(code) + $template = 0 + $template = 0 + else + return line + res1 = line.match(/\[(=*)\[/) + if res1 + $template = res1[1].length + 1 + if !$template_flag + line = line.trim() + # remote all spaces on both ends + line = adjust_space(line) + if !line.length + return '' + raw_line = line + line = line.replace(/(['"])[^\1]*?\1/, '') + # remove all quoted fragments for proper bracket processing + line = line.replace(/\s*--.+/, '') + # remove all comments; this ignores long bracket style comments + if /^((local )?function|repeat|while)\b/.test(line) and !/\bend\s*[\),;]*$/.test(line) or /\b(then|do)$/.test(line) and !/^elseif\b/.test(line) or /^if\b/.test(line) and /\bthen\b/.test(line) and !/\bend$/.test(line) or /\bfunction ?(?:\w+ )?\([^\)]*\)$/.test(line) and !/\bend$/.test(line) + $nextIndent = $currIndent + 1 + else if /^until\b/.test(line) or /^end\s*[\),;]*$/.test(line) or /^end\s*\)\s*\.\./.test(line) or /^else(if)?\b/.test(line) and /\bend$/.test(line) + $nextIndent = --$currIndent + else if /^else\b/.test(line) or /^elseif\b/.test(line) + $nextIndent = $currIndent + $currIndent = $currIndent - 1 + $brackets = (line.match(/\(/g) or []).length - ((line.match(/\)/g) or []).length) + # capture unbalanced brackets + $curly = (line.match(/\{/g) or []).length - ((line.match(/\}/g) or []).length) + # capture unbalanced curly brackets + # close (curly) brackets if needed + if $curly < 0 + $currIndent += $curly + if $brackets < 0 + $currIndent += $brackets + $nextIndent += $brackets + $curly + # console.log({last: $lastIndent, curr: $currIndent, next: $nextIndent, ext: $extIndent}) + if $currIndent - $lastIndent > 1 + $extIndent += $nextIndent - $lastIndent - 1 + $nextIndent = $currIndent = 1 + $lastIndent + if $currIndent - $lastIndent < -1 and $extIndent > 0 + $extIndent += $currIndent - $lastIndent + 1 + $currIndent = -1 + $lastIndent + if $nextIndent < $currIndent + $nextIndent = $currIndent + # console.log({last: $lastIndent, curr: $currIndent, next: $nextIndent, ext: $extIndent}) + warn_fn """negative indentation at line #{line_number}: #{raw_line}""" if $currIndent < 0 + new_line = (if raw_line.length and $currIndent > 0 and !$template_flag then indent.repeat($currIndent) else '') + raw_line + $useful = $prevLength > 0 or raw_line.length > 0 + $lastIndent = $currIndent + $currIndent = $nextIndent + $prevLength = raw_line.length + new_line or undefined - warn_fn 'positive indentation at the end' if $currIndent > 0 - new_code.join '\n' + warn_fn 'positive indentation at the end' if $currIndent > 0 + new_code.join '\n'