Skip to content

Commit 5747f24

Browse files
committed
Get rid of XXXSyntax.Cursor type
They are internal types which served as layout 'subscript' keys e.g. `layout[Cursor.propertyName]`. That looks nice, but in reality, the name is usually written very close: var propertyName: PropertyType { get { layout[Cursor.propertyName] } } So it didn't give us much benefit. In the contrary, it hid the "index" information which might be useful when investigating issues. So just get rid of them for now. Since they are just internal types. So no external API changes.
1 parent 64d5004 commit 5747f24

File tree

9 files changed

+2863
-6313
lines changed

9 files changed

+2863
-6313
lines changed

Sources/SwiftSyntax/Raw/RawSyntaxLayoutView.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,6 @@ struct RawSyntaxLayoutView {
149149
}
150150
}
151151

152-
/// Returns the child at the provided cursor in the layout.
153-
/// - Parameter index: The index of the child you're accessing.
154-
/// - Returns: The child at the provided index.
155-
subscript<CursorType: RawRepresentable>(_ index: CursorType) -> RawSyntax?
156-
where CursorType.RawValue == Int {
157-
return children[index.rawValue]
158-
}
159-
160152
/// The number of children, `present` or `missing`, in this node.
161153
var numberOfChildren: Int {
162154
return children.count

Sources/SwiftSyntax/SyntaxData.swift

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,6 @@ struct SyntaxData {
298298
return SyntaxData(AbsoluteRawSyntax(raw: raw!, info: info), parent: parent)
299299
}
300300

301-
/// Returns the child data at the provided cursor in this data's layout.
302-
/// - Note: This has O(n) performance, prefer using a proper Sequence type
303-
/// if applicable, instead of this.
304-
/// - Note: This function traps if the cursor is out of the bounds of the
305-
/// data's layout.
306-
///
307-
/// - Parameter cursor: The cursor to create and cache.
308-
/// - Parameter parent: The parent to associate the child with. This is
309-
/// normally the Syntax node that this `SyntaxData` belongs to.
310-
/// - Returns: The child's data at the provided cursor.
311-
func child<CursorType: RawRepresentable>(
312-
at cursor: CursorType, parent: Syntax) -> SyntaxData?
313-
where CursorType.RawValue == Int {
314-
return child(at: cursor.rawValue, parent: parent)
315-
}
316-
317301
/// Creates a copy of `self` and recursively creates `SyntaxData` nodes up to
318302
/// the root.
319303
/// - parameter newRaw: The new RawSyntax that will back the new `Data`
@@ -348,22 +332,6 @@ struct SyntaxData {
348332
return replacingSelf(newRaw)
349333
}
350334

351-
/// Creates a copy of `self` with the child at the provided cursor replaced
352-
/// with a new SyntaxData containing the raw syntax provided.
353-
///
354-
/// - Parameters:
355-
/// - child: The raw syntax for the new child to replace.
356-
/// - cursor: A cursor that points to the index of the child you wish to
357-
/// replace
358-
/// - Returns: The new root node created by this operation, and the new child
359-
/// syntax data.
360-
/// - SeeAlso: replacingSelf(_:)
361-
func replacingChild<CursorType: RawRepresentable>(_ child: RawSyntax?,
362-
at cursor: CursorType) -> SyntaxData
363-
where CursorType.RawValue == Int {
364-
return replacingChild(child, at: cursor.rawValue)
365-
}
366-
367335
func withLeadingTrivia(_ leadingTrivia: Trivia) -> SyntaxData {
368336
if let raw = raw.withLeadingTrivia(leadingTrivia) {
369337
return replacingSelf(raw)

Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ nodes whose base kind are that specified kind.
4747
/// ${line}
4848
% end
4949
public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
50-
% # ======
51-
% # Cursor
52-
% # ======
53-
%
54-
% if node.children:
55-
enum Cursor: Int {
56-
% for child in node.children:
57-
case ${child.swift_name}
58-
% end
59-
}
60-
% end
61-
6250
% # ==============
6351
% # Initialization
6452
% # ==============
@@ -111,7 +99,7 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
11199
public var syntaxNodeType: SyntaxProtocol.Type {
112100
return Swift.type(of: self)
113101
}
114-
% for child in node.children:
102+
% for (idx, child) in enumerate(node.children):
115103
% child_node = NODE_MAP.get(child.syntax_kind)
116104
%
117105
% # ===================
@@ -128,8 +116,7 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
128116
% end
129117
public var ${child.swift_name}: ${ret_type} {
130118
get {
131-
let childData = data.child(at: Cursor.${child.swift_name},
132-
parent: Syntax(self))
119+
let childData = data.child(at: ${idx}, parent: Syntax(self))
133120
% if child.is_optional:
134121
if childData == nil { return nil }
135122
% end
@@ -160,14 +147,13 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
160147
/// appended to its `${child.swift_name}` collection.
161148
public func add${child_elt}(_ element: ${child_elt_type}) -> ${node.name} {
162149
var collection: RawSyntax
163-
if let col = raw.layoutView![Cursor.${child.swift_name}] {
150+
if let col = raw.layoutView!.children[${idx}] {
164151
collection = col.layoutView!.appending(element.raw, arena: .default)
165152
} else {
166153
collection = RawSyntax.makeLayout(kind: SyntaxKind.${child_node.swift_syntax_kind},
167154
from: [element.raw], arena: .default)
168155
}
169-
let newData = data.replacingChild(collection,
170-
at: Cursor.${child.swift_name})
156+
let newData = data.replacingChild(collection, at: ${idx})
171157
return ${node.name}(newData)
172158
}
173159
% end
@@ -185,7 +171,7 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
185171
% else:
186172
let raw = newChild?.raw ?? ${make_missing_swift_child(child)}
187173
% end
188-
let newData = data.replacingChild(raw, at: Cursor.${child.swift_name})
174+
let newData = data.replacingChild(raw, at: ${idx})
189175
return ${node.name}(newData)
190176
}
191177
% end

0 commit comments

Comments
 (0)