diff --git a/README.md b/README.md index 89f2676..bcec69b 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,9 @@ VSCode(LSP)'s snippet feature in vim/nvim. - [vimcomplete](https://github.com/girishji/vimcomplete) - [ddc.vim](https://github.com/Shougo/ddc.vim) - [vim-easycompletion](https://github.com/jayli/vim-easycomplete) +- Support built-in completion + - A function can be registered for finding completions. + `set complete+=Fvsnip#completefunc` - Vim script interpolation - You can use Vim script interpolation as `${VIM:...Vim script expression...}`. - SnipMate-like syntax support diff --git a/autoload/vsnip.vim b/autoload/vsnip.vim index 8069eee..6ccf4b0 100644 --- a/autoload/vsnip.vim +++ b/autoload/vsnip.vim @@ -166,6 +166,25 @@ function! vsnip#get_context() abort return {} endfunction +" +" vsnip#completefunc +" +function! vsnip#completefunc(findstart, base) abort + if !a:findstart + if a:base ==# '' + return [] + endif + return vsnip#get_complete_items(bufnr('%')) + endif + + let line = getline('.') + let start = col('.') - 2 + while start >= 0 && line[start] =~# '\k' + let start -= 1 + endwhile + return start + 1 +endfunction + " " vsnip#get_complete_items " diff --git a/doc/vsnip.txt b/doc/vsnip.txt index c26c09f..040a405 100644 --- a/doc/vsnip.txt +++ b/doc/vsnip.txt @@ -117,7 +117,12 @@ FUNCTION *vsnip-function* Register your own custom variable resolver. + vsnip#completefunc() + Register for finding completions. +> + set complete+=Fvsnip#completefunc +< ============================================================================== MAPPING *vsnip-mapping* diff --git a/spec/autoload/vsnip.vimspec b/spec/autoload/vsnip.vimspec index 3d8bc2e..f2da6a4 100644 --- a/spec/autoload/vsnip.vimspec +++ b/spec/autoload/vsnip.vimspec @@ -145,5 +145,50 @@ Describe vsnip End + Describe #completefunc + + It should return start position + enew! + set filetype=basic_spec + call setline(1, ' if') + call cursor([1, 3]) + call s:expect(vsnip#completefunc(1, '')).to_equal(1) + End + + It should return complete items + enew! + set filetype=basic_spec + call s:expect(vsnip#completefunc(0, 'if')[-1]).to_equal({ + \ 'word': 'if', + \ 'abbr': 'if', + \ 'kind': 'Snippet', + \ 'menu': '[v] if', + \ 'dup': 1, + \ 'user_data': json_encode({ + \ 'vsnip': { + \ 'snippet': [ + \ "if ${1:condition}", + \ "\t$0", + \ "endif", + \ ] + \ } + \ }) + \ }, { + \ 'word': 'inline-fn', + \ 'abbr': 'inline-fn', + \ 'kind': 'Snippet', + \ 'menu': '[v] inline-fn', + \ 'dup': 1, + \ 'user_data': json_encode({ + \ 'vsnip': { + \ 'snippet': [ + \ "{ -> $1 }$0" + \ ] + \ } + \ }) + \ }) + End + End + End