From 304056d4995b086d989676db23ece7a45406e618 Mon Sep 17 00:00:00 2001 From: Keerthana Panyam Date: Tue, 11 Mar 2025 15:12:56 -0400 Subject: [PATCH] prevent premature autocomplete on intellisense suggestion When editing JS files, typing "window." triggers intellisense and automatically selects the first suggestion ("window.AbortController") The AI model would then use this default selection to insert a completion without user input. This commit updates the logic so that AI autocompletion only triggers once the user has typed a few characters beyond the default suggestion, ensuring that unwanted completions are not inserted early. --- .../src/autocomplete/completionProvider.ts | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/extensions/vscode/src/autocomplete/completionProvider.ts b/extensions/vscode/src/autocomplete/completionProvider.ts index f74de8409..c6e0b22e9 100644 --- a/extensions/vscode/src/autocomplete/completionProvider.ts +++ b/extensions/vscode/src/autocomplete/completionProvider.ts @@ -121,17 +121,23 @@ export class ContinueCompletionProvider return null; } - // If the text at the range isn't a prefix of the intellisense text, - // no completion will be displayed, regardless of what we return - if ( - context.selectedCompletionInfo && - !context.selectedCompletionInfo.text.startsWith( - document.getText(context.selectedCompletionInfo.range), - ) - ) { - return null; - } + // This code checks if there is a selected completion suggestion in the given context and ensures that it is valid. + // To improve the accuracy of suggestions it checks if the user has typed at least 4 characters + // This helps refine and filter out irrelevant autocomplete options + if (context.selectedCompletionInfo) { + const { text, range } = context.selectedCompletionInfo; + const typedText = document.getText(range); + const typedLength = range.end.character - range.start.character; + + if (typedLength < 4) { + return null; + } + + if (!text.startsWith(typedText)) { + return null; + } + } let injectDetails: string | undefined = undefined; // The first time intellisense dropdown shows up, and the first choice is selected,