made autocomplete trim end

This commit is contained in:
Leyla Becker 2025-08-04 20:02:37 -05:00
parent 59537fde51
commit d48e017130

View file

@ -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,