@@ -38,6 +38,48 @@ public protocol SyntaxCollection: SyntaxProtocol, Sequence {
38
38
/// versions of the collection with different children.
39
39
% end
40
40
public struct ${ node. name} : SyntaxCollection, SyntaxHashable {
41
+ % if node. collection_element_choices:
42
+ public enum Element: SyntaxProtocol {
43
+ % for choice_name in node. collection_element_choices:
44
+ % choice = NODE_MAP [ choice_name]
45
+ case `${choice.swift_syntax_kind}`( ${ choice. name} )
46
+ % end
47
+ public var _syntaxNode: Syntax {
48
+ switch self {
49
+ % for choice_name in node. collection_element_choices:
50
+ % choice = NODE_MAP [ choice_name]
51
+ case . ${ choice. swift_syntax_kind} ( let node) : return node. _syntaxNode
52
+ % end
53
+ }
54
+ }
55
+ init( _ data: SyntaxData) { self . init ( Syntax ( data) ) ! }
56
+ % for choice_name in node. collection_element_choices:
57
+ % choice_node = NODE_MAP . get ( choice_name)
58
+ % if choice_node and choice_node. is_base ( ) :
59
+ public init< Node: ${ choice_node. name} Protocol> ( _ node: Node) {
60
+ self = . ${ choice_node. swift_syntax_kind} ( ${ choice_node. name} ( node) )
61
+ }
62
+ % else:
63
+ public init( _ node: ${ choice_node. name} ) {
64
+ self = . ${ choice_node. swift_syntax_kind} ( node)
65
+ }
66
+ % end
67
+ % end
68
+ public init? < Node: SyntaxProtocol> ( _ syntaxNode: Node) {
69
+ % for choice_name in node. collection_element_choices:
70
+ % choice = NODE_MAP [ choice_name]
71
+ if let node = syntaxNode. as ( ${ choice. name} . self) {
72
+ self = . ${ choice. swift_syntax_kind} ( node)
73
+ return
74
+ }
75
+ % end
76
+ return nil
77
+ }
78
+ }
79
+ % else:
80
+ public typealias Element = ${ node. collection_element_type}
81
+ % end
82
+
41
83
public let _syntaxNode: Syntax
42
84
43
85
var layoutView: RawSyntaxLayoutView {
@@ -59,7 +101,7 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
59
101
self . _syntaxNode = Syntax ( data)
60
102
}
61
103
62
- public init( _ children: [ $ { node . collection_element_type } ] ) {
104
+ public init( _ children: [ Element ] ) {
63
105
let raw = RawSyntax . makeLayout ( kind: SyntaxKind . ${ node. swift_syntax_kind} ,
64
106
from: children. map { $0. raw } , arena: . default)
65
107
let data = SyntaxData . forRoot ( raw)
@@ -87,8 +129,7 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
87
129
///
88
130
/// - Parameter syntax: The element to append.
89
131
/// - Returns: A new `${node.name}` with that element appended to the end.
90
- public func appending(
91
- _ syntax: ${ node. collection_element_type} ) - > ${ node. name} {
132
+ public func appending( _ syntax: Element) - > ${ node. name} {
92
133
var newLayout = layoutView. formLayoutArray ( )
93
134
newLayout. append ( syntax. raw)
94
135
return replacingLayout ( newLayout)
@@ -100,8 +141,7 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
100
141
/// - Parameter syntax: The element to prepend.
101
142
/// - Returns: A new `${node.name}` with that element prepended to the
102
143
/// beginning.
103
- public func prepending(
104
- _ syntax: ${ node. collection_element_type} ) -> ${ node. name} {
144
+ public func prepending( _ syntax: Element) - > ${ node. name} {
105
145
return inserting ( syntax, at: 0 )
106
146
}
107
147
@@ -113,8 +153,7 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
113
153
/// - index: The index at which to insert the element in the collection.
114
154
///
115
155
/// - Returns: A new `${node.name}` with that element appended to the end.
116
- public func inserting( _ syntax: ${ node. collection_element_type} ,
117
- at index: Int) -> ${ node. name} {
156
+ public func inserting( _ syntax: Element, at index: Int) - > ${ node. name} {
118
157
var newLayout = layoutView. formLayoutArray ( )
119
158
/// Make sure the index is a valid insertion index (0 to 1 past the end)
120
159
precondition ( ( newLayout. startIndex... newLayout. endIndex) . contains ( index) ,
@@ -131,8 +170,7 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
131
170
/// - syntax: The element to replace with.
132
171
///
133
172
/// - Returns: A new `${node.name}` with the new element at the provided index.
134
- public func replacing( childAt index: Int ,
135
- with syntax: ${ node. collection_element_type} ) -> ${ node. name} {
173
+ public func replacing( childAt index: Int, with syntax: Element) - > ${ node. name} {
136
174
var newLayout = layoutView. formLayoutArray ( )
137
175
/// Make sure the index is a valid index for replacing
138
176
precondition ( ( newLayout. startIndex..< newLayout. endIndex) . contains ( index) ,
@@ -221,7 +259,6 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
221
259
222
260
/// Conformance for `${node.name}` to the `BidirectionalCollection` protocol.
223
261
extension ${ node. name} : BidirectionalCollection {
224
- public typealias Element = ${ node. collection_element_type}
225
262
public typealias Index = SyntaxChildrenIndex
226
263
227
264
public struct Iterator : IteratorProtocol {
@@ -233,13 +270,13 @@ extension ${node.name}: BidirectionalCollection {
233
270
self . iterator = rawChildren. makeIterator ( )
234
271
}
235
272
236
- public mutating func next( ) -> $ { node . collection_element_type } ? {
273
+ public mutating func next( ) -> Element ? {
237
274
guard let ( raw, info) = self . iterator. next ( ) else {
238
275
return nil
239
276
}
240
277
let absoluteRaw = AbsoluteRawSyntax ( raw: raw!, info: info)
241
278
let data = SyntaxData ( absoluteRaw, parent: parent)
242
- return $ { node . collection_element_type } ( data)
279
+ return Element ( data)
243
280
}
244
281
}
245
282
@@ -274,11 +311,11 @@ extension ${node.name}: BidirectionalCollection {
274
311
return rawChildren. distance ( from: start, to: end)
275
312
}
276
313
277
- public subscript( position: SyntaxChildrenIndex) - > $ { node . collection_element_type } {
314
+ public subscript( position: SyntaxChildrenIndex) - > Element {
278
315
let ( raw, info) = rawChildren [ position]
279
316
let absoluteRaw = AbsoluteRawSyntax ( raw: raw!, info: info)
280
317
let data = SyntaxData ( absoluteRaw, parent: Syntax ( self ) )
281
- return $ { node . collection_element_type } ( data)
318
+ return Element ( data)
282
319
}
283
320
}
284
321
% end
0 commit comments