Skip to content

Allow rewrite to detach from the node's parent #1851

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 28, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {

DeclSyntax(
"""
/// Rewrite `node` and anchor, making sure that the rewritten node also has
/// a parent if `node` had one.
public func rewrite(_ node: some SyntaxProtocol) -> Syntax {
/// Rewrite `node`, keeping its parent unless `detach` is `true`.
public func rewrite(_ node: some SyntaxProtocol, detach: Bool = false) -> Syntax {
let rewritten = self.visit(node.data)
if detach {
return rewritten
}

return withExtendedLifetime(rewritten) {
return Syntax(node.data.replacingSelf(rewritten.raw, rawNodeArena: rewritten.raw.arena, allocationArena: SyntaxArena()))
}
Expand Down Expand Up @@ -100,7 +103,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
/// Visit any Syntax node.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
@available(*, deprecated, renamed: "rewrite(_:)")
@available(*, deprecated, renamed: "rewrite(_:detach:)")
public func visit(_ node: Syntax) -> Syntax {
return visit(node.data)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftParserDiagnostics/DiagnosticExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extension FixIt.MultiNodeChange {
guard let node = node else {
return FixIt.MultiNodeChange(primitiveChanges: [])
}
var changes = [FixIt.Change.replace(oldNode: Syntax(node), newNode: MissingMaker().rewrite(node))]
var changes = [FixIt.Change.replace(oldNode: Syntax(node), newNode: MissingMaker().rewrite(node, detach: true))]
if transferTrivia {
changes += FixIt.MultiNodeChange.transferTriviaAtSides(from: [node]).primitiveChanges
}
Expand Down Expand Up @@ -123,7 +123,7 @@ extension FixIt.MultiNodeChange {
leadingTrivia: Trivia? = nil,
trailingTrivia: Trivia? = nil
) -> Self {
var presentNode = MissingNodesBasicFormatter(viewMode: .fixedUp).rewrite(node)
var presentNode = MissingNodesBasicFormatter(viewMode: .fixedUp).rewrite(node, detach: true)
presentNode = PresentMaker().rewrite(presentNode)

if let leadingTrivia {
Expand Down
11 changes: 7 additions & 4 deletions Sources/SwiftSyntax/generated/SyntaxRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ open class SyntaxRewriter {
self.viewMode = viewMode
}

/// Rewrite `node` and anchor, making sure that the rewritten node also has
/// a parent if `node` had one.
public func rewrite(_ node: some SyntaxProtocol) -> Syntax {
/// Rewrite `node`, keeping its parent unless `detach` is `true`.
public func rewrite(_ node: some SyntaxProtocol, detach: Bool = false) -> Syntax {
let rewritten = self.visit(node.data)
if detach {
return rewritten
}

return withExtendedLifetime(rewritten) {
return Syntax(node.data.replacingSelf(rewritten.raw, rawNodeArena: rewritten.raw.arena, allocationArena: SyntaxArena()))
}
Expand Down Expand Up @@ -68,7 +71,7 @@ open class SyntaxRewriter {
/// Visit any Syntax node.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
@available(*, deprecated, renamed: "rewrite(_:)")
@available(*, deprecated, renamed: "rewrite(_:detach:)")
public func visit(_ node: Syntax) -> Syntax {
return visit(node.data)
}
Expand Down