From 897535d60bf8d8efb1b26ae20e67a311290f83e1 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 2 Dec 2014 12:15:21 +0900 Subject: [PATCH] Better paren matching --- autoload/argwrap.vim | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/autoload/argwrap.vim b/autoload/argwrap.vim index 98271f9..4b0d903 100644 --- a/autoload/argwrap.vim +++ b/autoload/argwrap.vim @@ -65,7 +65,7 @@ function! argwrap#findClosestRange() endif endfunction -function! argwrap#extractArgumentText(range) +function! argwrap#extractContainerArgText(range) let l:text = '' for l:lineIndex in range(a:range.lineStart, a:range.lineEnd) @@ -107,7 +107,7 @@ function! argwrap#trimArgument(text) return substitute(a:text, '^\s*\(.\{-}\)\s*$', '\1', '') endfunction -function! argwrap#extractArguments(text) +function! argwrap#extractContainerArgs(text) let l:stack = [] let l:arguments = [] let l:argument = '' @@ -164,19 +164,30 @@ function! argwrap#unwrapContainer(range, container, arguments) exec printf('%d,%dd_', a:range.lineStart + 1, a:range.lineEnd) endfunction -function! argwrap#toggle() - let l:range = argwrap#findClosestRange() - if !argwrap#validateRange(l:range) - call search('[\(\[\{]', 'W') - call search('[^\(\[\{]', 'W') - let l:range = argwrap#findClosestRange() - if !argwrap#validateRange(l:range) - return +function! argwrap#adjustCursor() + let [l:buffer, l:line, l:col, l:offset] = getpos('.') + let l:lineText = getline('.') + + let l:col = match(l:lineText, '[\(\[\{]') + if l:col >= 0 + if l:col + 1 == strlen(l:lineText) + call cursor(l:line + 1, 0) + else + call cursor(l:line, l:col + 2) endif endif +endfunction - let l:argText = argwrap#extractArgumentText(l:range) - let l:arguments = argwrap#extractArguments(l:argText) +function! argwrap#toggle() + call argwrap#adjustCursor() + + let l:range = argwrap#findClosestRange() + if !argwrap#validateRange(l:range) + return + endif + + let l:argText = argwrap#extractContainerArgText(l:range) + let l:arguments = argwrap#extractContainerArgs(l:argText) let l:container = argwrap#extractContainer(l:range) if l:range.lineStart == l:range.lineEnd