@@ -37,18 +37,24 @@ public struct ${type.buildable()}${conformance_clause(conformances)} {
37
37
let ${ child. name ( ) } : ${ child. type ( ) . buildable ( ) }
38
38
% end
39
39
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
+
40
44
/// Creates a `${type.buildable()}` using the provided parameters.
41
45
/// - Parameters:
42
46
% for child in children:
43
47
/// - ${child.name()}: ${child.documentation()}
44
48
% end
45
49
public init(
50
+ leadingTrivia: Trivia = [ ] ,
46
51
${ ', \n '. join( [ '% s: % s% s' % (
47
52
child. name ( ) ,
48
53
child. type ( ) . expressible_as ( ) ,
49
54
child. type ( ) . default_initialization ( )
50
55
) for child in children] ) }
51
56
) {
57
+ self . leadingTrivia = leadingTrivia
52
58
% for child in children:
53
59
% assert_stmt = child. generate_assert_stmt_text_choices ( child. name ( ) )
54
60
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)} {
95
101
/// - Initializing syntax collections using result builders
96
102
/// - Initializing tokens without default text using strings
97
103
public init(
104
+ leadingTrivia: Trivia = [ ] ,
98
105
${ ', \n '. join( convenience_init_normal_parameters + convenience_init_result_builder_parameters) }
99
106
) {
100
107
self . init (
108
+ leadingTrivia: leadingTrivia,
101
109
${ ', \n '. join( delegated_init_args) }
102
110
)
103
111
}
104
112
% end
105
113
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 ( ) } {
107
119
let result = SyntaxFactory . make ${ type. base_name ( ) } (
108
120
${ ', \n '. join( [ '% s: % s' % ( child. name ( ) , child. generate_expr_build_syntax_node ( child. name ( ) , 'format') ) for child in children] ) }
109
121
)
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)
115
124
}
116
125
117
126
/// 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 )
120
129
return ${ base_type. syntax ( ) } ( result)
121
130
}
122
131
0 commit comments