Skip to content

Get rid of XXXSyntax.Cursor type #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Sources/SwiftSyntax/Raw/RawSyntaxLayoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,6 @@ struct RawSyntaxLayoutView {
}
}

/// Returns the child at the provided cursor in the layout.
/// - Parameter index: The index of the child you're accessing.
/// - Returns: The child at the provided index.
subscript<CursorType: RawRepresentable>(_ index: CursorType) -> RawSyntax?
where CursorType.RawValue == Int {
return children[index.rawValue]
}

/// The number of children, `present` or `missing`, in this node.
var numberOfChildren: Int {
return children.count
Expand Down
32 changes: 0 additions & 32 deletions Sources/SwiftSyntax/SyntaxData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,6 @@ struct SyntaxData {
return SyntaxData(AbsoluteRawSyntax(raw: raw!, info: info), parent: parent)
}

/// Returns the child data at the provided cursor in this data's layout.
/// - Note: This has O(n) performance, prefer using a proper Sequence type
/// if applicable, instead of this.
/// - Note: This function traps if the cursor is out of the bounds of the
/// data's layout.
///
/// - Parameter cursor: The cursor to create and cache.
/// - Parameter parent: The parent to associate the child with. This is
/// normally the Syntax node that this `SyntaxData` belongs to.
/// - Returns: The child's data at the provided cursor.
func child<CursorType: RawRepresentable>(
at cursor: CursorType, parent: Syntax) -> SyntaxData?
where CursorType.RawValue == Int {
return child(at: cursor.rawValue, parent: parent)
}

/// Creates a copy of `self` and recursively creates `SyntaxData` nodes up to
/// the root.
/// - parameter newRaw: The new RawSyntax that will back the new `Data`
Expand Down Expand Up @@ -348,22 +332,6 @@ struct SyntaxData {
return replacingSelf(newRaw)
}

/// Creates a copy of `self` with the child at the provided cursor replaced
/// with a new SyntaxData containing the raw syntax provided.
///
/// - Parameters:
/// - child: The raw syntax for the new child to replace.
/// - cursor: A cursor that points to the index of the child you wish to
/// replace
/// - Returns: The new root node created by this operation, and the new child
/// syntax data.
/// - SeeAlso: replacingSelf(_:)
func replacingChild<CursorType: RawRepresentable>(_ child: RawSyntax?,
at cursor: CursorType) -> SyntaxData
where CursorType.RawValue == Int {
return replacingChild(child, at: cursor.rawValue)
}

func withLeadingTrivia(_ leadingTrivia: Trivia) -> SyntaxData {
if let raw = raw.withLeadingTrivia(leadingTrivia) {
return replacingSelf(raw)
Expand Down
24 changes: 5 additions & 19 deletions Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,6 @@ nodes whose base kind are that specified kind.
/// ${line}
% end
public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
% # ======
% # Cursor
% # ======
%
% if node.children:
enum Cursor: Int {
% for child in node.children:
case ${child.swift_name}
% end
}
% end

% # ==============
% # Initialization
% # ==============
Expand Down Expand Up @@ -111,7 +99,7 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self)
}
% for child in node.children:
% for (idx, child) in enumerate(node.children):
% child_node = NODE_MAP.get(child.syntax_kind)
%
% # ===================
Expand All @@ -128,8 +116,7 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
% end
public var ${child.swift_name}: ${ret_type} {
get {
let childData = data.child(at: Cursor.${child.swift_name},
parent: Syntax(self))
let childData = data.child(at: ${idx}, parent: Syntax(self))
% if child.is_optional:
if childData == nil { return nil }
% end
Expand Down Expand Up @@ -160,14 +147,13 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
/// appended to its `${child.swift_name}` collection.
public func add${child_elt}(_ element: ${child_elt_type}) -> ${node.name} {
var collection: RawSyntax
if let col = raw.layoutView![Cursor.${child.swift_name}] {
if let col = raw.layoutView!.children[${idx}] {
collection = col.layoutView!.appending(element.raw, arena: .default)
} else {
collection = RawSyntax.makeLayout(kind: SyntaxKind.${child_node.swift_syntax_kind},
from: [element.raw], arena: .default)
}
let newData = data.replacingChild(collection,
at: Cursor.${child.swift_name})
let newData = data.replacingChild(collection, at: ${idx})
return ${node.name}(newData)
}
% end
Expand All @@ -185,7 +171,7 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
% else:
let raw = newChild?.raw ?? ${make_missing_swift_child(child)}
% end
let newData = data.replacingChild(raw, at: Cursor.${child.swift_name})
let newData = data.replacingChild(raw, at: ${idx})
return ${node.name}(newData)
}
% end
Expand Down
Loading