Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make omnifunc() complete keywords based on LSP triggerCharacters #418

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions autoload/lsp/completion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,29 @@ export def CompletionResolveReply(lspserver: dict<any>, 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<any>): list<any>
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<any> = buf.CurbufGetServerChecked('completion')
Expand All @@ -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)
Expand All @@ -462,7 +492,6 @@ def g:LspOmniFunc(findstart: number, base: string): any
endif

var res: list<dict<any>> = lspserver.completeItems

var prefix = lspserver.omniCompleteKeyword

# Don't attempt to filter on the items, when "isIncomplete" is set
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion autoload/lsp/lspserver.vim
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def ServerInitReply(lspserver: dict<any>, initResult: dict<any>): 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', [])
Comment on lines -117 to 119
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps just remove this 'if' which to do such assignment always if it had such cap,
besides 'omni complete' seems not only depended on 'omniComplete' this option only.

endif
Expand Down
4 changes: 2 additions & 2 deletions test/clangd_tests.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1519,8 +1519,8 @@ def g:Test_OmniComplete_Struct()
feedkeys("cwb\<C-X>\<C-O>\<C-N>\<C-Y>", 'xt')
assert_equal(' myTest.baz = 10;', getline('.'))
cursor(11, 12)
feedkeys("cw\<C-X>\<C-O>\<C-N>\<C-N>\<C-Y>", 'xt')
assert_equal(' pTest->foo = 20;', getline('.'))
feedkeys("cw\<C-X>\<C-O>\<C-N>\<C-Y>", 'xt')
assert_equal(' pTest->baz = 20;', getline('.'))
:%bw!
enddef

Expand Down