Skip to content

Commit 5059a26

Browse files
Refactors classified span computation
Refactors the way classified spans are computed for Razor code documents intooling. This change replaces the usage of legacy compiler APIs with a new `ClassifiedSpanVisitor`. The new implementation is streamlined to only produce the information needed for the `GetLanguageKind()` API, improving performance and reducing dependencies. This also includes changing the `ClassifiedSpanInternal` to `ClassifiedSpan` and removing the dependency on `Microsoft.AspNetCore.Razor.Language.Legacy`.
1 parent a8dcf4a commit 5059a26

File tree

3 files changed

+497
-12
lines changed

3 files changed

+497
-12
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RazorCodeDocumentExtensions.CachedData.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.ComponentModel;
77
using System.Runtime.CompilerServices;
88
using System.Threading;
9-
using Microsoft.AspNetCore.Razor.Language.Legacy;
109
using Microsoft.AspNetCore.Razor.Language.Syntax;
1110
using Microsoft.AspNetCore.Razor.PooledObjects;
1211
using Microsoft.CodeAnalysis;
@@ -41,7 +40,7 @@ private sealed class CachedData(RazorCodeDocument codeDocument)
4140

4241
private readonly SemaphoreSlim _stateLock = new(initialCount: 1);
4342
private SyntaxTree? _syntaxTree;
44-
private ImmutableArray<ClassifiedSpanInternal>? _classifiedSpans;
43+
private ImmutableArray<ClassifiedSpan>? _classifiedSpans;
4544
private ImmutableArray<SourceSpan>? _tagHelperSpans;
4645

4746
public SyntaxTree GetOrParseCSharpSyntaxTree(CancellationToken cancellationToken)
@@ -63,7 +62,7 @@ static SyntaxTree ParseSyntaxTree(RazorCodeDocument codeDocument, CancellationTo
6362
}
6463
}
6564

66-
public ImmutableArray<ClassifiedSpanInternal> GetOrComputeClassifiedSpans(CancellationToken cancellationToken)
65+
public ImmutableArray<ClassifiedSpan> GetOrComputeClassifiedSpans(CancellationToken cancellationToken)
6766
{
6867
if (_classifiedSpans is { } classifiedSpans)
6968
{
@@ -72,7 +71,7 @@ public ImmutableArray<ClassifiedSpanInternal> GetOrComputeClassifiedSpans(Cancel
7271

7372
using (_stateLock.DisposableWait(cancellationToken))
7473
{
75-
return _classifiedSpans ??= _codeDocument.GetRequiredSyntaxTree().GetClassifiedSpans();
74+
return _classifiedSpans ??= ClassifiedSpanVisitor.VisitRoot(_codeDocument.GetRequiredSyntaxTree());
7675
}
7776
}
7877

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/RazorCodeDocumentExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using System.Linq;
99
using System.Threading;
1010
using Microsoft.AspNetCore.Razor.Language.Intermediate;
11-
using Microsoft.AspNetCore.Razor.Language.Legacy;
11+
using Microsoft.AspNetCore.Razor.Language.Syntax;
1212
using Microsoft.CodeAnalysis;
1313
using Microsoft.CodeAnalysis.Razor;
1414
using Microsoft.CodeAnalysis.Razor.Protocol;
@@ -150,14 +150,14 @@ public static RazorLanguageKind GetLanguageKind(this RazorCodeDocument codeDocum
150150
return GetLanguageKindCore(classifiedSpans, tagHelperSpans, hostDocumentIndex, documentLength, rightAssociative);
151151
}
152152

153-
private static ImmutableArray<ClassifiedSpanInternal> GetClassifiedSpans(RazorCodeDocument document)
153+
private static ImmutableArray<ClassifiedSpan> GetClassifiedSpans(RazorCodeDocument document)
154154
=> GetCachedData(document).GetOrComputeClassifiedSpans(CancellationToken.None);
155155

156156
private static ImmutableArray<SourceSpan> GetTagHelperSpans(RazorCodeDocument document)
157157
=> GetCachedData(document).GetOrComputeTagHelperSpans(CancellationToken.None);
158158

159159
private static RazorLanguageKind GetLanguageKindCore(
160-
ImmutableArray<ClassifiedSpanInternal> classifiedSpans,
160+
ImmutableArray<ClassifiedSpan> classifiedSpans,
161161
ImmutableArray<SourceSpan> tagHelperSpans,
162162
int hostDocumentIndex,
163163
int hostDocumentLength,
@@ -178,7 +178,7 @@ private static RazorLanguageKind GetLanguageKindCore(
178178
{
179179
// We're at an edge.
180180

181-
if (classifiedSpan.SpanKind is SpanKindInternal.MetaCode or SpanKindInternal.Transition)
181+
if (classifiedSpan.Kind is SpanKind.MetaCode or SpanKind.Transition)
182182
{
183183
// If we're on an edge of a transition of some kind (MetaCode representing an open or closing piece of syntax such as <|,
184184
// and Transition representing an explicit transition to/from razor syntax, such as @|), prefer to classify to the span
@@ -236,13 +236,13 @@ private static RazorLanguageKind GetLanguageKindCore(
236236
// Default to Razor
237237
return RazorLanguageKind.Razor;
238238

239-
static RazorLanguageKind GetLanguageFromClassifiedSpan(ClassifiedSpanInternal classifiedSpan)
239+
static RazorLanguageKind GetLanguageFromClassifiedSpan(ClassifiedSpan classifiedSpan)
240240
{
241241
// Overlaps with request
242-
return classifiedSpan.SpanKind switch
242+
return classifiedSpan.Kind switch
243243
{
244-
SpanKindInternal.Markup => RazorLanguageKind.Html,
245-
SpanKindInternal.Code => RazorLanguageKind.CSharp,
244+
SpanKind.Markup => RazorLanguageKind.Html,
245+
SpanKind.Code => RazorLanguageKind.CSharp,
246246

247247
// Content type was non-C# or Html or we couldn't find a classified span overlapping the request position.
248248
// All other classified span kinds default back to Razor

0 commit comments

Comments
 (0)