Skip to content

Commit ce9df9f

Browse files
committed
Allow putting garbage nodes in between any two children of a Syntax node
1 parent 4929d2d commit ce9df9f

33 files changed

+30099
-2968
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
@@ -67,6 +67,12 @@ extension SyntaxNode {
6767
return CodeBlockSyntax(asSyntaxData)
6868
}
6969

70+
public var isGarbageNodes: Bool { return raw.kind == .garbageNodes }
71+
public var asGarbageNodes: GarbageNodesSyntax? {
72+
guard isGarbageNodes else { return nil }
73+
return GarbageNodesSyntax(asSyntaxData)
74+
}
75+
7076
public var isInOutExpr: Bool { return raw.kind == .inOutExpr }
7177
public var asInOutExpr: InOutExprSyntax? {
7278
guard isInOutExpr else { return nil }
@@ -1557,6 +1563,8 @@ extension Syntax {
15571563
return node
15581564
case .codeBlock(let node):
15591565
return node
1566+
case .garbageNodes(let node):
1567+
return node
15601568
case .inOutExpr(let node):
15611569
return node
15621570
case .poundColumnExpr(let node):

Sources/SwiftSyntax/gyb_generated/SyntaxAnyVisitor.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
117117
override open func visitPost(_ node: CodeBlockSyntax) {
118118
visitAnyPost(node._syntaxNode)
119119
}
120+
override open func visit(_ node: GarbageNodesSyntax) -> SyntaxVisitorContinueKind {
121+
return visitAny(node._syntaxNode)
122+
}
123+
124+
override open func visitPost(_ node: GarbageNodesSyntax) {
125+
visitAnyPost(node._syntaxNode)
126+
}
120127
override open func visit(_ node: InOutExprSyntax) -> SyntaxVisitorContinueKind {
121128
return visitAny(node._syntaxNode)
122129
}

0 commit comments

Comments
 (0)