This repository has been archived by the owner on Jul 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rafactor for snippet support. Sketch for neosnippet api. Shougo/neosn…
- Loading branch information
Showing
4 changed files
with
131 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
|
||
let g:cm#snippet#snippets = [] | ||
|
||
func! cm#snippet#init() | ||
if !exists('g:cm_completed_snippet_enable') | ||
if exists('g:loaded_neosnippet') | ||
let g:cm_completed_snippet_enable = 1 | ||
let g:cm_completed_snippet_engine = 'neosnippet' | ||
" Not compatible with neosnippet#enable_completed_snippet. NCM | ||
" choose a different approach | ||
let g:neosnippet#enable_completed_snippet=0 | ||
call s:neosnippet_init() | ||
elseif exists('g:did_plugin_ultisnips') | ||
let g:cm_completed_snippet_enable = 1 | ||
let g:cm_completed_snippet_engine = 'ultisnips' | ||
elseif exists('g:snipMateSources') | ||
let g:cm_completed_snippet_enable = 1 | ||
let g:cm_completed_snippet_engine = 'snipmate' | ||
call s:snipmate_init() | ||
else | ||
let g:cm_completed_snippet_enable = 0 | ||
let g:cm_completed_snippet_engine = '' | ||
endif | ||
endif | ||
if !exists('g:cm_completed_snippet_engine') | ||
let g:cm_completed_snippet_engine = '' | ||
endif | ||
endfunc | ||
|
||
func! cm#snippet#check_and_inject() | ||
|
||
if empty(v:completed_item) || !has_key(v:completed_item,'info') || empty(v:completed_item.info) || has_key(v:completed_item,'snippet') | ||
return '' | ||
endif | ||
|
||
let l:last_line = split(v:completed_item.info,'\n')[-1] | ||
if l:last_line[0:len('snippet@')-1]!='snippet@' | ||
return '' | ||
endif | ||
|
||
let l:snippet_id = str2nr(l:last_line[len('snippet@'):]) | ||
if l:snippet_id>=len(g:cm#snippet#snippets) || l:snippet_id<0 | ||
return '' | ||
endif | ||
|
||
" neosnippet recognize the snippet field of v:completed_item. Also useful | ||
" for checking. Kind of a hack. | ||
let v:completed_item.snippet = g:cm#snippet#snippets[l:snippet_id]['snippet'] | ||
let v:completed_item.snippet_word = g:cm#snippet#snippets[l:snippet_id]['word'] | ||
|
||
if v:completed_item.snippet == '' | ||
return '' | ||
endif | ||
|
||
if g:cm_completed_snippet_engine == 'ultisnips' | ||
|
||
call s:ultisnips_inject() | ||
|
||
" elseif g:cm_completed_snippet_engine == 'snipmate' | ||
" nothing needs to be done for snipmate | ||
|
||
elseif g:cm_completed_snippet_engine == 'neosnippet' | ||
|
||
call s:neosnippet_inject() | ||
|
||
endif | ||
|
||
return '' | ||
endfunc | ||
|
||
func! s:ultisnips_inject() | ||
if get(b:,'_cm_us_setup',0)==0 | ||
" UltiSnips_Manager.add_buffer_filetypes('%s.snips.ncm' % vim.eval('&filetype')) | ||
let b:_cm_us_setup = 1 | ||
let b:_cm_us_filetype = 'ncm' | ||
call UltiSnips#AddFiletypes(b:_cm_us_filetype) | ||
augroup cm | ||
autocmd InsertLeave <buffer> exec g:_uspy 'UltiSnips_Manager._added_snippets_source._snippets["ncm"]._snippets = []' | ||
augroup END | ||
endif | ||
exec g:_uspy 'UltiSnips_Manager._added_snippets_source._snippets["ncm"]._snippets = []' | ||
call UltiSnips#AddSnippetWithPriority(v:completed_item.snippet_word, v:completed_item.snippet, '', 'i', b:_cm_us_filetype, 1) | ||
endfunc | ||
|
||
func! s:neosnippet_init() | ||
augroup cm | ||
autocmd InsertEnter * call s:neosnippet_cleanup() | ||
augroup END | ||
let s:neosnippet_injected = [] | ||
endfunc | ||
|
||
func! s:neosnippet_inject() | ||
let snippets = neosnippet#variables#current_neosnippet() | ||
|
||
let item = {} | ||
let item['options'] = { "word": 1, "oneshot": 0, "indent": 0, "head": 0} | ||
let item['word'] = v:completed_item.snippet_word | ||
let item['snip'] = v:completed_item.snippet | ||
let item['description'] = '' | ||
|
||
let snippets.snippets[v:completed_item.snippet_word] = item | ||
|
||
" remember for cleanup | ||
let s:neosnippet_injected = add(s:neosnippet_injected, v:completed_item.snippet_word) | ||
endfunc | ||
|
||
func! s:neosnippet_cleanup() | ||
let cs = neosnippet#variables#current_neosnippet() | ||
for word in s:neosnippet_injected | ||
unlet cs.snippets[word] | ||
endfor | ||
let s:neosnippet_injected = [] | ||
endfunc | ||
|
||
func! s:snipmate_init() | ||
" inject ncm's handler into snipmate | ||
let g:snipMateSources.ncm = funcref#Function('cm#snippet#_snipmate_snippets') | ||
endfunc | ||
|
||
func! cm#snippet#_snipmate_snippets(scopes, trigger, result) | ||
if empty(v:completed_item) | ||
return | ||
endif | ||
let a:result[v:completed_item.snippet_word] = {'default': [v:completed_item.snippet, 0] } | ||
endfunc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters