@@ -118,6 +118,35 @@ export async function getCodeLensesAsync(): Promise<vscode.CodeLens[]> {
118118 // The number of code lens items to resolve. Set to a high number so we get pretty much everything in the document.
119119 const resolvedItemCount = 100 ;
120120
121+ let tryCount = 0 ;
122+ const maxRetryCount = 3 ;
123+ do {
124+ try {
125+ const result = await executeCodeLensProviderAsync ( activeEditor , resolvedItemCount ) ;
126+ return result ;
127+ } catch ( e ) {
128+ tryCount ++ ;
129+ // It is totally possible that the code lens request is cancelled due to the server returning a content modified error.
130+ // This is not an error condition - it just means that the server snapshot has moved on and we need to retry the request.
131+ //
132+ // This error is not thrown as an error type that matches ours, so we'll check the name of the error to determine if it was a cancellation.
133+ if ( Object . prototype . hasOwnProperty . call ( e , 'name' ) && ( e as any ) . name === 'Canceled' ) {
134+ console . log ( 'CodeLens provider was cancelled, retrying in 1 second' ) ;
135+ await sleep ( 1000 ) ;
136+ } else {
137+ console . log ( 'CodeLens provider encountered unexpected error' ) ;
138+ console . log ( JSON . stringify ( e ) ) ;
139+ throw e ;
140+ }
141+ }
142+ } while ( tryCount < maxRetryCount ) ;
143+ throw new Error ( `Failed to get code lenses after ${ maxRetryCount } retries` ) ;
144+ }
145+
146+ async function executeCodeLensProviderAsync (
147+ activeEditor : vscode . TextEditor ,
148+ resolvedItemCount : number
149+ ) : Promise < vscode . CodeLens [ ] > {
121150 const codeLenses = < vscode . CodeLens [ ] > (
122151 await vscode . commands . executeCommand (
123152 'vscode.executeCodeLensProvider' ,
@@ -130,7 +159,6 @@ export async function getCodeLensesAsync(): Promise<vscode.CodeLens[]> {
130159 if ( rangeCompare !== 0 ) {
131160 return rangeCompare ;
132161 }
133-
134162 return a . command ! . title . localeCompare ( b . command ! . command ) ;
135163 } ) ;
136164}
0 commit comments