Skip to content

Commit a5a7481

Browse files
Add cancellation token to react to Copilot API changes (#12773)
* Add cancellation token to react to Copilot API changes * Check for known LSP error code --------- Co-authored-by: Sean McManus <[email protected]>
1 parent 1ec1c9b commit a5a7481

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

Extension/src/LanguageServer/client.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ export interface Client {
790790
getShowConfigureIntelliSenseButton(): boolean;
791791
setShowConfigureIntelliSenseButton(show: boolean): void;
792792
addTrustedCompiler(path: string): Promise<void>;
793-
getIncludes(maxDepth: number): Promise<GetIncludesResult>;
793+
getIncludes(maxDepth: number, token: vscode.CancellationToken): Promise<GetIncludesResult>;
794794
getChatContext(token: vscode.CancellationToken): Promise<ChatContextResult>;
795795
}
796796

@@ -2206,29 +2206,17 @@ export class DefaultClient implements Client {
22062206
await this.languageClient.sendNotification(DidOpenNotification, params);
22072207
}
22082208

2209-
public async getIncludes(maxDepth: number): Promise<GetIncludesResult> {
2209+
public async getIncludes(maxDepth: number, token: vscode.CancellationToken): Promise<GetIncludesResult> {
22102210
const params: GetIncludesParams = { maxDepth: maxDepth };
22112211
await this.ready;
2212-
return this.languageClient.sendRequest(IncludesRequest, params);
2212+
return DefaultClient.withLspCancellationHandling(
2213+
() => this.languageClient.sendRequest(IncludesRequest, params, token), token);
22132214
}
22142215

22152216
public async getChatContext(token: vscode.CancellationToken): Promise<ChatContextResult> {
22162217
await withCancellation(this.ready, token);
2217-
let result: ChatContextResult;
2218-
try {
2219-
result = await this.languageClient.sendRequest(CppContextRequest, null, token);
2220-
} catch (e: any) {
2221-
if (e instanceof ResponseError && (e.code === RequestCancelled || e.code === ServerCancelled)) {
2222-
throw new vscode.CancellationError();
2223-
}
2224-
2225-
throw e;
2226-
}
2227-
if (token.isCancellationRequested) {
2228-
throw new vscode.CancellationError();
2229-
}
2230-
2231-
return result;
2218+
return DefaultClient.withLspCancellationHandling(
2219+
() => this.languageClient.sendRequest(CppContextRequest, null, token), token);
22322220
}
22332221

22342222
/**
@@ -2310,6 +2298,26 @@ export class DefaultClient implements Client {
23102298
this.dispatching.resolve();
23112299
}
23122300

2301+
private static async withLspCancellationHandling<T>(task: () => Promise<T>, token: vscode.CancellationToken): Promise<T> {
2302+
let result: T;
2303+
2304+
try {
2305+
result = await task();
2306+
} catch (e: any) {
2307+
if (e instanceof ResponseError && (e.code === RequestCancelled || e.code === ServerCancelled)) {
2308+
throw new vscode.CancellationError();
2309+
} else {
2310+
throw e;
2311+
}
2312+
}
2313+
2314+
if (token.isCancellationRequested) {
2315+
throw new vscode.CancellationError();
2316+
}
2317+
2318+
return result;
2319+
}
2320+
23132321
private callTaskWithTimeout<T>(task: () => Thenable<T>, ms: number, cancelToken?: vscode.CancellationTokenSource): Promise<T> {
23142322
let timer: NodeJS.Timeout;
23152323

@@ -4110,6 +4118,6 @@ class NullClient implements Client {
41104118
getShowConfigureIntelliSenseButton(): boolean { return false; }
41114119
setShowConfigureIntelliSenseButton(show: boolean): void { }
41124120
addTrustedCompiler(path: string): Promise<void> { return Promise.resolve(); }
4113-
getIncludes(): Promise<GetIncludesResult> { return Promise.resolve({} as GetIncludesResult); }
4121+
getIncludes(maxDepth: number, token: vscode.CancellationToken): Promise<GetIncludesResult> { return Promise.resolve({} as GetIncludesResult); }
41144122
getChatContext(token: vscode.CancellationToken): Promise<ChatContextResult> { return Promise.resolve({} as ChatContextResult); }
41154123
}

Extension/src/LanguageServer/extension.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ import { makeLspRange, rangeEquals, showInstallCompilerWalkthrough } from './uti
3636
interface CopilotApi {
3737
registerRelatedFilesProvider(
3838
providerId: { extensionId: string; languageId: string },
39-
callback: (uri: vscode.Uri) => Promise<{ entries: vscode.Uri[]; traits?: { name: string; value: string }[] }>
40-
): void;
39+
callback: (uri: vscode.Uri, token: vscode.CancellationToken) => Promise<{ entries: vscode.Uri[]; traits?: { name: string; value: string }[] }>
40+
): vscode.Disposable;
4141
}
4242

4343
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
@@ -270,8 +270,8 @@ export async function activate(): Promise<void> {
270270
for (const languageId of ['c', 'cpp', 'cuda-cpp']) {
271271
api.registerRelatedFilesProvider(
272272
{ extensionId: util.extensionContext.extension.id, languageId },
273-
async (_uri: vscode.Uri) =>
274-
({ entries: (await clients.ActiveClient.getIncludes(1))?.includedFiles.map(file => vscode.Uri.file(file)) ?? [] })
273+
async (_uri: vscode.Uri, token: vscode.CancellationToken) =>
274+
({ entries: (await clients.ActiveClient.getIncludes(1, token))?.includedFiles.map(file => vscode.Uri.file(file)) ?? [] })
275275
);
276276
}
277277
} catch {
@@ -1403,7 +1403,9 @@ export async function preReleaseCheck(): Promise<void> {
14031403
}
14041404

14051405
export async function getIncludes(maxDepth: number): Promise<any> {
1406-
const includes = await clients.ActiveClient.getIncludes(maxDepth);
1406+
const tokenSource = new vscode.CancellationTokenSource();
1407+
const includes = await clients.ActiveClient.getIncludes(maxDepth, tokenSource.token);
1408+
tokenSource.dispose();
14071409
return includes;
14081410
}
14091411

0 commit comments

Comments
 (0)