Skip to content

Commit 05810f0

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

File tree

2 files changed

+280
-55
lines changed

2 files changed

+280
-55
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 18 additions & 10 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,15 +254,18 @@ 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
266+
% end
267+
268+
% for protocol, conformances in SYNTAX_BUILDABLE_EXPRESSIBLE_BY_CONFORMANCES.items():
261269
% if 'ExpressibleByConditionElement' in conformances:
262270
extension ${protocol} {
263271
public func createConditionElement() -> ConditionElement {
@@ -282,4 +290,4 @@ extension ${protocol} {
282290
}
283291

284292
% end
285-
% end
293+
% end

0 commit comments

Comments
 (0)