Skip to content

Commit 7beb9bd

Browse files
Simplifies span writing in classifier
Refactors span writing logic in the `ClassifiedSpanVisitor` to improve readability and eliminate redundant null checks. Retrieves `AcceptedCharacters` directly within the method, using the node's edit handler or defaulting to `Any`. Also adds an assertion to validate current block during span writing.
1 parent d6dcfe9 commit 7beb9bd

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/ClassifiedSpanVisitor.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Immutable;
5+
using System.Diagnostics;
56
using Microsoft.AspNetCore.Razor.Language.Legacy;
67
using Microsoft.AspNetCore.Razor.Language.Syntax;
78
using Microsoft.AspNetCore.Razor.PooledObjects;
@@ -366,49 +367,40 @@ public void Dispose()
366367
}
367368
}
368369

369-
private void WriteSpan(SyntaxNode node, SpanKindInternal kind, AcceptedCharactersInternal? acceptedCharacters = null)
370+
private void WriteSpan(SyntaxNode node, SpanKindInternal kind)
370371
{
371372
if (node.IsMissing)
372373
{
373374
return;
374375
}
375376

377+
Debug.Assert(_currentBlock != null, "Current block should not be null when writing a span for a node.");
378+
376379
var spanSource = node.GetSourceSpan(_source);
377380
var blockSource = _currentBlock.GetSourceSpan(_source);
378-
if (!acceptedCharacters.HasValue)
379-
{
380-
acceptedCharacters = AcceptedCharactersInternal.Any;
381-
var context = node.GetEditHandler();
382-
if (context != null)
383-
{
384-
acceptedCharacters = context.AcceptedCharacters;
385-
}
386-
}
387381

388-
var span = new ClassifiedSpanInternal(spanSource, blockSource, kind, _currentBlockKind, acceptedCharacters.Value);
382+
var acceptedCharacters = node.GetEditHandler() is { } context
383+
? context.AcceptedCharacters
384+
: AcceptedCharactersInternal.Any;
385+
386+
var span = new ClassifiedSpanInternal(spanSource, blockSource, kind, _currentBlockKind, acceptedCharacters);
387+
389388
_spans.Add(span);
390389
}
391390

392-
private void WriteSpan(SyntaxToken token, SpanKindInternal kind, AcceptedCharactersInternal? acceptedCharacters = null)
391+
private void WriteSpan(SyntaxToken token, SpanKindInternal kind, AcceptedCharactersInternal acceptedCharacters)
393392
{
394393
if (token.IsMissing)
395394
{
396395
return;
397396
}
398397

398+
Debug.Assert(_currentBlock != null, "Current block should not be null when writing a span for a token.");
399+
399400
var spanSource = token.GetSourceSpan(_source);
400401
var blockSource = _currentBlock.GetSourceSpan(_source);
401-
if (!acceptedCharacters.HasValue)
402-
{
403-
acceptedCharacters = AcceptedCharactersInternal.Any;
404-
var context = token.GetEditHandler();
405-
if (context != null)
406-
{
407-
acceptedCharacters = context.AcceptedCharacters;
408-
}
409-
}
402+
var span = new ClassifiedSpanInternal(spanSource, blockSource, kind, _currentBlockKind, acceptedCharacters);
410403

411-
var span = new ClassifiedSpanInternal(spanSource, blockSource, kind, _currentBlockKind, acceptedCharacters.Value);
412404
_spans.Add(span);
413405
}
414406

0 commit comments

Comments
 (0)