Skip to content

Commit 9bcfabe

Browse files
authored
Add completefunc (#283)
* Add vsnip#completefunc() * Add test for vsnip#completefunc() * Add vsnip#completefunc to document
1 parent a803997 commit 9bcfabe

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ VSCode(LSP)'s snippet feature in vim/nvim.
2323
- [vimcomplete](https://github.com/girishji/vimcomplete)
2424
- [ddc.vim](https://github.com/Shougo/ddc.vim)
2525
- [vim-easycompletion](https://github.com/jayli/vim-easycomplete)
26+
- Support built-in completion
27+
- A function can be registered for finding completions.
28+
`set complete+=Fvsnip#completefunc`
2629
- Vim script interpolation
2730
- You can use Vim script interpolation as `${VIM:...Vim script expression...}`.
2831
- SnipMate-like syntax support

autoload/vsnip.vim

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,25 @@ function! vsnip#get_context() abort
166166
return {}
167167
endfunction
168168

169+
"
170+
" vsnip#completefunc
171+
"
172+
function! vsnip#completefunc(findstart, base) abort
173+
if !a:findstart
174+
if a:base ==# ''
175+
return []
176+
endif
177+
return vsnip#get_complete_items(bufnr('%'))
178+
endif
179+
180+
let line = getline('.')
181+
let start = col('.') - 2
182+
while start >= 0 && line[start] =~# '\k'
183+
let start -= 1
184+
endwhile
185+
return start + 1
186+
endfunction
187+
169188
"
170189
" vsnip#get_complete_items
171190
"

doc/vsnip.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ FUNCTION *vsnip-function*
117117

118118
Register your own custom variable resolver.
119119

120+
vsnip#completefunc()
120121

122+
Register for finding completions.
123+
>
124+
set complete+=Fvsnip#completefunc
125+
<
121126

122127
==============================================================================
123128
MAPPING *vsnip-mapping*

spec/autoload/vsnip.vimspec

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,50 @@ Describe vsnip
145145

146146
End
147147

148+
Describe #completefunc
149+
150+
It should return start position
151+
enew!
152+
set filetype=basic_spec
153+
call setline(1, ' if')
154+
call cursor([1, 3])
155+
call s:expect(vsnip#completefunc(1, '')).to_equal(1)
156+
End
157+
158+
It should return complete items
159+
enew!
160+
set filetype=basic_spec
161+
call s:expect(vsnip#completefunc(0, 'if')[-1]).to_equal({
162+
\ 'word': 'if',
163+
\ 'abbr': 'if',
164+
\ 'kind': 'Snippet',
165+
\ 'menu': '[v] if',
166+
\ 'dup': 1,
167+
\ 'user_data': json_encode({
168+
\ 'vsnip': {
169+
\ 'snippet': [
170+
\ "if ${1:condition}",
171+
\ "\t$0",
172+
\ "endif",
173+
\ ]
174+
\ }
175+
\ })
176+
\ }, {
177+
\ 'word': 'inline-fn',
178+
\ 'abbr': 'inline-fn',
179+
\ 'kind': 'Snippet',
180+
\ 'menu': '[v] inline-fn',
181+
\ 'dup': 1,
182+
\ 'user_data': json_encode({
183+
\ 'vsnip': {
184+
\ 'snippet': [
185+
\ "{ -> $1 }$0"
186+
\ ]
187+
\ }
188+
\ })
189+
\ })
190+
End
191+
End
192+
148193
End
149194

0 commit comments

Comments
 (0)