@@ -55,6 +55,12 @@ extension UnknownDeclSyntax: CustomReflectable {
55
55
// MARK: - MissingDeclSyntax
56
56
57
57
public struct MissingDeclSyntax : DeclSyntaxProtocol , SyntaxHashable {
58
+ enum Cursor : Int {
59
+ case garbageBeforeAttributes
60
+ case attributes
61
+ case garbageBetweenAttributesAndModifiers
62
+ case modifiers
63
+ }
58
64
59
65
public let _syntaxNode : Syntax
60
66
@@ -77,16 +83,178 @@ public struct MissingDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
77
83
return Swift . type ( of: self )
78
84
}
79
85
86
+ public var garbageBeforeAttributes : GarbageNodesSyntax ? {
87
+ get {
88
+ let childData = data. child ( at: Cursor . garbageBeforeAttributes,
89
+ parent: Syntax ( self ) )
90
+ if childData == nil { return nil }
91
+ return GarbageNodesSyntax ( childData!)
92
+ }
93
+ set ( value) {
94
+ self = withGarbageBeforeAttributes ( value)
95
+ }
96
+ }
97
+
98
+ /// Returns a copy of the receiver with its `garbageBeforeAttributes` replaced.
99
+ /// - param newChild: The new `garbageBeforeAttributes` to replace the node's
100
+ /// current `garbageBeforeAttributes`, if present.
101
+ public func withGarbageBeforeAttributes(
102
+ _ newChild: GarbageNodesSyntax ? ) -> MissingDeclSyntax {
103
+ let raw = newChild? . raw
104
+ let newData = data. replacingChild ( raw, at: Cursor . garbageBeforeAttributes)
105
+ return MissingDeclSyntax ( newData)
106
+ }
107
+
108
+ public var attributes : AttributeListSyntax ? {
109
+ get {
110
+ let childData = data. child ( at: Cursor . attributes,
111
+ parent: Syntax ( self ) )
112
+ if childData == nil { return nil }
113
+ return AttributeListSyntax ( childData!)
114
+ }
115
+ set ( value) {
116
+ self = withAttributes ( value)
117
+ }
118
+ }
119
+
120
+ /// Adds the provided `Attribute` to the node's `attributes`
121
+ /// collection.
122
+ /// - param element: The new `Attribute` to add to the node's
123
+ /// `attributes` collection.
124
+ /// - returns: A copy of the receiver with the provided `Attribute`
125
+ /// appended to its `attributes` collection.
126
+ public func addAttribute( _ element: Syntax ) -> MissingDeclSyntax {
127
+ var collection : RawSyntax
128
+ if let col = raw [ Cursor . attributes] {
129
+ collection = col. appending ( element. raw)
130
+ } else {
131
+ collection = RawSyntax . create ( kind: SyntaxKind . attributeList,
132
+ layout: [ element. raw] , length: element. raw. totalLength, presence: . present)
133
+ }
134
+ let newData = data. replacingChild ( collection,
135
+ at: Cursor . attributes)
136
+ return MissingDeclSyntax ( newData)
137
+ }
138
+
139
+ /// Returns a copy of the receiver with its `attributes` replaced.
140
+ /// - param newChild: The new `attributes` to replace the node's
141
+ /// current `attributes`, if present.
142
+ public func withAttributes(
143
+ _ newChild: AttributeListSyntax ? ) -> MissingDeclSyntax {
144
+ let raw = newChild? . raw
145
+ let newData = data. replacingChild ( raw, at: Cursor . attributes)
146
+ return MissingDeclSyntax ( newData)
147
+ }
148
+
149
+ public var garbageBetweenAttributesAndModifiers : GarbageNodesSyntax ? {
150
+ get {
151
+ let childData = data. child ( at: Cursor . garbageBetweenAttributesAndModifiers,
152
+ parent: Syntax ( self ) )
153
+ if childData == nil { return nil }
154
+ return GarbageNodesSyntax ( childData!)
155
+ }
156
+ set ( value) {
157
+ self = withGarbageBetweenAttributesAndModifiers ( value)
158
+ }
159
+ }
160
+
161
+ /// Returns a copy of the receiver with its `garbageBetweenAttributesAndModifiers` replaced.
162
+ /// - param newChild: The new `garbageBetweenAttributesAndModifiers` to replace the node's
163
+ /// current `garbageBetweenAttributesAndModifiers`, if present.
164
+ public func withGarbageBetweenAttributesAndModifiers(
165
+ _ newChild: GarbageNodesSyntax ? ) -> MissingDeclSyntax {
166
+ let raw = newChild? . raw
167
+ let newData = data. replacingChild ( raw, at: Cursor . garbageBetweenAttributesAndModifiers)
168
+ return MissingDeclSyntax ( newData)
169
+ }
170
+
171
+ public var modifiers : ModifierListSyntax ? {
172
+ get {
173
+ let childData = data. child ( at: Cursor . modifiers,
174
+ parent: Syntax ( self ) )
175
+ if childData == nil { return nil }
176
+ return ModifierListSyntax ( childData!)
177
+ }
178
+ set ( value) {
179
+ self = withModifiers ( value)
180
+ }
181
+ }
182
+
183
+ /// Adds the provided `Modifier` to the node's `modifiers`
184
+ /// collection.
185
+ /// - param element: The new `Modifier` to add to the node's
186
+ /// `modifiers` collection.
187
+ /// - returns: A copy of the receiver with the provided `Modifier`
188
+ /// appended to its `modifiers` collection.
189
+ public func addModifier( _ element: DeclModifierSyntax ) -> MissingDeclSyntax {
190
+ var collection : RawSyntax
191
+ if let col = raw [ Cursor . modifiers] {
192
+ collection = col. appending ( element. raw)
193
+ } else {
194
+ collection = RawSyntax . create ( kind: SyntaxKind . modifierList,
195
+ layout: [ element. raw] , length: element. raw. totalLength, presence: . present)
196
+ }
197
+ let newData = data. replacingChild ( collection,
198
+ at: Cursor . modifiers)
199
+ return MissingDeclSyntax ( newData)
200
+ }
201
+
202
+ /// Returns a copy of the receiver with its `modifiers` replaced.
203
+ /// - param newChild: The new `modifiers` to replace the node's
204
+ /// current `modifiers`, if present.
205
+ public func withModifiers(
206
+ _ newChild: ModifierListSyntax ? ) -> MissingDeclSyntax {
207
+ let raw = newChild? . raw
208
+ let newData = data. replacingChild ( raw, at: Cursor . modifiers)
209
+ return MissingDeclSyntax ( newData)
210
+ }
211
+
80
212
81
213
public func _validateLayout( ) {
82
214
let rawChildren = Array ( RawSyntaxChildren ( Syntax ( self ) ) )
83
- assert ( rawChildren. count == 0 )
215
+ assert ( rawChildren. count == 4 )
216
+ // Check child #0 child is GarbageNodesSyntax or missing
217
+ if let raw = rawChildren [ 0 ] . raw {
218
+ let info = rawChildren [ 0 ] . syntaxInfo
219
+ let absoluteRaw = AbsoluteRawSyntax ( raw: raw, info: info)
220
+ let syntaxData = SyntaxData ( absoluteRaw, parent: Syntax ( self ) )
221
+ let syntaxChild = Syntax ( syntaxData)
222
+ assert ( syntaxChild. is ( GarbageNodesSyntax . self) )
223
+ }
224
+ // Check child #1 child is AttributeListSyntax or missing
225
+ if let raw = rawChildren [ 1 ] . raw {
226
+ let info = rawChildren [ 1 ] . syntaxInfo
227
+ let absoluteRaw = AbsoluteRawSyntax ( raw: raw, info: info)
228
+ let syntaxData = SyntaxData ( absoluteRaw, parent: Syntax ( self ) )
229
+ let syntaxChild = Syntax ( syntaxData)
230
+ assert ( syntaxChild. is ( AttributeListSyntax . self) )
231
+ }
232
+ // Check child #2 child is GarbageNodesSyntax or missing
233
+ if let raw = rawChildren [ 2 ] . raw {
234
+ let info = rawChildren [ 2 ] . syntaxInfo
235
+ let absoluteRaw = AbsoluteRawSyntax ( raw: raw, info: info)
236
+ let syntaxData = SyntaxData ( absoluteRaw, parent: Syntax ( self ) )
237
+ let syntaxChild = Syntax ( syntaxData)
238
+ assert ( syntaxChild. is ( GarbageNodesSyntax . self) )
239
+ }
240
+ // Check child #3 child is ModifierListSyntax or missing
241
+ if let raw = rawChildren [ 3 ] . raw {
242
+ let info = rawChildren [ 3 ] . syntaxInfo
243
+ let absoluteRaw = AbsoluteRawSyntax ( raw: raw, info: info)
244
+ let syntaxData = SyntaxData ( absoluteRaw, parent: Syntax ( self ) )
245
+ let syntaxChild = Syntax ( syntaxData)
246
+ assert ( syntaxChild. is ( ModifierListSyntax . self) )
247
+ }
84
248
}
85
249
}
86
250
87
251
extension MissingDeclSyntax : CustomReflectable {
88
252
public var customMirror : Mirror {
89
253
return Mirror ( self , children: [
254
+ " garbageBeforeAttributes " : garbageBeforeAttributes. map ( Syntax . init) ? . asProtocol ( SyntaxProtocol . self) as Any ,
255
+ " attributes " : attributes. map ( Syntax . init) ? . asProtocol ( SyntaxProtocol . self) as Any ,
256
+ " garbageBetweenAttributesAndModifiers " : garbageBetweenAttributesAndModifiers. map ( Syntax . init) ? . asProtocol ( SyntaxProtocol . self) as Any ,
257
+ " modifiers " : modifiers. map ( Syntax . init) ? . asProtocol ( SyntaxProtocol . self) as Any ,
90
258
] )
91
259
}
92
260
}
0 commit comments