From d2aee8c04ae0e640e68f9b594517768bb4aa85b1 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Tue, 18 Jun 2024 13:23:52 -0700 Subject: [PATCH] Detach node when accessing .trimmed 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. --- Release Notes/601.md | 4 ++++ Sources/SwiftSyntax/SyntaxProtocol.swift | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Release Notes/601.md b/Release Notes/601.md index 9bf8ee872d9..5586443e05d 100644 --- a/Release Notes/601.md +++ b/Release Notes/601.md @@ -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` diff --git a/Sources/SwiftSyntax/SyntaxProtocol.swift b/Sources/SwiftSyntax/SyntaxProtocol.swift index c548a4d08af..dcdb3be4b30 100644 --- a/Sources/SwiftSyntax/SyntaxProtocol.swift +++ b/Sources/SwiftSyntax/SyntaxProtocol.swift @@ -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