]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Add support for snippet completion. Tested using the vim-vsnip plugin
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 30 Oct 2022 18:48:04 +0000 (11:48 -0700)
committerYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 30 Oct 2022 18:48:04 +0000 (11:48 -0700)
autoload/lsp/lspserver.vim
autoload/lsp/options.vim
doc/lsp.txt

index de7b22cc67bb2cac4becf76632952730aa74eacb..101c03faeca70c21307867471e927b204fae809e 100644 (file)
@@ -127,7 +127,7 @@ def InitServer(lspserver: dict<any>)
        completionItem: {
          documentationFormat: ['plaintext', 'markdown'],
          resolveSupport: {properties: ['detail', 'documentation']},
-         snippetSupport: false
+         snippetSupport: opt.lspOptions.snippetSupport
        },
        completionItemKind: {valueSet: range(1, 25)}
       },
@@ -545,6 +545,21 @@ def LspCompleteItemKindChar(kind: number): string
   return kindMap[kind]
 enddef
 
+# Remove all the snippet placeholders from 'str' and return the value.
+# Based on a similar function in the vim-lsp plugin.
+def MakeValidWord(str_arg: string): string
+  var str = substitute(str_arg, '\$[0-9]\+\|\${\%(\\.\|[^}]\)\+}', '', 'g')
+  str = substitute(str, '\\\(.\)', '\1', 'g')
+  var valid = matchstr(str, '^[^"'' (<{\[\t\r\n]\+')
+  if empty(valid)
+    return str
+  endif
+  if valid =~# ':$'
+    return valid[: -2]
+  endif
+  return valid
+enddef
+
 # process the 'textDocument/completion' reply from the LSP server
 # Result: CompletionItem[] | CompletionList | null
 def CompletionReply(lspserver: dict<any>, cItems: any)
@@ -569,6 +584,11 @@ def CompletionReply(lspserver: dict<any>, cItems: any)
     else
       d.word = item.label
     endif
+    if item->get('insertTextFormat', 1) == 2
+      # snippet completion.  Needs a snippet plugin to expand the snippet.
+      # Remove all the snippet placeholders
+      d.word = MakeValidWord(d.word)
+    endif
     d.abbr = item.label
     d.dup = 1
     if item->has_key('kind')
index 5308e83c0482d7754f34c2bcf62da0dd1a193599..35dd4af7780401e716e7612df738c38883f1da5d 100644 (file)
@@ -37,7 +37,9 @@ export var lspOptions: dict<any> = {
   # Don't print message when a configured language server is missing.
   ignoreMissingServer: false,
   # Use a floating menu to show the code action menu instead of asking for input
-  usePopupInCodeAction: false
+  usePopupInCodeAction: false,
+  # enable snippet completion support
+  snippetSupport: false
 }
 
 # set LSP options from user provided options
index 71bbdc6b67a87a7ecd54239865b9c5ee88a87aac..007e684fd033bbdf624ed2de5bf3af71aad0b83d 100644 (file)
@@ -2,7 +2,7 @@
 
 Author: Yegappan Lakshmanan  (yegappan AT yahoo DOT com)
 For Vim version 9.0 and above
-Last change: Oct 26, 2022
+Last change: Oct 30, 2022
 
 ==============================================================================
                                                *lsp-license*
@@ -238,41 +238,44 @@ autoComplete              In insert mode, automatically complete the current
 autoHighlight          In normal mode, automatically highlight all the
                        occurrences of the symbol under the cursor. By default
                        this is set to false.
-showSignature          In insert mode, automatically show the current symbol
-                       signature in a popup. By default this is set to true.
-echoSignature          In insert mode, echo the current symbol signature
-                       instead of showing it in a popup. By default this is
-                       set to false.
 autoHighlightDiags     Automatically place signs on the lines with a
                        diagnostic message from the LSP server. By default
                        this is set to true.
 autoPopulateDiags      Automatically populate the location list with
                        diagnostics from the LSP server. By default this is
                        set to false.
+echoSignature          In insert mode, echo the current symbol signature
+                       instead of showing it in a popup. By default this is
+                       set to false.
+ignoreMissingServer    Do not print a missing language server executable.
+                       By default this is set to false.
 keepFocusInReferences  Focus on the location list window after
                        LspShowReferences.  By default this is set to false.
+noDiagHoverOnLine      Suppress diagnostic hover from appearing when
+                       the mouse is over the line instead of the signature.
+                       By default this is set to true.
 noNewlineInCompletion  Suppress adding a new line on completion selection
                        with <CR>.
                        By default this is set to false.
-outlineWinSize         Outline window size, by default this is 20.
 outlineOnRight         Open the outline window on the right side, by default
                        this is false.
-noDiagHoverOnLine      Suppress diagnostic hover from appearing when
-                       the mouse is over the line instead of the signature.
-                       By default this is set to true.
-showDiagOnStatusLine   Show a diagnostic message on a status line.
-                       By default this is set to false.
+outlineWinSize         Outline window size, by default this is 20.
 showDiagInPopup                When using the |:LspDiagCurrent| command to display
                        the diagnostic message for the current line, use a
                        popup window to display the message instead of
                        echoing in the status area.  By default this is set to
                        true.
+showDiagOnStatusLine   Show a diagnostic message on a status line.
+                       By default this is set to false.
+showSignature          In insert mode, automatically show the current symbol
+                       signature in a popup. By default this is set to true.
+snippetSupport         Enable snippet completion support.  Need a snippet
+                       completion plugin like vim-vsnip. By default this is
+                       set to false.
 usePopupInCodeAction    When using the |:LspCodeAction| command to display the
                        code action for the current line, use a popup menu
                        instead of echoing.
                        By default this is set to false.
-ignoreMissingServer    Do not print a missing language server executable.
-                       By default this is set to false.
 
 For example, to disable the automatic placement of signs for the LSP
 diagnostic messages, you can add the following line to your .vimrc file: