@@ -166,24 +166,25 @@ private func createTokenFormatFunction() -> FunctionDecl {
166
166
output: " Syntax "
167
167
)
168
168
) {
169
- VariableDecl ( " var node = node " )
169
+ VariableDecl ( " var leadingTrivia = node.leadingTrivia " )
170
+ VariableDecl ( " var trailingTrivia = node.trailingTrivia " )
170
171
SwitchStmt ( expression: MemberAccessExpr ( base: " node " , name: " tokenKind " ) ) {
171
172
for token in SYNTAX_TOKENS where token. name != " ContextualKeyword " {
172
173
SwitchCase ( label: SwitchCaseLabel ( caseItems: CaseItem ( pattern: ExpressionPattern ( expression: MemberAccessExpr ( name: token. swiftKind) ) ) ) ) {
173
174
if token. requiresLeadingSpace {
174
175
IfStmt (
175
176
"""
176
- if node. leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
177
- node. leadingTrivia += .space
177
+ if leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
178
+ leadingTrivia += .space
178
179
}
179
180
"""
180
181
)
181
182
}
182
183
if token. requiresTrailingSpace {
183
184
IfStmt (
184
185
"""
185
- if node. trailingTrivia.isEmpty {
186
- node. trailingTrivia += .space
186
+ if trailingTrivia.isEmpty {
187
+ trailingTrivia += .space
187
188
}
188
189
"""
189
190
)
@@ -200,20 +201,30 @@ private func createTokenFormatFunction() -> FunctionDecl {
200
201
SwitchStmt (
201
202
"""
202
203
switch node.text {
203
- case " async " :
204
- if node. trailingTrivia.isEmpty {
205
- node. trailingTrivia += .space
206
- }
207
- default:
208
- break
204
+ case " async " :
205
+ if trailingTrivia.isEmpty {
206
+ trailingTrivia += .space
207
+ }
208
+ default:
209
+ break
209
210
}
210
211
"""
211
212
)
212
213
}
213
214
}
214
- SequenceExpr ( " node.leadingTrivia = node.leadingTrivia.indented(indentation: indentation) " )
215
- SequenceExpr ( " node.trailingTrivia = node.trailingTrivia.indented(indentation: indentation) " )
216
- SequenceExpr ( " lastRewrittenToken = node " )
217
- ReturnStmt ( " return Syntax(node) " )
215
+ SequenceExpr ( " leadingTrivia = leadingTrivia.indented(indentation: indentation) " )
216
+ SequenceExpr ( " trailingTrivia = trailingTrivia.indented(indentation: indentation) " )
217
+ VariableDecl (
218
+ """
219
+ let rewritten = TokenSyntax(
220
+ node.tokenKind,
221
+ leadingTrivia: leadingTrivia,
222
+ trailingTrivia: trailingTrivia,
223
+ presence: node.presence
224
+ )
225
+ """
226
+ )
227
+ SequenceExpr ( " lastRewrittenToken = rewritten " )
228
+ ReturnStmt ( " return Syntax(rewritten) " )
218
229
}
219
230
}
0 commit comments