Skip to content

Commit 0d4c8a4

Browse files
authored
Merge pull request #2690 from ahoppen/detach-node-on-trim
Detach node when accessing .trimmed
2 parents a2c99c0 + d2aee8c commit 0d4c8a4

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Release Notes/601.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
## API Behavior Changes
1111

12+
- `SyntaxProtocol.trimmed` detaches the node
13+
- 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.
14+
- Pull Request: https://github.com/apple/swift-syntax/pull/2689
15+
1216
## Deprecations
1317

1418
- `IncrementalEdit` deprecated in favor of `SourceEdit`

Sources/SwiftSyntax/SyntaxProtocol.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,11 @@ extension SyntaxProtocol {
578578

579579
/// A copy of this node without the leading trivia of the first token in the
580580
/// node and the trailing trivia of the last token in the node.
581+
///
582+
/// The trimmed node is detached from its parent.
581583
public var trimmed: Self {
582584
// TODO: Should only need one new node here
583-
return self.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
585+
return self.detached.with(\.leadingTrivia, []).with(\.trailingTrivia, [])
584586
}
585587

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

0 commit comments

Comments
 (0)