Better handling of nested quotes. Closes #134.

This commit is contained in:
Israel Chauca Fuentes
2013-07-22 20:52:05 -04:00
parent 7345bfddef
commit 543be33b13
3 changed files with 40 additions and 6 deletions

View File

@@ -111,6 +111,22 @@ function! delimitMate#IsEmptyPair(str) "{{{
return 0 return 0
endfunction "}}} endfunction "}}}
function! delimitMate#RightQ(char) "{{{
let i = 0
while delimitMate#GetCharFromCursor(i) ==# a:char
let i += 1
endwhile
return i
endfunction "}}}
function! delimitMate#LeftQ(char) "{{{
let i = 0
while delimitMate#GetCharFromCursor(i - 1) ==# a:char
let i -= 1
endwhile
return i * -1
endfunction "}}}
function! delimitMate#GetCharFromCursor(...) "{{{ function! delimitMate#GetCharFromCursor(...) "{{{
let idx = col('.') - 1 let idx = col('.') - 1
if !a:0 || (a:0 && a:1 >= 0) if !a:0 || (a:0 && a:1 >= 0)
@@ -334,10 +350,17 @@ function! delimitMate#QuoteDelim(char) "{{{
endif endif
let char_at = delimitMate#GetCharFromCursor(0) let char_at = delimitMate#GetCharFromCursor(0)
let char_before = delimitMate#GetCharFromCursor(-1) let char_before = delimitMate#GetCharFromCursor(-1)
if char_at == a:char && let nesting_on = index(s:g('nesting_quotes'), a:char) > -1
\ index(s:g('nesting_quotes'), a:char) < 0 let left_q = nesting_on ? delimitMate#LeftQ(a:char) : 0
" Get out of the string. if nesting_on && left_q > 1
return "\<Right>" " Nesting quotes.
let right_q = delimitMate#RightQ(a:char)
let quotes = right_q > left_q + 1 ? 0 : left_q - right_q + 2
let lefts = quotes - 1
return repeat(a:char, quotes) . repeat("\<Left>", lefts)
elseif char_at == a:char
" Inside an empty pair, jump out
return a:char . "\<Del>"
elseif delimitMate#IsSmartQuote(a:char) elseif delimitMate#IsSmartQuote(a:char)
" Seems like a smart quote, insert a single char. " Seems like a smart quote, insert a single char.
return a:char return a:char

View File

@@ -182,8 +182,10 @@ e.g.: >
Values: A list of quotes. ~ Values: A list of quotes. ~
Default: [] ~ Default: [] ~
Quotes listed here will not be able to jump out of the empty pair, thus When adding a third quote listed in this option is inserted, three quotes will
allowing the autoclosed quotes to be nested. be inserted to the right of the cursor and the cursor will stay in the middle.
If more quotes are inserted the number of quotes on both sides of the cursor
will stay balanced.
e.g.: > e.g.: >
let delimitMate_nesting_quotes = ['"','`'] let delimitMate_nesting_quotes = ['"','`']
au FileType python let b:delimitMate_nesting_quotes = ['"'] au FileType python let b:delimitMate_nesting_quotes = ['"']
@@ -714,6 +716,7 @@ This script was inspired by the auto-completion of delimiters on TextMate.
- Add delimitMate_eol_marker. - Add delimitMate_eol_marker.
- Reduce the number of mappings. - Reduce the number of mappings.
- Stop using setline(). - Stop using setline().
- Better handling of nested quotes.
|---------|------------|-----------------------------------------------------| |---------|------------|-----------------------------------------------------|
2.6 2011-01-14 * - Add smart_matchpairs feature. 2.6 2011-01-14 * - Add smart_matchpairs feature.
- Add mapping to jump over contiguous delimiters. - Add mapping to jump over contiguous delimiters.

View File

@@ -30,6 +30,7 @@ let g:delimitMate_autoclose = 1
# Handle backspace gracefully. # Handle backspace gracefully.
set backspace= set backspace=
"'\<Esc>a\<BS>x" "'x'" "'\<Esc>a\<BS>x" "'x'"
set backspace=2
set cpo=ces$ set cpo=ces$
"'x" "'x'" "'x" "'x'"
# Make sure smart quote works beyond first column. # Make sure smart quote works beyond first column.
@@ -40,3 +41,10 @@ set cpo=ces$
"}\<Home>'" "''}" "}\<Home>'" "''}"
"'\<Del>abc '" "'abc '" "'\<Del>abc '" "'abc '"
"''abc '" "''abc ''" "''abc '" "''abc ''"
# Nesting quotes:
let g:delimitMate_nesting_quotes = split(g:delimitMate_quotes, '\s\+')
"'''x" "'''x'''"
"''''x" "''''x''''"
"''x" "''x"
"'x" "'x'"
unlet g:delimitMate_nesting_quotes