Improve Fast Wrap

(|)"foo"     TO ("foo")
    (|)"\\f\"oo" TO ("\\f\"oo")
    (|)func()    TO (func())
This commit is contained in:
jiangfriend@gmail.com 2012-05-23 12:46:42 +08:00
parent df761f3f28
commit d0d077a57d
2 changed files with 55 additions and 20 deletions

View File

@ -53,9 +53,16 @@ Features
* Fast Wrap * Fast Wrap
input: |'hello' (press (<M-e> at|) input: |'hello' (press (<M-e> at |)
output: ('hello') output: ('hello')
wrap string, only support c style string
input: |'h\\el\'lo' (press (<M-e> at |)
output ('h\\ello\'')
input: |[foo, bar()] (press (<M-e> at |)
output: ([foo, bar()])
* Quick jump to closed pair. * Quick jump to closed pair.
input: input:

View File

@ -184,34 +184,62 @@ endfunction
function! AutoPairsJump() function! AutoPairsJump()
call search('["\]'')}]','W') call search('["\]'')}]','W')
endfunction 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 " Fast wrap the word in brackets
function! AutoPairsFastWrap() function! AutoPairsFastWrap()
let line = getline('.') let line = getline('.')
let current_char = line[col('.')-1] let current_char = line[col('.')-1]
let next_char = line[col('.')] let next_char = line[col('.')]
let open_pair_pattern = '\v[({\[''"]'
" Ignore EOL let at_end = col('.') >= col('$') - 1
if col('.') == col('$') normal x
return '' " Skip blank
end if next_char =~ '\v\s' || at_end
call search('\v\S', 'W')
normal! x let line = getline('.')
if next_char =~ '\s' let next_char = line[col('.')-1]
call search('\S', 'W')
let next_char = getline('.')[col('.')-1]
end end
if has_key(g:AutoExtraPairs, next_char) if has_key(g:AutoPairs, next_char)
let close = g:AutoExtraPairs[next_char] let followed_open_pair = next_char
call search(close, 'W') let inputed_close_pair = current_char
return "\<RIGHT>".current_char."\<LEFT>" let followed_close_pair = g:AutoPairs[next_char]
else if followed_close_pair != followed_open_pair
if next_char =~ '\w' " TODO replace system searchpair to skip string and nested pair.
execute "normal! he" " 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 end
execute "normal! a".current_char return "\<RIGHT>".inputed_close_pair."\<LEFT>"
return "" else
normal e
return "\<RIGHT>".current_char."\<LEFT>"
end end
endfunction endfunction