Skip to content

Commit a52dca1

Browse files
committed
Add default conformance to syntax collection
1 parent af253dc commit a52dca1

File tree

2 files changed

+316
-55
lines changed

2 files changed

+316
-55
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS
55
from gyb_syntax_support.kinds import syntax_buildable_child_type, syntax_buildable_default_init_value
66
from gyb_syntax_support.protocolsMap import SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES
7+
SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES = syntax_collection_element_to_collection_mapping()
78
# -*- mode: Swift -*-
89
# Ignore the following admonition it applies to the resulting .swift file only
910
}%
@@ -42,7 +43,9 @@ public protocol ${kind}ListBuildable: SyntaxListBuildable {
4243

4344
% buildable_type = kind + 'Buildable'
4445
% expressible_as_type = 'ExpressibleAs' + buildable_type
45-
% expressible_as_protocols = SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.get(expressible_as_type)
46+
% expressible_as_protocols = SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.get(buildable_type) or []
47+
% expressible_as_protocols += SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.get(buildable_type) or []
48+
% expressible_as_protocols = map(lambda x : 'ExpressibleAs' + x, expressible_as_protocols)
4649
% if expressible_as_protocols:
4750
public protocol ${expressible_as_type}: ${', '.join(expressible_as_protocols)} {
4851
% else:
@@ -220,7 +223,9 @@ public struct ${node.syntax_kind}: SyntaxBuildable {
220223
% end
221224
% if node.is_buildable() or node.is_syntax_collection():
222225
% expressible_as_type = 'ExpressibleAs' + node.syntax_kind
223-
% expressible_as_protocols = SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.get(expressible_as_type)
226+
% expressible_as_protocols = SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.get(node.syntax_kind) or []
227+
% expressible_as_protocols += SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.get(node.syntax_kind) or []
228+
% expressible_as_protocols = map(lambda x : 'ExpressibleAs' + x, expressible_as_protocols)
224229
% if expressible_as_protocols:
225230
public protocol ${expressible_as_type}: ${', '.join(expressible_as_protocols)} {
226231
% else:
@@ -249,6 +254,17 @@ extension TokenSyntax: ExpressibleAsTokenSyntax {
249254

250255
// MARK: - Syntax buildable expressible as conformances
251256

257+
% for protocol, conformances in SYNTAX_COLLECTION_EXPRESSIBLE_AS_CONFORMANCES.items():
258+
% for conformance in conformances:
259+
extension ExpressibleAs${protocol} {
260+
public func create${conformance}() -> ${conformance} {
261+
${conformance}([self])
262+
}
263+
}
264+
265+
% end
266+
% end
267+
252268
% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items():
253269
% if 'ExpressibleAsConditionElementList' in conformances:
254270
extension ${protocol} {
@@ -258,28 +274,31 @@ extension ${protocol} {
258274
}
259275

260276
% end
261-
% if 'ExpressibleAsConditionElement' in conformances:
262-
extension ${protocol} {
277+
% end
278+
279+
% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_AS_CONFORMANCES.items():
280+
% if 'ConditionElement' in conformances:
281+
extension ExpressibleAs${protocol} {
263282
public func createConditionElement() -> ConditionElement {
264283
ConditionElement(condition: self)
265284
}
266285
}
267286

268287
% end
269-
% if 'ExpressibleAsCodeBlockItem' in conformances:
270-
extension ${protocol} {
288+
% if 'CodeBlockItem' in conformances:
289+
extension ExpressibleAs${protocol} {
271290
public func createCodeBlockItem() -> CodeBlockItem {
272291
CodeBlockItem(item: self)
273292
}
274293
}
275294

276295
% end
277-
% if 'ExpressibleAsMemberDeclListItem' in conformances:
278-
extension ${protocol} {
296+
% if 'MemberDeclListItem' in conformances:
297+
extension ExpressibleAs${protocol} {
279298
public func createMemberDeclListItem() -> MemberDeclListItem {
280299
MemberDeclListItem(decl: self)
281300
}
282301
}
283302

284303
% end
285-
% end
304+
% end

0 commit comments

Comments
 (0)