Skip to content

Commit 6887c19

Browse files
authored
Merge pull request #149 from ahoppen/syntax-collections
Make SyntaxCollections conform to BidirectionalCollection
2 parents d47cce9 + f8ef071 commit 6887c19

File tree

6 files changed

+1972
-1690
lines changed

6 files changed

+1972
-1690
lines changed

Sources/SwiftSyntax/Syntax.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ public extension SyntaxProtocol {
114114
return SyntaxChildren(_syntaxNode)
115115
}
116116

117+
/// The index of this node in a `SyntaxChildren` collection.
118+
var index: SyntaxChildrenIndex {
119+
return SyntaxChildrenIndex(self.data.absoluteRaw.info)
120+
}
121+
117122
/// Whether or not this node is marked as `present`.
118123
var isPresent: Bool {
119124
return raw.isPresent
@@ -163,7 +168,8 @@ public extension SyntaxProtocol {
163168
guard let parent = self.parent else {
164169
return nil
165170
}
166-
for absoluteRaw in PresentRawSyntaxPreviousSiblings(_syntaxNode) {
171+
let siblings = PresentRawSyntaxChildren(parent)
172+
for absoluteRaw in siblings[..<self.index].reversed() {
167173
let child = Syntax(SyntaxData(absoluteRaw, parent: parent))
168174
if let token = child.lastToken {
169175
return token
@@ -178,7 +184,9 @@ public extension SyntaxProtocol {
178184
guard let parent = self.parent else {
179185
return nil
180186
}
181-
for absoluteRaw in PresentRawSyntaxNextSiblings(_syntaxNode) {
187+
let siblings = PresentRawSyntaxChildren(parent)
188+
let nextSiblingIndex = siblings.index(after: self.index)
189+
for absoluteRaw in siblings[nextSiblingIndex...] {
182190
let child = Syntax(SyntaxData(absoluteRaw, parent: parent))
183191
if let token = child.firstToken {
184192
return token

0 commit comments

Comments
 (0)