Skip to content

Commit 438f48b

Browse files
committed
Allow putting garbage nodes in between any two children of a Syntax node
1 parent 53abe99 commit 438f48b

33 files changed

+30428
-2963
lines changed

Sources/SwiftSyntax/SyntaxBuilders.swift.gyb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public struct ${Builder} {
3434
Array<RawSyntax?>(repeating: nil, count: ${len(node.children)})
3535

3636
internal init() {}
37-
% for child in node.children:
37+
% # SyntaxBuilder doesn't support adding garbage nodes at the moment
38+
% # This could be added in the future, if needed.
39+
% for child in node.non_garbage_children:
3840
% child_node = NODE_MAP.get(child.syntax_kind)
3941
% if child_node and child_node.is_syntax_collection():
4042
% child_elt = child.collection_element_name

Sources/SwiftSyntax/SyntaxFactory.swift.gyb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ public enum SyntaxFactory {
5252
% elif node.children:
5353
% child_params = []
5454
% for child in node.children:
55-
% param_type = child.type_name
56-
% if child.is_optional:
57-
% param_type = param_type + "?"
55+
% param_type = child.type_name
56+
% if child.is_optional:
57+
% param_type = param_type + "?"
58+
% end
59+
% if child.is_garbage_nodes():
60+
% child_params.append("garbage %s: %s = nil" % (child.swift_name, param_type))
61+
% else:
5862
% child_params.append("%s: %s" % (child.swift_name, param_type))
63+
% end
64+
% end
5965
% child_params = ', '.join(child_params)
6066
public static func make${node.syntax_kind}(${child_params}) -> ${node.name} {
6167
let layout: [RawSyntax?] = [

Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
115115
% # ===============
116116
% # Adding children
117117
% # ===============
118-
% if child_node and child_node.is_syntax_collection():
118+
% # We don't currently support adding elements to a specific garbage collection.
119+
% # If needed, this could be added in the future, but for now withGarbage should be sufficient.
120+
% if child_node and child_node.is_syntax_collection() and not child.is_garbage_nodes():
119121
% child_elt = child.collection_element_name
120122
% child_elt_type = child_node.collection_element_type
121123
% if not child_elt:

Sources/SwiftSyntax/SyntaxTreeViewMode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public enum SyntaxTreeViewMode {
3535
case .sourceAccurate:
3636
return node.presence == .present
3737
case .fixedUp:
38-
return true
38+
return node.kind != .garbageNodes
3939
case ._all:
4040
return true
4141
}

Sources/SwiftSyntax/gyb_generated/Misc.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ extension SyntaxNode {
103103
return CodeBlockSyntax(asSyntaxData)
104104
}
105105

106+
public var isGarbageNodes: Bool { return raw.kind == .garbageNodes }
107+
public var asGarbageNodes: GarbageNodesSyntax? {
108+
guard isGarbageNodes else { return nil }
109+
return GarbageNodesSyntax(asSyntaxData)
110+
}
111+
106112
public var isInOutExpr: Bool { return raw.kind == .inOutExpr }
107113
public var asInOutExpr: InOutExprSyntax? {
108114
guard isInOutExpr else { return nil }
@@ -1601,6 +1607,8 @@ extension Syntax {
16011607
return node
16021608
case .codeBlock(let node):
16031609
return node
1610+
case .garbageNodes(let node):
1611+
return node
16041612
case .inOutExpr(let node):
16051613
return node
16061614
case .poundColumnExpr(let node):

Sources/SwiftSyntax/gyb_generated/SyntaxAnyVisitor.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
159159
override open func visitPost(_ node: CodeBlockSyntax) {
160160
visitAnyPost(node._syntaxNode)
161161
}
162+
override open func visit(_ node: GarbageNodesSyntax) -> SyntaxVisitorContinueKind {
163+
return visitAny(node._syntaxNode)
164+
}
165+
166+
override open func visitPost(_ node: GarbageNodesSyntax) {
167+
visitAnyPost(node._syntaxNode)
168+
}
162169
override open func visit(_ node: InOutExprSyntax) -> SyntaxVisitorContinueKind {
163170
return visitAny(node._syntaxNode)
164171
}

0 commit comments

Comments
 (0)