-
Notifications
You must be signed in to change notification settings - Fork 4k
/
TextLineCollection.cs
164 lines (140 loc) · 4.85 KB
/
TextLineCollection.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
// 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 System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.Text
{
/// <summary>
/// Abstract base class for <see cref="TextLine"/> collections.
/// </summary>
public abstract class TextLineCollection : IReadOnlyList<TextLine>
{
/// <summary>
/// The count of <see cref="TextLine"/> items in the collection
/// </summary>
public abstract int Count { get; }
/// <summary>
/// Gets the <see cref="TextLine"/> item at the specified index.
/// </summary>
public abstract TextLine this[int index] { get; }
/// <summary>
/// The index of the TextLine that encompasses the character position.
/// </summary>
public abstract int IndexOf(int position);
/// <summary>
/// Gets a <see cref="TextLine"/> that encompasses the character position.
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public virtual TextLine GetLineFromPosition(int position)
{
return this[this.IndexOf(position)];
}
/// <summary>
/// Gets a <see cref="LinePosition"/> corresponding to a character position.
/// </summary>
public virtual LinePosition GetLinePosition(int position)
{
var line = GetLineFromPosition(position);
return new LinePosition(line.LineNumber, position - line.Start);
}
/// <summary>
/// Convert a <see cref="TextSpan"/> to a <see cref="LinePositionSpan"/>.
/// </summary>
public LinePositionSpan GetLinePositionSpan(TextSpan span)
{
return new LinePositionSpan(GetLinePosition(span.Start), GetLinePosition(span.End));
}
/// <summary>
/// Convert a <see cref="LinePosition"/> to a position.
/// </summary>
public int GetPosition(LinePosition position)
{
if (position.Line >= this.Count)
{
throw new ArgumentOutOfRangeException(nameof(position.Line), string.Format(CodeAnalysisResources.LineCannotBeGreaterThanEnd, position.Line, this.Count));
}
return this[position.Line].Start + position.Character;
}
/// <summary>
/// Convert a <see cref="LinePositionSpan"/> to <see cref="TextSpan"/>.
/// </summary>
public TextSpan GetTextSpan(LinePositionSpan span)
{
return TextSpan.FromBounds(GetPosition(span.Start), GetPosition(span.End));
}
public Enumerator GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator<TextLine> IEnumerable<TextLine>.GetEnumerator()
{
return this.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
[SuppressMessage("Performance", "CA1067", Justification = "Equality not actually implemented")]
public struct Enumerator : IEnumerator<TextLine>, IEnumerator
{
private readonly TextLineCollection _lines;
private int _index;
internal Enumerator(TextLineCollection lines, int index = -1)
{
_lines = lines;
_index = index;
}
public TextLine Current
{
get
{
var ndx = _index;
if (ndx >= 0 && ndx < _lines.Count)
{
return _lines[ndx];
}
else
{
return default(TextLine);
}
}
}
public bool MoveNext()
{
if (_index < _lines.Count - 1)
{
_index = _index + 1;
return true;
}
return false;
}
object IEnumerator.Current
{
get { return this.Current; }
}
bool IEnumerator.MoveNext()
{
return this.MoveNext();
}
void IEnumerator.Reset()
{
}
void IDisposable.Dispose()
{
}
public override bool Equals(object? obj)
{
throw new NotSupportedException();
}
public override int GetHashCode()
{
throw new NotSupportedException();
}
}
}
}