Handle wrapping of empty braces in a better way

This commit is contained in:
Alex Yatskov 2015-03-26 18:28:22 +09:00
parent 4bc6e8623a
commit e111ac083a

View File

@ -26,9 +26,9 @@ function! argwrap#compareRanges(range1, range2)
let [l:buffer, l:line, l:col, l:offset] = getpos('.') let [l:buffer, l:line, l:col, l:offset] = getpos('.')
let l:lineDiff1 = a:range1.lineStart - l:line let l:lineDiff1 = a:range1.lineStart - l:line
let l:colDiff1 = a:range1.colStart - l:col let l:colDiff1 = a:range1.colStart - l:col
let l:lineDiff2 = a:range2.lineStart - l:line let l:lineDiff2 = a:range2.lineStart - l:line
let l:colDiff2 = a:range2.colStart - l:col let l:colDiff2 = a:range2.colStart - l:col
if l:lineDiff1 < l:lineDiff2 if l:lineDiff1 < l:lineDiff2
return 1 return 1
@ -45,7 +45,7 @@ endfunction
function! argwrap#findRange(braces) function! argwrap#findRange(braces)
let [l:lineStart, l:colStart] = searchpairpos(a:braces[0], '', a:braces[1], 'Wnb') let [l:lineStart, l:colStart] = searchpairpos(a:braces[0], '', a:braces[1], 'Wnb')
let [l:lineEnd, l:colEnd] = searchpairpos(a:braces[0], '', a:braces[1], 'Wcn') let [l:lineEnd, l:colEnd] = searchpairpos(a:braces[0], '', a:braces[1], 'Wcn')
return {'lineStart': l:lineStart, 'colStart': l:colStart, 'lineEnd': l:lineEnd, 'colEnd': l:colEnd} return {'lineStart': l:lineStart, 'colStart': l:colStart, 'lineEnd': l:lineEnd, 'colEnd': l:colEnd}
endfunction endfunction
@ -82,8 +82,8 @@ function! argwrap#extractContainerArgText(range)
endif endif
if l:extractStart < l:extractEnd if l:extractStart < l:extractEnd
let l:extract = l:lineText[l:extractStart : l:extractEnd - 1] let l:extract = l:lineText[l:extractStart : l:extractEnd - 1]
let l:text .= substitute(l:extract, '^\s*\(.\{-}\)\s*$', '\1 ', '') let l:text .= substitute(l:extract, '^\s*\(.\{-}\)\s*$', '\1 ', '')
endif endif
endfor endfor
@ -91,7 +91,7 @@ function! argwrap#extractContainerArgText(range)
endfunction endfunction
function! argwrap#updateScope(stack, char) function! argwrap#updateScope(stack, char)
let l:pairs = {'"': '"', '''': '''', ')': '(', ']': '[', '}': '{'} let l:pairs = {'"': '"', '''': '''', ')': '(', ']': '[', '}': '{'}
let l:length = len(a:stack) let l:length = len(a:stack)
if l:length > 0 && get(l:pairs, a:char, '') == a:stack[l:length - 1] if l:length > 0 && get(l:pairs, a:char, '') == a:stack[l:length - 1]
@ -108,29 +108,34 @@ function! argwrap#trimArgument(text)
endfunction endfunction
function! argwrap#extractContainerArgs(text) function! argwrap#extractContainerArgs(text)
let l:stack = [] let l:text = substitute(a:text, '^\s*\(.\{-}\)\s*$', '\1', '')
let l:stack = []
let l:arguments = [] let l:arguments = []
let l:argument = '' let l:argument = ''
for l:index in range(strlen(a:text)) if len(l:text) > 0
let l:char = a:text[l:index] for l:index in range(strlen(l:text))
call argwrap#updateScope(l:stack, l:char) let l:char = l:text[l:index]
call argwrap#updateScope(l:stack, l:char)
if len(l:stack) == 0 && l:char == ',' if len(l:stack) == 0 && l:char == ','
call add(l:arguments, argwrap#trimArgument(l:argument)) call add(l:arguments, argwrap#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, argwrap#trimArgument(l:argument))
endif
call add(l:arguments, argwrap#trimArgument(l:argument))
return l:arguments return l:arguments
endfunction endfunction
function! argwrap#extractContainer(range) function! argwrap#extractContainer(range)
let l:textStart = getline(a:range.lineStart) let l:textStart = getline(a:range.lineStart)
let l:textEnd = getline(a:range.lineEnd) let l:textEnd = getline(a:range.lineEnd)
let l:indent = matchstr(l:textStart, '\s*') let l:indent = matchstr(l:textStart, '\s*')
let l:prefix = l:textStart[strlen(l:indent) : a:range.colStart - 1] let l:prefix = l:textStart[strlen(l:indent) : a:range.colStart - 1]
@ -141,7 +146,7 @@ endfunction
function! argwrap#wrapContainer(range, container, arguments, wrapBrace) function! argwrap#wrapContainer(range, container, arguments, wrapBrace)
let l:argCount = len(a:arguments) let l:argCount = len(a:arguments)
let l:line = a:range.lineStart let l:line = a:range.lineStart
call setline(l:line, a:container.indent . a:container.prefix) call setline(l:line, a:container.indent . a:container.prefix)
@ -200,7 +205,7 @@ function! argwrap#toggle()
return return
endif endif
let l:argText = argwrap#extractContainerArgText(l:range) let l:argText = argwrap#extractContainerArgText(l:range)
let l:arguments = argwrap#extractContainerArgs(l:argText) let l:arguments = argwrap#extractContainerArgs(l:argText)
if len(l:arguments) == 0 if len(l:arguments) == 0
return return