Skip to content

Commit

Permalink
fix: block Vim until completion resolve finishes (#4847)
Browse files Browse the repository at this point in the history
  • Loading branch information
statiolake authored Jan 3, 2024
1 parent 0006118 commit e42d5dc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions autoload/coc/pum.vim
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function! coc#pum#close(...) abort
endif
call s:close_pum()
if !get(a:, 2, 0)
call coc#rpc#notify('CompleteStop', [kind])
call coc#rpc#request('CompleteStop', [kind])
endif
endif
return ''
Expand Down Expand Up @@ -115,7 +115,7 @@ function! coc#pum#_insert() abort
endif
doautocmd <nomodeline> TextChangedI
call s:close_pum()
call coc#rpc#notify('CompleteStop', [''])
call coc#rpc#request('CompleteStop', [''])
endif
return ''
endfunction
Expand Down
6 changes: 5 additions & 1 deletion src/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ export default (opts: Attach, requestApi = false): Plugin => {
timing.start(method)
try {
events.requesting = true
if (method == 'CocAutocmd') {
if (method == 'CompleteStop') {
logger.trace('Event: ', method, ...args)
await events.fire(method, args)
resp.send(undefined)
} else if (method == 'CocAutocmd') {
logger.trace('Request autocmd:', ...args)
await events.fire(args[0], args.slice(1))
resp.send(undefined)
Expand Down
14 changes: 10 additions & 4 deletions src/completion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export class Completion implements Disposable {
events.on('CursorMovedI', () => {
clearTimeout(this.triggerTimer)
}, null, this.disposables)
events.on('CompleteStop', kind => {
this.stop(false, kind)
events.on('CompleteStop', async kind => {
await this.stopAwaitable(false, kind)
}, null, this.disposables)
events.on('InsertEnter', this.onInsertEnter, this, this.disposables)
events.on('TextChangedI', this.onTextChangedI, this, this.disposables)
Expand Down Expand Up @@ -360,6 +360,10 @@ export class Completion implements Disposable {
}

public stop(close: boolean, kind: CompleteFinishKind = CompleteFinishKind.Normal): void {
void this.stopAwaitable(close, kind)
}

public async stopAwaitable(close: boolean, kind: CompleteFinishKind = CompleteFinishKind.Normal): Promise<void> {
let { complete } = this
if (complete == null) return
let inserted = kind === CompleteFinishKind.Confirm || (this.popupEvent?.inserted && kind != CompleteFinishKind.Cancel)
Expand All @@ -374,14 +378,16 @@ export class Completion implements Disposable {
events.completing = false
this.cancel()
doc._forceSync()
void events.fire('CompleteDone', [toCompleteDoneItem(item, resolved?.item)])
const completeDone = events.fire('CompleteDone', [toCompleteDoneItem(item, resolved?.item)])
if (close) this.nvim.call('coc#pum#_close', [], true)
if (resolved && inserted) {
this._mru.add(line.slice(character, inputStart) + input, item)
}
let confirmDone = Promise.resolve()
if (kind == CompleteFinishKind.Confirm && resolved) {
void this.confirmCompletion(resolved.source, resolved.item, option)
confirmDone = this.confirmCompletion(resolved.source, resolved.item, option)
}
await Promise.all([completeDone, confirmDone])
}

private async confirmCompletion(source: ISource, item: CompleteItem, option: CompleteOption): Promise<void> {
Expand Down

0 comments on commit e42d5dc

Please sign in to comment.