Skip to content

Commit 4b64757

Browse files
committed
Factor out utility method for format-leadingTrivia params
1 parent e472363 commit 4b64757

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
@@ -119,22 +119,7 @@ let buildableCollectionNodesFile = SourceFile {
119119
modifiers: [TokenSyntax.public],
120120
identifier: .identifier("build\(type.baseName)"),
121121
signature: FunctionSignature(
122-
input: ParameterClause(
123-
parameterList: [
124-
FunctionParameter(
125-
firstName: .identifier("format"),
126-
colon: .colon,
127-
type: "Format",
128-
trailingComma: .comma
129-
),
130-
FunctionParameter(
131-
firstName: .identifier("leadingTrivia"),
132-
colon: .colon,
133-
type: OptionalType(wrappedType: "Trivia"),
134-
defaultArgument: InitializerClause(value: "nil")
135-
),
136-
]
137-
),
122+
input: createFormatLeadingTriviaParameters(withDefaultTrivia: true),
138123
output: type.syntax
139124
)
140125
) {
@@ -202,22 +187,7 @@ let buildableCollectionNodesFile = SourceFile {
202187
modifiers: [TokenSyntax.public],
203188
identifier: .identifier("buildSyntax"),
204189
signature: FunctionSignature(
205-
input: ParameterClause(
206-
parameterList: [
207-
FunctionParameter(
208-
firstName: .identifier("format"),
209-
colon: .colon,
210-
type: "Format",
211-
trailingComma: .comma
212-
),
213-
FunctionParameter(
214-
firstName: .identifier("leadingTrivia"),
215-
colon: .colon,
216-
type: OptionalType(wrappedType: "Trivia"),
217-
defaultArgument: InitializerClause(value: "nil")
218-
),
219-
]
220-
),
190+
input: createFormatLeadingTriviaParameters(withDefaultTrivia: true),
221191
output: "Syntax"
222192
)
223193
) {

0 commit comments

Comments
 (0)