Skip to content

Commit

Permalink
Don't report that we're synchronized if the line count is wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwengier committed Oct 22, 2023
1 parent 21e637f commit 4e0239e
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/razor/src/semantic/semanticTokensRangeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ export class SemanticTokensRangeHandler {
);
}

if (semanticTokensParams.requiredHostDocumentVersion == 1) {
// HACK: Workaround for https://github.com/dotnet/razor/issues/9197 to stop errors from being thrown.
// Sometimes we get asked for semantic tokens on v1, and we have sent a v1 to Roslyn, but its the wrong v1.
// To prevent Roslyn throwing, lets validate the range we're asking about with the generated document they
// would have seen.
const endLine = semanticTokensParams.ranges[0].end.line;
const lineCount = this.countLines(razorDocument.csharpDocument.getContent());

if (lineCount < endLine) {
return new ProvideSemanticTokensResponse(this.emptyTokensResponse, -1);
}
}

const tokens = await vscode.commands.executeCommand<vscode.SemanticTokens>(
'vscode.provideDocumentRangeSemanticTokens',
razorDocument.csharpDocument.uri,
Expand All @@ -92,4 +105,15 @@ export class SemanticTokensRangeHandler {
semanticTokensParams.requiredHostDocumentVersion
);
}

private countLines(text: string) {
let lineCount = 0;
for (const i of text) {
if (i === '\n') {
lineCount++;
}
}

return lineCount;
}
}

0 comments on commit 4e0239e

Please sign in to comment.