mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
SwiftSyntax: allow any Syntax nodes to access their leading/trailing trivia. (#15278)
Although libSyntax is designed in a way that trivia is attached to tokens, we shouldn't restrict clients to access trivia only from a token. An example can be doc-comment, which conceptually attaches to a declaration rather than the start token of a declaration, like at-attributes.
This commit is contained in:
@@ -214,22 +214,40 @@ extension RawSyntax {
|
||||
}
|
||||
}
|
||||
|
||||
func accumulateLeadingTrivia(_ pos: AbsolutePosition) -> Bool {
|
||||
var leadingTrivia: Trivia? {
|
||||
switch self {
|
||||
case .node(_, let layout, _):
|
||||
for child in layout {
|
||||
guard let child = child else { continue }
|
||||
if child.accumulateLeadingTrivia(pos) {
|
||||
return true
|
||||
}
|
||||
guard let result = child.leadingTrivia else { continue }
|
||||
return result
|
||||
}
|
||||
return false
|
||||
return nil
|
||||
case let .token(_, leadingTrivia, _, presence):
|
||||
guard case .present = presence else { return false }
|
||||
for piece in leadingTrivia {
|
||||
piece.accumulateAbsolutePosition(pos)
|
||||
guard case .present = presence else { return nil }
|
||||
return leadingTrivia
|
||||
}
|
||||
}
|
||||
|
||||
var trailingTrivia: Trivia? {
|
||||
switch self {
|
||||
case .node(_, let layout, _):
|
||||
for child in layout.reversed() {
|
||||
guard let child = child else { continue }
|
||||
guard let result = child.trailingTrivia else { continue }
|
||||
return result
|
||||
}
|
||||
return true
|
||||
return nil
|
||||
case let .token(_, _, trailingTrivia, presence):
|
||||
guard case .present = presence else { return nil }
|
||||
return trailingTrivia
|
||||
}
|
||||
}
|
||||
|
||||
func accumulateLeadingTrivia(_ pos: AbsolutePosition) {
|
||||
guard let trivia = leadingTrivia else { return }
|
||||
for piece in trivia {
|
||||
piece.accumulateAbsolutePosition(pos)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user