Skip to content

Commit

Permalink
Improve manual completion narrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Mar 5, 2024
1 parent 4612328 commit 762ab0f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
16 changes: 1 addition & 15 deletions denops/ddc/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,21 +272,7 @@ export function main(denops: Denops) {
const [skip, context, options] = await contextBuilder
.createContext(denops, event);

const visible = await ddc.visible(denops, context, options);
if (
visible && ddc.prevUi !== "" &&
options.autoCompleteEvents.indexOf(event) > 0 &&
ddc.prevEvent === "Manual"
) {
// NOTE: If UI is visible, use prevSources/prevUi/prevEvent to update
// current items

options.sources = ddc.prevSources;
options.ui = ddc.prevUi;

// Overwrite event if manual completion
context.event = ddc.prevEvent;
}
await ddc.checkManualCompletion(denops, context, options, event);

await ddc.onEvent(
denops,
Expand Down
39 changes: 32 additions & 7 deletions denops/ddc/ddc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ export class Ddc {
#currentUiParams: BaseUiParams = defaultDummy();
#visibleUi = false;
#prevInput = "";

prevSources: UserSource[] = [];
prevUi = "";
prevEvent = "";
#prevSources: UserSource[] = [];
#prevUi = "";
#prevEvent = "";

constructor(loader: Loader) {
this.#loader = loader;
Expand Down Expand Up @@ -238,7 +237,7 @@ export class Ddc {
onCallback: OnCallback,
options: DdcOptions,
): Promise<[number, DdcItem[]]> {
this.prevSources = options.sources;
this.#prevSources = options.sources;

const rs = await Promise.all(options.sources.map(async (userSource) => {
const [s, o, p] = await this.getSource(
Expand Down Expand Up @@ -548,6 +547,30 @@ export class Ddc {
});
}

async checkManualCompletion(
denops: Denops,
context: Context,
options: DdcOptions,
event: string,
) {
// NOTE: Continue manual completion if narrowing words
const check = options.autoCompleteEvents.indexOf(event) > 0 &&
this.#prevEvent === "Manual" &&
context.input.startsWith(this.#prevInput) &&
context.input.replace(/\S+$/, "") ===
this.#prevInput.replace(/\S+$/, "") &&
await this.visible(denops, context, options);

if (check) {
// NOTE: Use prevSources/prevUi/prevEvent to update current items
options.sources = this.#prevSources;
options.ui = this.#prevUi;

// Overwrite event if manual completion
context.event = this.#prevEvent;
}
}

async doCompletion(
denops: Denops,
context: Context,
Expand Down Expand Up @@ -623,8 +646,8 @@ export class Ddc {
uiParams,
});

this.prevUi = options.ui;
this.prevEvent = context.event;
this.#prevUi = options.ui;
this.#prevEvent = context.event;
this.#visibleUi = true;
}

Expand All @@ -650,6 +673,8 @@ export class Ddc {
uiParams,
});
this.#visibleUi = false;
this.#prevEvent = "";
this.#prevInput = "";
}

async visible(
Expand Down

0 comments on commit 762ab0f

Please sign in to comment.