]> Sergey Matveev's repositories - vim-lsp.git/blob - autoload/lsp/snippet.vim
Pass buffer number as the argument to the autocmd handlers
[vim-lsp.git] / autoload / lsp / snippet.vim
1 vim9script
2
3 # Snippet support
4
5 # Integration with the UltiSnips plugin
6 export def CompletionUltiSnips(prefix: string, items: list<dict<any>>)
7   call UltiSnips#SnippetsInCurrentScope(1)
8   for key in matchfuzzy(g:current_ulti_dict_info->keys(), prefix)
9     var item = g:current_ulti_dict_info[key]
10     var parts = split(item.location, ':')
11     var txt = parts[0]->readfile()[parts[1]->str2nr() : parts[1]->str2nr() + 20]
12     var restxt = item.description .. "\n\n"
13     for line in txt
14       if line->empty() || line[0 : 6] == "snippet"
15         break
16       else
17         restxt = restxt .. line .. "\n"
18       endif
19     endfor
20     items->add({
21       label: key,
22       data: {
23         entryNames: [key],
24       },
25       kind: 15,
26       documentation: restxt,
27     })
28   endfor
29 enddef
30
31 # Integration with the vim-vsnip plugin
32 export def CompletionVsnip(items: list<dict<any>>)
33   def Pattern(abbr: string): string
34     var chars = escape(abbr, '\/?')->split('\zs')
35     var chars_pattern = '\%(\V' .. chars->join('\m\|\V') .. '\m\)'
36     var separator = chars[0] =~ '\a' ? '\<' : ''
37     return $'{separator}\V{chars[0]}\m{chars_pattern}*$'
38   enddef
39
40   if charcol('.') == 1
41     return
42   endif
43   var starttext = getline('.')->slice(0, charcol('.') - 1)
44   for item in vsnip#get_complete_items(bufnr())
45     var match = starttext->matchstrpos(Pattern(item.abbr))
46     if match[0] != ''
47       var user_data = item.user_data->json_decode()
48       var documentation = []
49       for line in vsnip#to_string(user_data.vsnip.snippet)->split("\n")
50         documentation->add(line)
51       endfor
52       items->add({
53         label: item.abbr,
54         filterText: item.word,
55         insertTextFormat: 2,
56         textEdit: {
57           newText: user_data.vsnip.snippet->join("\n"),
58           range: {
59             start: {
60               line: line('.'),
61               character: match[1],
62             },
63             ['end']: {
64               line: line('.'),
65               character: match[2],
66             },
67           },
68         },
69         data: {
70           entryNames: [item.word],
71         },
72         kind: 15,
73         documentation: {
74           kind: 'markdown',
75           value: documentation->join("\n"),
76         },
77       })
78     endif
79   endfor
80 enddef
81
82 # vim: tabstop=8 shiftwidth=2 softtabstop=2