Skip to content

Commit 20f5b75

Browse files
thomasfaingnaertprabirshrestha
authored andcommitted
UltiSnips integration (#306)
* Implement basic UltiSnips integration * Fix already inserted text not being removed * Fix incorrect whitespace when nesting snippets * Fix tabstops not working in nested snippets * Make functions abort on error * Check if Vim has the user_data patch * Add basic documentation * Add explanation UltiSnips integration to README * Use single quoted string in plugin/lsp.vim * Allow plugins to override get_completion_item * Remove UltiSnips specific code to set user_data * Remove CompleteDone autocmd from omni.vim * Revert "Add explanation UltiSnips integration to README" This reverts commit ee12343. * Revert "Add basic documentation" This reverts commit 979275b. * Allow changing supported capabilities * Remove g:lsp_ultisnips_integration * Mention snippets in READE and documentation * Add documentation for new configuration options * Pass server_info to get_capabilities * Change configuration variables to be a List This way, we don't need to start them with a capital letter. * Add lsp_complete_done autocmd * Add documentation of lsp_complete_done
1 parent 7551671 commit 20f5b75

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ vim-lsp supports incremental changes of Language Server Protocol.
4545

4646
Refer to docs on configuring omnifunc or [asyncomplete.vim](https://github.com/prabirshrestha/asyncomplete.vim).
4747

48+
## Snippets
49+
vim-lsp does not support snippets by default. If you want snippet integration, you will first have to install a third-party snippet plugin and a plugin that integrates it in vim-lsp.
50+
At the moment, you have two options:
51+
1. [UltiSnips](https://github.com/SirVer/ultisnips) together with [vim-lsp-ultisnips](https://github.com/thomasfaingnaert/vim-lsp-ultisnips)
52+
2. [neosnippet.vim](https://github.com/Shougo/neosnippet.vim) together with [vim-lsp-neosnippet](https://github.com/thomasfaingnaert/vim-lsp-neosnippet)
53+
54+
For more information, refer to the readme and documentation of the respective plugins.
55+
4856
## Supported commands
4957

5058
**Note:**

autoload/lsp.vim

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,14 @@ function! s:ensure_start(buf, server_name, cb) abort
369369
endif
370370
endfunction
371371

372+
function! lsp#default_get_supported_capabilities(server_info) abort
373+
return {
374+
\ 'workspace': {
375+
\ 'applyEdit': v:true
376+
\ }
377+
\ }
378+
endfunction
379+
372380
function! s:ensure_init(buf, server_name, cb) abort
373381
let l:server = s:servers[a:server_name]
374382

@@ -406,11 +414,7 @@ function! s:ensure_init(buf, server_name, cb) abort
406414
if has_key(l:server_info, 'capabilities')
407415
let l:capabilities = l:server_info['capabilities']
408416
else
409-
let l:capabilities = {
410-
\ 'workspace': {
411-
\ 'applyEdit': v:true
412-
\ }
413-
\ }
417+
let l:capabilities = call(g:lsp_get_supported_capabilities[0], [server_info])
414418
endif
415419

416420
let l:request = {

autoload/lsp/omni.vim

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function! s:remove_typed_part(word) abort
167167
return strpart(a:word, l:overlap_length)
168168
endfunction
169169

170-
function! lsp#omni#get_vim_completion_item(item, ...) abort
170+
function! lsp#omni#default_get_vim_completion_item(item, ...) abort
171171
let l:do_remove_typed_part = get(a:, 1, 0)
172172

173173
if g:lsp_insert_text_enabled && has_key(a:item, 'insertText') && !empty(a:item['insertText'])
@@ -242,6 +242,10 @@ function! lsp#omni#get_vim_completion_item(item, ...) abort
242242
return l:completion
243243
endfunction
244244

245+
function! lsp#omni#get_vim_completion_item(...) abort
246+
return call(g:lsp_get_vim_completion_item[0], a:000)
247+
endfunction
248+
245249
augroup lsp_completion_item_text_edit
246250
autocmd!
247251
autocmd CompleteDone * call <SID>apply_text_edits()
@@ -265,16 +269,19 @@ function! s:apply_text_edits() abort
265269
" ],
266270
" }
267271
if !g:lsp_text_edit_enabled
272+
doautocmd User lsp_complete_done
268273
return
269274
endif
270275

271276
" completion faild or not select complete item
272277
if empty(v:completed_item)
278+
doautocmd User lsp_complete_done
273279
return
274280
endif
275281

276282
" check user_data
277283
if !has_key(v:completed_item, 'user_data')
284+
doautocmd User lsp_complete_done
278285
return
279286
endif
280287

@@ -283,10 +290,12 @@ function! s:apply_text_edits() abort
283290
let l:user_data = json_decode(v:completed_item['user_data'])
284291
catch
285292
" do nothing if user_data is not json type string.
293+
doautocmd User lsp_complete_done
286294
return
287295
endtry
288296

289297
if type(l:user_data) != type({})
298+
doautocmd User lsp_complete_done
290299
return
291300
endif
292301

@@ -319,6 +328,8 @@ function! s:apply_text_edits() abort
319328
let l:pos[2] += l:col_offset
320329
call setpos("'a", l:saved_mark)
321330
call setpos('.', l:pos)
331+
332+
doautocmd User lsp_complete_done
322333
endfunction
323334

324335
function! s:expand_range(text_edit, expand_length) abort

doc/vim-lsp.txt

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ CONTENTS *vim-lsp-contents*
2222
g:lsp_textprop_enabled |g:lsp_textprop_enabled|
2323
g:lsp_use_event_queue |g:lsp_use_event_queue|
2424
g:lsp_highlight_references_enabled |g:lsp_highlight_references_enabled|
25+
g:lsp_get_vim_completion_item |g:lsp_get_vim_completion_item|
26+
g:lsp_get_supported_capabilities |g:lsp_get_supported_capabilities|
2527
Functions |vim-lsp-functions|
2628
enable |vim-lsp-enable|
2729
disable |vim-lsp-disable|
@@ -48,10 +50,13 @@ CONTENTS *vim-lsp-contents*
4850
LspRename |LspRename|
4951
LspTypeDefinition |LspTypeDefinition|
5052
LspWorkspaceSymbol |LspWorkspaceSymbol|
53+
Autocommands |vim-lsp-autocommands|
54+
lsp_complete_done |lsp_complete_done|
5155
Mappings |vim-lsp-mappings|
5256
Autocomplete |vim-lsp-autocomplete|
5357
omnifunc |vim-lsp-omnifunc|
5458
asyncomplete.vim |vim-lsp-asyncomplete|
59+
Snippets |vim-lsp-snippets|
5560
License |vim-lsp-license|
5661

5762

@@ -281,6 +286,32 @@ g:lsp_highlight_references_enabled *g:lsp_highlight_references_enabled*
281286
highlight lspReference ctermfg=red guifg=red ctermbg=green guibg=green
282287
<
283288

289+
g:lsp_get_vim_completion_item *g:lsp_get_vim_completion_item*
290+
Type: |List|
291+
Default: `[function('lsp#omni#default_get_vim_completion_item')]`
292+
293+
A |List| containing one element of type |Funcref|. This element is a
294+
reference to the function that vim-lsp should use to produce the items in
295+
the completion menu. Changing this variable allows customizing how items
296+
are displayed in the completion menu, or adding custom `user_data` to
297+
items (see |complete-items|).
298+
299+
Note: You can reuse functionality provided by vim-lsp by calling
300+
`lsp#omni#default_get_vim_completion_item` from within your function.
301+
302+
g:lsp_get_supported_capabilities *g:lsp_get_supported_capabilities*
303+
Type: |List|
304+
Default: `[function('lsp#default_get_supported_capabilities')]`
305+
306+
A |List| containing one element of type |Funcref|. This element is a
307+
reference to the function that vim-lsp should use to obtain the supported
308+
LSP capabilities. Changing this variable allows customizing which
309+
capabilities vim-lsp sends to a language server.
310+
311+
Note: You can obtain the default supported capabilities of vim-lsp by
312+
calling `lsp#omni#default_get_supported_capabilities` from within your
313+
function.
314+
284315
===============================================================================
285316
FUNCTIONS *vim-lsp-functions*
286317

@@ -455,12 +486,12 @@ Get the status of a server.
455486
"running", "not running".
456487

457488
*vim-lsp-utils-find_nearest_parent_file_directory*
458-
utils#find_nearest_parent_file_directory
489+
utils#find_nearest_parent_file_directory
459490

460-
Find the nearest parent directory which contains the specific files or
461-
diretories. The method has two parameters. The first is the path where
462-
searching starts. The second is the files or directories names which
463-
you want to find. The return value is the directory path which is found
491+
Find the nearest parent directory which contains the specific files or
492+
diretories. The method has two parameters. The first is the path where
493+
searching starts. The second is the files or directories names which
494+
you want to find. The return value is the directory path which is found
464495
the most times.
465496
This method is mainly used to generate 'root_uri' when registering server.
466497

@@ -471,7 +502,7 @@ This method is mainly used to generate 'root_uri' when registering server.
471502
\ 'cmd': {server_info->['ccls']},
472503
\ 'root_uri':{server_info->lsp#utils#path_to_uri(
473504
\ lsp#utils#find_nearest_parent_file_directory(
474-
\ lsp#utils#get_buffer_path(),
505+
\ lsp#utils#get_buffer_path(),
475506
\ ['.ccls', 'compile_commands.json', '.git/']
476507
\ ))},
477508
\ 'initialization_options': {},
@@ -577,6 +608,21 @@ LspStatus *LspStatus*
577608

578609
Prints the status of all registered servers.
579610

611+
===============================================================================
612+
Autocommands *vim-lsp-autocommands*
613+
614+
lsp_complete_done *lsp_complete_done*
615+
616+
This autocommand is run after Insert mode completion is done, similar to
617+
|CompleteDone|. However, the difference is that |lsp_complete_done| is run
618+
only after vim-lsp has finished executing its internal |CompleteDone|
619+
autocommands (e.g. applying text edits). It is thus ideal to use for snippet
620+
expansion, or custom post processing of completed items. Just like
621+
|CompleteDone|, the Vim variable |v:completed_item| contains information about
622+
the completed item. It is guaranteed that vim-lsp does not change the content
623+
of this variable during its |CompleteDone| autocommands.
624+
625+
580626
===============================================================================
581627
Mappings *vim-lsp-mappings*
582628

@@ -636,6 +682,24 @@ Example:
636682

637683
For additional configuration refer to asyncomplete.vim docs.
638684

685+
===============================================================================
686+
Snippets *vim-lsp-snippets*
687+
688+
To integrate snippets in vim-lsp, you will first have to install a third-party
689+
snippet plugin, and a plugin that integrates it in vim-lsp. At the moment,
690+
you have two options:
691+
692+
1. UltiSnips and vim-lsp-ultisnips
693+
https://github.com/SirVer/ultisnips
694+
https://github.com/thomasfaingnaert/vim-lsp-ultisnips
695+
696+
2. neosnippet.vim and vim-lsp-neosnippet
697+
https://github.com/Shougo/neosnippet.vim
698+
https://github.com/thomasfaingnaert/vim-lsp-neosnippet
699+
700+
Refer to the readme and docs of vim-lsp-ultisnips and vim-lsp-neosnippet for
701+
more information and configuration options.
702+
639703
===============================================================================
640704
License *vim-lsp-license*
641705

plugin/lsp.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ let g:lsp_insert_text_enabled= get(g:, 'lsp_insert_text_enabled', 1)
2626
let g:lsp_text_edit_enabled = get(g:, 'lsp_text_edit_enabled', has('patch-8.0.1493'))
2727
let g:lsp_highlight_references_enabled = get(g:, 'lsp_highlight_references_enabled', 1)
2828

29+
let g:lsp_get_vim_completion_item = get(g:, 'lsp_get_vim_completion_item', [function('lsp#omni#default_get_vim_completion_item')])
30+
let g:lsp_get_supported_capabilities = get(g:, 'lsp_get_supported_capabilities', [function('lsp#default_get_supported_capabilities')])
31+
2932
if g:lsp_auto_enable
3033
augroup lsp_auto_enable
3134
autocmd!

0 commit comments

Comments
 (0)