diff --git a/autoload/lsp/completion.vim b/autoload/lsp/completion.vim index 1fa85e2c..ae0124ea 100644 --- a/autoload/lsp/completion.vim +++ b/autoload/lsp/completion.vim @@ -425,6 +425,29 @@ export def CompletionResolveReply(lspserver: dict, cItem: any) ShowCompletionDocumentation(cItem) enddef +# Return trigger kind and trigger char. If completion trigger is not a keyword +# and not one of the triggerCharacters, return -1 for triggerKind. +def GetTriggerAttributes(lspserver: dict): list + var triggerKind: number = 1 + var triggerChar: string = '' + + # Trigger kind is 1 for keyword and 2 for trigger char initiated completion. + var line: string = getline('.') + var cur_col = charcol('.') + if line[cur_col - 2] !~ '\k' + var trigChars = lspserver.completionTriggerChars + var trigidx = trigChars->index(line[cur_col - 2]) + if trigidx == -1 + triggerKind = -1 + else + triggerKind = 2 + triggerChar = trigChars[trigidx] + endif + endif + return [triggerKind, triggerChar] +enddef + + # omni complete handler def g:LspOmniFunc(findstart: number, base: string): any var lspserver: dict = buf.CurbufGetServerChecked('completion') @@ -433,13 +456,20 @@ def g:LspOmniFunc(findstart: number, base: string): any endif if findstart + + var [triggerKind, triggerChar] = GetTriggerAttributes(lspserver) + if triggerKind < 0 + return -1 + endif + # first send all the changes in the current buffer to the LSP server listener_flush() lspserver.omniCompletePending = true lspserver.completeItems = [] + # initiate a request to LSP server to get list of completions - lspserver.getCompletion(1, '') + lspserver.getCompletion(triggerKind, triggerChar) # locate the start of the word var line = getline('.')->strpart(0, col('.') - 1) @@ -462,7 +492,6 @@ def g:LspOmniFunc(findstart: number, base: string): any endif var res: list> = lspserver.completeItems - var prefix = lspserver.omniCompleteKeyword # Don't attempt to filter on the items, when "isIncomplete" is set @@ -499,30 +528,11 @@ def LspComplete() return endif - var cur_col: number = charcol('.') - var line: string = getline('.') - - if cur_col == 0 || line->empty() + var [triggerKind, triggerChar] = GetTriggerAttributes(lspserver) + if triggerKind < 0 return endif - # 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' - var trigChars = lspserver.completionTriggerChars - var trigidx = trigChars->index(line[cur_col - 2]) - if trigidx == -1 - return - endif - # completion triggered by one of the trigger characters - triggerKind = 2 - triggerChar = trigChars[trigidx] - endif - # first send all the changes in the current buffer to the LSP server listener_flush() diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim index 315ba27b..53cc8952 100644 --- a/autoload/lsp/lspserver.vim +++ b/autoload/lsp/lspserver.vim @@ -114,7 +114,7 @@ def ServerInitReply(lspserver: dict, initResult: dict): void capabilities.ProcessServerCaps(lspserver, caps) if caps->has_key('completionProvider') - if opt.lspOptions.autoComplete + if opt.lspOptions.autoComplete || opt.lspOptions.omniComplete lspserver.completionTriggerChars = caps.completionProvider->get('triggerCharacters', []) endif diff --git a/test/clangd_tests.vim b/test/clangd_tests.vim index d977e91f..1da68c00 100644 --- a/test/clangd_tests.vim +++ b/test/clangd_tests.vim @@ -1519,8 +1519,8 @@ def g:Test_OmniComplete_Struct() feedkeys("cwb\\\\", 'xt') assert_equal(' myTest.baz = 10;', getline('.')) cursor(11, 12) - feedkeys("cw\\\\\", 'xt') - assert_equal(' pTest->foo = 20;', getline('.')) + feedkeys("cw\\\\", 'xt') + assert_equal(' pTest->baz = 20;', getline('.')) :%bw! enddef