diff --git a/src/semanticTokenProvider.ts b/src/semanticTokenProvider.ts index fbc43f5a92..fbc021e825 100644 --- a/src/semanticTokenProvider.ts +++ b/src/semanticTokenProvider.ts @@ -26,7 +26,14 @@ export function registerSemanticTokensProvider(context: vscode.ExtensionContext) class SemanticTokensProvider implements vscode.DocumentSemanticTokensProvider { async provideDocumentSemanticTokens(document: vscode.TextDocument, token: vscode.CancellationToken): Promise { + const versionBeforeRequest: number = document.version; const response = await vscode.commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.PROVIDE_SEMANTIC_TOKENS, document.uri.toString()); + const versionAfterRequest: number = document.version; + + if (versionBeforeRequest !== versionAfterRequest) { + await waitForDocumentChangesToEnd(document); + throw new Error("busy"); + } if (token.isCancellationRequested) { return undefined; } @@ -37,6 +44,19 @@ class SemanticTokensProvider implements vscode.DocumentSemanticTokensProvider { } } +function waitForDocumentChangesToEnd(document: vscode.TextDocument): Promise { + let version = document.version; + return new Promise((resolve) => { + const iv = setInterval(() => { + if (document.version === version) { + clearInterval(iv); + resolve(); + } + version = document.version; + }, 400); + }); +} + const semanticTokensProvider = new SemanticTokensProvider(); async function getSemanticTokensLegend(): Promise {