From ec841bba3130fb3f0afd02a8b9f2f228f539b3d8 Mon Sep 17 00:00:00 2001 From: Chapman Pendery <35637443+cpendery@users.noreply.github.com> Date: Wed, 6 Mar 2024 00:30:35 -0500 Subject: [PATCH] fix: stale suggestion appearing after command execution (#185) Signed-off-by: Chapman Pendery --- src/ui/suggestionManager.ts | 5 +++++ src/ui/ui-root.ts | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ui/suggestionManager.ts b/src/ui/suggestionManager.ts index 7676fe6..74c5d38 100644 --- a/src/ui/suggestionManager.ts +++ b/src/ui/suggestionManager.ts @@ -87,6 +87,11 @@ export class SuggestionManager { ); } + validate(suggestion: SuggestionsSequence): SuggestionsSequence { + const commandText = this.#term.getCommandState().commandText; + return !commandText ? { data: "", rows: 0 } : suggestion; + } + async render(remainingLines: number): Promise { await this._loadSuggestions(); if (!this.#suggestBlob) { diff --git a/src/ui/ui-root.ts b/src/ui/ui-root.ts index fc1adc6..ae669d8 100644 --- a/src/ui/ui-root.ts +++ b/src/ui/ui-root.ts @@ -57,8 +57,9 @@ export const render = async (shell: Shell, underTest: boolean) => { writeOutput(data); } - setImmediate(async () => { - const suggestion = await suggestionManager.render(term.getCursorState().remainingLines); + process.nextTick(async () => { + // validate result to prevent stale suggestion being provided + const suggestion = suggestionManager.validate(await suggestionManager.render(term.getCursorState().remainingLines)); const commandState = term.getCommandState(); if (suggestion.data != "" && commandState.cursorTerminated && !commandState.hasOutput) {