Skip to content

Commit 021df63

Browse files
committed
Factor out utility method for format-leadingTrivia params
1 parent 78ecdeb commit 021df63

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
@@ -121,22 +121,7 @@ let buildableCollectionNodesFile = SourceFile {
121121
modifiers: [TokenSyntax.public],
122122
identifier: .identifier("build\(type.baseName)"),
123123
signature: FunctionSignature(
124-
input: ParameterClause(
125-
parameterList: [
126-
FunctionParameter(
127-
firstName: .identifier("format"),
128-
colon: .colon,
129-
type: "Format",
130-
trailingComma: .comma
131-
),
132-
FunctionParameter(
133-
firstName: .identifier("leadingTrivia"),
134-
colon: .colon,
135-
type: OptionalType(wrappedType: "Trivia"),
136-
defaultArgument: InitializerClause(value: "nil")
137-
),
138-
]
139-
),
124+
input: createFormatLeadingTriviaParameters(withDefaultTrivia: true),
140125
output: type.syntax
141126
)
142127
) {
@@ -201,22 +186,7 @@ let buildableCollectionNodesFile = SourceFile {
201186
modifiers: [TokenSyntax.public],
202187
identifier: .identifier("buildSyntax"),
203188
signature: FunctionSignature(
204-
input: ParameterClause(
205-
parameterList: [
206-
FunctionParameter(
207-
firstName: .identifier("format"),
208-
colon: .colon,
209-
type: "Format",
210-
trailingComma: .comma
211-
),
212-
FunctionParameter(
213-
firstName: .identifier("leadingTrivia"),
214-
colon: .colon,
215-
type: OptionalType(wrappedType: "Trivia"),
216-
defaultArgument: InitializerClause(value: "nil")
217-
),
218-
]
219-
),
189+
input: createFormatLeadingTriviaParameters(withDefaultTrivia: true),
220190
output: "Syntax"
221191
)
222192
) {

0 commit comments

Comments
 (0)