Skip to content

Commit e336406

Browse files
authored
Merge pull request #6580 from davidwengier/PreventError
Don't report that we're synchronized if the line count is wrong
2 parents 21e637f + 4e0239e commit e336406

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/razor/src/semantic/semanticTokensRangeHandler.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ export class SemanticTokensRangeHandler {
7373
);
7474
}
7575

76+
if (semanticTokensParams.requiredHostDocumentVersion == 1) {
77+
// HACK: Workaround for https://github.com/dotnet/razor/issues/9197 to stop errors from being thrown.
78+
// Sometimes we get asked for semantic tokens on v1, and we have sent a v1 to Roslyn, but its the wrong v1.
79+
// To prevent Roslyn throwing, lets validate the range we're asking about with the generated document they
80+
// would have seen.
81+
const endLine = semanticTokensParams.ranges[0].end.line;
82+
const lineCount = this.countLines(razorDocument.csharpDocument.getContent());
83+
84+
if (lineCount < endLine) {
85+
return new ProvideSemanticTokensResponse(this.emptyTokensResponse, -1);
86+
}
87+
}
88+
7689
const tokens = await vscode.commands.executeCommand<vscode.SemanticTokens>(
7790
'vscode.provideDocumentRangeSemanticTokens',
7891
razorDocument.csharpDocument.uri,
@@ -92,4 +105,15 @@ export class SemanticTokensRangeHandler {
92105
semanticTokensParams.requiredHostDocumentVersion
93106
);
94107
}
108+
109+
private countLines(text: string) {
110+
let lineCount = 0;
111+
for (const i of text) {
112+
if (i === '\n') {
113+
lineCount++;
114+
}
115+
}
116+
117+
return lineCount;
118+
}
95119
}

0 commit comments

Comments
 (0)