From d0d077a57d0fa296e745a072f49fb76b44f4c295 Mon Sep 17 00:00:00 2001 From: "jiangfriend@gmail.com" Date: Wed, 23 May 2012 12:46:42 +0800 Subject: [PATCH] Improve Fast Wrap (|)"foo" TO ("foo") (|)"\\f\"oo" TO ("\\f\"oo") (|)func() TO (func()) --- README.md | 9 +++++- plugin/auto-pairs.vim | 66 ++++++++++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7216728..629e4c0 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,16 @@ Features * Fast Wrap - input: |'hello' (press ( at|) + input: |'hello' (press ( at |) output: ('hello') + wrap string, only support c style string + input: |'h\\el\'lo' (press ( at |) + output ('h\\ello\'') + + input: |[foo, bar()] (press ( at |) + output: ([foo, bar()]) + * Quick jump to closed pair. input: diff --git a/plugin/auto-pairs.vim b/plugin/auto-pairs.vim index a198dc1..3f1ce8b 100644 --- a/plugin/auto-pairs.vim +++ b/plugin/auto-pairs.vim @@ -184,34 +184,62 @@ endfunction function! AutoPairsJump() call search('["\]'')}]','W') endfunction +" string_chunk cannot use standalone +let s:string_chunk = '\v%(\\\_.|[^\1]|[\r\n]){-}' +let s:ss_pattern = '\v''' . s:string_chunk . '''' +let s:ds_pattern = '\v"' . s:string_chunk . '"' + +func! s:RegexpQuote(str) + return substitute(a:str, '\v[\[\{\(\<\>\)\}\]]', '\\&', 'g') +endf + +func! s:RegexpQuoteInSquare(str) + return substitute(a:str, '\v[\[\]]', '\\&', 'g') +endf + +" Search next open or close pair +func! s:FormatChunk(open, close) + let open = s:RegexpQuote(a:open) + let close = s:RegexpQuote(a:close) + let open2 = s:RegexpQuoteInSquare(a:open) + let close2 = s:RegexpQuoteInSquare(a:close) + if open == close + return '\v'.open.s:string_chunk.close + else + return '\v%(' . s:ss_pattern . '|' . s:ds_pattern . '|' . '[^'.open2.close2.']|[\r\n]' . '){-}(['.open2.close2.'])' + end +endf " Fast wrap the word in brackets function! AutoPairsFastWrap() let line = getline('.') let current_char = line[col('.')-1] let next_char = line[col('.')] - - " Ignore EOL - if col('.') == col('$') - return '' - end - - normal! x - if next_char =~ '\s' - call search('\S', 'W') - let next_char = getline('.')[col('.')-1] + let open_pair_pattern = '\v[({\[''"]' + let at_end = col('.') >= col('$') - 1 + normal x + " Skip blank + if next_char =~ '\v\s' || at_end + call search('\v\S', 'W') + let line = getline('.') + let next_char = line[col('.')-1] end - if has_key(g:AutoExtraPairs, next_char) - let close = g:AutoExtraPairs[next_char] - call search(close, 'W') - return "\".current_char."\" - else - if next_char =~ '\w' - execute "normal! he" + if has_key(g:AutoPairs, next_char) + let followed_open_pair = next_char + let inputed_close_pair = current_char + let followed_close_pair = g:AutoPairs[next_char] + if followed_close_pair != followed_open_pair + " TODO replace system searchpair to skip string and nested pair. + " eg: (|){"hello}world"} will transform to ({"hello})world"} + call searchpair('\V'.followed_open_pair, '', '\V'.followed_close_pair, 'W') + else + call search(s:FormatChunk(followed_open_pair, followed_close_pair), 'We') end - execute "normal! a".current_char - return "" + return "\".inputed_close_pair."\" + else + normal e + return "\".current_char."\" end endfunction