Skip to content

Commit ef2b2ab

Browse files
committed
Factor out utility method for format-leadingTrivia params
1 parent 2a40750 commit ef2b2ab

File tree

3 files changed

+27
-57
lines changed

3 files changed

+27
-57
lines changed

Sources/SwiftSyntaxBuilderGeneration/SyntaxUtilities.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,24 @@ func createTypeInheritanceClause(conformances: [String]) -> TypeInheritanceClaus
6161
}
6262
}
6363
}
64+
65+
/// Create a parameter clause of the form `format: .comma, leadingTrivia: Trivia = nil`
66+
/// where the presence of ` = nil` is controlled by `withDefaultTrivia`.
67+
func createFormatLeadingTriviaParameters(withDefaultTrivia: Bool = false) -> ParameterClause {
68+
ParameterClause(
69+
parameterList: [
70+
FunctionParameter(
71+
firstName: .identifier("format"),
72+
colon: .colon,
73+
type: "Format",
74+
trailingComma: .comma
75+
),
76+
FunctionParameter(
77+
firstName: .identifier("leadingTrivia"),
78+
colon: .colon,
79+
type: OptionalType(wrappedType: "Trivia"),
80+
defaultArgument: withDefaultTrivia ? InitializerClause(value: "nil") : nil
81+
),
82+
]
83+
)
84+
}

Sources/SwiftSyntaxBuilderGeneration/Templates/BuildableBaseProtocolsFile.swift

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let buildableBaseProtocolsFile = SourceFile {
4646
].map { .docLineComment($0) + .newline }.reduce([], +),
4747
identifier: .identifier("build\(type.baseName)List"),
4848
signature: FunctionSignature(
49-
input: formatLeadingTriviaParameters(),
49+
input: createFormatLeadingTriviaParameters(),
5050
output: ArrayType(elementType: type.syntax)
5151
),
5252
body: nil
@@ -66,7 +66,7 @@ let buildableBaseProtocolsFile = SourceFile {
6666
].map { .docLineComment($0) + .newline }.reduce([], +),
6767
identifier: .identifier("build\(type.baseName)"),
6868
signature: FunctionSignature(
69-
input: formatLeadingTriviaParameters(),
69+
input: createFormatLeadingTriviaParameters(),
7070
output: type.syntax
7171
),
7272
body: nil
@@ -98,7 +98,7 @@ let buildableBaseProtocolsFile = SourceFile {
9898
].map { .docLineComment($0) + .newline }.reduce([], +),
9999
identifier: .identifier("build\(type.baseName)List"),
100100
signature: FunctionSignature(
101-
input: formatLeadingTriviaParameters(withDefaultTrivia: true),
101+
input: createFormatLeadingTriviaParameters(withDefaultTrivia: true),
102102
output: ArrayType(elementType: type.syntax)
103103
)
104104
) {
@@ -122,7 +122,7 @@ let buildableBaseProtocolsFile = SourceFile {
122122
].map { .docLineComment($0) + .newline }.reduce([], +),
123123
identifier: .identifier("buildSyntax"),
124124
signature: FunctionSignature(
125-
input: formatLeadingTriviaParameters(withDefaultTrivia: true),
125+
input: createFormatLeadingTriviaParameters(withDefaultTrivia: true),
126126
output: "Syntax"
127127
)
128128
) {
@@ -137,24 +137,3 @@ let buildableBaseProtocolsFile = SourceFile {
137137
}
138138
}
139139
}
140-
141-
// Generate a (format: Format, leadingTrivia: Trivia?) parameter clause
142-
// to avoid duplication among the protocols above.
143-
private func formatLeadingTriviaParameters(withDefaultTrivia: Bool = false) -> ParameterClause {
144-
ParameterClause(
145-
parameterList: [
146-
FunctionParameter(
147-
firstName: .identifier("format"),
148-
colon: .colon,
149-
type: "Format",
150-
trailingComma: .comma
151-
),
152-
FunctionParameter(
153-
firstName: .identifier("leadingTrivia"),
154-
colon: .colon,
155-
type: OptionalType(wrappedType: "Trivia"),
156-
defaultArgument: withDefaultTrivia ? InitializerClause(value: "nil") : nil
157-
),
158-
]
159-
)
160-
}

Sources/SwiftSyntaxBuilderGeneration/Templates/BuildableCollectionNodesFile.swift

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,7 @@ let buildableCollectionNodesFile = SourceFile {
115115
modifiers: [TokenSyntax.public],
116116
identifier: .identifier("build\(type.baseName)"),
117117
signature: FunctionSignature(
118-
input: ParameterClause(
119-
parameterList: [
120-
FunctionParameter(
121-
firstName: .identifier("format"),
122-
colon: .colon,
123-
type: "Format",
124-
trailingComma: .comma
125-
),
126-
FunctionParameter(
127-
firstName: .identifier("leadingTrivia"),
128-
colon: .colon,
129-
type: OptionalType(wrappedType: "Trivia"),
130-
defaultArgument: InitializerClause(value: "nil")
131-
),
132-
]
133-
),
118+
input: createFormatLeadingTriviaParameters(withDefaultTrivia: true),
134119
output: type.syntax
135120
)
136121
) {
@@ -190,22 +175,7 @@ let buildableCollectionNodesFile = SourceFile {
190175
modifiers: [TokenSyntax.public],
191176
identifier: .identifier("buildSyntax"),
192177
signature: FunctionSignature(
193-
input: ParameterClause(
194-
parameterList: [
195-
FunctionParameter(
196-
firstName: .identifier("format"),
197-
colon: .colon,
198-
type: "Format",
199-
trailingComma: .comma
200-
),
201-
FunctionParameter(
202-
firstName: .identifier("leadingTrivia"),
203-
colon: .colon,
204-
type: OptionalType(wrappedType: "Trivia"),
205-
defaultArgument: InitializerClause(value: "nil")
206-
),
207-
]
208-
),
178+
input: createFormatLeadingTriviaParameters(withDefaultTrivia: true),
209179
output: "Syntax"
210180
)
211181
) {

0 commit comments

Comments
 (0)