]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Move snippet related code to a separate file
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 18 Jun 2023 22:53:52 +0000 (15:53 -0700)
committerYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 18 Jun 2023 22:53:52 +0000 (15:53 -0700)
autoload/lsp/completion.vim
autoload/lsp/snippet.vim [new file with mode: 0644]

index 711f198b1b0fcd70e4d5b6877c49884b7308985e..66ea9c5d6d23ec9b74289171a576965915153aeb 100644 (file)
@@ -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<bool> = {}
@@ -111,83 +112,6 @@ def MakeValidWord(str_arg: string): string
   return valid
 enddef
 
-# Integration with the UltiSnips plugin
-def CompletionUltiSnips(prefix: string, items: list<dict<any>>)
-  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<dict<any>>)
-  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<dict<any>>)
     var words = {}
@@ -240,9 +164,9 @@ export def CompletionReply(lspserver: dict<any>, 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 (file)
index 0000000..05c685f
--- /dev/null
@@ -0,0 +1,82 @@
+vim9script
+
+# Snippet support
+
+# Integration with the UltiSnips plugin
+export def CompletionUltiSnips(prefix: string, items: list<dict<any>>)
+  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<dict<any>>)
+  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