Skip to content

Add a .trimmed to remove leading/trailing trivia #1308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,19 @@ public extension SyntaxProtocol {
data.raw.write(to: &target)
}

/// A copy of this node without the leading trivia of the first token in the
/// node and the trailing trivia of the last token in the node.
var trimmed: Self {
// TODO: Should only need one new node here
return self.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
}

/// The description of this node without the leading trivia of the first token
/// in the node and the trailing trivia of the last token in the node
/// in the node and the trailing trivia of the last token in the node.
var trimmedDescription: String {
// TODO: We shouldn't need to create two intermediate nodes from the `with`
// functions just to get the description without trivia.
return self.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description
// TODO: We shouldn't need to create to copies just to get the description
// without trivia.
return self.trimmed.description
}
}

Expand Down