/
TextLine.cs
203 lines (177 loc) · 6.13 KB
/
TextLine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Text
{
/// <summary>
/// Information about the character boundaries of a single line of text.
/// </summary>
public readonly struct TextLine : IEquatable<TextLine>
{
private readonly SourceText? _text;
private readonly int _start;
private readonly int _endIncludingBreaks;
private TextLine(SourceText text, int start, int endIncludingBreaks)
{
_text = text;
_start = start;
_endIncludingBreaks = endIncludingBreaks;
}
/// <summary>
/// Creates a <see cref="TextLine"/> instance.
/// </summary>
/// <param name="text">The source text.</param>
/// <param name="span">The span of the line.</param>
/// <returns>An instance of <see cref="TextLine"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException">The span does not represent a text line.</exception>
public static TextLine FromSpan(SourceText text, TextSpan span)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
if (span.Start > text.Length || span.Start < 0 || span.End > text.Length)
{
throw new ArgumentOutOfRangeException(nameof(span));
}
if (text.Length > 0)
{
// check span is start of line
if (span.Start > 0 && !TextUtilities.IsAnyLineBreakCharacter(text[span.Start - 1]))
{
throw new ArgumentOutOfRangeException(nameof(span), CodeAnalysisResources.SpanDoesNotIncludeStartOfLine);
}
bool endIncludesLineBreak = false;
if (span.End > span.Start)
{
endIncludesLineBreak = TextUtilities.IsAnyLineBreakCharacter(text[span.End - 1]);
}
if (!endIncludesLineBreak && span.End < text.Length)
{
var lineBreakLen = TextUtilities.GetLengthOfLineBreak(text, span.End);
if (lineBreakLen > 0)
{
// adjust span to include line breaks
endIncludesLineBreak = true;
span = new TextSpan(span.Start, span.Length + lineBreakLen);
}
}
// check end of span is at end of line
if (span.End < text.Length && !endIncludesLineBreak)
{
throw new ArgumentOutOfRangeException(nameof(span), CodeAnalysisResources.SpanDoesNotIncludeEndOfLine);
}
return new TextLine(text, span.Start, span.End);
}
else
{
return new TextLine(text, 0, 0);
}
}
/// <summary>
/// Gets the source text.
/// </summary>
public SourceText? Text
{
get { return _text; }
}
/// <summary>
/// Gets the zero-based line number.
/// </summary>
public int LineNumber
{
get
{
return _text?.Lines.IndexOf(_start) ?? 0;
}
}
/// <summary>
/// Gets the start position of the line.
/// </summary>
public int Start
{
get { return _start; }
}
/// <summary>
/// Gets the end position of the line not including the line break.
/// </summary>
public int End
{
get { return _endIncludingBreaks - this.LineBreakLength; }
}
private int LineBreakLength
{
get
{
if (_text == null || _text.Length == 0 || _endIncludingBreaks == _start)
{
return 0;
}
int startLineBreak;
int lineBreakLength;
TextUtilities.GetStartAndLengthOfLineBreakEndingAt(_text, _endIncludingBreaks - 1, out startLineBreak, out lineBreakLength);
return lineBreakLength;
}
}
/// <summary>
/// Gets the end position of the line including the line break.
/// </summary>
public int EndIncludingLineBreak
{
get { return _endIncludingBreaks; }
}
/// <summary>
/// Gets the line span not including the line break.
/// </summary>
public TextSpan Span
{
get { return TextSpan.FromBounds(this.Start, this.End); }
}
/// <summary>
/// Gets the line span including the line break.
/// </summary>
public TextSpan SpanIncludingLineBreak
{
get { return TextSpan.FromBounds(this.Start, this.EndIncludingLineBreak); }
}
public override string ToString()
{
if (_text == null || _text.Length == 0)
{
return string.Empty;
}
else
{
return _text.ToString(this.Span);
}
}
public static bool operator ==(TextLine left, TextLine right)
{
return left.Equals(right);
}
public static bool operator !=(TextLine left, TextLine right)
{
return !left.Equals(right);
}
public bool Equals(TextLine other)
{
return other._text == _text
&& other._start == _start
&& other._endIncludingBreaks == _endIncludingBreaks;
}
public override bool Equals(object? obj)
{
if (obj is TextLine)
{
return Equals((TextLine)obj);
}
return false;
}
public override int GetHashCode()
{
return Hash.Combine(_text, Hash.Combine(_start, _endIncludingBreaks));
}
}
}