]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Support using omni completion when auto completion is also enabled. Support LSP...
authorYegappan Lakshmanan <yegappan@yahoo.com>
Fri, 21 Oct 2022 16:05:52 +0000 (09:05 -0700)
committerYegappan Lakshmanan <yegappan@yahoo.com>
Fri, 21 Oct 2022 16:05:52 +0000 (09:05 -0700)
autoload/lsp/handlers.vim
autoload/lsp/lsp.vim
autoload/lsp/lspserver.vim
doc/lsp.txt
plugin/lsp.vim

index 48cf696435bab09449cb1d5e7aa79d7acb8c5be1..9757e0a5c708bc3ce0ae5eaf51c47f822a803ce7 100644 (file)
@@ -144,7 +144,7 @@ def ProcessCompletionReply(lspserver: dict<any>, req: dict<any>, reply: dict<any
     completeItems->add(d)
   endfor
 
-  if opt.lspOptions.autoComplete
+  if opt.lspOptions.autoComplete && !lspserver.omniCompletePending
     if completeItems->empty()
       # no matches
       return
@@ -185,7 +185,7 @@ def ProcessCompletionReply(lspserver: dict<any>, req: dict<any>, reply: dict<any
     complete(start_col, completeItems)
   else
     lspserver.completeItems = completeItems
-    lspserver.completePending = false
+    lspserver.omniCompletePending = false
   endif
 enddef
 
@@ -492,6 +492,11 @@ def ProcessUnsupportedNotif(lspserver: dict<any>, reply: dict<any>)
   util.ErrMsg($'Error: Unsupported notification message received from the LSP server ({lspserver.path}), message = {reply->string()}')
 enddef
 
+# process log trace notification messages
+def ProcessLogTraceNotif(lspserver: dict<any>, reply: dict<any>)
+  :echomsg $'Log trace notification: {reply->string()}'
+enddef
+
 # per-filetype private map inside to record if ntf once or not
 var ftypeNtfOnceMap: dict<bool> = {}
 # process unsupported notification messages but only notify once
@@ -514,6 +519,7 @@ export def ProcessNotif(lspserver: dict<any>, reply: dict<any>): void
       'window/logMessage': ProcessLogMsgNotif,
       'textDocument/publishDiagnostics': ProcessDiagNotif,
       '$/progress': ProcessUnsupportedNotif,
+      '$/logTrace': ProcessLogTraceNotif,
       'telemetry/event': ProcessUnsupportedNotifOnce,
       # Java language server sends the 'language/status' notification which is
       # not in the LSP specification
index 6fd28e260ee4218948aad965e230e63b892830f3..bd4480065b84a75875713979424bba8134f666eb 100644 (file)
@@ -454,7 +454,7 @@ enddef
 # set the LSP server trace level for the current buffer
 # Params: SetTraceParams
 export def SetTraceServer(traceVal: string)
-  if ['off', 'message', 'verbose']->index(traceVal) == -1
+  if ['off', 'messages', 'verbose']->index(traceVal) == -1
     util.ErrMsg($'Error: Unsupported LSP server trace value {traceVal}')
     return
   endif
@@ -546,22 +546,25 @@ def g:LspComplete()
 
   # Trigger kind is 1 for 24x7 code complete or manual invocation
   var triggerKind: number = 1
+  var triggerChar: string = ''
 
   # If the character before the cursor is not a keyword character or is not
   # one of the LSP completion trigger characters, then do nothing.
   if line[cur_col - 2] !~ '\k'
-    if lspserver.completionTriggerChars->index(line[cur_col - 2]) == -1
+    var trigidx = lspserver.completionTriggerChars->index(line[cur_col - 2])
+    if trigidx == -1
       return
     endif
     # completion triggered by one of the trigger characters
     triggerKind = 2
+    triggerChar = lspserver.completionTriggerChars[trigidx]
   endif
 
   # first send all the changes in the current buffer to the LSP server
   listener_flush()
 
   # initiate a request to LSP server to get list of completions
-  lspserver.getCompletion(triggerKind)
+  lspserver.getCompletion(triggerKind, triggerChar)
 
   return
 enddef
@@ -577,10 +580,10 @@ def g:LspOmniFunc(findstart: number, base: string): any
     # first send all the changes in the current buffer to the LSP server
     listener_flush()
 
-    lspserver.completePending = v:true
+    lspserver.omniCompletePending = v:true
     lspserver.completeItems = []
     # initiate a request to LSP server to get list of completions
-    lspserver.getCompletion(1)
+    lspserver.getCompletion(1, '')
 
     # locate the start of the word
     var line = getline('.')
@@ -592,7 +595,7 @@ def g:LspOmniFunc(findstart: number, base: string): any
   else
     # Wait for the list of matches from the LSP server
     var count: number = 0
-    while lspserver.completePending && count < 1000
+    while lspserver.omniCompletePending && count < 1000
       if complete_check()
        return v:none
       endif
index e4fc26701bb096ae2f0f750099629d229aea1017..c420471935e46ba54f57cd3643bd3739cdd3793f 100644 (file)
@@ -55,7 +55,7 @@ def StartServer(lspserver: dict<any>): number
   lspserver.caps = {}
   lspserver.nextID = 1
   lspserver.requests = {}
-  lspserver.completePending = false
+  lspserver.omniCompletePending = false
   lspserver.completionTriggerChars = []
   lspserver.signaturePopup = -1
   lspserver.workspaceFolders = [getcwd()]
@@ -187,7 +187,8 @@ def StopServer(lspserver: dict<any>): number
   return 0
 enddef
 
-# set the LSP server trace level using $/setTrace notification
+# Set the LSP server trace level using the $/setTrace notification
+# Support values for "traceVal" are "off", "messages" and "verbose".
 def SetTrace(lspserver: dict<any>, traceVal: string)
   var notif: dict<any> = lspserver.createNotification('$/setTrace')
   notif.params->extend({value: traceVal})
@@ -417,7 +418,7 @@ enddef
 # Get a list of completion items.
 # Request: "textDocument/completion"
 # Param: CompletionParams
-def GetCompletion(lspserver: dict<any>, triggerKind_arg: number): void
+def GetCompletion(lspserver: dict<any>, triggerKind_arg: number, triggerChar: string): void
   # Check whether LSP server supports completion
   if !lspserver.caps->has_key('completionProvider')
     util.ErrMsg("Error: LSP server does not support completion")
@@ -435,7 +436,7 @@ def GetCompletion(lspserver: dict<any>, triggerKind_arg: number): void
   #   interface TextDocumentPositionParams
   req.params = GetLspTextDocPosition()
   #   interface CompletionContext
-  req.params.context = {triggerKind: triggerKind_arg}
+  req.params.context = {triggerKind: triggerKind_arg, triggerCharacter: triggerChar}
 
   lspserver.sendMessage(req)
   if exists('g:LSPTest') && g:LSPTest
@@ -1100,7 +1101,7 @@ export def NewLspServer(path: string, args: list<string>, isSync: bool, initiali
     nextID: 1,
     caps: {},
     requests: {},
-    completePending: false,
+    omniCompletePending: false,
     completionTriggerChars: [],
     signaturePopup: -1,
     diagsMap: {},
index 22152a245861ec90fc845b07f958631491ab1f3f..96e606eace1fd7a3ba9553e8ea6fc8f2c37f72d3 100644 (file)
@@ -2,7 +2,7 @@
 
 Author: Yegappan Lakshmanan  (yegappan AT yahoo DOT com)
 For Vim version 9.0 and above
-Last change: Oct 7, 2022
+Last change: Oct 21, 2022
 
 ==============================================================================
                                                *lsp-license*
@@ -602,6 +602,14 @@ default. The following example disables omni-completion for python: >
                \     }
                \   ]
 <
+If you want to use omni completion, in addition to the automatic completion,
+then you can set the 'omnifunc' option to the "LspOmniFunc" function: >
+
+       set omnifunc=LspOmniFunc
+<
+To use omni completion, press CTRL-X CTRL-O in insert mode.  Refer to
+|compl-omni| for more information.
+
 ==============================================================================
 7. Autocommands                                                *lsp-autocmds*
 
index 0974a12ffd02ff9b718dfd6e44546bf76588663c..f0df9da9fb910cc7dc033e6415caea656a693d6d 100644 (file)
@@ -26,6 +26,16 @@ def g:LspServerReady(): bool
   return lsp.ServerReady()
 enddef
 
+# Command line completion function for the LspSetTrace command.
+def LspServerTraceComplete(arglead: string, cmdline: string, cursorpos: number): list<string>
+  var l = ['off', 'messages', 'verbose']
+  if arglead->empty()
+    return l
+  else
+    return filter(l, (_, val) => val =~ arglead)
+  endif
+enddef
+
 augroup LSPAutoCmds
   au!
   autocmd BufNewFile,BufReadPost * lsp.AddFile(expand('<abuf>')->str2nr())
@@ -45,7 +55,7 @@ augroup END
 command! -nargs=0 -bar LspShowServers lsp.ShowServers()
 command! -nargs=0 -bar LspShowServerCapabilities lsp.ShowServerCapabilities()
 command! -nargs=0 -bar LspServerRestart lsp.RestartServer()
-command! -nargs=1 -bar LspSetTrace lsp.SetTraceServer(<q-args>)
+command! -nargs=1 -complete=customlist,LspServerTraceComplete -bar LspSetTrace lsp.SetTraceServer(<q-args>)
 command! -nargs=0 -bar LspGotoDefinition lsp.GotoDefinition(v:false)
 command! -nargs=0 -bar LspGotoDeclaration lsp.GotoDeclaration(v:false)
 command! -nargs=0 -bar LspGotoTypeDef lsp.GotoTypedef(v:false)