Skip to content

Commit

Permalink
fix(completion): use notification for on_complete & on_enter
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Feb 24, 2025
1 parent 6497a28 commit ef591cb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
11 changes: 6 additions & 5 deletions doc/coc-api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Debug extensions |coc-api-debug|

==============================================================================

This is the guide for extend coc.nvim by create vim completion sources and
coc.nvim extensions.
The guide for extend coc.nvim by create vim completion sources and coc.nvim
extensions.

------------------------------------------------------------------------------
VIM SOURCES *coc-api-vim-source*
Expand Down Expand Up @@ -134,8 +134,8 @@ Complete function: ~
items, word is used when not exists.
• filterText: A string that should be used when filtering a set of
complete items, word is used when not exists.
• insertText: The text to insert, could be snippet text, word is used when
not exists.
• insertText: The text to insert, could be textmate snippet text, word is
used when not exists.
• isSnippet: The text to insert is snippet when is truthy value, when
truthy and `on_complete` not provided by vim source, the `insertText` is
expanded as textmate snippet when confirm completion.
Expand Down Expand Up @@ -386,7 +386,8 @@ The error messages are not stored by vim's message history, use

When error happens on the vim side, the promise would be rejected when sending
request to vim, for notifications, vim would send `nvim_error_event` to the
NodeJS process, and the node-client would create error log for it.
NodeJS process, and the node-client would create error log for it (could be
opened by |:CocOpenLog|).

Use the log file: ~

Expand Down
14 changes: 6 additions & 8 deletions src/__tests__/handler/codelens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,12 @@ describe('codeLenes featrue', () => {
await nvim.call('setline', [1, arr])
await doc.synchronize()
await codeLens.checkProvider()
await nvim.command('normal! gg')
await nvim.command('normal! G')
await helper.wait(100)
let buf = codeLens.buffers.getItem(doc.bufnr)
let codelens = buf.currentCodeLens
expect(codelens).toBeDefined()
expect(codelens[0].command).toBeDefined()
expect(codelens[1].command).toBeDefined()
await nvim.command('normal! ggG')
let bufnr = doc.bufnr
await helper.waitValue(() => {
let buf = codeLens.buffers.getItem(bufnr)
return buf && buf.currentCodeLens && buf.currentCodeLens[0].command != null
}, true)
})

it('should use picker for multiple codeLenses', async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/snippets/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,9 @@ describe('SnippetSession', () => {
await session.start('${1:foo} bar$0', defaultRange)
await nvim.input('<backspace>')
await session.forceSynchronize()
let col = await nvim.call('col', ['.'])
expect(col).toBe(5)
await helper.waitValue(async () => {
return await nvim.call('col', ['.'])
}, 5)
})

it('should prefer range contains current cursor', async () => {
Expand Down
10 changes: 5 additions & 5 deletions src/completion/source-vim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export function checkInclude(name: string, fns: ReadonlyArray<string>): boolean

export default class VimSource extends Source {

private async callOptionalFunc(fname: string, args: any[]): Promise<any> {
private async callOptionalFunc(fname: string, args: any[], isNotify = false): Promise<any> {
let exists = checkInclude(fname, this.remoteFns)
if (!exists) return null
let name = `coc#source#${this.name}#${getMethodName(fname, this.remoteFns)}`
if (isNotify) return this.nvim.call(name, args, true)
return await this.nvim.call(name, args)
}

Expand Down Expand Up @@ -55,23 +56,22 @@ export default class VimSource extends Source {

public async onCompleteDone(item: ExtendedCompleteItem, opt: CompleteOption): Promise<void> {
if (checkInclude('on_complete', this.remoteFns)) {
await this.callOptionalFunc('on_complete', [item])
await this.callOptionalFunc('on_complete', [item], true)
} else if (item.isSnippet && item.insertText) {
await this.insertSnippet(item.insertText, opt)
}
}

public onEnter(bufnr: number): void {
if (!checkInclude('on_enter', this.remoteFns)) return
let doc = workspace.getDocument(bufnr)
if (!doc) return
if (!doc || !checkInclude('on_enter', this.remoteFns)) return
let { filetypes } = this
if (filetypes && !filetypes.includes(doc.filetype)) return
void this.callOptionalFunc('on_enter', [{
bufnr,
uri: doc.uri,
languageId: doc.filetype
}])
}], true)
}

public async doComplete(opt: CompleteOption, token: CancellationToken): Promise<CompleteResult<ExtendedCompleteItem> | null> {
Expand Down

0 comments on commit ef591cb

Please sign in to comment.