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

Remove isFinalized semantic tokens logic #6194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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 @@ -3,6 +3,7 @@

#nullable disable

using System;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -133,15 +134,15 @@ public TestRazorSemanticTokensInfoService(
}

// We can't get C# responses without significant amounts of extra work, so let's just shim it for now, any non-Null result is fine.
internal override Task<SemanticRangeResponse> GetCSharpSemanticRangesAsync(
internal override Task<SemanticRange[]> GetCSharpSemanticRangesAsync(
RazorCodeDocument codeDocument,
TextDocumentIdentifier textDocumentIdentifier,
Range range,
long documentVersion,
CancellationToken cancellationToken,
string previousResultId = null)
{
var result = SemanticRangeResponse.Default;
var result = Array.Empty<SemanticRange>();
return Task.FromResult(result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,19 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Semantic
/// </summary>
internal class ProvideSemanticTokensResponse
{
public ProvideSemanticTokensResponse(int[]? tokens, bool isFinalized, long? hostDocumentSyncVersion)
public ProvideSemanticTokensResponse(int[]? tokens, long? hostDocumentSyncVersion)
{
Tokens = tokens;
IsFinalized = isFinalized;
HostDocumentSyncVersion = hostDocumentSyncVersion;
}

public int[]? Tokens { get; }

public bool IsFinalized { get; }

public long? HostDocumentSyncVersion { get; }

public override bool Equals(object obj)
{
if (obj is not ProvideSemanticTokensResponse other ||
other.IsFinalized != IsFinalized ||
other.HostDocumentSyncVersion != HostDocumentSyncVersion)
if (obj is not ProvideSemanticTokensResponse other || other.HostDocumentSyncVersion != HostDocumentSyncVersion)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,10 @@ private static async Task<VersionStamp> GetDocumentSemanticVersionAsync(Document
cancellationToken.ThrowIfCancellationRequested();
var razorSemanticRanges = TagHelperSemanticRangeVisitor.VisitAllNodes(codeDocument, range);
IReadOnlyList<SemanticRange>? csharpSemanticRanges = null;
var isCSharpFinalized = false;

try
{
(csharpSemanticRanges, isCSharpFinalized) = await GetCSharpSemanticRangesAsync(
csharpSemanticRanges = await GetCSharpSemanticRangesAsync(
codeDocument, textDocumentIdentifier, range, documentVersion, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
Expand All @@ -356,7 +355,7 @@ private static async Task<VersionStamp> GetDocumentSemanticVersionAsync(Document
var data = ConvertSemanticRangesToSemanticTokensData(combinedSemanticRanges, codeDocument);
var tokens = new SemanticTokens { Data = data };

if (isCSharpFinalized && tokens is not null)
if (tokens is not null)
{
_tokensCache.CacheTokens(textDocumentIdentifier.Uri, semanticVersion, range, tokens.Data.ToArray());
}
Expand Down Expand Up @@ -401,7 +400,7 @@ private static async Task<VersionStamp> GetDocumentSemanticVersionAsync(Document
}

// Internal and virtual for testing only
internal virtual async Task<SemanticRangeResponse> GetCSharpSemanticRangesAsync(
internal virtual async Task<SemanticRange[]?> GetCSharpSemanticRangesAsync(
RazorCodeDocument codeDocument,
TextDocumentIdentifier textDocumentIdentifier,
Range razorRange,
Expand All @@ -415,7 +414,7 @@ internal virtual async Task<SemanticRangeResponse> GetCSharpSemanticRangesAsync(
!TryGetMinimalCSharpRange(codeDocument, razorRange, out csharpRange))
{
// There's no C# in the range.
return new SemanticRangeResponse(SemanticRanges: Array.Empty<SemanticRange>(), IsCSharpFinalized: true);
return Array.Empty<SemanticRange>();
}

var csharpResponse = await GetMatchingCSharpResponseAsync(textDocumentIdentifier, documentVersion, csharpRange, cancellationToken);
Expand All @@ -425,19 +424,19 @@ internal virtual async Task<SemanticRangeResponse> GetCSharpSemanticRangesAsync(
if (csharpResponse is null)
{
_logger.LogWarning($"Issue with retrieving C# response for Razor range: {razorRange}");
return SemanticRangeResponse.Default;
return null;
}

var razorRanges = new List<SemanticRange>();

SemanticRange? previousSemanticRange = null;
for (var i = 0; i < csharpResponse.Data.Length; i += TokenSize)
for (var i = 0; i < csharpResponse.Length; i += TokenSize)
{
var lineDelta = csharpResponse.Data[i];
var charDelta = csharpResponse.Data[i + 1];
var length = csharpResponse.Data[i + 2];
var tokenType = csharpResponse.Data[i + 3];
var tokenModifiers = csharpResponse.Data[i + 4];
var lineDelta = csharpResponse[i];
var charDelta = csharpResponse[i + 1];
var length = csharpResponse[i + 2];
var tokenType = csharpResponse[i + 3];
var tokenModifiers = csharpResponse[i + 4];

var semanticRange = DataToSemanticRange(
lineDelta, charDelta, length, tokenType, tokenModifiers, previousSemanticRange);
Expand All @@ -453,9 +452,7 @@ internal virtual async Task<SemanticRangeResponse> GetCSharpSemanticRangesAsync(
previousSemanticRange = semanticRange;
}

var result = razorRanges.ToArray();
var semanticRanges = new SemanticRangeResponse(result, IsCSharpFinalized: csharpResponse.IsCSharpFinalized);
return semanticRanges;
return razorRanges.ToArray();
}

// Internal for testing only
Expand Down Expand Up @@ -505,7 +502,7 @@ internal static bool TryGetMinimalCSharpRange(RazorCodeDocument codeDocument, Ra
return false;
}

private async Task<SemanticTokensResponse?> GetMatchingCSharpResponseAsync(
private async Task<int[]?> GetMatchingCSharpResponseAsync(
TextDocumentIdentifier textDocumentIdentifier,
long documentVersion,
Range csharpRange,
Expand All @@ -518,15 +515,15 @@ internal static bool TryGetMinimalCSharpRange(RazorCodeDocument codeDocument, Ra
if (csharpResponse is null)
{
// C# isn't ready yet, don't make Razor wait for it
return SemanticTokensResponse.Default;
return Array.Empty<int>();
}
else if (csharpResponse.HostDocumentSyncVersion != null && csharpResponse.HostDocumentSyncVersion != documentVersion)
{
// No C# response or C# is out of sync with us. Unrecoverable, return null to indicate no change. It will retry in a bit.
return null;
}

var response = new SemanticTokensResponse(csharpResponse.Tokens ?? Array.Empty<int>(), csharpResponse.IsFinalized);
var response = csharpResponse.Tokens ?? Array.Empty<int>();
return response;
}

Expand Down Expand Up @@ -636,17 +633,6 @@ private static IEnumerable<int> GetData(
}, cancellationToken);
}

private record SemanticTokensResponse(int[] Data, bool IsCSharpFinalized)
{
public static SemanticTokensResponse Default => new(Array.Empty<int>(), false);
}

// Internal for testing
internal record SemanticRangeResponse(SemanticRange[]? SemanticRanges, bool IsCSharpFinalized)
{
public static SemanticRangeResponse Default => new(null, false);
}

private record SemanticTokensCacheResponse(VersionStamp SemanticVersion, Range Range, SemanticTokens SemanticTokens);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,7 @@ public override async Task<RazorDocumentRangeFormattingResponse> RazorRangeForma
{
// If we're unable to synchronize we won't produce useful results, but we have to indicate
// it's due to out of sync by providing the old version
return new ProvideSemanticTokensResponse(
tokens: null, isFinalized: false, hostDocumentSyncVersion: csharpDoc.HostDocumentSyncVersion);
return new ProvideSemanticTokensResponse(tokens: null, hostDocumentSyncVersion: csharpDoc.HostDocumentSyncVersion);
}

var csharpTextDocument = semanticTokensParams.TextDocument with { Uri = csharpDoc.Uri };
Expand All @@ -399,12 +398,10 @@ public override async Task<RazorDocumentRangeFormattingResponse> RazorRangeForma
if (result is null)
{
// Weren't able to re-invoke C# semantic tokens but we have to indicate it's due to out of sync by providing the old version
return new ProvideSemanticTokensResponse(
tokens: null, isFinalized: false, hostDocumentSyncVersion: csharpDoc.HostDocumentSyncVersion);
return new ProvideSemanticTokensResponse(tokens: null, hostDocumentSyncVersion: csharpDoc.HostDocumentSyncVersion);
}

var response = new ProvideSemanticTokensResponse(
result.Data, result.IsFinalized, semanticTokensParams.RequiredHostDocumentVersion);
var response = new ProvideSemanticTokensResponse(result.Data, semanticTokensParams.RequiredHostDocumentVersion);

return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public async Task GetSemanticTokens_CSharp_RazorIfNotReady()
End = new OSharp.Position { Line = 3, Character = 0 }
};

var csharpTokens = new ProvideSemanticTokensResponse(
tokens: Array.Empty<int>(), isFinalized: false, hostDocumentSyncVersion: 1);
var csharpTokens = new ProvideSemanticTokensResponse(tokens: Array.Empty<int>(), hostDocumentSyncVersion: 1);
await AssertSemanticTokensAsync(documentText, isRazorFile: false, razorRange, csharpTokens: csharpTokens, documentVersion: 1);
}

Expand Down Expand Up @@ -95,8 +94,7 @@ public async Task GetSemanticTokens_CSharp_VSCodeWorks()
End = new OSharp.Position { Line = 2, Character = 0 }
};

var csharpTokens = new ProvideSemanticTokensResponse(
tokens: Array.Empty<int>(), isFinalized: true, hostDocumentSyncVersion: null);
var csharpTokens = new ProvideSemanticTokensResponse(tokens: Array.Empty<int>(), hostDocumentSyncVersion: null);
await AssertSemanticTokensAsync(documentText, isRazorFile: false, razorRange, csharpTokens: csharpTokens, documentVersion: 1);
}

Expand Down Expand Up @@ -782,7 +780,7 @@ private async Task AssertSemanticTokensAsync(

if (csharpTokens is null)
{
csharpTokens = new ProvideSemanticTokensResponse(tokens: null, isFinalized: true, documentVersion);
csharpTokens = new ProvideSemanticTokensResponse(tokens: null, documentVersion);
}

var (documentSnapshots, textDocumentIdentifiers) = CreateDocumentSnapshot(documentTexts, isRazorArray, DefaultTagHelpers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -117,7 +116,6 @@ private protected async Task<ProvideSemanticTokensResponse> GetCSharpSemanticTok
var codeDocument = CreateCodeDocument(documentText, isRazorFile, DefaultTagHelpers);
var csharpRange = GetMappedCSharpRange(codeDocument, razorRange);
var csharpTokens = Array.Empty<int>();
var isFinalized = true;

if (csharpRange is not null)
{
Expand All @@ -130,16 +128,14 @@ private protected async Task<ProvideSemanticTokensResponse> GetCSharpSemanticTok
using var workspace = CreateTestWorkspace(files, exportProvider);
await using var csharpLspServer = await CreateCSharpLspServerAsync(workspace, exportProvider, SemanticTokensServerCapabilities);

var result = await csharpLspServer.ExecuteRequestAsync<SemanticTokensRangeParams, VSSemanticTokensResponse>(
var result = await csharpLspServer.ExecuteRequestAsync<SemanticTokensRangeParams, SemanticTokens>(
Methods.TextDocumentSemanticTokensRangeName,
CreateVSSemanticTokensRangeParams(csharpRange.AsVSRange(), documentUri), CancellationToken.None);

csharpTokens = result?.Data;
isFinalized = result?.IsFinalized ?? false;
}

var csharpResponse = new ProvideSemanticTokensResponse(
tokens: csharpTokens, isFinalized, hostDocumentSyncVersion);
var csharpResponse = new ProvideSemanticTokensResponse(tokens: csharpTokens, hostDocumentSyncVersion);
return csharpResponse;
}

Expand Down Expand Up @@ -211,11 +207,5 @@ private static void GenerateSemanticBaseline(IEnumerable<int>? actual, string ba

return results.ToArray();
}

private class VSSemanticTokensResponse : SemanticTokens
{
[DataMember(Name = "isFinalized")]
public bool IsFinalized { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public async Task ProvideSemanticTokensAsync_ReturnsSemanticTokensAsync()
requiredHostDocumentVersion: 0,
range: new OmniSharp.Extensions.LanguageServer.Protocol.Models.Range());
var expectedResults = new ProvideSemanticTokensResponse(
expectedcSharpResults.Data, expectedcSharpResults.IsFinalized, documentVersion);
expectedcSharpResults.Data, documentVersion);

// Act
var result = await target.ProvideSemanticTokensRangeAsync(request, CancellationToken.None);
Expand Down