@@ -178,6 +178,22 @@ public protocol SyntaxProtocol: CustomStringConvertible,
178
178
static var structure : SyntaxNodeStructure { get }
179
179
}
180
180
181
+ // MARK: Access underlying data
182
+
183
+ extension SyntaxProtocol {
184
+ var data : SyntaxData {
185
+ return _syntaxNode. data
186
+ }
187
+
188
+ /// Access the raw syntax assuming the node is a Syntax.
189
+ @_spi ( RawSyntax)
190
+ public var raw : RawSyntax {
191
+ return _syntaxNode. data. raw
192
+ }
193
+ }
194
+
195
+ // MARK: Casting
196
+
181
197
// Casting functions to specialized syntax nodes.
182
198
public extension SyntaxProtocol {
183
199
/// Converts the given specialized node to this type. Returns `nil` if the
@@ -202,6 +218,8 @@ public extension SyntaxProtocol {
202
218
}
203
219
}
204
220
221
+ // MARK: Modifying
222
+
205
223
public extension SyntaxProtocol {
206
224
/// Returns a new syntax node that has the child at `keyPath` replaced by
207
225
/// `value`.
@@ -210,29 +228,21 @@ public extension SyntaxProtocol {
210
228
copy [ keyPath: keyPath] = value
211
229
return copy
212
230
}
213
- }
214
-
215
- extension SyntaxProtocol {
216
- var data : SyntaxData {
217
- return _syntaxNode. data
218
- }
219
-
220
- /// Access the raw syntax assuming the node is a Syntax.
221
- @_spi ( RawSyntax)
222
- public var raw : RawSyntax {
223
- return _syntaxNode. data. raw
224
- }
225
231
226
232
/// Return this subtree with this node as the root, ie. detach this node
227
233
/// from its parent.
228
- public var detached : Self {
234
+ var detached : Self {
229
235
// Make sure `self` (and thus the arena of `self.raw`) can’t get deallocated
230
236
// before the detached node can be created.
231
237
return withExtendedLifetime ( self ) {
232
238
return Syntax ( raw: self . raw, rawNodeArena: self . raw. arena) . cast ( Self . self)
233
239
}
234
240
}
241
+ }
242
+
243
+ // MARK: Type information
235
244
245
+ extension SyntaxProtocol {
236
246
/// The kind of the syntax node, e.g. if it is a `functionDecl`.
237
247
///
238
248
/// If you want to check for a node's kind and cast the node to that type, see
@@ -241,16 +251,19 @@ extension SyntaxProtocol {
241
251
return raw. kind
242
252
}
243
253
244
- /// The dynamic metatype of the concrete node. You almost always want to prefer this
245
- /// over `type(of: self)` because if `self` is a ``DeclSyntax`` representing a
246
- /// ``FunctionDeclSyntax``, `type(of: self)` will return ``DeclSyntax``, while
247
- /// `syntaxNodeType` looks at the dynamic kind of this node and returns
248
- /// ``FunctionDeclSyntax``.
254
+ /// The dynamic metatype of the concrete node.
255
+ ///
256
+ /// You almost always want to prefer this over `type(of: self)` because if
257
+ /// `self` is a ``DeclSyntax`` representing a ``FunctionDeclSyntax``,
258
+ /// `type(of: self)` will return ``DeclSyntax``, while `syntaxNodeType` looks
259
+ /// at the dynamic kind of this node and returns ``FunctionDeclSyntax``.
249
260
public var syntaxNodeType : SyntaxProtocol . Type {
250
261
return self . raw. kind. syntaxNodeType
251
262
}
252
263
}
253
264
265
+ // MARK: Children / parent
266
+
254
267
public extension SyntaxProtocol {
255
268
/// A sequence over the `present` children of this node.
256
269
func children( viewMode: SyntaxTreeViewMode ) -> SyntaxChildren {
@@ -262,34 +275,20 @@ public extension SyntaxProtocol {
262
275
return SyntaxChildrenIndex ( self . data. absoluteInfo)
263
276
}
264
277
265
- /// Whether the tree contained by this layout has any
266
- /// - missing nodes or
267
- /// - unexpected nodes or
268
- /// - tokens with a ``TokenDiagnostic`` of severity ``TokenDiagnostic/Severity-swift.enum/error``.
269
- var hasError : Bool {
270
- return raw. hasError
271
- }
272
-
273
- /// Whether the tree contained by this layout has any tokens with a
274
- /// ``TokenDiagnostic`` of severity `warning`.
275
- var hasWarning : Bool {
276
- return raw. recursiveFlags. contains ( . hasWarning)
277
- }
278
-
279
- /// Whether this tree contains a missing token or unexpected node.
280
- var hasSequenceExpr : Bool {
281
- return raw. recursiveFlags. contains ( . hasSequenceExpr)
282
- }
283
-
284
- var hasMaximumNestingLevelOverflow : Bool {
285
- return raw. recursiveFlags. contains ( . hasMaximumNestingLevelOverflow)
286
- }
287
-
288
278
/// The parent of this syntax node, or `nil` if this node is the root.
289
279
var parent : Syntax ? {
290
280
return data. parent. map ( Syntax . init ( _: ) )
291
281
}
292
282
283
+ /// The root of the tree in which this node resides.
284
+ var root : Syntax {
285
+ var this = _syntaxNode
286
+ while let parent = this. parent {
287
+ this = parent
288
+ }
289
+ return this
290
+ }
291
+
293
292
/// Whether or not this node has a parent.
294
293
var hasParent : Bool {
295
294
return parent != nil
@@ -309,7 +308,11 @@ public extension SyntaxProtocol {
309
308
var previousToken : TokenSyntax ? {
310
309
return self . previousToken ( viewMode: . sourceAccurate)
311
310
}
311
+ }
312
+
313
+ // MARK: Accessing tokens
312
314
315
+ public extension SyntaxProtocol {
313
316
/// Recursively walks through the tree to find the token semantically before
314
317
/// this node.
315
318
func previousToken( viewMode: SyntaxTreeViewMode ) -> TokenSyntax ? {
@@ -392,6 +395,11 @@ public extension SyntaxProtocol {
392
395
return nil
393
396
}
394
397
398
+ /// Sequence of tokens that are part of this Syntax node.
399
+ func tokens( viewMode: SyntaxTreeViewMode ) -> TokenSequence {
400
+ return TokenSequence ( _syntaxNode, viewMode: viewMode)
401
+ }
402
+
395
403
/// Find the syntax token at the given absolute position within this
396
404
/// syntax node or any of its children.
397
405
func token( at position: AbsolutePosition ) -> TokenSyntax ? {
@@ -414,7 +422,38 @@ public extension SyntaxProtocol {
414
422
415
423
fatalError ( " Children of syntax node do not cover all positions in it " )
416
424
}
425
+ }
426
+
427
+ // MARK: Recursive flags
428
+
429
+ public extension SyntaxProtocol {
430
+ /// Whether the tree contained by this layout has any
431
+ /// - missing nodes or
432
+ /// - unexpected nodes or
433
+ /// - tokens with a ``TokenDiagnostic`` of severity ``TokenDiagnostic/Severity-swift.enum/error``.
434
+ var hasError : Bool {
435
+ return raw. hasError
436
+ }
417
437
438
+ /// Whether the tree contained by this layout has any tokens with a
439
+ /// ``TokenDiagnostic`` of severity `warning`.
440
+ var hasWarning : Bool {
441
+ return raw. recursiveFlags. contains ( . hasWarning)
442
+ }
443
+
444
+ /// Whether this tree contains a missing token or unexpected node.
445
+ var hasSequenceExpr : Bool {
446
+ return raw. recursiveFlags. contains ( . hasSequenceExpr)
447
+ }
448
+
449
+ var hasMaximumNestingLevelOverflow : Bool {
450
+ return raw. recursiveFlags. contains ( . hasMaximumNestingLevelOverflow)
451
+ }
452
+ }
453
+
454
+ // MARK: Position / length / range
455
+
456
+ public extension SyntaxProtocol {
418
457
/// The absolute position of the starting point of this node. If the first token
419
458
/// is with leading trivia, the position points to the start of the leading
420
459
/// trivia.
@@ -454,6 +493,20 @@ public extension SyntaxProtocol {
454
493
return raw. contentLength
455
494
}
456
495
496
+ /// The length of this node including all of its trivia.
497
+ var totalLength : SourceLength {
498
+ return raw. totalLength
499
+ }
500
+
501
+ /// The textual byte length of this node exluding leading and trailing trivia.
502
+ var byteSizeAfterTrimmingTrivia : Int {
503
+ return contentLength. utf8Length
504
+ }
505
+ }
506
+
507
+ // MARK: Trivia
508
+
509
+ public extension SyntaxProtocol {
457
510
/// The leading trivia of this syntax node. Leading trivia is attached to
458
511
/// the first token syntax contained by this node. Without such token, this
459
512
/// property will return nil.
@@ -499,43 +552,10 @@ public extension SyntaxProtocol {
499
552
var trailingTriviaLength : SourceLength {
500
553
return raw. trailingTriviaLength
501
554
}
502
-
503
- /// The length of this node including all of its trivia.
504
- var totalLength : SourceLength {
505
- return raw. totalLength
506
- }
507
-
508
- /// When isImplicit is true, the syntax node doesn't include any
509
- /// underlying tokens, e.g. an empty CodeBlockItemList.
510
- var isImplicit : Bool {
511
- return raw. isEmpty
512
- }
513
-
514
- /// The textual byte length of this node exluding leading and trailing trivia.
515
- var byteSizeAfterTrimmingTrivia : Int {
516
- return contentLength. utf8Length
517
- }
518
-
519
- /// The root of the tree in which this node resides.
520
- var root : Syntax {
521
- var this = _syntaxNode
522
- while let parent = this. parent {
523
- this = parent
524
- }
525
- return this
526
- }
527
-
528
- /// Sequence of tokens that are part of this Syntax node.
529
- func tokens( viewMode: SyntaxTreeViewMode ) -> TokenSequence {
530
- return TokenSequence ( _syntaxNode, viewMode: viewMode)
531
- }
532
-
533
- /// Returns a value representing the unique identity of the node.
534
- var id : SyntaxIdentifier {
535
- return data. nodeId
536
- }
537
555
}
538
556
557
+ // MARK: Content
558
+
539
559
/// Provides the source-accurate text for a node
540
560
public extension SyntaxProtocol {
541
561
/// A source-accurate description of this node.
@@ -599,6 +619,23 @@ public extension SyntaxProtocol {
599
619
}
600
620
}
601
621
622
+ // MARK: Miscellaneous
623
+
624
+ public extension SyntaxProtocol {
625
+ /// When isImplicit is true, the syntax node doesn't include any
626
+ /// underlying tokens, e.g. an empty CodeBlockItemList.
627
+ var isImplicit : Bool {
628
+ return raw. isEmpty
629
+ }
630
+
631
+ /// Returns a value representing the unique identity of the node.
632
+ var id : SyntaxIdentifier {
633
+ return data. nodeId
634
+ }
635
+ }
636
+
637
+ // MARK: Debug description
638
+
602
639
/// Provides debug descriptions for a node
603
640
public extension SyntaxProtocol {
604
641
/// A simple description of this node (ie. its type).
0 commit comments