Skip to content

Commit 27d0697

Browse files
authored
Merge pull request #1308 from bnbarham/trimmed-trivia
Add a `.trimmed` to remove leading/trailing trivia
2 parents 41ce85a + 281b5d9 commit 27d0697

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Sources/SwiftSyntax/Syntax.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,19 @@ 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+
// TODO: Should only need one new node here
572+
return self.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
573+
}
574+
568575
/// The description of this node without the leading trivia of the first token
569-
/// in the node and the trailing trivia of the last token in the node
576+
/// in the node and the trailing trivia of the last token in the node.
570577
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
578+
// TODO: We shouldn't need to create to copies just to get the description
579+
// without trivia.
580+
return self.trimmed.description
574581
}
575582
}
576583

0 commit comments

Comments
 (0)