Skip to content

Commit ba7c530

Browse files
committed
Add convenience initializers for more nodes
1 parent 1888022 commit ba7c530

11 files changed

+837
-249
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from gyb_syntax_support import *
33
from gyb_syntax_support.kinds import lowercase_first_word
44
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS
5-
from gyb_syntax_support.kinds import syntax_buildable_child_type
5+
from gyb_syntax_support.kinds import syntax_buildable_child_type, syntax_buildable_default_init_value
66
# -*- mode: Swift -*-
77
# Ignore the following admonition it applies to the resulting .swift file only
88
}%
@@ -103,8 +103,9 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
103103
public init(
104104
% init_parameters = []
105105
% for child in node.children:
106+
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
106107
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
107-
% default_value = " = nil" if child.is_optional else ""
108+
% default_value = syntax_buildable_default_init_value(child, child_token) # " = nil" if child.is_optional else ""
108109
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
109110
% end
110111
${',\n '.join(init_parameters)}

Sources/SwiftSyntaxBuilder/BuildablesConvenienceInitializers.swift.gyb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
%{
22
from gyb_syntax_support import *
3-
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS, lowercase_first_word, syntax_buildable_child_type
3+
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS, lowercase_first_word, syntax_buildable_child_type, syntax_buildable_default_init_value
44
NODE_MAP = create_node_map()
55
# -*- mode: Swift -*-
66
# Ignore the following admonition it applies to the resulting .swift file only
77
}%
8-
//// Automatically Generated From DeclBuildables.swift.gyb.
8+
//// Automatically Generated From BuildablesConvenienceInitializers.swift.gyb.
99
//// Do Not Edit Directly!
1010
//===----------------------------------------------------------------------===//
1111
//
@@ -23,36 +23,36 @@ import SwiftSyntax
2323

2424
% for node in SYNTAX_NODES:
2525
% if node.is_buildable():
26-
% has_syntax_collection_child = False
26+
% should_create_convenience_initializer = False
2727
% for child in node.children:
2828
% child_node = NODE_MAP.get(child.syntax_kind)
29-
% if child_node and child_node.is_syntax_collection():
30-
% has_syntax_collection_child = True
29+
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
30+
% if (child_node and child_node.is_syntax_collection()) or (child_token and not child_token.text) or (child.syntax_kind is "IdentifierToken"):
31+
% # If node have syntax collection as child, we can create a convenient initializer
32+
% # If the child token have dont have a text, it takes a String as parameter, we can create a convenient initializer
33+
% # If child token is an IdentifierToken it can be made as a String, we can create a convenient initializer
34+
% should_create_convenience_initializer = True
3135
% end
3236
% end
33-
% if has_syntax_collection_child:
37+
% if should_create_convenience_initializer:
3438
extension ${node.syntax_kind} {
3539
public init(
3640
% init_parameters = []
3741
% for child in node.children:
3842
% child_node = NODE_MAP.get(child.syntax_kind)
39-
% token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
43+
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
4044
% if child_node and child_node.is_syntax_collection():
4145
% # Allow initializing syntax collections with result builders
4246
% default_value = "? = { nil }" if child.is_optional else " = { .empty }"
4347
% init_parameters.append("@%sBuilder %sBuilder: () -> %s%s" % (child.syntax_kind, child.swift_name, child.syntax_kind, default_value))
44-
% elif child.syntax_kind is "IdentifierToken" or (token and not token.text):
48+
% elif child.syntax_kind is "IdentifierToken" or (child_token and not child_token.text):
4549
% # Allow initializing identifier or a token without a text with String value
4650
% param_type = "String?" if child.is_optional else "String"
4751
% init_parameters.append("%s: %s" % (child.swift_name, param_type))
4852
% else:
4953
% # When type is not handled above, use default value
5054
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
51-
% default_value = ""
52-
% if token and token.text and not child.is_optional:
53-
% default_value = " = Tokens.`%s`" % lowercase_first_word(token.name)
54-
% elif child.is_optional:
55-
% default_value = " = nil"
55+
% default_value = syntax_buildable_default_init_value(child, child_token)
5656
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
5757
% end
5858
% end
@@ -75,7 +75,7 @@ extension ${node.syntax_kind} {
7575
% if child.is_optional:
7676
% init_parameters.append("%s: %s.map(Tokens.%s)" % (child.swift_name, child.swift_name, lowercase_first_word(token.name)))
7777
% else:
78-
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, token.name, child.swift_name))
78+
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, lowercase_first_word(token.name), child.swift_name))
7979
% end
8080
% else:
8181
% init_parameters.append("%s: %s" % (child.swift_name, child.swift_name))
@@ -88,4 +88,4 @@ extension ${node.syntax_kind} {
8888

8989
% end
9090
% end
91-
% end
91+
% end

0 commit comments

Comments
 (0)