/
SyntaxList`1.cs
533 lines (466 loc) · 14.9 KB
/
SyntaxList`1.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// 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 Microsoft.CodeAnalysis.Syntax;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// A list of <see cref="SyntaxNode"/>.
/// </summary>
public readonly partial struct SyntaxList<TNode> : IReadOnlyList<TNode>, IEquatable<SyntaxList<TNode>>
where TNode : SyntaxNode
{
private readonly SyntaxNode _node;
internal SyntaxList(SyntaxNode node)
{
_node = node;
}
/// <summary>
/// Creates a singleton list of syntax nodes.
/// </summary>
/// <param name="node">The single element node.</param>
public SyntaxList(TNode node)
: this((SyntaxNode)node)
{
}
/// <summary>
/// Creates a list of syntax nodes.
/// </summary>
/// <param name="nodes">A sequence of element nodes.</param>
public SyntaxList(IEnumerable<TNode> nodes)
: this(CreateNode(nodes))
{
}
private static SyntaxNode CreateNode(IEnumerable<TNode> nodes)
{
if (nodes == null)
{
return null;
}
var collection = nodes as ICollection<TNode>;
var builder = (collection != null) ? new SyntaxListBuilder<TNode>(collection.Count) : SyntaxListBuilder<TNode>.Create();
foreach (TNode node in nodes)
{
builder.Add(node);
}
return builder.ToList().Node;
}
internal SyntaxNode Node
{
get
{
return _node;
}
}
/// <summary>
/// The number of nodes in the list.
/// </summary>
public int Count
{
get
{
return _node == null ? 0 : (_node.IsList ? _node.SlotCount : 1);
}
}
/// <summary>
/// Gets the node at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the node to get or set.</param>
/// <returns>The node at the specified index.</returns>
public TNode this[int index]
{
get
{
if (_node != null)
{
if (_node.IsList)
{
if (unchecked((uint)index < (uint)_node.SlotCount))
{
return (TNode)_node.GetNodeSlot(index);
}
}
else if (index == 0)
{
return (TNode)_node;
}
}
throw new ArgumentOutOfRangeException();
}
}
internal SyntaxNode ItemInternal(int index)
{
if (_node.IsList)
{
return _node.GetNodeSlot(index);
}
Debug.Assert(index == 0);
return _node;
}
/// <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 (this.Count == 0)
{
return default(TextSpan);
}
else
{
return TextSpan.FromBounds(this[0].FullSpan.Start, this[this.Count - 1].FullSpan.End);
}
}
}
/// <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 (this.Count == 0)
{
return default(TextSpan);
}
else
{
return TextSpan.FromBounds(this[0].Span.Start, this[this.Count - 1].Span.End);
}
}
}
/// <summary>
/// Returns the string representation of the nodes in this list, not including
/// the first node's leading trivia and the last node's trailing trivia.
/// </summary>
/// <returns>
/// The string representation of the nodes in this list, not including
/// the first node's leading trivia and the last node's trailing trivia.
/// </returns>
public override string ToString()
{
return _node != null ? _node.ToString() : string.Empty;
}
/// <summary>
/// Returns the full string representation of the nodes in this list including
/// the first node's leading trivia and the last node's trailing trivia.
/// </summary>
/// <returns>
/// The full string representation of the nodes in this list including
/// the first node's leading trivia and the last node's trailing trivia.
/// </returns>
public string ToFullString()
{
return _node != null ? _node.ToFullString() : string.Empty;
}
/// <summary>
/// Creates a new list with the specified node added at the end.
/// </summary>
/// <param name="node">The node to add.</param>
public SyntaxList<TNode> Add(TNode node)
{
return this.Insert(this.Count, node);
}
/// <summary>
/// Creates a new list with the specified nodes added at the end.
/// </summary>
/// <param name="nodes">The nodes to add.</param>
public SyntaxList<TNode> AddRange(IEnumerable<TNode> nodes)
{
return this.InsertRange(this.Count, nodes);
}
/// <summary>
/// Creates a new list with the specified node inserted at the index.
/// </summary>
/// <param name="index">The index to insert at.</param>
/// <param name="node">The node to insert.</param>
public SyntaxList<TNode> Insert(int index, TNode node)
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
return InsertRange(index, new[] { node });
}
/// <summary>
/// Creates a new list with the specified nodes inserted at the index.
/// </summary>
/// <param name="index">The index to insert at.</param>
/// <param name="nodes">The nodes to insert.</param>
public SyntaxList<TNode> InsertRange(int index, IEnumerable<TNode> nodes)
{
if (index < 0 || index > this.Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
if (nodes == null)
{
throw new ArgumentNullException(nameof(nodes));
}
var list = this.ToList();
list.InsertRange(index, nodes);
if (list.Count == 0)
{
return this;
}
else
{
return CreateList(list[0].Green, list);
}
}
/// <summary>
/// Creates a new list with the element at specified index removed.
/// </summary>
/// <param name="index">The index of the element to remove.</param>
public SyntaxList<TNode> RemoveAt(int index)
{
if (index < 0 || index > this.Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
return this.Remove(this[index]);
}
/// <summary>
/// Creates a new list with the element removed.
/// </summary>
/// <param name="node">The element to remove.</param>
public SyntaxList<TNode> Remove(TNode node)
{
return CreateList(this.Where(x => x != node).ToList());
}
/// <summary>
/// Creates a new list with the specified element replaced with the new node.
/// </summary>
/// <param name="nodeInList">The element to replace.</param>
/// <param name="newNode">The new node.</param>
public SyntaxList<TNode> Replace(TNode nodeInList, TNode newNode)
{
return ReplaceRange(nodeInList, new[] { newNode });
}
/// <summary>
/// Creates a new list with the specified element replaced with new nodes.
/// </summary>
/// <param name="nodeInList">The element to replace.</param>
/// <param name="newNodes">The new nodes.</param>
public SyntaxList<TNode> ReplaceRange(TNode nodeInList, IEnumerable<TNode> newNodes)
{
if (nodeInList == null)
{
throw new ArgumentNullException(nameof(nodeInList));
}
if (newNodes == null)
{
throw new ArgumentNullException(nameof(newNodes));
}
var index = this.IndexOf(nodeInList);
if (index >= 0 && index < this.Count)
{
var list = this.ToList();
list.RemoveAt(index);
list.InsertRange(index, newNodes);
return CreateList(list);
}
else
{
throw new ArgumentException(nameof(nodeInList));
}
}
private static SyntaxList<TNode> CreateList(List<TNode> items)
{
if (items.Count == 0)
{
return default(SyntaxList<TNode>);
}
else
{
return CreateList(items[0].Green, items);
}
}
private static SyntaxList<TNode> CreateList(GreenNode creator, List<TNode> items)
{
if (items.Count == 0)
{
return default(SyntaxList<TNode>);
}
var newGreen = creator.CreateList(items.Select(n => n.Green));
return new SyntaxList<TNode>(newGreen.CreateRed());
}
/// <summary>
/// The first node in the list.
/// </summary>
public TNode First()
{
return this[0];
}
/// <summary>
/// The first node in the list or default if the list is empty.
/// </summary>
public TNode FirstOrDefault()
{
if (this.Any())
{
return this[0];
}
else
{
return null;
}
}
/// <summary>
/// The last node in the list.
/// </summary>
public TNode Last()
{
return this[this.Count - 1];
}
/// <summary>
/// The last node in the list or default if the list is empty.
/// </summary>
public TNode LastOrDefault()
{
if (this.Any())
{
return this[this.Count - 1];
}
else
{
return null;
}
}
/// <summary>
/// True if the list has at least one node.
/// </summary>
public bool Any()
{
Debug.Assert(_node == null || Count != 0);
return _node != null;
}
// for debugging
private TNode[] Nodes
{
get { return this.ToArray(); }
}
/// <summary>
/// Get's the enumerator for this list.
/// </summary>
public Enumerator GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator<TNode> IEnumerable<TNode>.GetEnumerator()
{
if (this.Any())
{
return new EnumeratorImpl(this);
}
return SpecializedCollections.EmptyEnumerator<TNode>();
}
IEnumerator IEnumerable.GetEnumerator()
{
if (this.Any())
{
return new EnumeratorImpl(this);
}
return SpecializedCollections.EmptyEnumerator<TNode>();
}
public static bool operator ==(SyntaxList<TNode> left, SyntaxList<TNode> right)
{
return left._node == right._node;
}
public static bool operator !=(SyntaxList<TNode> left, SyntaxList<TNode> right)
{
return left._node != right._node;
}
public bool Equals(SyntaxList<TNode> other)
{
return _node == other._node;
}
public override bool Equals(object obj)
{
return obj is SyntaxList<TNode> && Equals((SyntaxList<TNode>)obj);
}
public override int GetHashCode()
{
return _node?.GetHashCode() ?? 0;
}
public static implicit operator SyntaxList<TNode>(SyntaxList<SyntaxNode> nodes)
{
return new SyntaxList<TNode>(nodes._node);
}
public static implicit operator SyntaxList<SyntaxNode>(SyntaxList<TNode> nodes)
{
return new SyntaxList<SyntaxNode>(nodes.Node);
}
/// <summary>
/// The index of the node in this list, or -1 if the node is not in the list.
/// </summary>
public int IndexOf(TNode node)
{
var index = 0;
foreach (var child in this)
{
if (object.Equals(child, node))
{
return index;
}
index++;
}
return -1;
}
public int IndexOf(Func<TNode, bool> predicate)
{
var index = 0;
foreach (var child in this)
{
if (predicate(child))
{
return index;
}
index++;
}
return -1;
}
internal int IndexOf(int rawKind)
{
var index = 0;
foreach (var child in this)
{
if (child.RawKind == rawKind)
{
return index;
}
index++;
}
return -1;
}
public int LastIndexOf(TNode node)
{
for (int i = this.Count - 1; i >= 0; i--)
{
if (object.Equals(this[i], node))
{
return i;
}
}
return -1;
}
public int LastIndexOf(Func<TNode, bool> predicate)
{
for (int i = this.Count - 1; i >= 0; i--)
{
if (predicate(this[i]))
{
return i;
}
}
return -1;
}
}
}