Syntax awareness.

This commit is contained in:
Israel Chauca Fuentes
2010-05-24 22:11:23 -05:00
parent e5ce5b0967
commit da66a3f7c9
5 changed files with 137 additions and 60 deletions

View File

@@ -1,7 +1,7 @@
" ============================================================================
" File: autoload/delimitMate.vim
" Version: 2.2
" Modified: 2010-05-16
" Version: 2.3
" Modified: 2010-05-24
" Description: This plugin provides auto-completion for quotes, parens, etc.
" Maintainer: Israel Chauca F. <israelchauca@gmail.com>
" Manual: Read ":help delimitMate".
@@ -304,8 +304,16 @@ function! delimitMate#RestoreRegister() " {{{
echo ""
endfunction " }}}
function! delimitMate#GetSyntaxRegion(line, col) "{{{
return synIDattr(synIDtrans(synID(a:line, a:col, 1)), 'name')
endfunction " }}}
function! delimitMate#GetCurrentSyntaxRegion() "{{{
return synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name')
let col = col('.')
if col == col('$')
let col = col - 1
endif
return delimitMate#GetSyntaxRegion(line('.'), col)
endfunction " }}}
function! delimitMate#GetCurrentSyntaxRegionIf(char) "{{{
@@ -313,23 +321,25 @@ function! delimitMate#GetCurrentSyntaxRegionIf(char) "{{{
let origin_line = getline('.')
let changed_line = strpart(origin_line, 0, col - 1) . a:char . strpart(origin_line, col - 1)
call setline('.', changed_line)
let region = synIDattr(synIDtrans(synID(line('.'), col, 1)), 'name')
let region = delimitMate#GetSyntaxRegion(line('.'), col)
call setline('.', origin_line)
return region
endfunction "}}}
function! delimitMate#IsForbidden(char) "{{{
if b:delimitMate_excluded_regions_enabled = 0
if b:delimitMate_excluded_regions_enabled == 0
return 0
endif
let result = index(b:delimitMate_excluded_regions_list, delimitMate#GetCurrentSyntaxRegion()) >= 0
if result >= 0
return result + 1
"let result = index(b:delimitMate_excluded_regions_list, delimitMate#GetCurrentSyntaxRegion()) >= 0
if index(b:delimitMate_excluded_regions_list, delimitMate#GetCurrentSyntaxRegion()) >= 0
"echom "Forbidden 1!"
return 1
endif
let region = delimitMate#GetCurrentSyntaxRegionIf(a:char)
let result = index(b:delimitMate_excluded_regions_list, region) >= 0
"let result = index(b:delimitMate_excluded_regions_list, region) >= 0
"return result || region == 'Comment'
return result + 1
"echom "Forbidden 2!"
return index(b:delimitMate_excluded_regions_list, region) >= 0
endfunction "}}}
function! delimitMate#FlushBuffer() " {{{
@@ -340,6 +350,9 @@ endfunction " }}}
" Doers {{{
function! delimitMate#JumpIn(char) " {{{
if delimitMate#IsForbidden(a:char)
return ''
endif
let line = getline('.')
let col = col('.')-2
if (col) < 0
@@ -354,6 +367,9 @@ function! delimitMate#JumpIn(char) " {{{
endfunction " }}}
function! delimitMate#JumpOut(char) "{{{
if delimitMate#IsForbidden(a:char)
return a:char
endif
let line = getline('.')
let col = col('.')-2
if line[col+1] == a:char
@@ -363,7 +379,13 @@ function! delimitMate#JumpOut(char) "{{{
endif
endfunction " }}}
function! delimitMate#JumpAny() " {{{
function! delimitMate#JumpAny(key) " {{{
if delimitMate#IsForbidden('')
return a:key
endif
if !delimitMate#ShouldJump()
return a:key
endif
" Let's get the character on the right.
let char = getline('.')[col('.')-1]
if char == " "
@@ -383,6 +405,9 @@ function! delimitMate#JumpAny() " {{{
endfunction " delimitMate#JumpAny() }}}
function! delimitMate#SkipDelim(char) "{{{
if delimitMate#IsForbidden(a:char)
return a:char
endif
let col = col('.') - 1
let line = getline('.')
if col > 0
@@ -410,6 +435,9 @@ function! delimitMate#SkipDelim(char) "{{{
endfunction "}}}
function! delimitMate#QuoteDelim(char) "{{{
if delimitMate#IsForbidden(a:char)
return a:char
endif
let line = getline('.')
let col = col('.') - 2
if line[col] == "\\"
@@ -442,6 +470,9 @@ function! delimitMate#MapMsg(msg) "{{{
endfunction "}}}
function! delimitMate#ExpandReturn() "{{{
if delimitMate#IsForbidden("")
return "\<CR>"
endif
if delimitMate#WithinEmptyPair()
" Expand:
call delimitMate#FlushBuffer()
@@ -453,6 +484,9 @@ function! delimitMate#ExpandReturn() "{{{
endfunction "}}}
function! delimitMate#ExpandSpace() "{{{
if delimitMate#IsForbidden("\<Space>")
return "\<Space>"
endif
if delimitMate#WithinEmptyPair()
" Expand:
call insert(b:delimitMate_buffer, 's')
@@ -463,16 +497,17 @@ function! delimitMate#ExpandSpace() "{{{
endfunction "}}}
function! delimitMate#BS() " {{{
if delimitMate#IsForbidden("")
return "\<BS>"
endif
if delimitMate#WithinEmptyPair()
"call delimitMate#RmBuffer(1)
return "\<BS>" . delimitMate#Del()
" return "\<Right>\<BS>\<BS>"
elseif b:delimitMate_expand_space &&
\ delimitMate#IsSpaceExpansion()
elseif delimitMate#IsSpaceExpansion()
"call delimitMate#RmBuffer(1)
return "\<BS>" . delimitMate#Del()
elseif b:delimitMate_expand_cr &&
\ delimitMate#IsCRExpansion()
elseif delimitMate#IsCRExpansion()
return "\<BS>\<Del>"
else
return "\<BS>"
@@ -499,7 +534,7 @@ function! delimitMate#Finish() " {{{
let b:delimitMate_buffer = []
let line = getline('.')
let col = col('.') -2
echom 'col: ' . col . '-' . line[:col] . "|" . line[col+len+1:] . '%' . buffer
"echom 'col: ' . col . '-' . line[:col] . "|" . line[col+len+1:] . '%' . buffer
if col < 0
call setline('.', line[col+len+1:])
else
@@ -529,7 +564,7 @@ endfunction " }}}
function! delimitMate#NoAutoClose() "{{{
" inoremap <buffer> ) <C-R>=delimitMate#SkipDelim('\)')<CR>
for delim in b:delimitMate_right_delims + b:delimitMate_quotes_list
exec 'inoremap <buffer> ' . delim . ' <C-R>=delimitMate#SkipDelim("' . escape(delim,'"') . '")<CR>'
exec 'inoremap <buffer> ' . delim . ' <C-R>=delimitMate#SkipDelim("' . escape(delim,'"\|') . '")<CR>'
endfor
endfunction "}}}
@@ -576,21 +611,21 @@ function! delimitMate#ExtraMappings() "{{{
inoremap <buffer> <BS> <C-R>=delimitMate#BS()<CR>
" If pair is empty, delete closing delimiter:
inoremap <buffer> <expr> <S-BS> delimitMate#WithinEmptyPair() ? "\<Del>" : "\<S-BS>"
inoremap <buffer> <expr> <S-BS> delimitMate#WithinEmptyPair() && !delimitMate#IsForbidden("") ? "\<Del>" : "\<S-BS>"
" Expand return if inside an empty pair:
if b:delimitMate_expand_cr != 0
inoremap <buffer> <expr> <CR> delimitMate#WithinEmptyPair() ? "\<C-R>=delimitMate#ExpandReturn()\<CR>" : "\<CR>"
inoremap <buffer> <CR> <C-R>=delimitMate#ExpandReturn()<CR>
endif
" Expand space if inside an empty pair:
if b:delimitMate_expand_space != 0
inoremap <buffer> <expr> <Space> delimitMate#WithinEmptyPair() ? "\<C-R>=delimitMate#ExpandSpace()\<CR>" : "\<Space>"
inoremap <buffer> <Space> <C-R>=delimitMate#ExpandSpace()<CR>
endif
" Jump out ot any empty pair:
if b:delimitMate_tab2exit
inoremap <buffer> <expr> <S-Tab> delimitMate#ShouldJump() ? "\<C-R>=delimitMate#JumpAny()\<CR>" : "\<S-Tab>"
inoremap <buffer> <S-Tab> <C-R>=delimitMate#JumpAny("\<S-Tab>")<CR>
endif
" Fix the re-do feature:

View File

@@ -226,7 +226,12 @@ function! delimitMateTests#Main() " {{{
" Deactivate parens on comments: The first call to a closing delimiter
" will not work here as expected, but it does in real life tests.
set ft=vim
call Type("Deactivate parens on comments", "\"()", ["\"()"], ["autoclose:0"], 1)
call Type("Deactivate parens on comments", "\"()[]", ["\"()[]|"], ["autoclose:0"], 1)
set ft=
" Deactivate quotes on comments: See previous note.
set ft=vim
call Type("Deactivate parens on comments", "\"(`", ["\"(``|"], [], 1)
set ft=
" Manual close at start of line