made autocomplete trim end
This commit is contained in:
parent
59537fde51
commit
d48e017130
1 changed files with 45 additions and 2 deletions
|
@ -119,7 +119,9 @@ const predictTokens = async (
|
||||||
reject(err);
|
reject(err);
|
||||||
} finally {
|
} finally {
|
||||||
response.abort();
|
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) => {
|
export const getAutoCompleteProvider = (extension: ExtensionState) => {
|
||||||
const provider: vscode.InlineCompletionItemProvider = {
|
const provider: vscode.InlineCompletionItemProvider = {
|
||||||
async provideInlineCompletionItems(document, position, context, token) {
|
async provideInlineCompletionItems(document, position, context, token) {
|
||||||
|
@ -163,7 +206,7 @@ export const getAutoCompleteProvider = (extension: ExtensionState) => {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const completions = await tokenProvider(extension, document, position, context, token);
|
const completions = await getCompletions(extension, document, position, context, token);
|
||||||
|
|
||||||
return completions.map((text) => ({
|
return completions.map((text) => ({
|
||||||
insertText: text,
|
insertText: text,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue