@@ -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 {
0 commit comments