@@ -180,20 +180,41 @@ open class BasicFormat: SyntaxRewriter {
180
180
}
181
181
}
182
182
183
- /// Whether a trailing newline on `token` should be added.
184
- open func requiresTrailingNewline( _ token: TokenSyntax ) -> Bool {
185
- // We don't want to add newlines inside string interpolation
186
- if isInsideStringInterpolation ( token) && token. tokenKind != . multilineStringQuote {
187
- return false
188
- }
189
-
190
- let syntaxToken : Syntax = Syntax ( token)
191
- switch syntaxToken. keyPathInParent {
192
- case \StringLiteralExprSyntax . openQuote:
193
- return token. tokenKind == . multilineStringQuote
194
- default :
195
- return false
196
- }
183
+ open func requiresNewline( between first: TokenSyntax ? , and second: TokenSyntax ? ) -> Bool {
184
+ switch ( first? . tokenKind, second? . tokenKind) {
185
+ case ( . atSign, _) ,
186
+ ( . colon, _) ,
187
+ ( . comma, _) ,
188
+ ( . eof, _) ,
189
+ ( . equal, _) ,
190
+ ( . identifier, . comma) ,
191
+ ( . identifier, . leftBrace) ,
192
+ ( . identifier, . leftParen) ,
193
+ ( . identifier, . period) ,
194
+ ( . identifier, . rightParen) ,
195
+ ( . keyword, . arrow) ,
196
+ ( . keyword( . await ) , . identifier) ,
197
+ ( . keyword( . async ) , _) ,
198
+ ( . keyword( . false ) , _) ,
199
+ ( . keyword( . func) , _) ,
200
+ ( . keyword( . let) , . identifier) ,
201
+ ( . keyword( . let) , . wildcard) ,
202
+ ( . keyword( . throws) , . rightBrace) ,
203
+ ( . keyword( . true ) , _) ,
204
+ ( . keyword( . var) , . identifier) ,
205
+ ( . keyword( . var) , . wildcard) ,
206
+ ( . leftParen, _) ,
207
+ ( . period, . identifier) ,
208
+ ( . rightParen, . identifier) ,
209
+ ( . rightParen, . keyword( . async ) ) ,
210
+ ( . rightSquareBracket, _) ,
211
+ ( _, . colon) ,
212
+ ( _, . eof) ,
213
+ ( _, . equal) :
214
+ return false
215
+ default :
216
+ return true
217
+ }
197
218
}
198
219
199
220
open func requiresWhitespace( between first: TokenSyntax ? , and second: TokenSyntax ? ) -> Bool {
@@ -281,15 +302,6 @@ open class BasicFormat: SyntaxRewriter {
281
302
return true
282
303
}
283
304
284
- open func requiresNewline( between first: TokenSyntax ? , and second: TokenSyntax ? ) -> Bool {
285
- switch ( first? . tokenKind, second? . tokenKind) {
286
- case ( . multilineStringQuote, . stringSegment) :
287
- return true
288
- default :
289
- return false
290
- }
291
- }
292
-
293
305
/// Whether the formatter should consider this token as being mutable.
294
306
/// This allows the diagnostic generator to only assume that missing nodes
295
307
/// will be mutated. Thus, if two tokens need to be separated by a space, it
@@ -368,7 +380,7 @@ open class BasicFormat: SyntaxRewriter {
368
380
var leadingTrivia = token. leadingTrivia
369
381
var trailingTrivia = token. trailingTrivia
370
382
371
- if requiresLeadingNewline ( token) {
383
+ if requiresNewline ( between : previousToken , and : token) {
372
384
// Add a leading newline if the token requires it unless
373
385
// - it already starts with a newline or
374
386
// - the previous token ends with a newline
@@ -393,6 +405,12 @@ open class BasicFormat: SyntaxRewriter {
393
405
anchorPoints [ token] = currentIndentationLevel
394
406
}
395
407
408
+ if requiresNewline ( between: token, and: nextToken) {
409
+ if !trailingTrivia. endsWithNewline && !nextTokenWillStartWithNewline {
410
+ trailingTrivia += . newline
411
+ }
412
+ }
413
+
396
414
// Add a trailing space to the token unless
397
415
// - it already ends with a whitespace or
398
416
// - the next token will start starts with a newline after the rewrite
0 commit comments