Skip to content

Commit

Permalink
Fix semantic tokens offset due to document updates
Browse files Browse the repository at this point in the history
Signed-off-by: 0dinD <[email protected]>
  • Loading branch information
0dinD authored and fbricon committed Sep 25, 2020
1 parent 864c503 commit 85941bf
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/semanticTokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ export function registerSemanticTokensProvider(context: vscode.ExtensionContext)

class SemanticTokensProvider implements vscode.DocumentSemanticTokensProvider {
async provideDocumentSemanticTokens(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.SemanticTokens> {
const versionBeforeRequest: number = document.version;
const response = <any> 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;
}
Expand All @@ -37,6 +44,19 @@ class SemanticTokensProvider implements vscode.DocumentSemanticTokensProvider {
}
}

function waitForDocumentChangesToEnd(document: vscode.TextDocument): Promise<void> {
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<vscode.SemanticTokensLegend | undefined> {
Expand Down

0 comments on commit 85941bf

Please sign in to comment.