- Refactored expand snippet behavior.

This commit is contained in:
Shougo Matsushita 2012-03-14 00:39:45 +09:00
parent 23146787d0
commit 55803fcfcd
2 changed files with 48 additions and 76 deletions

View File

@ -1,7 +1,7 @@
"============================================================================= "=============================================================================
" FILE: snippets_complete.vim " FILE: snippets_complete.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com> " AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
" Last Modified: 08 Mar 2012. " Last Modified: 14 Mar 2012.
" License: MIT license {{{ " License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining " Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the " a copy of this software and associated documentation files (the
@ -299,22 +299,23 @@ function! s:set_snippet_pattern(dict)"{{{
let abbr_pattern = printf('%%.%ds..%%s', let abbr_pattern = printf('%%.%ds..%%s',
\ g:neocomplcache_max_keyword_width-10) \ g:neocomplcache_max_keyword_width-10)
let word = substitute(a:dict.word, '\%(<\\n>\)\+$', '', '') let a:dict.word = substitute(a:dict.word, '\n$', '', '')
let menu_pattern = a:dict.word =~ '\${\d\+\%(:.\{-}\)\?\\\@<!}' ? let menu_pattern =
\ (a:dict.word =~ '\${\d\+\%(:.\{-}\)\?\\\@<!}') ?
\ '<Snip> ' : '[Snip] ' \ '<Snip> ' : '[Snip] '
let abbr = has_key(a:dict, 'abbr')? a:dict.abbr : let abbr = has_key(a:dict, 'abbr')? a:dict.abbr :
\ substitute(a:dict.word, \ substitute(a:dict.word,
\ '\${\d\+\%(:.\{-}\)\?\\\@<!}\|\$<\d\+\%(:.\{-}\)\?\\\@<!>\|'. \ '\${\d\+\%(:.\{-}\)\?\\\@<!}\|\$<\d\+\%(:.\{-}\)\?\\\@<!>\|'.
\ '\$\d\+\|<\%(\\n\|\\t\)>\|\s\+', ' ', 'g') \ '\$\d\+\|\s\+\|\n', ' ', 'g')
let abbr = (g:neocomplcache_max_keyword_width >= 0 && let abbr = (g:neocomplcache_max_keyword_width >= 0 &&
\ len(abbr) > g:neocomplcache_max_keyword_width)? \ len(abbr) > g:neocomplcache_max_keyword_width)?
\ printf(abbr_pattern, abbr, abbr[-8:]) : abbr \ printf(abbr_pattern, abbr, abbr[-8:]) : abbr
let dict = { let dict = {
\ 'word' : a:dict.name, \ 'word' : a:dict.name,
\ 'snip' : word, 'abbr' : a:dict.name, \ 'snip' : a:dict.word, 'abbr' : a:dict.name,
\ 'description' : word, \ 'description' : a:dict.word,
\ 'menu' : menu_pattern . abbr, 'dup' : 1 \ 'menu' : menu_pattern . abbr, 'dup' : 1
\} \}
if has_key(a:dict, 'prev_word') if has_key(a:dict, 'prev_word')
@ -408,7 +409,8 @@ function! s:load_snippets(snippet, snippets_file)"{{{
call neocomplcache#print_error('Warning: ' . a:snippets_file . ':' call neocomplcache#print_error('Warning: ' . a:snippets_file . ':'
\ . linenr . ': duplicated snippet name `' \ . linenr . ': duplicated snippet name `'
\ . snippet_pattern.name . '`') \ . snippet_pattern.name . '`')
call neocomplcache#print_error('Please delete this snippet name before.') call neocomplcache#print_error(
\ 'Please delete this snippet name before.')
endif endif
elseif has_key(snippet_pattern, 'name') elseif has_key(snippet_pattern, 'name')
" Only in snippets. " Only in snippets.
@ -423,17 +425,13 @@ function! s:load_snippets(snippet, snippets_file)"{{{
elseif line =~ '^\s' elseif line =~ '^\s'
if snippet_pattern.word == '' if snippet_pattern.word == ''
let snippet_pattern.word = matchstr(line, '^\s\+\zs.*$') let snippet_pattern.word = matchstr(line, '^\s\+\zs.*$')
elseif line =~ '^\t'
let line = substitute(line, '^\s', '', '')
let snippet_pattern.word .= '<\n>' .
\ substitute(line, '^\t\+', repeat('<\\t>',
\ matchend(line, '^\t\+')), '')
else else
let snippet_pattern.word .= '<\n>' . matchstr(line, '^\s\+\zs.*$') let snippet_pattern.word .= "\n"
\ . matchstr(line, '^\s\+\zs.*$')
endif endif
elseif line =~ '^$' elseif line =~ '^$'
" Blank line. " Blank line.
let snippet_pattern.word .= '<\n>' let snippet_pattern.word .= "\n"
endif endif
endif endif
@ -500,6 +498,7 @@ function! s:snippets_jump_or_expand(cur_text, col)"{{{
\ a:cur_text, a:col, cur_word) \ a:cur_text, a:col, cur_word)
endif endif
endfunction"}}} endfunction"}}}
function! neocomplcache#sources#snippets_complete#expand(cur_text, col, trigger_name)"{{{ function! neocomplcache#sources#snippets_complete#expand(cur_text, col, trigger_name)"{{{
if a:trigger_name == '' if a:trigger_name == ''
let pos = getpos('.') let pos = getpos('.')
@ -523,26 +522,31 @@ function! neocomplcache#sources#snippets_complete#expand(cur_text, col, trigger_
if snip_word =~ '\\\@<!`.*\\\@<!`' if snip_word =~ '\\\@<!`.*\\\@<!`'
let snip_word = s:eval_snippet(snip_word) let snip_word = s:eval_snippet(snip_word)
endif endif
if snip_word =~ '\n'
let snip_word = substitute(snip_word, '\n', '<\\n>', 'g')
endif
" Substitute escaped `. " Substitute escaped `.
let snip_word = substitute(snip_word, '\\`', '`', 'g') let snip_word = substitute(snip_word, '\\`', '`', 'g')
" Insert snippets. " Insert snippets.
let next_line = getline('.')[a:col-1 :] let next_line = getline('.')[a:col-1 :]
call setline(line('.'), cur_text . snip_word . next_line) let snippet_lines = split(snip_word, '\n', 1)
let pos = getpos('.') if empty(snippet_lines)
let pos[2] = len(cur_text)+len(snip_word)+1 return
call setpos('.', pos) endif
let next_col = len(cur_text)+len(snip_word)+1
if snip_word =~ '<\\t>' let begin_line = line('.')
call s:expand_tabline() let end_line = line('.') + len(snippet_lines) - 1
else let s:begin_snippet = begin_line
call s:expand_newline() let s:end_snippet = end_line
end let s:snippet_holder_cnt = 1
let snippet_lines[0] = cur_text . snippet_lines[0]
let next_col = len(snippet_lines[-1]) + 1
let snippet_lines[-1] = snippet_lines[-1] . next_line
call setline(line('.'), snippet_lines)
call cursor(0, next_col)
call s:indent_snippet(begin_line, end_line)
if has('folding') && foldclosed(line('.')) if has('folding') && foldclosed(line('.'))
" Open fold. " Open fold.
silent! normal! zO silent! normal! zO
@ -560,64 +564,29 @@ function! neocomplcache#sources#snippets_complete#expand(cur_text, col, trigger_
let &l:iminsert = 0 let &l:iminsert = 0
let &l:imsearch = 0 let &l:imsearch = 0
endfunction"}}} endfunction"}}}
function! s:expand_newline()"{{{ function! s:indent_snippet(begin, end)"{{{
let match = match(getline('.'), '<\\n>')
let s:snippet_holder_cnt = 1
let s:begin_snippet = line('.')
let s:end_snippet = line('.')
let formatoptions = &l:formatoptions
setlocal formatoptions-=r
let equalprg = &l:equalprg let equalprg = &l:equalprg
setlocal equalprg= setlocal equalprg=
while match >= 0 for line_nr in range(a:begin, a:end)
let end = getline('.')[matchend(getline('.'), '<\\n>') :] call cursor(line_nr, 0)
" Substitute CR.
silent! execute 's/<\\n>//' . (&gdefault ? 'g' : '')
" Return. if &l:expandtab && getline('.') =~ '^\t\+'
let pos = getpos('.') " Expand tab.
let pos[2] = match+1 cal setline('.', substitute(getline('.'),
call setpos('.', pos) \ '^\t\+', repeat(' ', &shiftwidth *
silent execute 'normal!' \ len(matchstr(getline('.'), '^\t\+'))), ''))
\ (match+1 >= col('$')? 'a' : 'i')."\<CR>"
let pos = getpos('.')
startinsert!
normal! ==
call setpos('.', pos)
" Next match.
let match = match(getline('.'), '<\\n>')
let s:end_snippet += 1
endwhile
let &l:formatoptions = formatoptions
let &l:equalprg = equalprg
endfunction"}}}
function! s:expand_tabline()"{{{
let tablines = split(getline('.'), '<\\n>')
let indent = matchstr(tablines[0], '^\s\+')
let line = line('.')
call setline(line, tablines[0])
for tabline in tablines[1:]
if &expandtab
let tabline = substitute(tabline, '<\\t>',
\ repeat(' ', &shiftwidth), 'g')
else else
let tabline = substitute(tabline, '<\\t>', '\t', 'g') let pos = getpos('.')
startinsert!
normal! ==
call setpos('.', pos)
endif endif
call append(line, indent . tabline)
let line += 1
endfor endfor
let s:snippet_holder_cnt = 1 let &l:equalprg = equalprg
let s:begin_snippet = line('.')
let s:end_snippet = line('.') + len(tablines) - 1
endfunction"}}} endfunction"}}}
function! s:snippets_force_jump(cur_text, col)"{{{ function! s:snippets_force_jump(cur_text, col)"{{{
if !s:search_snippet_range(s:begin_snippet, s:end_snippet) if !s:search_snippet_range(s:begin_snippet, s:end_snippet)
if s:snippet_holder_cnt != 0 if s:snippet_holder_cnt != 0

View File

@ -295,6 +295,9 @@ snippet *neocomplcache-snippets-complete-unite-action-snippet*
============================================================================== ==============================================================================
CHANGELOG *neocomplcache-snippets-complete-changelog* CHANGELOG *neocomplcache-snippets-complete-changelog*
2012-03-14
- Refactored expand snippet behavior.
2012-03-08 2012-03-08
- Use shiftwidth instead of softabstop. - Use shiftwidth instead of softabstop.
- Fixed plugin indentation problem. - Fixed plugin indentation problem.