Fix VSCode extension: cancel pending completion request when make a new completion request. (#110)

add-tracing
Zhiming Ma 2023-04-18 00:00:08 +08:00 committed by GitHub
parent dc88f03a1f
commit 5d5421f2b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 3 deletions

View File

@ -10,13 +10,14 @@ import {
TextDocument, TextDocument,
workspace, workspace,
} from "vscode"; } from "vscode";
import { CompletionResponse, EventType, ChoiceEvent, ApiError } from "./generated"; import { CompletionResponse, EventType, ChoiceEvent, ApiError, CancelablePromise, CancelError } from "./generated";
import { TabbyClient } from "./TabbyClient"; import { TabbyClient } from "./TabbyClient";
import { sleep } from "./utils"; import { sleep } from "./utils";
export class TabbyCompletionProvider implements InlineCompletionItemProvider { export class TabbyCompletionProvider implements InlineCompletionItemProvider {
private uuid = Date.now(); private uuid = Date.now();
private latestTimestamp: number = 0; private latestTimestamp: number = 0;
private pendingCompletion: CancelablePromise<CompletionResponse> | null = null;
private tabbyClient = TabbyClient.getInstance(); private tabbyClient = TabbyClient.getInstance();
// User Settings // User Settings
@ -65,17 +66,25 @@ export class TabbyCompletionProvider implements InlineCompletionItemProvider {
} }
); );
const completion = await this.tabbyClient.api.default.completionsV1CompletionsPost({ if (this.pendingCompletion) {
this.pendingCompletion.cancel();
}
this.pendingCompletion = this.tabbyClient.api.default.completionsV1CompletionsPost({
prompt: prompt as string, // Prompt is already nil-checked prompt: prompt as string, // Prompt is already nil-checked
language: document.languageId, // https://code.visualstudio.com/docs/languages/identifiers language: document.languageId, // https://code.visualstudio.com/docs/languages/identifiers
}).then((response: CompletionResponse) => { });
const completion = await this.pendingCompletion.then((response: CompletionResponse) => {
this.tabbyClient.changeStatus("ready"); this.tabbyClient.changeStatus("ready");
return response; return response;
}).catch((_: CancelError) => {
return null;
}).catch((err: ApiError) => { }).catch((err: ApiError) => {
console.error(err); console.error(err);
this.tabbyClient.changeStatus("disconnected"); this.tabbyClient.changeStatus("disconnected");
return null; return null;
}); });
this.pendingCompletion = null;
const hasSuffixParen = this.hasSuffixParen(document, position); const hasSuffixParen = this.hasSuffixParen(document, position);
const replaceRange = hasSuffixParen const replaceRange = hasSuffixParen