From: Yegappan Lakshmanan Date: Sun, 18 Jun 2023 22:53:52 +0000 (-0700) Subject: Move snippet related code to a separate file X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=3850fdaf2a0c524760c276f57cff600bd75d2938;p=vim-lsp.git Move snippet related code to a separate file --- diff --git a/autoload/lsp/completion.vim b/autoload/lsp/completion.vim index 711f198..66ea9c5 100644 --- a/autoload/lsp/completion.vim +++ b/autoload/lsp/completion.vim @@ -6,6 +6,7 @@ import './util.vim' import './buffer.vim' as buf import './options.vim' as opt import './textedit.vim' +import './snippet.vim' # per-filetype omni-completion enabled/disabled table var ftypeOmniCtrlMap: dict = {} @@ -111,83 +112,6 @@ def MakeValidWord(str_arg: string): string return valid enddef -# Integration with the UltiSnips plugin -def CompletionUltiSnips(prefix: string, items: list>) - call UltiSnips#SnippetsInCurrentScope(1) - for key in matchfuzzy(g:current_ulti_dict_info->keys(), prefix) - var item = g:current_ulti_dict_info[key] - var parts = split(item.location, ':') - var txt = parts[0]->readfile()[parts[1]->str2nr() : parts[1]->str2nr() + 20] - var restxt = item.description .. "\n\n" - for line in txt - if line->empty() || line[0 : 6] == "snippet" - break - else - restxt = restxt .. line .. "\n" - endif - endfor - items->add({ - label: key, - data: { - entryNames: [key], - }, - kind: 15, - documentation: restxt, - }) - endfor -enddef - -# Integration with the vim-vsnip plugin -def CompletionVsnip(items: list>) - def Pattern(abbr: string): string - var chars = escape(abbr, '\/?')->split('\zs') - var chars_pattern = '\%(\V' .. chars->join('\m\|\V') .. '\m\)' - var separator = chars[0] =~ '\a' ? '\<' : '' - return $'{separator}\V{chars[0]}\m{chars_pattern}*$' - enddef - - if charcol('.') == 1 - return - endif - var starttext = getline('.')->slice(0, charcol('.') - 1) - for item in vsnip#get_complete_items(bufnr('%')) - var match = starttext->matchstrpos(Pattern(item.abbr)) - if match[0] != '' - var user_data = item.user_data->json_decode() - var documentation = [] - for line in vsnip#to_string(user_data.vsnip.snippet)->split("\n") - documentation->add(line) - endfor - items->add({ - label: item.abbr, - filterText: item.word, - insertTextFormat: 2, - textEdit: { - newText: user_data.vsnip.snippet->join("\n"), - range: { - start: { - line: line('.'), - character: match[1], - }, - ['end']: { - line: line('.'), - character: match[2], - }, - }, - }, - data: { - entryNames: [item.word], - }, - kind: 15, - documentation: { - kind: 'markdown', - value: documentation->join("\n"), - }, - }) - endif - endfor -enddef - # add completion from current buf def CompletionFromBuffer(items: list>) var words = {} @@ -240,9 +164,9 @@ export def CompletionReply(lspserver: dict, cItems: any) var start_col = start_idx + 1 if opt.lspOptions.ultisnipsSupport - CompletionUltiSnips(prefix, items) + snippet.CompletionUltiSnips(prefix, items) elseif opt.lspOptions.vsnipSupport - CompletionVsnip(items) + snippet.CompletionVsnip(items) endif if opt.lspOptions.useBufferCompletion diff --git a/autoload/lsp/snippet.vim b/autoload/lsp/snippet.vim new file mode 100644 index 0000000..05c685f --- /dev/null +++ b/autoload/lsp/snippet.vim @@ -0,0 +1,82 @@ +vim9script + +# Snippet support + +# Integration with the UltiSnips plugin +export def CompletionUltiSnips(prefix: string, items: list>) + call UltiSnips#SnippetsInCurrentScope(1) + for key in matchfuzzy(g:current_ulti_dict_info->keys(), prefix) + var item = g:current_ulti_dict_info[key] + var parts = split(item.location, ':') + var txt = parts[0]->readfile()[parts[1]->str2nr() : parts[1]->str2nr() + 20] + var restxt = item.description .. "\n\n" + for line in txt + if line->empty() || line[0 : 6] == "snippet" + break + else + restxt = restxt .. line .. "\n" + endif + endfor + items->add({ + label: key, + data: { + entryNames: [key], + }, + kind: 15, + documentation: restxt, + }) + endfor +enddef + +# Integration with the vim-vsnip plugin +export def CompletionVsnip(items: list>) + def Pattern(abbr: string): string + var chars = escape(abbr, '\/?')->split('\zs') + var chars_pattern = '\%(\V' .. chars->join('\m\|\V') .. '\m\)' + var separator = chars[0] =~ '\a' ? '\<' : '' + return $'{separator}\V{chars[0]}\m{chars_pattern}*$' + enddef + + if charcol('.') == 1 + return + endif + var starttext = getline('.')->slice(0, charcol('.') - 1) + for item in vsnip#get_complete_items(bufnr('%')) + var match = starttext->matchstrpos(Pattern(item.abbr)) + if match[0] != '' + var user_data = item.user_data->json_decode() + var documentation = [] + for line in vsnip#to_string(user_data.vsnip.snippet)->split("\n") + documentation->add(line) + endfor + items->add({ + label: item.abbr, + filterText: item.word, + insertTextFormat: 2, + textEdit: { + newText: user_data.vsnip.snippet->join("\n"), + range: { + start: { + line: line('.'), + character: match[1], + }, + ['end']: { + line: line('.'), + character: match[2], + }, + }, + }, + data: { + entryNames: [item.word], + }, + kind: 15, + documentation: { + kind: 'markdown', + value: documentation->join("\n"), + }, + }) + endif + endfor +enddef + +# vim: tabstop=8 shiftwidth=2 softtabstop=2