1
- // Licensed to the .NET Foundation under one or more agreements.
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System . Collections . Immutable ;
@@ -17,8 +17,9 @@ internal class ClassifiedSpanVisitor : SyntaxWalker
17
17
private readonly RazorSourceDocument _source ;
18
18
private readonly ImmutableArray < ClassifiedSpanInternal > . Builder _spans ;
19
19
20
- private BlockKindInternal _currentBlockKind ;
21
20
private SyntaxNode ? _currentBlock ;
21
+ private SourceSpan ? _currentBlockSpan ;
22
+ private BlockKindInternal _currentBlockKind ;
22
23
23
24
private ClassifiedSpanVisitor ( RazorSourceDocument source , ImmutableArray < ClassifiedSpanInternal > . Builder spans )
24
25
{
@@ -42,8 +43,8 @@ public override void VisitRazorCommentBlock(RazorCommentBlockSyntax node)
42
43
{
43
44
using ( CommentBlock ( node ) )
44
45
{
45
- WriteSpan ( node . StartCommentTransition , SpanKindInternal . Transition , AcceptedCharactersInternal . None ) ;
46
- WriteSpan ( node . StartCommentStar , SpanKindInternal . MetaCode , AcceptedCharactersInternal . None ) ;
46
+ AddSpan ( node . StartCommentTransition , SpanKindInternal . Transition , AcceptedCharactersInternal . None ) ;
47
+ AddSpan ( node . StartCommentStar , SpanKindInternal . MetaCode , AcceptedCharactersInternal . None ) ;
47
48
48
49
var comment = node . Comment ;
49
50
@@ -53,10 +54,10 @@ public override void VisitRazorCommentBlock(RazorCommentBlockSyntax node)
53
54
comment = SyntaxFactory . Token ( SyntaxKind . Marker , parent : node , position : node . StartCommentStar . EndPosition ) ;
54
55
}
55
56
56
- WriteSpan ( comment , SpanKindInternal . Comment , AcceptedCharactersInternal . Any ) ;
57
+ AddSpan ( comment , SpanKindInternal . Comment , AcceptedCharactersInternal . Any ) ;
57
58
58
- WriteSpan ( node . EndCommentStar , SpanKindInternal . MetaCode , AcceptedCharactersInternal . None ) ;
59
- WriteSpan ( node . EndCommentTransition , SpanKindInternal . Transition , AcceptedCharactersInternal . None ) ;
59
+ AddSpan ( node . EndCommentStar , SpanKindInternal . MetaCode , AcceptedCharactersInternal . None ) ;
60
+ AddSpan ( node . EndCommentTransition , SpanKindInternal . Transition , AcceptedCharactersInternal . None ) ;
60
61
}
61
62
}
62
63
@@ -257,49 +258,49 @@ public override void VisitMarkupDynamicAttributeValue(MarkupDynamicAttributeValu
257
258
258
259
public override void VisitRazorMetaCode ( RazorMetaCodeSyntax node )
259
260
{
260
- WriteSpan ( node , SpanKindInternal . MetaCode ) ;
261
+ AddSpan ( node , SpanKindInternal . MetaCode ) ;
261
262
base . VisitRazorMetaCode ( node ) ;
262
263
}
263
264
264
265
public override void VisitCSharpTransition ( CSharpTransitionSyntax node )
265
266
{
266
- WriteSpan ( node , SpanKindInternal . Transition ) ;
267
+ AddSpan ( node , SpanKindInternal . Transition ) ;
267
268
base . VisitCSharpTransition ( node ) ;
268
269
}
269
270
270
271
public override void VisitMarkupTransition ( MarkupTransitionSyntax node )
271
272
{
272
- WriteSpan ( node , SpanKindInternal . Transition ) ;
273
+ AddSpan ( node , SpanKindInternal . Transition ) ;
273
274
base . VisitMarkupTransition ( node ) ;
274
275
}
275
276
276
277
public override void VisitCSharpStatementLiteral ( CSharpStatementLiteralSyntax node )
277
278
{
278
- WriteSpan ( node , SpanKindInternal . Code ) ;
279
+ AddSpan ( node , SpanKindInternal . Code ) ;
279
280
base . VisitCSharpStatementLiteral ( node ) ;
280
281
}
281
282
282
283
public override void VisitCSharpExpressionLiteral ( CSharpExpressionLiteralSyntax node )
283
284
{
284
- WriteSpan ( node , SpanKindInternal . Code ) ;
285
+ AddSpan ( node , SpanKindInternal . Code ) ;
285
286
base . VisitCSharpExpressionLiteral ( node ) ;
286
287
}
287
288
288
289
public override void VisitCSharpEphemeralTextLiteral ( CSharpEphemeralTextLiteralSyntax node )
289
290
{
290
- WriteSpan ( node , SpanKindInternal . Code ) ;
291
+ AddSpan ( node , SpanKindInternal . Code ) ;
291
292
base . VisitCSharpEphemeralTextLiteral ( node ) ;
292
293
}
293
294
294
295
public override void VisitUnclassifiedTextLiteral ( UnclassifiedTextLiteralSyntax node )
295
296
{
296
- WriteSpan ( node , SpanKindInternal . None ) ;
297
+ AddSpan ( node , SpanKindInternal . None ) ;
297
298
base . VisitUnclassifiedTextLiteral ( node ) ;
298
299
}
299
300
300
301
public override void VisitMarkupLiteralAttributeValue ( MarkupLiteralAttributeValueSyntax node )
301
302
{
302
- WriteSpan ( node , SpanKindInternal . Markup ) ;
303
+ AddSpan ( node , SpanKindInternal . Markup ) ;
303
304
base . VisitMarkupLiteralAttributeValue ( node ) ;
304
305
}
305
306
@@ -311,13 +312,13 @@ public override void VisitMarkupTextLiteral(MarkupTextLiteralSyntax node)
311
312
return ;
312
313
}
313
314
314
- WriteSpan ( node , SpanKindInternal . Markup ) ;
315
+ AddSpan ( node , SpanKindInternal . Markup ) ;
315
316
base . VisitMarkupTextLiteral ( node ) ;
316
317
}
317
318
318
319
public override void VisitMarkupEphemeralTextLiteral ( MarkupEphemeralTextLiteralSyntax node )
319
320
{
320
- WriteSpan ( node , SpanKindInternal . Markup ) ;
321
+ AddSpan ( node , SpanKindInternal . Markup ) ;
321
322
base . VisitMarkupEphemeralTextLiteral ( node ) ;
322
323
}
323
324
@@ -352,22 +353,31 @@ private BlockSaver Block(SyntaxNode node, BlockKindInternal kind)
352
353
_currentBlock = node ;
353
354
_currentBlockKind = kind ;
354
355
356
+ // This is a new block, so we reset the current block span.
357
+ // It will be computed when the first span is written.
358
+ _currentBlockSpan = null ;
359
+
355
360
return saver ;
356
361
}
357
362
358
363
private readonly ref struct BlockSaver ( ClassifiedSpanVisitor visitor )
359
364
{
360
365
private readonly SyntaxNode ? _previousBlock = visitor . _currentBlock ;
366
+ private readonly SourceSpan ? _previousBlockSpan = visitor . _currentBlockSpan ;
361
367
private readonly BlockKindInternal _previousKind = visitor . _currentBlockKind ;
362
368
363
369
public void Dispose ( )
364
370
{
365
371
visitor . _currentBlock = _previousBlock ;
372
+ visitor . _currentBlockSpan = _previousBlockSpan ;
366
373
visitor . _currentBlockKind = _previousKind ;
367
374
}
368
375
}
369
376
370
- private void WriteSpan ( SyntaxNode node , SpanKindInternal kind )
377
+ private SourceSpan CurrentBlockSpan
378
+ => _currentBlockSpan ??= _currentBlock . AssumeNotNull ( ) . GetSourceSpan ( _source ) ;
379
+
380
+ private void AddSpan ( SyntaxNode node , SpanKindInternal kind )
371
381
{
372
382
if ( node . IsMissing )
373
383
{
@@ -376,19 +386,16 @@ private void WriteSpan(SyntaxNode node, SpanKindInternal kind)
376
386
377
387
Debug . Assert ( _currentBlock != null , "Current block should not be null when writing a span for a node." ) ;
378
388
379
- var spanSource = node . GetSourceSpan ( _source ) ;
380
- var blockSource = _currentBlock . GetSourceSpan ( _source ) ;
389
+ var nodeSpan = node . GetSourceSpan ( _source ) ;
381
390
382
391
var acceptedCharacters = node . GetEditHandler ( ) is { } context
383
392
? context . AcceptedCharacters
384
393
: AcceptedCharactersInternal . Any ;
385
394
386
- var span = new ClassifiedSpanInternal ( spanSource , blockSource , kind , _currentBlockKind , acceptedCharacters ) ;
387
-
388
- _spans . Add ( span ) ;
395
+ AddSpan ( nodeSpan , kind , acceptedCharacters ) ;
389
396
}
390
397
391
- private void WriteSpan ( SyntaxToken token , SpanKindInternal kind , AcceptedCharactersInternal acceptedCharacters )
398
+ private void AddSpan ( SyntaxToken token , SpanKindInternal kind , AcceptedCharactersInternal acceptedCharacters )
392
399
{
393
400
if ( token . IsMissing )
394
401
{
@@ -397,13 +404,14 @@ private void WriteSpan(SyntaxToken token, SpanKindInternal kind, AcceptedCharact
397
404
398
405
Debug . Assert ( _currentBlock != null , "Current block should not be null when writing a span for a token." ) ;
399
406
400
- var spanSource = token . GetSourceSpan ( _source ) ;
401
- var blockSource = _currentBlock . GetSourceSpan ( _source ) ;
402
- var span = new ClassifiedSpanInternal ( spanSource , blockSource , kind , _currentBlockKind , acceptedCharacters ) ;
407
+ var tokenSpan = token . GetSourceSpan ( _source ) ;
403
408
404
- _spans . Add ( span ) ;
409
+ AddSpan ( tokenSpan , kind , acceptedCharacters ) ;
405
410
}
406
411
412
+ private void AddSpan ( SourceSpan span , SpanKindInternal kind , AcceptedCharactersInternal acceptedCharacters )
413
+ => _spans . Add ( new ( span , CurrentBlockSpan , kind , _currentBlockKind , acceptedCharacters ) ) ;
414
+
407
415
private sealed class Policy : IPooledObjectPolicy < ImmutableArray < ClassifiedSpanInternal > . Builder >
408
416
{
409
417
public static readonly Policy Instance = new ( ) ;
0 commit comments