Skip to content

Commit 7f7bca7

Browse files
committed
Re-arrange properties in Syntax.swift
This gives the file a little order and makes it easier to spot naming inconsistencies.
1 parent b46348a commit 7f7bca7

File tree

1 file changed

+113
-76
lines changed

1 file changed

+113
-76
lines changed

Sources/SwiftSyntax/Syntax.swift

Lines changed: 113 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,22 @@ public protocol SyntaxProtocol: CustomStringConvertible,
178178
static var structure: SyntaxNodeStructure { get }
179179
}
180180

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+
181197
// Casting functions to specialized syntax nodes.
182198
public extension SyntaxProtocol {
183199
/// Converts the given specialized node to this type. Returns `nil` if the
@@ -202,6 +218,8 @@ public extension SyntaxProtocol {
202218
}
203219
}
204220

221+
// MARK: Modifying
222+
205223
public extension SyntaxProtocol {
206224
/// Returns a new syntax node that has the child at `keyPath` replaced by
207225
/// `value`.
@@ -210,29 +228,21 @@ public extension SyntaxProtocol {
210228
copy[keyPath: keyPath] = value
211229
return copy
212230
}
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-
}
225231

226232
/// Return this subtree with this node as the root, ie. detach this node
227233
/// from its parent.
228-
public var detached: Self {
234+
var detached: Self {
229235
// Make sure `self` (and thus the arena of `self.raw`) can’t get deallocated
230236
// before the detached node can be created.
231237
return withExtendedLifetime(self) {
232238
return Syntax(raw: self.raw, rawNodeArena: self.raw.arena).cast(Self.self)
233239
}
234240
}
241+
}
242+
243+
// MARK: Type information
235244

245+
extension SyntaxProtocol {
236246
/// The kind of the syntax node, e.g. if it is a `functionDecl`.
237247
///
238248
/// If you want to check for a node's kind and cast the node to that type, see
@@ -241,16 +251,19 @@ extension SyntaxProtocol {
241251
return raw.kind
242252
}
243253

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``.
249260
public var syntaxNodeType: SyntaxProtocol.Type {
250261
return self.raw.kind.syntaxNodeType
251262
}
252263
}
253264

265+
// MARK: Children / parent
266+
254267
public extension SyntaxProtocol {
255268
/// A sequence over the `present` children of this node.
256269
func children(viewMode: SyntaxTreeViewMode) -> SyntaxChildren {
@@ -262,34 +275,20 @@ public extension SyntaxProtocol {
262275
return SyntaxChildrenIndex(self.data.absoluteInfo)
263276
}
264277

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-
288278
/// The parent of this syntax node, or `nil` if this node is the root.
289279
var parent: Syntax? {
290280
return data.parent.map(Syntax.init(_:))
291281
}
292282

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+
293292
/// Whether or not this node has a parent.
294293
var hasParent: Bool {
295294
return parent != nil
@@ -309,7 +308,11 @@ public extension SyntaxProtocol {
309308
var previousToken: TokenSyntax? {
310309
return self.previousToken(viewMode: .sourceAccurate)
311310
}
311+
}
312+
313+
// MARK: Accessing tokens
312314

315+
public extension SyntaxProtocol {
313316
/// Recursively walks through the tree to find the token semantically before
314317
/// this node.
315318
func previousToken(viewMode: SyntaxTreeViewMode) -> TokenSyntax? {
@@ -392,6 +395,11 @@ public extension SyntaxProtocol {
392395
return nil
393396
}
394397

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+
395403
/// Find the syntax token at the given absolute position within this
396404
/// syntax node or any of its children.
397405
func token(at position: AbsolutePosition) -> TokenSyntax? {
@@ -414,7 +422,38 @@ public extension SyntaxProtocol {
414422

415423
fatalError("Children of syntax node do not cover all positions in it")
416424
}
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+
}
417437

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 {
418457
/// The absolute position of the starting point of this node. If the first token
419458
/// is with leading trivia, the position points to the start of the leading
420459
/// trivia.
@@ -454,6 +493,20 @@ public extension SyntaxProtocol {
454493
return raw.contentLength
455494
}
456495

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 {
457510
/// The leading trivia of this syntax node. Leading trivia is attached to
458511
/// the first token syntax contained by this node. Without such token, this
459512
/// property will return nil.
@@ -499,43 +552,10 @@ public extension SyntaxProtocol {
499552
var trailingTriviaLength: SourceLength {
500553
return raw.trailingTriviaLength
501554
}
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-
}
537555
}
538556

557+
// MARK: Content
558+
539559
/// Provides the source-accurate text for a node
540560
public extension SyntaxProtocol {
541561
/// A source-accurate description of this node.
@@ -599,6 +619,23 @@ public extension SyntaxProtocol {
599619
}
600620
}
601621

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+
602639
/// Provides debug descriptions for a node
603640
public extension SyntaxProtocol {
604641
/// A simple description of this node (ie. its type).

0 commit comments

Comments
 (0)