From 55db5fd5efa638a402b7d47c943b69dfd74d0ee0 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Tue, 22 Mar 2022 11:29:04 -0700 Subject: [PATCH] Remove isFinalized logic --- .../RazorSemanticTokensBenchmark.cs | 5 ++- .../Models/ProvideSemanticTokensResponse.cs | 9 +--- .../DefaultRazorSemanticTokensInfoService.cs | 44 +++++++------------ ...tRazorLanguageServerCustomMessageTarget.cs | 9 ++-- ...efaultRazorSemanticTokenInfoServiceTest.cs | 8 ++-- .../Semantic/SemanticTokenTestBase.cs | 14 +----- ...orLanguageServerCustomMessageTargetTest.cs | 2 +- 7 files changed, 29 insertions(+), 62 deletions(-) diff --git a/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorSemanticTokensBenchmark.cs b/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorSemanticTokensBenchmark.cs index e8e8a003063..089229254d1 100644 --- a/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorSemanticTokensBenchmark.cs +++ b/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorSemanticTokensBenchmark.cs @@ -3,6 +3,7 @@ #nullable disable +using System; using System.IO; using System.Linq; using System.Threading; @@ -133,7 +134,7 @@ 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 GetCSharpSemanticRangesAsync( + internal override Task GetCSharpSemanticRangesAsync( RazorCodeDocument codeDocument, TextDocumentIdentifier textDocumentIdentifier, Range range, @@ -141,7 +142,7 @@ internal override Task GetCSharpSemanticRangesAsync( CancellationToken cancellationToken, string previousResultId = null) { - var result = SemanticRangeResponse.Default; + var result = Array.Empty(); return Task.FromResult(result); } } diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Semantic/Models/ProvideSemanticTokensResponse.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Semantic/Models/ProvideSemanticTokensResponse.cs index d07129a5506..784bf17fca9 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Semantic/Models/ProvideSemanticTokensResponse.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Semantic/Models/ProvideSemanticTokensResponse.cs @@ -11,24 +11,19 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Semantic /// 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; } diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Semantic/Services/DefaultRazorSemanticTokensInfoService.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Semantic/Services/DefaultRazorSemanticTokensInfoService.cs index bd9fdec9e03..ec6c5541516 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Semantic/Services/DefaultRazorSemanticTokensInfoService.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Semantic/Services/DefaultRazorSemanticTokensInfoService.cs @@ -330,11 +330,10 @@ private static async Task GetDocumentSemanticVersionAsync(Document cancellationToken.ThrowIfCancellationRequested(); var razorSemanticRanges = TagHelperSemanticRangeVisitor.VisitAllNodes(codeDocument, range); IReadOnlyList? csharpSemanticRanges = null; - var isCSharpFinalized = false; try { - (csharpSemanticRanges, isCSharpFinalized) = await GetCSharpSemanticRangesAsync( + csharpSemanticRanges = await GetCSharpSemanticRangesAsync( codeDocument, textDocumentIdentifier, range, documentVersion, cancellationToken).ConfigureAwait(false); } catch (Exception ex) @@ -356,7 +355,7 @@ private static async Task 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()); } @@ -401,7 +400,7 @@ private static async Task GetDocumentSemanticVersionAsync(Document } // Internal and virtual for testing only - internal virtual async Task GetCSharpSemanticRangesAsync( + internal virtual async Task GetCSharpSemanticRangesAsync( RazorCodeDocument codeDocument, TextDocumentIdentifier textDocumentIdentifier, Range razorRange, @@ -415,7 +414,7 @@ internal virtual async Task GetCSharpSemanticRangesAsync( !TryGetMinimalCSharpRange(codeDocument, razorRange, out csharpRange)) { // There's no C# in the range. - return new SemanticRangeResponse(SemanticRanges: Array.Empty(), IsCSharpFinalized: true); + return Array.Empty(); } var csharpResponse = await GetMatchingCSharpResponseAsync(textDocumentIdentifier, documentVersion, csharpRange, cancellationToken); @@ -425,19 +424,19 @@ internal virtual async Task 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? 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); @@ -453,9 +452,7 @@ internal virtual async Task GetCSharpSemanticRangesAsync( previousSemanticRange = semanticRange; } - var result = razorRanges.ToArray(); - var semanticRanges = new SemanticRangeResponse(result, IsCSharpFinalized: csharpResponse.IsCSharpFinalized); - return semanticRanges; + return razorRanges.ToArray(); } // Internal for testing only @@ -505,7 +502,7 @@ internal static bool TryGetMinimalCSharpRange(RazorCodeDocument codeDocument, Ra return false; } - private async Task GetMatchingCSharpResponseAsync( + private async Task GetMatchingCSharpResponseAsync( TextDocumentIdentifier textDocumentIdentifier, long documentVersion, Range csharpRange, @@ -518,7 +515,7 @@ 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(); } else if (csharpResponse.HostDocumentSyncVersion != null && csharpResponse.HostDocumentSyncVersion != documentVersion) { @@ -526,7 +523,7 @@ internal static bool TryGetMinimalCSharpRange(RazorCodeDocument codeDocument, Ra return null; } - var response = new SemanticTokensResponse(csharpResponse.Tokens ?? Array.Empty(), csharpResponse.IsFinalized); + var response = csharpResponse.Tokens ?? Array.Empty(); return response; } @@ -636,17 +633,6 @@ private static IEnumerable GetData( }, cancellationToken); } - private record SemanticTokensResponse(int[] Data, bool IsCSharpFinalized) - { - public static SemanticTokensResponse Default => new(Array.Empty(), 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); } } diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/DefaultRazorLanguageServerCustomMessageTarget.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/DefaultRazorLanguageServerCustomMessageTarget.cs index db38109abd6..c14611e1bb7 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/DefaultRazorLanguageServerCustomMessageTarget.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/DefaultRazorLanguageServerCustomMessageTarget.cs @@ -373,8 +373,7 @@ public override async Task 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 }; @@ -399,12 +398,10 @@ public override async Task 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; } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/DefaultRazorSemanticTokenInfoServiceTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/DefaultRazorSemanticTokenInfoServiceTest.cs index 3ef101ddfe8..52980889a87 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/DefaultRazorSemanticTokenInfoServiceTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/DefaultRazorSemanticTokenInfoServiceTest.cs @@ -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(), isFinalized: false, hostDocumentSyncVersion: 1); + var csharpTokens = new ProvideSemanticTokensResponse(tokens: Array.Empty(), hostDocumentSyncVersion: 1); await AssertSemanticTokensAsync(documentText, isRazorFile: false, razorRange, csharpTokens: csharpTokens, documentVersion: 1); } @@ -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(), isFinalized: true, hostDocumentSyncVersion: null); + var csharpTokens = new ProvideSemanticTokensResponse(tokens: Array.Empty(), hostDocumentSyncVersion: null); await AssertSemanticTokensAsync(documentText, isRazorFile: false, razorRange, csharpTokens: csharpTokens, documentVersion: 1); } @@ -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); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokenTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokenTestBase.cs index 45e544efe27..33ded2d5a47 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokenTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Semantic/SemanticTokenTestBase.cs @@ -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; @@ -117,7 +116,6 @@ private protected async Task GetCSharpSemanticTok var codeDocument = CreateCodeDocument(documentText, isRazorFile, DefaultTagHelpers); var csharpRange = GetMappedCSharpRange(codeDocument, razorRange); var csharpTokens = Array.Empty(); - var isFinalized = true; if (csharpRange is not null) { @@ -130,16 +128,14 @@ private protected async Task GetCSharpSemanticTok using var workspace = CreateTestWorkspace(files, exportProvider); await using var csharpLspServer = await CreateCSharpLspServerAsync(workspace, exportProvider, SemanticTokensServerCapabilities); - var result = await csharpLspServer.ExecuteRequestAsync( + var result = await csharpLspServer.ExecuteRequestAsync( 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; } @@ -211,11 +207,5 @@ private static void GenerateSemanticBaseline(IEnumerable? actual, string ba return results.ToArray(); } - - private class VSSemanticTokensResponse : SemanticTokens - { - [DataMember(Name = "isFinalized")] - public bool IsFinalized { get; set; } - } } } diff --git a/src/Razor/test/Microsoft.VisualStudio.LanguageServerClient.Razor.Test/DefaultRazorLanguageServerCustomMessageTargetTest.cs b/src/Razor/test/Microsoft.VisualStudio.LanguageServerClient.Razor.Test/DefaultRazorLanguageServerCustomMessageTargetTest.cs index 19a8fa7a584..1eadbcca7b9 100644 --- a/src/Razor/test/Microsoft.VisualStudio.LanguageServerClient.Razor.Test/DefaultRazorLanguageServerCustomMessageTargetTest.cs +++ b/src/Razor/test/Microsoft.VisualStudio.LanguageServerClient.Razor.Test/DefaultRazorLanguageServerCustomMessageTargetTest.cs @@ -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);