Skip to content

Add convenience initializers for simple expr values #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/SwiftSyntaxBuilder/Buildables.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from gyb_syntax_support import *
from gyb_syntax_support.kinds import lowercase_first_word
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS
from gyb_syntax_support.kinds import syntax_buildable_child_type
from gyb_syntax_support.kinds import syntax_buildable_child_type, syntax_buildable_default_init_value
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
Expand Down Expand Up @@ -103,8 +103,9 @@ public struct ${node.syntax_kind}: ${node.base_kind}Buildable {
public init(
% init_parameters = []
% for child in node.children:
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
% default_value = " = nil" if child.is_optional else ""
% default_value = syntax_buildable_default_init_value(child, child_token)
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
% end
${',\n '.join(init_parameters)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
%{
from gyb_syntax_support import *
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS, lowercase_first_word, syntax_buildable_child_type
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS, lowercase_first_word, syntax_buildable_child_type, syntax_buildable_default_init_value
NODE_MAP = create_node_map()
# -*- mode: Swift -*-
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From DeclBuildables.swift.gyb.
//// Automatically Generated From BuildablesConvenienceInitializers.swift.gyb.
//// Do Not Edit Directly!
//===----------------------------------------------------------------------===//
//
Expand All @@ -23,36 +23,35 @@ import SwiftSyntax

% for node in SYNTAX_NODES:
% if node.is_buildable():
% has_syntax_collection_child = False
% should_create_convenience_initializer = False
% for child in node.children:
% child_node = NODE_MAP.get(child.syntax_kind)
% if child_node and child_node.is_syntax_collection():
% has_syntax_collection_child = True
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
% if (child_node and child_node.is_syntax_collection()) or (child_token and not child_token.text):
% # If the child token doesn’t have a text, we can create a convenience initializer that takes a string.
% # If the child token doesn’t have a text, it takes a String as parameter, we can create a convenient initializer.
% should_create_convenience_initializer = True
% end
% end
% if has_syntax_collection_child:
% if should_create_convenience_initializer:
extension ${node.syntax_kind} {
public init(
% init_parameters = []
% for child in node.children:
% child_node = NODE_MAP.get(child.syntax_kind)
% token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
% child_token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
% if child_node and child_node.is_syntax_collection():
% # Allow initializing syntax collections with result builders
% default_value = "? = { nil }" if child.is_optional else " = { .empty }"
% init_parameters.append("@%sBuilder %sBuilder: () -> %s%s" % (child.syntax_kind, child.swift_name, child.syntax_kind, default_value))
% elif child.syntax_kind is "IdentifierToken" or (token and not token.text):
% elif child_token and not child_token.text:
% # Allow initializing identifier or a token without a text with String value
% param_type = "String?" if child.is_optional else "String"
% init_parameters.append("%s: %s" % (child.swift_name, param_type))
% else:
% # When type is not handled above, use default value
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
% default_value = ""
% if token and token.text and not child.is_optional:
% default_value = " = Tokens.`%s`" % lowercase_first_word(token.name)
% elif child.is_optional:
% default_value = " = nil"
% default_value = syntax_buildable_default_init_value(child, child_token)
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
% end
% end
Expand All @@ -75,7 +74,7 @@ extension ${node.syntax_kind} {
% if child.is_optional:
% init_parameters.append("%s: %s.map(Tokens.%s)" % (child.swift_name, child.swift_name, lowercase_first_word(token.name)))
% else:
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, token.name, child.swift_name))
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, lowercase_first_word(token.name), child.swift_name))
% end
% else:
% init_parameters.append("%s: %s" % (child.swift_name, child.swift_name))
Expand All @@ -88,4 +87,4 @@ extension ${node.syntax_kind} {

% end
% end
% end
% end
Loading