Skip to content

Commit fe3d42b

Browse files
committed
Add default conformance to syntax collection
1 parent 32786eb commit fe3d42b

File tree

2 files changed

+304
-55
lines changed

2 files changed

+304
-55
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 24 additions & 16 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_BY_CONFORMANCES
7+
SYNTAX_COLLECTION_EXPRESSIBLE_BY_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,8 +43,10 @@ public protocol ${kind}ListBuildable: SyntaxListBuildable {
4243

4344
% buildable_type = kind + 'Buildable'
4445
% expressible_by_type = 'ExpressibleBy' + buildable_type
45-
% expressible_by_protocols = SYNTAX_BUILDABLE_EXPRESSIBLE_BY_CONFORMANCES.get(expressible_by_type)
46-
% if expressible_by_protocols:
46+
% expressible_by_protocols = SYNTAX_COLLECTION_EXPRESSIBLE_BY_CONFORMANCES.get(buildable_type) or []
47+
% expressible_by_protocols += SYNTAX_BUILDABLE_EXPRESSIBLE_BY_CONFORMANCES.get(buildable_type) or []
48+
% expressible_by_protocols = map(lambda x : 'ExpressibleBy' +x, expressible_by_protocols)
49+
% if expressible_by_protocols and expressible_by_protocols is not []:
4750
public protocol ${expressible_by_type}: ${', '.join(expressible_by_protocols)} {
4851
% else:
4952
public protocol ${expressible_by_type} {
@@ -220,8 +223,10 @@ public struct ${node.syntax_kind}: SyntaxBuildable {
220223
% end
221224
% if node.is_buildable() or node.is_syntax_collection():
222225
% expressible_by_type = 'ExpressibleBy' + node.syntax_kind
223-
% expressible_by_protocols = SYNTAX_BUILDABLE_EXPRESSIBLE_BY_CONFORMANCES.get(expressible_by_type)
224-
% if expressible_by_protocols:
226+
% expressible_by_protocols = SYNTAX_COLLECTION_EXPRESSIBLE_BY_CONFORMANCES.get(node.syntax_kind) or []
227+
% expressible_by_protocols += SYNTAX_BUILDABLE_EXPRESSIBLE_BY_CONFORMANCES.get(node.syntax_kind) or []
228+
% expressible_by_protocols = map(lambda x : 'ExpressibleBy' +x, expressible_by_protocols)
229+
% if expressible_by_protocols and expressible_by_protocols is not []:
225230
public protocol ${expressible_by_type}: ${', '.join(expressible_by_protocols)} {
226231
% else:
227232
public protocol ${expressible_by_type} {
@@ -249,37 +254,40 @@ extension TokenSyntax: ExpressibleByTokenSyntax {
249254

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

252-
% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_BY_CONFORMANCES.items():
253-
% if 'ExpressibleByConditionElementList' in conformances:
254-
extension ${protocol} {
255-
public func createConditionElementList() -> ConditionElementList {
256-
ConditionElementList([self])
257+
% for protocol, conformances in SYNTAX_COLLECTION_EXPRESSIBLE_BY_CONFORMANCES.items():
258+
% for conformance in conformances:
259+
extension ExpressibleBy${protocol} {
260+
public func create${conformance}() -> ${conformance} {
261+
${conformance}([self])
257262
}
258263
}
259264

260265
% end
261-
% if 'ExpressibleByConditionElement' in conformances:
262-
extension ${protocol} {
266+
% end
267+
268+
% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_BY_CONFORMANCES.items():
269+
% if 'ConditionElement' in conformances:
270+
extension ExpressibleBy${protocol} {
263271
public func createConditionElement() -> ConditionElement {
264272
ConditionElement(condition: self)
265273
}
266274
}
267275

268276
% end
269-
% if 'ExpressibleByCodeBlockItem' in conformances:
270-
extension ${protocol} {
277+
% if 'CodeBlockItem' in conformances:
278+
extension ExpressibleBy${protocol} {
271279
public func createCodeBlockItem() -> CodeBlockItem {
272280
CodeBlockItem(item: self)
273281
}
274282
}
275283

276284
% end
277-
% if 'ExpressibleByMemberDeclListItem' in conformances:
278-
extension ${protocol} {
285+
% if 'MemberDeclListItem' in conformances:
286+
extension ExpressibleBy${protocol} {
279287
public func createMemberDeclListItem() -> MemberDeclListItem {
280288
MemberDeclListItem(decl: self)
281289
}
282290
}
283291

284292
% end
285-
% end
293+
% end

0 commit comments

Comments
 (0)