Skip to content

Commit 574ce2e

Browse files
committed
Add a .trimmed to remove leading/trailing trivia
It's very common in macros to want the node without any trivia, add `withoutTrivia` but under a name that makes it clear it isn't removing *all* trivia.
1 parent 41ce85a commit 574ce2e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Sources/SwiftSyntax/Syntax.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,18 @@ public extension SyntaxProtocol {
565565
data.raw.write(to: &target)
566566
}
567567

568+
/// A copy of this node without the leading trivia of the first token in the
569+
/// node and the trailing trivia of the last token in the node
570+
var trimmed: Self {
571+
return self.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
572+
}
573+
568574
/// The description of this node without the leading trivia of the first token
569575
/// in the node and the trailing trivia of the last token in the node
570576
var trimmedDescription: String {
571-
// TODO: We shouldn't need to create two intermediate nodes from the `with`
572-
// functions just to get the description without trivia.
573-
return self.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description
577+
// TODO: We shouldn't need to create to copies just to get the description
578+
// without trivia.
579+
return self.trimmed.description
574580
}
575581
}
576582

0 commit comments

Comments
 (0)