From d48e01713021dbb30de0ebbee2cfaf99e4e9b5a6 Mon Sep 17 00:00:00 2001 From: Leyla Becker Date: Mon, 4 Aug 2025 20:02:37 -0500 Subject: [PATCH] made autocomplete trim end --- src/autoComplete.ts | 47 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/autoComplete.ts b/src/autoComplete.ts index a7d3ef8..c9ea361 100644 --- a/src/autoComplete.ts +++ b/src/autoComplete.ts @@ -119,7 +119,9 @@ const predictTokens = async ( reject(err); } finally { response.abort(); - clearTimeout(timeout); + try { + clearTimeout(timeout); + } catch { } }; }); @@ -156,6 +158,47 @@ const tokenProvider = async ( ); }; +const getCompletions = async ( + extension: ExtensionState, + document: vscode.TextDocument, + position: vscode.Position, + _context: vscode.InlineCompletionContext, + token: vscode.CancellationToken, +) => { + const tokens = await tokenProvider( + extension, + document, + position, + _context, + token, + ); + + const maxTokenLength = Math.max(...tokens.map((token) => token.length)); + + const documentEndPosition = new vscode.Position(document.lineCount - 1, document.lineAt(document.lineCount - 1).text.length); + + const tokenOffset = document.positionAt(position.character + maxTokenLength); + + const endPosition = documentEndPosition.isBefore(tokenOffset) ? documentEndPosition : tokenOffset; + + const remainingText = document.getText(new vscode.Range(position, endPosition)); + + return tokens.map((token) => { + for (let index = token.length - 1; index >= 0; index--) { + if (index > remainingText.length) { + continue; + } + + const tail = token.substring(index, token.length); + const tailMatchesDocument = tail === remainingText.substring(0, index); + if (tailMatchesDocument) { + return tail; + } + } + return token; + }); +}; + export const getAutoCompleteProvider = (extension: ExtensionState) => { const provider: vscode.InlineCompletionItemProvider = { async provideInlineCompletionItems(document, position, context, token) { @@ -163,7 +206,7 @@ export const getAutoCompleteProvider = (extension: ExtensionState) => { return []; } try { - const completions = await tokenProvider(extension, document, position, context, token); + const completions = await getCompletions(extension, document, position, context, token); return completions.map((text) => ({ insertText: text,