Skip to content

Commit d4fb767

Browse files
committed
Move ancestorOrSelf to SwiftSyntax module
1 parent 9f2fab0 commit d4fb767

File tree

3 files changed

+13
-27
lines changed

3 files changed

+13
-27
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -676,17 +676,3 @@ fileprivate extension TokenSyntax {
676676
}
677677
}
678678
}
679-
680-
fileprivate extension SyntaxProtocol {
681-
/// Returns this node or the first ancestor that satisfies `condition`.
682-
func ancestorOrSelf<T>(mapping map: (Syntax) -> T?) -> T? {
683-
var walk: Syntax? = Syntax(self)
684-
while let unwrappedParent = walk {
685-
if let mapped = map(unwrappedParent) {
686-
return mapped
687-
}
688-
walk = unwrappedParent.parent
689-
}
690-
return nil
691-
}
692-
}

Sources/SwiftParserDiagnostics/SyntaxExtensions.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,6 @@ extension SyntaxProtocol {
119119
}
120120
}
121121

122-
/// Returns this node or the first ancestor that satisfies `condition`.
123-
func ancestorOrSelf<T>(mapping map: (Syntax) -> T?) -> T? {
124-
var walk: Syntax? = Syntax(self)
125-
while let unwrappedParent = walk {
126-
if let mapped = map(unwrappedParent) {
127-
return mapped
128-
}
129-
walk = unwrappedParent.parent
130-
}
131-
return nil
132-
}
133-
134122
/// Returns `true` if the next token's leading trivia should be made leading trivia
135123
/// of this mode, when it is switched from being missing to present.
136124
var shouldBeInsertedAfterNextTokenTrivia: Bool {

Sources/SwiftSyntax/SyntaxProtocol.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ extension SyntaxProtocol {
186186
}
187187
}
188188

189-
// MARK: Children / parent
189+
// MARK: Children / parent / ancestor
190190

191191
extension SyntaxProtocol {
192192
/// A sequence over the children of this node.
@@ -238,6 +238,18 @@ extension SyntaxProtocol {
238238
public var previousToken: TokenSyntax? {
239239
return self.previousToken(viewMode: .sourceAccurate)
240240
}
241+
242+
/// Returns this node or the first ancestor that satisfies `condition`.
243+
public func ancestorOrSelf<T>(mapping map: (Syntax) -> T?) -> T? {
244+
var walk: Syntax? = Syntax(self)
245+
while let unwrappedParent = walk {
246+
if let mapped = map(unwrappedParent) {
247+
return mapped
248+
}
249+
walk = unwrappedParent.parent
250+
}
251+
return nil
252+
}
241253
}
242254

243255
// MARK: Accessing tokens

0 commit comments

Comments
 (0)