Skip to content

Commit 5c8acc3

Browse files
committed
Remove child(at:) and hasChild methods from RawSyntaxLayoutView
1 parent 66f2fc7 commit 5c8acc3

File tree

6 files changed

+11
-26
lines changed

6 files changed

+11
-26
lines changed

Sources/SwiftSyntax/Raw/RawSyntaxLayoutView.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,12 @@ struct RawSyntaxLayoutView {
149149
}
150150
}
151151

152-
func child(at index: Int) -> RawSyntax? {
153-
guard hasChild(at: index) else { return nil }
154-
return children[index]
155-
}
156-
157-
func hasChild(at index: Int) -> Bool {
158-
children[index] != nil
159-
}
160-
161152
/// Returns the child at the provided cursor in the layout.
162153
/// - Parameter index: The index of the child you're accessing.
163154
/// - Returns: The child at the provided index.
164155
subscript<CursorType: RawRepresentable>(_ index: CursorType) -> RawSyntax?
165156
where CursorType.RawValue == Int {
166-
return child(at: index.rawValue)
157+
return children[index.rawValue]
167158
}
168159

169160
/// The number of children, `present` or `missing`, in this node.

Sources/SwiftSyntax/SyntaxChildren.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ struct RawSyntaxChildren: BidirectionalCollection {
172172
}
173173

174174
func index(after index: SyntaxChildrenIndex) -> SyntaxChildrenIndex {
175-
let node = parent.layoutView!.child(at: Int(index.data!.indexInParent))
175+
let node = parent.layoutView!.children[Int(index.data!.indexInParent)]
176176
return self.index(index, advancedBy: node)
177177
}
178178

179179
func index(before index: SyntaxChildrenIndex) -> SyntaxChildrenIndex {
180180
if let index = index.data {
181181
// We are reversing a non-end index.
182-
let previousNode = parent.layoutView!.child(at: Int(index.indexInParent - 1))
182+
let previousNode = parent.layoutView!.children[Int(index.indexInParent - 1)]
183183
let previousNodeLength = UInt32(previousNode?.totalLength.utf8Length ?? 0)
184184
let reversedIndexInTree = index.indexInTree.reversedBy(previousNode)
185185
return SyntaxChildrenIndex(offset: index.offset - previousNodeLength,
@@ -231,7 +231,7 @@ struct RawSyntaxChildren: BidirectionalCollection {
231231
// Accessing the end index is undefined. So we can assume a non-end index.
232232
let index = index.data!
233233

234-
let child = parent.layoutView!.child(at: Int(index.indexInParent))
234+
let child = parent.layoutView!.children[Int(index.indexInParent)]
235235
let info = AbsoluteSyntaxInfo(index: index, rootId: rootId)
236236
return (child, info)
237237
}

Sources/SwiftSyntax/SyntaxClassifier.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ fileprivate struct AbsoluteNode {
118118
var firstChild: AbsoluteNode? {
119119
guard let layoutView = raw.layoutView else { return nil }
120120
var curPos = position.advancedToFirstChild()
121-
for i in 0..<layoutView.children.count {
122-
let childOpt = layoutView.child(at: i)
121+
for childOpt in layoutView.children {
123122
if let child = childOpt, viewMode.shouldTraverse(node: child) {
124123
return AbsoluteNode(raw: child, position: curPos, parent: self)
125124
}
@@ -130,9 +129,7 @@ fileprivate struct AbsoluteNode {
130129

131130
func nextSibling(parent: AbsoluteNode) -> AbsoluteNode? {
132131
var curPos = position.advancedBySibling(raw)
133-
let parentLayoutView = parent.raw.layoutView!
134-
for i in Int(position.indexInParent+1) ..< parentLayoutView.children.count {
135-
let siblingOpt = parent.raw.layoutView!.child(at: i)
132+
for siblingOpt in parent.raw.layoutView!.children.dropFirst(Int(position.indexInParent + 1)) {
136133
if let sibling = siblingOpt, viewMode.shouldTraverse(node: sibling) {
137134
return AbsoluteNode(raw: sibling, position: curPos, parent: parent)
138135
}

Sources/SwiftSyntax/SyntaxData.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ struct AbsoluteRawSyntax {
147147
func firstChild(viewMode: SyntaxTreeViewMode) -> AbsoluteRawSyntax? {
148148
guard let layoutView = raw.layoutView else { return nil }
149149
var curInfo = info.advancedToFirstChild()
150-
for i in 0..<layoutView.children.count {
151-
let childOpt = layoutView.child(at: i)
150+
for childOpt in layoutView.children {
152151
if let child = childOpt, viewMode.shouldTraverse(node: child) {
153152
return AbsoluteRawSyntax(raw: child, info: curInfo)
154153
}
@@ -160,9 +159,7 @@ struct AbsoluteRawSyntax {
160159
/// Returns next `present` sibling.
161160
func nextSibling(parent: AbsoluteRawSyntax, viewMode: SyntaxTreeViewMode) -> AbsoluteRawSyntax? {
162161
var curInfo = info.advancedBySibling(raw)
163-
let parentLayoutView = parent.raw.layoutView!
164-
for i in Int(info.indexInParent+1) ..< parentLayoutView.children.count {
165-
let siblingOpt = parentLayoutView.child(at: i)
162+
for siblingOpt in parent.raw.layoutView!.children.dropFirst(Int(info.indexInParent+1)) {
166163
if let sibling = siblingOpt, viewMode.shouldTraverse(node: sibling) {
167164
return AbsoluteRawSyntax(raw: sibling, info: curInfo)
168165
}
@@ -294,7 +291,7 @@ struct SyntaxData {
294291
/// normally the Syntax node that this `SyntaxData` belongs to.
295292
/// - Returns: The child's data at the provided index.
296293
func child(at index: Int, parent: Syntax) -> SyntaxData? {
297-
if !raw.layoutView!.hasChild(at: index) { return nil }
294+
if raw.layoutView!.children[index] == nil { return nil }
298295
var iter = RawSyntaxChildren(absoluteRaw).makeIterator()
299296
for _ in 0..<index { _ = iter.next() }
300297
let (raw, info) = iter.next()!

Sources/SwiftSyntax/SyntaxRewriter.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ open class SyntaxRewriter {
231231
newLayout = ContiguousArray<RawSyntax?>()
232232
newLayout!.reserveCapacity(node.raw.layoutView!.children.count)
233233
for j in 0..<childIndex {
234-
newLayout!.append(node.raw.layoutView!.child(at: j))
234+
newLayout!.append(node.raw.layoutView!.children[j])
235235
}
236236
}
237237

Sources/SwiftSyntax/gyb_generated/SyntaxRewriter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5692,7 +5692,7 @@ open class SyntaxRewriter {
56925692
newLayout = ContiguousArray<RawSyntax?>()
56935693
newLayout!.reserveCapacity(node.raw.layoutView!.children.count)
56945694
for j in 0..<childIndex {
5695-
newLayout!.append(node.raw.layoutView!.child(at: j))
5695+
newLayout!.append(node.raw.layoutView!.children[j])
56965696
}
56975697
}
56985698

0 commit comments

Comments
 (0)