11
11
using System . Threading . Tasks ;
12
12
using Microsoft . AspNetCore . Razor . Language ;
13
13
using Microsoft . AspNetCore . Razor . Language . Syntax ;
14
+ using Microsoft . AspNetCore . Razor . PooledObjects ;
14
15
using Microsoft . CodeAnalysis ;
15
16
using Microsoft . CodeAnalysis . Razor . ProjectSystem ;
16
17
using Microsoft . CodeAnalysis . Text ;
@@ -19,7 +20,7 @@ namespace Microsoft.CodeAnalysis.Razor.Formatting;
19
20
20
21
internal sealed class FormattingContext
21
22
{
22
- private IReadOnlyList < FormattingSpan > ? _formattingSpans ;
23
+ private ImmutableArray < FormattingSpan > ? _formattingSpans ;
23
24
private IReadOnlyDictionary < int , IndentationContext > ? _indentations ;
24
25
private readonly bool _useNewFormattingEngine ;
25
26
@@ -143,25 +144,27 @@ public IReadOnlyDictionary<int, IndentationContext> GetIndentations()
143
144
return _indentations ;
144
145
}
145
146
146
- private IReadOnlyList < FormattingSpan > GetFormattingSpans ( )
147
+ private ImmutableArray < FormattingSpan > GetFormattingSpans ( )
147
148
{
148
- if ( _formattingSpans is null )
149
+ return _formattingSpans ??= ComputeFormattingSpans ( CodeDocument ) ;
150
+
151
+ static ImmutableArray < FormattingSpan > ComputeFormattingSpans ( RazorCodeDocument codeDocument )
149
152
{
150
- var syntaxTree = CodeDocument . GetRequiredSyntaxTree ( ) ;
151
- var inGlobalNamespace = CodeDocument . TryGetNamespace ( fallbackToRootNamespace : true , out var @namespace ) &&
153
+ var syntaxTree = codeDocument . GetRequiredSyntaxTree ( ) ;
154
+ var inGlobalNamespace = codeDocument . TryGetNamespace ( fallbackToRootNamespace : true , out var @namespace ) &&
152
155
string . IsNullOrEmpty ( @namespace ) ;
153
- _formattingSpans = GetFormattingSpans ( syntaxTree , inGlobalNamespace : inGlobalNamespace ) ;
154
- }
155
156
156
- return _formattingSpans ;
157
+ return GetFormattingSpans ( syntaxTree , inGlobalNamespace : inGlobalNamespace ) ;
158
+ }
157
159
}
158
160
159
- private static IReadOnlyList < FormattingSpan > GetFormattingSpans ( RazorSyntaxTree syntaxTree , bool inGlobalNamespace )
161
+ private static ImmutableArray < FormattingSpan > GetFormattingSpans ( RazorSyntaxTree syntaxTree , bool inGlobalNamespace )
160
162
{
161
- var visitor = new FormattingVisitor ( inGlobalNamespace : inGlobalNamespace ) ;
162
- visitor . Visit ( syntaxTree . Root ) ;
163
+ using var _ = ArrayBuilderPool < FormattingSpan > . GetPooledObject ( out var formattingSpans ) ;
164
+
165
+ FormattingVisitor . VisitRoot ( syntaxTree , formattingSpans , inGlobalNamespace ) ;
163
166
164
- return visitor . FormattingSpans ;
167
+ return formattingSpans . ToImmutableAndClear ( ) ;
165
168
}
166
169
167
170
/// <summary>
0 commit comments