From 895003b09fb36ad628636b41fc5f36381306fdbe Mon Sep 17 00:00:00 2001 From: Yegappan Lakshmanan Date: Fri, 21 Oct 2022 09:05:52 -0700 Subject: [PATCH] Support using omni completion when auto completion is also enabled. Support LSP server trace message --- autoload/lsp/handlers.vim | 10 ++++++++-- autoload/lsp/lsp.vim | 15 +++++++++------ autoload/lsp/lspserver.vim | 11 ++++++----- doc/lsp.txt | 10 +++++++++- plugin/lsp.vim | 12 +++++++++++- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/autoload/lsp/handlers.vim b/autoload/lsp/handlers.vim index 48cf696..9757e0a 100644 --- a/autoload/lsp/handlers.vim +++ b/autoload/lsp/handlers.vim @@ -144,7 +144,7 @@ def ProcessCompletionReply(lspserver: dict, req: dict, reply: dictadd(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, req: dict, reply: dict, reply: dict) 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, reply: dict) + :echomsg $'Log trace notification: {reply->string()}' +enddef + # per-filetype private map inside to record if ntf once or not var ftypeNtfOnceMap: dict = {} # process unsupported notification messages but only notify once @@ -514,6 +519,7 @@ export def ProcessNotif(lspserver: dict, reply: dict): 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 diff --git a/autoload/lsp/lsp.vim b/autoload/lsp/lsp.vim index 6fd28e2..bd44800 100644 --- a/autoload/lsp/lsp.vim +++ b/autoload/lsp/lsp.vim @@ -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 diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index e4fc267..c420471 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -55,7 +55,7 @@ def StartServer(lspserver: dict): 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): 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, traceVal: string) var notif: dict = 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, triggerKind_arg: number): void +def GetCompletion(lspserver: dict, 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, 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, isSync: bool, initiali nextID: 1, caps: {}, requests: {}, - completePending: false, + omniCompletePending: false, completionTriggerChars: [], signaturePopup: -1, diagsMap: {}, diff --git a/doc/lsp.txt b/doc/lsp.txt index 22152a2..96e606e 100644 --- a/doc/lsp.txt +++ b/doc/lsp.txt @@ -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* diff --git a/plugin/lsp.vim b/plugin/lsp.vim index 0974a12..f0df9da 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -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 + 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('')->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() +command! -nargs=1 -complete=customlist,LspServerTraceComplete -bar LspSetTrace lsp.SetTraceServer() 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) -- 2.48.1