@@ -38,6 +38,40 @@ 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_MAP [ choice_name]
58
+ public init( ${ choice. swift_syntax_kind} node: ${ choice. name} ) { self = . ${ choice. swift_syntax_kind} ( node) }
59
+ % end
60
+ public init? < Node: SyntaxProtocol> ( _ syntaxNode: Node) {
61
+ % for choice_name in node. collection_element_choices:
62
+ % choice = NODE_MAP [ choice_name]
63
+ if let node = syntaxNode. as ( ${ choice. name} . self) {
64
+ self = . ${ choice. swift_syntax_kind} ( node)
65
+ return
66
+ }
67
+ % end
68
+ return nil
69
+ }
70
+ }
71
+ % else:
72
+ public typealias Element = ${ node. collection_element_type}
73
+ % end
74
+
41
75
public let _syntaxNode: Syntax
42
76
43
77
var layoutView: RawSyntaxLayoutView {
@@ -59,7 +93,7 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
59
93
self . _syntaxNode = Syntax ( data)
60
94
}
61
95
62
- public init( _ children: [ $ { node . collection_element_type } ] ) {
96
+ public init( _ children: [ Element ] ) {
63
97
let raw = RawSyntax . makeLayout ( kind: SyntaxKind . ${ node. swift_syntax_kind} ,
64
98
from: children. map { $0. raw } , arena: . default)
65
99
let data = SyntaxData . forRoot ( raw)
@@ -87,8 +121,7 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
87
121
///
88
122
/// - Parameter syntax: The element to append.
89
123
/// - Returns: A new `${node.name}` with that element appended to the end.
90
- public func appending(
91
- _ syntax: ${ node. collection_element_type} ) - > ${ node. name} {
124
+ public func appending( _ syntax: Element) - > ${ node. name} {
92
125
var newLayout = layoutView. formLayoutArray ( )
93
126
newLayout. append ( syntax. raw)
94
127
return replacingLayout ( newLayout)
@@ -100,8 +133,7 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
100
133
/// - Parameter syntax: The element to prepend.
101
134
/// - Returns: A new `${node.name}` with that element prepended to the
102
135
/// beginning.
103
- public func prepending(
104
- _ syntax: ${ node. collection_element_type} ) -> ${ node. name} {
136
+ public func prepending( _ syntax: Element) - > ${ node. name} {
105
137
return inserting ( syntax, at: 0 )
106
138
}
107
139
@@ -113,8 +145,7 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
113
145
/// - index: The index at which to insert the element in the collection.
114
146
///
115
147
/// - 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} {
148
+ public func inserting( _ syntax: Element, at index: Int) - > ${ node. name} {
118
149
var newLayout = layoutView. formLayoutArray ( )
119
150
/// Make sure the index is a valid insertion index (0 to 1 past the end)
120
151
precondition ( ( newLayout. startIndex... newLayout. endIndex) . contains ( index) ,
@@ -131,8 +162,7 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
131
162
/// - syntax: The element to replace with.
132
163
///
133
164
/// - 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} {
165
+ public func replacing( childAt index: Int, with syntax: Element) - > ${ node. name} {
136
166
var newLayout = layoutView. formLayoutArray ( )
137
167
/// Make sure the index is a valid index for replacing
138
168
precondition ( ( newLayout. startIndex..< newLayout. endIndex) . contains ( index) ,
@@ -221,7 +251,6 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
221
251
222
252
/// Conformance for `${node.name}` to the `BidirectionalCollection` protocol.
223
253
extension ${ node. name} : BidirectionalCollection {
224
- public typealias Element = ${ node. collection_element_type}
225
254
public typealias Index = SyntaxChildrenIndex
226
255
227
256
public struct Iterator : IteratorProtocol {
@@ -233,13 +262,13 @@ extension ${node.name}: BidirectionalCollection {
233
262
self . iterator = rawChildren. makeIterator ( )
234
263
}
235
264
236
- public mutating func next( ) -> $ { node . collection_element_type } ? {
265
+ public mutating func next( ) -> Element ? {
237
266
guard let ( raw, info) = self . iterator. next ( ) else {
238
267
return nil
239
268
}
240
269
let absoluteRaw = AbsoluteRawSyntax ( raw: raw!, info: info)
241
270
let data = SyntaxData ( absoluteRaw, parent: parent)
242
- return $ { node . collection_element_type } ( data)
271
+ return Element ( data)
243
272
}
244
273
}
245
274
@@ -274,11 +303,11 @@ extension ${node.name}: BidirectionalCollection {
274
303
return rawChildren. distance ( from: start, to: end)
275
304
}
276
305
277
- public subscript( position: SyntaxChildrenIndex) - > $ { node . collection_element_type } {
306
+ public subscript( position: SyntaxChildrenIndex) - > Element {
278
307
let ( raw, info) = rawChildren [ position]
279
308
let absoluteRaw = AbsoluteRawSyntax ( raw: raw!, info: info)
280
309
let data = SyntaxData ( absoluteRaw, parent: Syntax ( self ) )
281
- return $ { node . collection_element_type } ( data)
310
+ return Element ( data)
282
311
}
283
312
}
284
313
% end
0 commit comments