Argument unwrapping now works

This commit is contained in:
Alex Yatskov 2014-12-01 18:53:00 +09:00
parent a98f27847f
commit 120b5a6d9c
2 changed files with 25 additions and 22 deletions

View File

@ -12,14 +12,10 @@ Foo(
) )
Foo( Foo(
a, a
[ [b, "c"]
b,
"c"
],
d d
) )
)
Foo( Foo(
a, a,

View File

@ -62,7 +62,7 @@ function! UpdateScopeStack(stack, char)
endif endif
endfunction endfunction
function! StripArgument(text) function! TrimArgument(text)
let l:stripped = substitute(a:text, "\\s\\+", "", "") let l:stripped = substitute(a:text, "\\s\\+", "", "")
let l:stripped = substitute(l:stripped, "^\\s\\+", "", "") let l:stripped = substitute(l:stripped, "^\\s\\+", "", "")
return l:stripped return l:stripped
@ -78,47 +78,54 @@ function! ExtractArguments(text)
call UpdateScopeStack(l:stack, l:char) call UpdateScopeStack(l:stack, l:char)
if len(l:stack) == 0 && l:char == "," if len(l:stack) == 0 && l:char == ","
call add(l:arguments, StripArgument(l:argument)) call add(l:arguments, TrimArgument(l:argument))
let l:argument = "" let l:argument = ""
else else
let l:argument .= l:char let l:argument .= l:char
endif endif
endfor endfor
call add(l:arguments, StripArgument(l:argument)) call add(l:arguments, TrimArgument(l:argument))
return l:arguments return l:arguments
endfunction endfunction
function! ExtractContainer(range) function! ExtractContainer(range)
let l:line = getline(a:range.lineStart) let l:prefix = getline(a:range.lineStart)[: a:range.colStart - 1]
let l:prefix = l:line[: a:range.colStart - 1] let l:suffix = getline(a:range.lineEnd)[a:range.colEnd - 1:]
let l:line = getline(a:range.lineEnd)
let l:suffix = l:line[a:range.colEnd - 1:]
return {"prefix": l:prefix, "suffix": l:suffix} return {"prefix": l:prefix, "suffix": l:suffix}
endfunction endfunction
function! RebuildContainer(range, container, arguments) function! WrapContainer(range, container, arguments)
let l:line = a:range.lineStart let l:line = a:range.lineStart
call setline(l:line, a:container.prefix) call setline(l:line, a:container.prefix)
for l:argument in a:arguments for l:argument in a:arguments
call append(l:line, l:argument) call append(l:line, l:argument)
let l:line += 1 let l:line += 1
call cursor(l:line, 1) exec printf("%s>", l:line)
normal! >>
endfor endfor
call append(l:line, a:container.suffix) call append(l:line, a:container.suffix)
endfunction endfunction
function! UnwrapContainer(range, container, arguments)
let l:text = a:container.prefix . join(a:arguments, ", ") . a:container.suffix
call setline(a:range.lineStart, l:text)
exec printf("%d,%dd", a:range.lineStart + 1, a:range.lineEnd)
endfunction
function! Unwrap()
let l:range = FindRange()
let l:argText = ExtractArgumentText(l:range)
let l:arguments = ExtractArguments(l:argText)
let l:container = ExtractContainer(l:range)
call UnwrapContainer(l:range, l:container, l:arguments)
endfunction
function! Wrap() function! Wrap()
let l:range = FindRange() let l:range = FindRange()
let l:argText = ExtractArgumentText(l:range) let l:argText = ExtractArgumentText(l:range)
let l:arguments = ExtractArguments(l:argText) let l:arguments = ExtractArguments(l:argText)
let l:container = ExtractContainer(l:range) let l:container = ExtractContainer(l:range)
echo l:arguments call WrapContainer(l:range, l:container, l:arguments)
call RebuildContainer(l:range, l:container, l:arguments)
endfunction endfunction