Improve completed snippet feature implementation

This commit is contained in:
Shougo Matsushita 2015-12-26 11:54:30 +09:00
parent 0bc303430b
commit 402f77feb3
2 changed files with 54 additions and 47 deletions

View File

@ -29,58 +29,14 @@ set cpo&vim
function! neosnippet#handlers#_complete_done() "{{{
if empty(v:completed_item)
\ || !g:neosnippet#enable_completed_snippet
\ || v:completed_item.word !~ '($'
\ || s:is_auto_pairs()
return
endif
let pairs = { '(' : ')', '{' : '}', '"' : '"' }
if index(keys(pairs), v:completed_item.word[-1:]) < 0
let snippet = neosnippet#parser#_get_completed_snippet(v:completed_item)
if snippet == ''
return
endif
let key = v:completed_item.word[-1:]
let pair = pairs[key]
let item = v:completed_item
let abbr = (item.abbr != '') ? item.abbr : item.word
if len(item.menu) > 5
" Combine menu.
let abbr .= ' ' . item.menu
endif
if item.info != ''
let abbr = split(item.info, '\n')[0]
endif
" Make snippet arguments
let cnt = 1
let snippet = ''
if key == '('
for arg in split(substitute(neosnippet#handlers#_get_in_paren(abbr),
\ '(\zs.\{-}\ze)', '', 'g'), '[^[]\zs\s*,\s*')
if arg ==# 'self' && &filetype ==# 'python'
" Ignore self argument
continue
endif
if cnt != 1
let snippet .= ', '
endif
let snippet .= printf('${%d:#:%s}', cnt, escape(arg, '{}'))
let cnt += 1
endfor
endif
if !s:is_auto_pairs()
if key != '(' && snippet[-1:] ==# key
let snippet .= '${' . cnt . '}' . pair
let cnt += 1
elseif snippet[-1:] !=# pair
let snippet .= pair
endif
let snippet .= '${' . cnt . '}'
endif
let [cur_text, col, _] = neosnippet#mappings#_pre_trigger()
call neosnippet#view#_insert(snippet, {}, cur_text, col)

View File

@ -292,6 +292,57 @@ function! neosnippet#parser#_initialize_snippet_options() "{{{
\ }
endfunction"}}}
function! neosnippet#parser#_get_completed_snippet(completed_item) "{{{
let pairs = { '(' : ')', '{' : '}', '"' : '"' }
if index(keys(pairs), a:completed_item.word[-1:]) < 0
return ''
endif
let key = a:completed_item.word[-1:]
let pair = pairs[key]
let item = a:completed_item
let abbr = (item.abbr != '') ? item.abbr : item.word
if len(item.menu) > 5
" Combine menu.
let abbr .= ' ' . item.menu
endif
if item.info != ''
let abbr = split(item.info, '\n')[0]
endif
" Make snippet arguments
let cnt = 1
let snippet = ''
if key == '('
for arg in split(substitute(neosnippet#handlers#_get_in_paren(abbr),
\ '(\zs.\{-}\ze)', '', 'g'), '[^[]\zs\s*,\s*')
if arg ==# 'self' && &filetype ==# 'python'
" Ignore self argument
continue
endif
if cnt != 1
let snippet .= ', '
endif
let snippet .= printf('${%d:#:%s}', cnt, escape(arg, '{}'))
let cnt += 1
endfor
endif
if key != '(' && snippet[-1:] ==# key
let snippet .= '${' . cnt . '}' . pair
let cnt += 1
elseif snippet[-1:] !=# pair
let snippet .= pair
endif
let snippet .= '${' . cnt . '}'
return snippet
endfunction"}}}
let &cpo = s:save_cpo
unlet s:save_cpo