Skip to content

Commit ead746b

Browse files
authored
Merge pull request #475 from fwcd/buildable-nodes-leading-trivia
Add ability to attach leading trivia directly to buildable nodes
2 parents d5ead9d + 70f46d2 commit ead746b

File tree

4 files changed

+3223
-1606
lines changed

4 files changed

+3223
-1606
lines changed

Sources/SwiftSyntaxBuilder/BuildableNodes.swift.gyb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,24 @@ public struct ${type.buildable()}${conformance_clause(conformances)} {
3737
let ${child.name()}: ${child.type().buildable()}
3838
% end
3939

40+
/// The leading trivia attached to this syntax node once built.
41+
/// This is typically used to add comments (e.g. for documentation).
42+
let leadingTrivia: Trivia
43+
4044
/// Creates a `${type.buildable()}` using the provided parameters.
4145
/// - Parameters:
4246
% for child in children:
4347
/// - ${child.name()}: ${child.documentation()}
4448
% end
4549
public init(
50+
leadingTrivia: Trivia = [],
4651
${',\n '.join(['%s: %s%s' % (
4752
child.name(),
4853
child.type().expressible_as(),
4954
child.type().default_initialization()
5055
) for child in children])}
5156
) {
57+
self.leadingTrivia = leadingTrivia
5258
% for child in children:
5359
% assert_stmt = child.generate_assert_stmt_text_choices(child.name())
5460
self.${child.name()} = ${child.type().generate_expr_convert_param_type_to_storage_type(child.name())}
@@ -95,28 +101,31 @@ public struct ${type.buildable()}${conformance_clause(conformances)} {
95101
/// - Initializing syntax collections using result builders
96102
/// - Initializing tokens without default text using strings
97103
public init(
104+
leadingTrivia: Trivia = [],
98105
${',\n '.join(convenience_init_normal_parameters + convenience_init_result_builder_parameters)}
99106
) {
100107
self.init(
108+
leadingTrivia: leadingTrivia,
101109
${',\n '.join(delegated_init_args)}
102110
)
103111
}
104112
% end
105113

106-
func build${type.base_name()}(format: Format, leadingTrivia: Trivia? = nil) -> ${type.syntax()} {
114+
/// Builds a `${type.syntax()}`.
115+
/// - Parameter format: The `Format` to use.
116+
/// - Parameter leadingTrivia: Additional leading trivia to attach, typically used for indentation.
117+
/// - Returns: The built `${type.syntax()}`.
118+
func build${type.base_name()}(format: Format, leadingTrivia additionalLeadingTrivia: Trivia? = nil) -> ${type.syntax()} {
107119
let result = SyntaxFactory.make${type.base_name()}(
108120
${',\n '.join(['%s: %s' % (child.name(), child.generate_expr_build_syntax_node(child.name(), 'format')) for child in children])}
109121
)
110-
if let leadingTrivia = leadingTrivia {
111-
return result.withLeadingTrivia(leadingTrivia + (result.leadingTrivia ?? []))
112-
} else {
113-
return result
114-
}
122+
let combinedLeadingTrivia = leadingTrivia + (additionalLeadingTrivia ?? []) + (result.leadingTrivia ?? [])
123+
return result.withLeadingTrivia(combinedLeadingTrivia)
115124
}
116125

117126
/// Conformance to `${base_type.buildable()}`.
118-
public func build${base_type.base_name()}(format: Format, leadingTrivia: Trivia? = nil) -> ${base_type.syntax()} {
119-
let result = build${type.base_name()}(format: format, leadingTrivia: leadingTrivia)
127+
public func build${base_type.base_name()}(format: Format, leadingTrivia additionalLeadingTrivia: Trivia? = nil) -> ${base_type.syntax()} {
128+
let result = build${type.base_name()}(format: format, leadingTrivia: additionalLeadingTrivia)
120129
return ${base_type.syntax()}(result)
121130
}
122131

0 commit comments

Comments
 (0)