Skip to content

Commit

Permalink
Merge pull request swiftlang#149 from ahoppen/syntax-collections
Browse files Browse the repository at this point in the history
Make SyntaxCollections conform to BidirectionalCollection
  • Loading branch information
ahoppen committed Nov 8, 2019
2 parents d47cce9 + f8ef071 commit 6887c19
Show file tree
Hide file tree
Showing 6 changed files with 1,972 additions and 1,690 deletions.
12 changes: 10 additions & 2 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public extension SyntaxProtocol {
return SyntaxChildren(_syntaxNode)
}

/// The index of this node in a `SyntaxChildren` collection.
var index: SyntaxChildrenIndex {
return SyntaxChildrenIndex(self.data.absoluteRaw.info)
}

/// Whether or not this node is marked as `present`.
var isPresent: Bool {
return raw.isPresent
Expand Down Expand Up @@ -163,7 +168,8 @@ public extension SyntaxProtocol {
guard let parent = self.parent else {
return nil
}
for absoluteRaw in PresentRawSyntaxPreviousSiblings(_syntaxNode) {
let siblings = PresentRawSyntaxChildren(parent)
for absoluteRaw in siblings[..<self.index].reversed() {
let child = Syntax(SyntaxData(absoluteRaw, parent: parent))
if let token = child.lastToken {
return token
Expand All @@ -178,7 +184,9 @@ public extension SyntaxProtocol {
guard let parent = self.parent else {
return nil
}
for absoluteRaw in PresentRawSyntaxNextSiblings(_syntaxNode) {
let siblings = PresentRawSyntaxChildren(parent)
let nextSiblingIndex = siblings.index(after: self.index)
for absoluteRaw in siblings[nextSiblingIndex...] {
let child = Syntax(SyntaxData(absoluteRaw, parent: parent))
if let token = child.firstToken {
return token
Expand Down
Loading

0 comments on commit 6887c19

Please sign in to comment.