Skip to content

Detach node when accessing .trimmed #2690

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
Jun 20, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions Release Notes/601.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

## API Behavior Changes

- `SyntaxProtocol.trimmed` detaches the node
- Description: Getting a trimmed version of a node detaches it from its parent. Having the trimmed node be attached to a parent was not intuitive because eg. printing the parent node would have the trimmed trivia missing, most likely forming invalid Swift code.
- Pull Request: https://github.com/apple/swift-syntax/pull/2689

## Deprecations

- `IncrementalEdit` deprecated in favor of `SourceEdit`
Expand Down
4 changes: 3 additions & 1 deletion Sources/SwiftSyntax/SyntaxProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,11 @@ extension SyntaxProtocol {

/// 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.
///
/// The trimmed node is detached from its parent.
public var trimmed: Self {
// TODO: Should only need one new node here
return self.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
return self.detached.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
}

/// A copy of this node with pieces that match `matching` trimmed from the
Expand Down