-
Notifications
You must be signed in to change notification settings - Fork 4k
/
SyntaxTriviaList.cs
513 lines (438 loc) · 16.6 KB
/
SyntaxTriviaList.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Syntax;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Represents a read-only list of <see cref="SyntaxTrivia"/>.
/// </summary>
[StructLayout(LayoutKind.Auto)]
public readonly partial struct SyntaxTriviaList : IEquatable<SyntaxTriviaList>, IReadOnlyList<SyntaxTrivia>
{
public static SyntaxTriviaList Empty => default(SyntaxTriviaList);
internal SyntaxTriviaList(in SyntaxToken token, GreenNode node, int position, int index = 0)
{
Token = token;
Node = node;
Position = position;
Index = index;
}
internal SyntaxTriviaList(in SyntaxToken token, GreenNode node)
{
Token = token;
Node = node;
Position = token.Position;
Index = 0;
}
public SyntaxTriviaList(SyntaxTrivia trivia)
{
Token = default(SyntaxToken);
Node = trivia.UnderlyingNode;
Position = 0;
Index = 0;
}
/// <summary>
/// Creates a list of trivia.
/// </summary>
/// <param name="trivias">An array of trivia.</param>
public SyntaxTriviaList(params SyntaxTrivia[] trivias)
: this(default, CreateNode(trivias), 0, 0)
{
}
/// <summary>
/// Creates a list of trivia.
/// </summary>
/// <param name="trivias">A sequence of trivia.</param>
public SyntaxTriviaList(IEnumerable<SyntaxTrivia> trivias)
: this(default, SyntaxTriviaListBuilder.Create(trivias).Node, 0, 0)
{
}
private static GreenNode CreateNode(SyntaxTrivia[] trivias)
{
if (trivias == null)
{
return null;
}
var builder = new SyntaxTriviaListBuilder(trivias.Length);
builder.Add(trivias);
return builder.ToList().Node;
}
internal SyntaxToken Token { get; }
internal GreenNode Node { get; }
internal int Position { get; }
internal int Index { get; }
public int Count
{
get { return Node == null ? 0 : (Node.IsList ? Node.SlotCount : 1); }
}
public SyntaxTrivia ElementAt(int index)
{
return this[index];
}
/// <summary>
/// Gets the trivia at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the trivia to get.</param>
/// <returns>The token at the specified index.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="index" /> is less than 0.-or-<paramref name="index" /> is equal to or greater than <see cref="Count" />. </exception>
public SyntaxTrivia this[int index]
{
get
{
if (Node != null)
{
if (Node.IsList)
{
if (unchecked((uint)index < (uint)Node.SlotCount))
{
return new SyntaxTrivia(Token, Node.GetSlot(index), Position + Node.GetSlotOffset(index), Index + index);
}
}
else if (index == 0)
{
return new SyntaxTrivia(Token, Node, Position, Index);
}
}
throw new ArgumentOutOfRangeException(nameof(index));
}
}
/// <summary>
/// The absolute span of the list elements in characters, including the leading and trailing trivia of the first and last elements.
/// </summary>
public TextSpan FullSpan
{
get
{
if (Node == null)
{
return default(TextSpan);
}
return new TextSpan(this.Position, Node.FullWidth);
}
}
/// <summary>
/// The absolute span of the list elements in characters, not including the leading and trailing trivia of the first and last elements.
/// </summary>
public TextSpan Span
{
get
{
if (Node == null)
{
return default(TextSpan);
}
return TextSpan.FromBounds(Position + Node.GetLeadingTriviaWidth(),
Position + Node.FullWidth - Node.GetTrailingTriviaWidth());
}
}
/// <summary>
/// Returns the first trivia in the list.
/// </summary>
/// <returns>The first trivia in the list.</returns>
/// <exception cref="InvalidOperationException">The list is empty.</exception>
public SyntaxTrivia First()
{
if (Any())
{
return this[0];
}
throw new InvalidOperationException();
}
/// <summary>
/// Returns the last trivia in the list.
/// </summary>
/// <returns>The last trivia in the list.</returns>
/// <exception cref="InvalidOperationException">The list is empty.</exception>
public SyntaxTrivia Last()
{
if (Any())
{
return this[this.Count - 1];
}
throw new InvalidOperationException();
}
/// <summary>
/// Does this list have any items.
/// </summary>
public bool Any()
{
return Node != null;
}
/// <summary>
/// Returns a list which contains all elements of <see cref="SyntaxTriviaList"/> in reversed order.
/// </summary>
/// <returns><see cref="Reversed"/> which contains all elements of <see cref="SyntaxTriviaList"/> in reversed order</returns>
public Reversed Reverse()
{
return new Reversed(this);
}
public Enumerator GetEnumerator()
{
return new Enumerator(in this);
}
public int IndexOf(SyntaxTrivia triviaInList)
{
for (int i = 0, n = this.Count; i < n; i++)
{
var trivia = this[i];
if (trivia == triviaInList)
{
return i;
}
}
return -1;
}
internal int IndexOf(int rawKind)
{
for (int i = 0, n = this.Count; i < n; i++)
{
if (this[i].RawKind == rawKind)
{
return i;
}
}
return -1;
}
/// <summary>
/// Creates a new <see cref="SyntaxTriviaList"/> with the specified trivia added to the end.
/// </summary>
/// <param name="trivia">The trivia to add.</param>
public SyntaxTriviaList Add(SyntaxTrivia trivia)
{
return Insert(this.Count, trivia);
}
/// <summary>
/// Creates a new <see cref="SyntaxTriviaList"/> with the specified trivia added to the end.
/// </summary>
/// <param name="trivia">The trivia to add.</param>
public SyntaxTriviaList AddRange(IEnumerable<SyntaxTrivia> trivia)
{
return InsertRange(this.Count, trivia);
}
/// <summary>
/// Creates a new <see cref="SyntaxTriviaList"/> with the specified trivia inserted at the index.
/// </summary>
/// <param name="index">The index in the list to insert the trivia at.</param>
/// <param name="trivia">The trivia to insert.</param>
public SyntaxTriviaList Insert(int index, SyntaxTrivia trivia)
{
if (trivia == default(SyntaxTrivia))
{
throw new ArgumentOutOfRangeException(nameof(trivia));
}
return InsertRange(index, new[] { trivia });
}
private static readonly ObjectPool<SyntaxTriviaListBuilder> s_builderPool =
new ObjectPool<SyntaxTriviaListBuilder>(() => SyntaxTriviaListBuilder.Create());
private static SyntaxTriviaListBuilder GetBuilder()
=> s_builderPool.Allocate();
private static void ClearAndFreeBuilder(SyntaxTriviaListBuilder builder)
{
// It's possible someone might create a list with a huge amount of trivia
// in it. We don't want to hold onto such items forever. So only cache
// reasonably sized lists. In IDE testing, around 99% of all trivia lists
// were 16 or less elements.
const int MaxBuilderCount = 16;
if (builder.Count <= MaxBuilderCount)
{
builder.Clear();
s_builderPool.Free(builder);
}
}
/// <summary>
/// Creates a new <see cref="SyntaxTriviaList"/> with the specified trivia inserted at the index.
/// </summary>
/// <param name="index">The index in the list to insert the trivia at.</param>
/// <param name="trivia">The trivia to insert.</param>
public SyntaxTriviaList InsertRange(int index, IEnumerable<SyntaxTrivia> trivia)
{
var thisCount = this.Count;
if (index < 0 || index > thisCount)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
if (trivia == null)
{
throw new ArgumentNullException(nameof(trivia));
}
// Just return ourselves if we're not being asked to add anything.
var triviaCollection = trivia as ICollection<SyntaxTrivia>;
if (triviaCollection != null && triviaCollection.Count == 0)
{
return this;
}
var builder = GetBuilder();
try
{
for (int i = 0; i < index; i++)
{
builder.Add(this[i]);
}
builder.AddRange(trivia);
for (int i = index; i < thisCount; i++)
{
builder.Add(this[i]);
}
return builder.Count == thisCount ? this : builder.ToList();
}
finally
{
ClearAndFreeBuilder(builder);
}
}
/// <summary>
/// Creates a new <see cref="SyntaxTriviaList"/> with the element at the specified index removed.
/// </summary>
/// <param name="index">The index identifying the element to remove.</param>
public SyntaxTriviaList RemoveAt(int index)
{
if (index < 0 || index >= this.Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
var list = this.ToList();
list.RemoveAt(index);
return new SyntaxTriviaList(default(SyntaxToken), Node.CreateList(list.Select(n => n.UnderlyingNode)), 0, 0);
}
/// <summary>
/// Creates a new <see cref="SyntaxTriviaList"/> with the specified element removed.
/// </summary>
/// <param name="triviaInList">The trivia element to remove.</param>
public SyntaxTriviaList Remove(SyntaxTrivia triviaInList)
{
var index = this.IndexOf(triviaInList);
if (index >= 0 && index < this.Count)
{
return this.RemoveAt(index);
}
return this;
}
/// <summary>
/// Creates a new <see cref="SyntaxTriviaList"/> with the specified element replaced with new trivia.
/// </summary>
/// <param name="triviaInList">The trivia element to replace.</param>
/// <param name="newTrivia">The trivia to replace the element with.</param>
public SyntaxTriviaList Replace(SyntaxTrivia triviaInList, SyntaxTrivia newTrivia)
{
if (newTrivia == default(SyntaxTrivia))
{
throw new ArgumentOutOfRangeException(nameof(newTrivia));
}
return ReplaceRange(triviaInList, new[] { newTrivia });
}
/// <summary>
/// Creates a new <see cref="SyntaxTriviaList"/> with the specified element replaced with new trivia.
/// </summary>
/// <param name="triviaInList">The trivia element to replace.</param>
/// <param name="newTrivia">The trivia to replace the element with.</param>
public SyntaxTriviaList ReplaceRange(SyntaxTrivia triviaInList, IEnumerable<SyntaxTrivia> newTrivia)
{
var index = this.IndexOf(triviaInList);
if (index >= 0 && index < this.Count)
{
var list = this.ToList();
list.RemoveAt(index);
list.InsertRange(index, newTrivia);
return new SyntaxTriviaList(default(SyntaxToken), Node.CreateList(list.Select(n => n.UnderlyingNode)), 0, 0);
}
throw new ArgumentOutOfRangeException(nameof(triviaInList));
}
// for debugging
private SyntaxTrivia[] Nodes => this.ToArray();
IEnumerator<SyntaxTrivia> IEnumerable<SyntaxTrivia>.GetEnumerator()
{
if (Node == null)
{
return SpecializedCollections.EmptyEnumerator<SyntaxTrivia>();
}
return new EnumeratorImpl(in this);
}
IEnumerator IEnumerable.GetEnumerator()
{
if (Node == null)
{
return SpecializedCollections.EmptyEnumerator<SyntaxTrivia>();
}
return new EnumeratorImpl(in this);
}
/// <summary>
/// get the green node at the specific slot
/// </summary>
private GreenNode GetGreenNodeAt(int i)
{
return GetGreenNodeAt(Node, i);
}
private static GreenNode GetGreenNodeAt(GreenNode node, int i)
{
Debug.Assert(node.IsList || (i == 0 && !node.IsList));
return node.IsList ? node.GetSlot(i) : node;
}
public bool Equals(SyntaxTriviaList other)
{
return Node == other.Node && Index == other.Index && Token.Equals(other.Token);
}
public static bool operator ==(SyntaxTriviaList left, SyntaxTriviaList right)
{
return left.Equals(right);
}
public static bool operator !=(SyntaxTriviaList left, SyntaxTriviaList right)
{
return !left.Equals(right);
}
public override bool Equals(object obj)
{
return (obj is SyntaxTriviaList) && Equals((SyntaxTriviaList)obj);
}
public override int GetHashCode()
{
return Hash.Combine(Token.GetHashCode(), Hash.Combine(Node, Index));
}
/// <summary>
/// Copy <paramref name="count"/> number of items starting at <paramref name="offset"/> from this list into <paramref name="array"/> starting at <paramref name="arrayOffset"/>.
/// </summary>
internal void CopyTo(int offset, SyntaxTrivia[] array, int arrayOffset, int count)
{
if (offset < 0 || count < 0 || this.Count < offset + count)
{
throw new IndexOutOfRangeException();
}
if (count == 0)
{
return;
}
// get first one without creating any red node
var first = this[offset];
array[arrayOffset] = first;
// calculate trivia position from the first ourselves from now on
var position = first.Position;
var current = first;
for (int i = 1; i < count; i++)
{
position += current.FullWidth;
current = new SyntaxTrivia(Token, GetGreenNodeAt(offset + i), position, Index + i);
array[arrayOffset + i] = current;
}
}
public override string ToString()
{
return Node != null ? Node.ToString() : string.Empty;
}
public string ToFullString()
{
return Node != null ? Node.ToFullString() : string.Empty;
}
public static SyntaxTriviaList Create(SyntaxTrivia trivia)
{
return new SyntaxTriviaList(trivia);
}
}
}