3
3
4
4
using System . Diagnostics ;
5
5
using Microsoft . AspNetCore . Razor . Language . Syntax ;
6
+ using Microsoft . CodeAnalysis . Text ;
6
7
7
8
namespace Microsoft . AspNetCore . Razor . Language ;
8
9
9
10
/// <summary>
10
- /// Helper that can be used to efficiently build up a <see cref="SourceSpan"/> from a set of syntax tokens.
11
+ /// Helper that can be used to efficiently build up a <see cref="SourceSpan"/> or
12
+ /// <see cref="CodeAnalysis.Text.TextSpan"/> from a set of syntax tokens.
11
13
/// </summary>
12
- internal ref struct SourceSpanComputer ( RazorSourceDocument source )
14
+ internal ref struct SpanComputer ( )
13
15
{
14
- private readonly RazorSourceDocument _source = source ;
15
-
16
16
private SyntaxToken _firstToken ;
17
17
private SyntaxToken _lastToken ;
18
18
@@ -91,7 +91,7 @@ public void Add(UnclassifiedTextLiteralSyntax? literal)
91
91
Add ( literal ? . LiteralTokens ) ;
92
92
}
93
93
94
- public readonly SourceSpan ToSourceSpan ( )
94
+ public readonly SourceSpan ToSourceSpan ( RazorSourceDocument source )
95
95
{
96
96
if ( _firstToken . Kind == SyntaxKind . None )
97
97
{
@@ -107,11 +107,28 @@ public readonly SourceSpan ToSourceSpan()
107
107
108
108
var length = end - start ;
109
109
110
- var text = _source . Text ;
110
+ var text = source . Text ;
111
111
var startLinePosition = text . Lines . GetLinePosition ( start ) ;
112
112
var endLinePosition = text . Lines . GetLinePosition ( end ) ;
113
113
var lineCount = endLinePosition . Line - startLinePosition . Line ;
114
114
115
- return new SourceSpan ( _source . FilePath , absoluteIndex : start , startLinePosition . Line , startLinePosition . Character , length , lineCount , endLinePosition . Character ) ;
115
+ return new SourceSpan ( source . FilePath , absoluteIndex : start , startLinePosition . Line , startLinePosition . Character , length , lineCount , endLinePosition . Character ) ;
116
+ }
117
+
118
+ public readonly TextSpan ToTextSpan ( )
119
+ {
120
+ if ( _firstToken . Kind == SyntaxKind . None )
121
+ {
122
+ return default ;
123
+ }
124
+
125
+ Debug . Assert ( _lastToken . Kind != SyntaxKind . None , "Last token should not be None when first token is set." ) ;
126
+
127
+ var start = _firstToken . Span . Start ;
128
+ var end = _lastToken . Span . End ;
129
+
130
+ Debug . Assert ( start <= end , "Start position should not be greater than end position." ) ;
131
+
132
+ return TextSpan . FromBounds ( start , end ) ;
116
133
}
117
134
}
0 commit comments