From e9ed21a996da25ac2c65ee0df4053aeae77689ba Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 1 Dec 2014 22:34:52 +0900 Subject: [PATCH] Handle wrapping of other bracket types --- argwrap.vim | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- sample.txt | 11 ++++++----- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/argwrap.vim b/argwrap.vim index 0b6f9e5..693c0b4 100644 --- a/argwrap.vim +++ b/argwrap.vim @@ -18,12 +18,54 @@ " CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -function! argwrap#findRange() - let [l:lineStart, l:colStart] = searchpairpos("(", "", ")", "Wnb") - let [l:lineEnd, l:colEnd] = searchpairpos("(", "", ")", "Wn") +function! argwrap#validateRange(range) + return len(a:range) > 0 && !(a:range.lineStart == 0 && a:range.colStart == 0 || a:range.lineEnd == 0 && a:range.colEnd == 0) +endfunction + +function! argwrap#compareRanges(range1, range2) + let [l:buffer, l:line, l:col, l:offset] = getpos(".") + + let l:lineDiff1 = a:range1.lineStart - l:line + let l:colDiff1 = a:range1.colStart - l:col + let l:lineDiff2 = a:range2.lineStart - l:line + let l:colDiff2 = a:range2.colStart - l:col + + if l:lineDiff1 < l:lineDiff2 + return 1 + elseif l:lineDiff1 > l:lineDiff2 + return -1 + elseif l:colDiff1 < l:colDiff2 + return 1 + elseif l:colDiff1 > l:colDiff2 + return -1 + else + return 0 + endif +endfunction + +function! argwrap#findRange(braces) + 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], "Wn") return {"lineStart": l:lineStart, "colStart": l:colStart, "lineEnd": l:lineEnd, "colEnd": l:colEnd} endfunction +function! argwrap#findClosestRange() + let l:ranges = [] + + for l:braces in [["(", ")"], ["\\[", "\\]"], ["{", "}"]] + let l:range = argwrap#findRange(braces) + if argwrap#validateRange(l:range) + call add(l:ranges, l:range) + endif + endfor + + if len(l:ranges) == 0 + return {} + else + return sort(l:ranges, "argwrap#compareRanges")[0] + endif +endfunction + function! argwrap#extractArgumentText(range) let l:text = "" @@ -120,8 +162,8 @@ function! argwrap#unwrapContainer(range, container, arguments) endfunction function! argwrap#toggle() - let l:range = argwrap#findRange() - if l:range.lineStart == 0 && l:range.colStart == 0 || l:range.lineEnd == 0 && l:range.colEnd == 0 + let l:range = argwrap#findClosestRange() + if !argwrap#validateRange(l:range) return endif diff --git a/sample.txt b/sample.txt index 72271f2..f277d4d 100644 --- a/sample.txt +++ b/sample.txt @@ -1,15 +1,18 @@ a = Foo(a, [b, z], "hello, world", c) +Foo(a, b) + Foo( a, - b + [b, c], + d ) -Foo(a, [b, c], d) +Foo(a, [b, "c"], d) Foo( a, - [b, "c"], + [b, c], d ) @@ -18,5 +21,3 @@ Foo( [b, c], d ) - -Foo(a, [b, c], d)