Skip to content
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
38 changes: 29 additions & 9 deletions denops/ddc/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const main: Entrypoint = (denops: Denops) => {
const cbContext = createCallbackContext();
const lock = new Lock(0);
let queuedEvent: DdcEvent | null = null;
const autoCompleteTimers = new Map<number, number>();

const setAlias = (extType: DdcExtType, alias: string, base: string) => {
loader.registerAlias(extType, alias, base);
Expand Down Expand Up @@ -260,6 +261,11 @@ export const main: Entrypoint = (denops: Denops) => {
async onEvent(arg1: unknown): Promise<void> {
queuedEvent = ensure(arg1, is.String) as DdcEvent;

// Revoke immediately to cancel any pending callback waits from the
// previous event processing, allowing it to fail fast and release the
// lock sooner.
cbContext.revoke();

// NOTE: must be locked
await lock.lock(async () => {
while (queuedEvent !== null) {
Expand Down Expand Up @@ -385,15 +391,29 @@ export const main: Entrypoint = (denops: Denops) => {

// Check auto complete delay.
if (options.autoCompleteDelay > 0) {
// Cancel previous completion
await ddc.cancelCompletion(denops, context, options);

await new Promise((resolve) =>
setTimeout(
resolve,
options.autoCompleteDelay,
)
);
// Cancel previous timer for this buffer (debounce)
clearTimeout(autoCompleteTimers.get(context.bufNr));

// Set a new per-buffer debounce timer. When it fires, cancel the
// previous completion once and then run doCompletion, avoiding
// repeated cancelCompletion/hide calls during rapid input.
const timerId = setTimeout(() => {
autoCompleteTimers.delete(context.bufNr);

lock.lock(async () => {
await ddc.cancelCompletion(denops, context, options);

if (options.hideOnEvents || event === "Update") {
// Hide the current completion
await ddc.hide(denops, context, options);
}

await ddc.doCompletion(denops, context, cbContext, options);
});
}, options.autoCompleteDelay);

autoCompleteTimers.set(context.bufNr, timerId);
return;
}

if (options.hideOnEvents || event === "Update") {
Expand Down
1 change: 1 addition & 0 deletions denops/ddc/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ export class ContextBuilderImpl implements ContextBuilder {
}

const context = {
bufNr: world.bufnr,
cursor: world.cursor,
event: event,
filetype: world.filetype,
Expand Down
1 change: 1 addition & 0 deletions denops/ddc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type SourceName = string;
export type FilterName = string;

export type Context = {
bufNr: number;
cursor: (number | undefined)[];
event: DdcEvent;
filetype: string;
Expand Down
Loading