Skip to content

Commit

Permalink
Merge pull request #418 from girishji/main
Browse files Browse the repository at this point in the history
Make omnifunc() complete keywords based on LSP triggerCharacters
  • Loading branch information
yegappan authored Dec 1, 2023
2 parents f74b4da + e0c3e71 commit cb905c8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
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', [])
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

0 comments on commit cb905c8

Please sign in to comment.