Skip to content

Commit 1b37fd1

Browse files
committed
track and pass cancellation token + fix copilot hover showing up before IntelliSense hover in some cases
1 parent ba5f149 commit 1b37fd1

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

Extension/src/LanguageServer/Providers/CopilotHoverProvider.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class CopilotHoverProvider implements vscode.HoverProvider {
1616
private client: DefaultClient;
1717
private currentDocument: vscode.TextDocument | undefined;
1818
private currentPosition: vscode.Position | undefined;
19+
private currentCancellationToken: vscode.CancellationToken | undefined;
1920
private waiting: boolean = false;
2021
private ready: boolean = false;
2122
private cancelled: boolean = false;
@@ -33,6 +34,13 @@ export class CopilotHoverProvider implements vscode.HoverProvider {
3334
return undefined;
3435
}
3536

37+
// Wait for the main hover provider to finish and confirm it has content.
38+
const hoverProvider = this.client.getHoverProvider();
39+
if (!await hoverProvider?.contentReady) {
40+
this.reset();
41+
return undefined;
42+
}
43+
3644
if (!this.isNewHover(document, position)) {
3745
if (this.ready) {
3846
const contentMarkdown = new vscode.MarkdownString(`$(sparkle) Copilot\n\n${this.content}`, true);
@@ -46,16 +54,12 @@ export class CopilotHoverProvider implements vscode.HoverProvider {
4654

4755
// Fresh hover, reset state.
4856
this.reset();
49-
// Wait for the main hover provider to finish and confirm it has content.
50-
const hoverProvider = this.client.getHoverProvider();
51-
if (!await hoverProvider?.contentReady) {
52-
return undefined;
53-
}
5457
if (token.isCancellationRequested) {
5558
throw new vscode.CancellationError();
5659
}
5760
this.currentDocument = document;
5861
this.currentPosition = position;
62+
this.currentCancellationToken = token;
5963
const commandString = "$(sparkle) [" + localize("generate.copilot.description", "Generate Copilot summary") + "](command:C_Cpp.ShowCopilotHover \"" + localize("copilot.disclaimer", "AI-generated content may be incorrect.") + "\")";
6064
const commandMarkdown = new vscode.MarkdownString(commandString);
6165
commandMarkdown.supportThemeIcons = true;
@@ -86,9 +90,14 @@ export class CopilotHoverProvider implements vscode.HoverProvider {
8690
uri: document.uri.toString(),
8791
position: Position.create(position.line, position.character)
8892
};
93+
8994
await this.client.ready;
95+
if (this.currentCancellationToken?.isCancellationRequested) {
96+
throw new vscode.CancellationError();
97+
}
98+
9099
try {
91-
const response = await this.client.languageClient.sendRequest(GetCopilotHoverInfoRequest, params);
100+
const response = await this.client.languageClient.sendRequest(GetCopilotHoverInfoRequest, params, this.currentCancellationToken);
92101
requestInfo = response.content;
93102
} catch (e: any) {
94103
if (e instanceof ResponseError && (e.code === RequestCancelled || e.code === ServerCancelled)) {
@@ -121,6 +130,9 @@ export class CopilotHoverProvider implements vscode.HoverProvider {
121130
this.waiting = false;
122131
this.ready = false;
123132
this.content = undefined;
133+
this.currentDocument = undefined;
134+
this.currentPosition = undefined;
135+
this.currentCancellationToken = undefined;
124136
}
125137

126138
private isNewHover(document: vscode.TextDocument, position: vscode.Position): boolean {

Extension/src/LanguageServer/client.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ export class DefaultClient implements Client {
12771277
const settings: CppSettings = new CppSettings();
12781278
this.currentCopilotHoverEnabled = new PersistentWorkspaceState<string>("cpp.copilotHover", settings.copilotHover);
12791279
if (settings.copilotHover === "enabled" ||
1280-
(settings.copilotHover === "default" && await telemetry.isFlightEnabled("cpp.copilotHover"))) {
1280+
(settings.copilotHover === "default" && await telemetry.isFlightEnabled("CppCopilotHover"))) {
12811281
this.copilotHoverProvider = new CopilotHoverProvider(this);
12821282
this.disposables.push(vscode.languages.registerHoverProvider(util.documentSelector, this.copilotHoverProvider));
12831283
}
@@ -4047,11 +4047,6 @@ export class DefaultClient implements Client {
40474047
public getCopilotHoverProvider(): CopilotHoverProvider | undefined {
40484048
return this.copilotHoverProvider;
40494049
}
4050-
4051-
public async getCopilotHoverInfo(): Promise<GetCopilotHoverInfoResult> {
4052-
await this.ready;
4053-
return this.languageClient.sendRequest(GetCopilotHoverInfoRequest, null);
4054-
}
40554050
}
40564051

40574052
function getLanguageServerFileName(): string {
@@ -4164,7 +4159,6 @@ class NullClient implements Client {
41644159
setShowConfigureIntelliSenseButton(show: boolean): void { }
41654160
addTrustedCompiler(path: string): Promise<void> { return Promise.resolve(); }
41664161
getCopilotHoverProvider(): CopilotHoverProvider | undefined { return undefined; }
4167-
getCopilotHoverInfo(): Promise<GetCopilotHoverInfoResult> { return Promise.resolve({} as GetCopilotHoverInfoResult); }
41684162
getIncludes(maxDepth: number, token: vscode.CancellationToken): Promise<GetIncludesResult> { return Promise.resolve({} as GetIncludesResult); }
41694163
getChatContext(token: vscode.CancellationToken): Promise<ChatContextResult> { return Promise.resolve({} as ChatContextResult); }
41704164
}

0 commit comments

Comments
 (0)