Skip to content

Commit

Permalink
ensure tokens and cancellation are all in sync between copilot hover …
Browse files Browse the repository at this point in the history
…and the extension callback
  • Loading branch information
spebl committed Oct 28, 2024
1 parent 223d52b commit c055ce2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export class CopilotHoverProvider implements vscode.HoverProvider {
return this.currentPosition;
}

public getCurrentHoverCancellationToken(): vscode.CancellationToken | undefined {
return this.currentCancellationToken;
}

public async getRequestInfo(document: vscode.TextDocument, position: vscode.Position): Promise<string> {
let requestInfo = "";
const params: GetCopilotHoverInfoParams = {
Expand Down Expand Up @@ -124,7 +128,7 @@ export class CopilotHoverProvider implements vscode.HoverProvider {
return false;
}

private reset(): void {
public reset(): void {
// If there was a previous call, cancel it.
if (this.waiting) {
this.cancelled = true;
Expand Down
5 changes: 3 additions & 2 deletions Extension/src/LanguageServer/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@ async function onCopilotHover(): Promise<void> {
chatResponse = await model.sendRequest(
messages,
{},
new vscode.CancellationTokenSource().token
copilotHoverProvider.getCurrentHoverCancellationToken()
);
} catch (err) {
if (err instanceof vscode.LanguageModelError) {
Expand Down Expand Up @@ -1484,7 +1484,8 @@ async function showCopilotContent(copilotHoverProvider: CopilotHoverProvider, ho
// Check if the cursor has been manually moved by the user. If so, exit.
const currentCursorPosition = vscode.window.activeTextEditor?.selection.active;
if (!currentCursorPosition?.isEqual(hoverPosition)) {
return false;
// Reset implies cancellation, but we need to ensure cancellation is acknowledged before returning.
copilotHoverProvider.reset();
}

if (copilotHoverProvider.isCancelled(hoverDocument, hoverPosition)) {
Expand Down

0 comments on commit c055ce2

Please sign in to comment.