Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't report that we're synchronized if the line count is wrong #9437

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.LanguageServer.Common;
using Microsoft.AspNetCore.Razor.LanguageServer.Semantic;
using Microsoft.AspNetCore.Razor.LanguageServer.Semantic.Models;
Expand Down Expand Up @@ -50,7 +51,7 @@ internal partial class RazorCustomMessageTarget
cancellationToken).ConfigureAwait(false);
}

public async Task<ProvideSemanticTokensResponse?> ProvideSemanticTokensAsync(
private async Task<ProvideSemanticTokensResponse?> ProvideSemanticTokensAsync(
ProvideSemanticTokensRangesParams semanticTokensParams,
string lspMethodName,
Func<JToken, bool> capabilitiesFilter,
Expand All @@ -69,6 +70,28 @@ internal partial class RazorCustomMessageTarget

var (synchronized, csharpDoc) = await TrySynchronizeVirtualDocumentAsync<CSharpVirtualDocumentSnapshot>((int)semanticTokensParams.RequiredHostDocumentVersion, semanticTokensParams.TextDocument, cancellationToken);

if (synchronized && csharpDoc.HostDocumentSyncVersion == 1)
{
// HACK: Workaround for https://github.com/dotnet/razor/issues/9197 to stop Roslyn NFWs
// 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.
var lastGeneratedDocumentLine = requestParams switch
{
SemanticTokensRangeParams range => range.Range.End.Line,
SemanticTokensRangesParams ranges => ranges.Ranges[^1].End.Line,
_ => Assumed.Unreachable<int>()
};

if (csharpDoc.Snapshot.LineCount < lastGeneratedDocumentLine)
{
// We report this as a fail to synchronize, as that's essentially what it is: We were asked for v1, with X lines
// and whilst we have v1, we don't have X lines, so we need to wait for a future update to arrive and give us
// more content.
return new ProvideSemanticTokensResponse(tokens: null, -1);
}
}

if (csharpDoc is null)
{
return null;
Expand Down