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}'
This commit is contained in:
hxsf 2017-03-30 12:08:51 +08:00
parent 25b3ef0417
commit 3c6479680b
3 changed files with 135 additions and 81 deletions

View File

@ -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

View File

@ -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

View File

@ -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'