12
12
13
13
import Basics
14
14
import PackageModel
15
+ import SwiftBasicFormat
15
16
import SwiftSyntax
16
17
import SwiftParser
17
18
@@ -244,12 +245,22 @@ extension Array<ArrayElementSyntax> {
244
245
/// Append a new argument expression.
245
246
mutating func append( expression: ExprSyntax ) {
246
247
// Add a comma on the prior expression, if there is one.
248
+ let leadingTrivia : Trivia ?
247
249
if count > 0 {
248
- self [ count - 1 ] . trailingComma = TokenSyntax . commaToken ( trailingTrivia: . space)
250
+ self [ count - 1 ] . trailingComma = TokenSyntax . commaToken ( )
251
+ leadingTrivia = . newline
252
+
253
+ // Adjust the first element to start with a newline
254
+ if count == 1 {
255
+ self [ 0 ] . leadingTrivia = . newline
256
+ }
257
+ } else {
258
+ leadingTrivia = nil
249
259
}
250
260
251
261
append (
252
262
ArrayElementSyntax (
263
+ leadingTrivia: leadingTrivia,
253
264
expression: expression
254
265
)
255
266
)
@@ -262,12 +273,26 @@ extension Array<LabeledExprSyntax> {
262
273
/// Append a potentially labeled argument with the argument expression.
263
274
mutating func append( label: String ? , expression: ExprSyntax ) {
264
275
// Add a comma on the prior expression, if there is one.
276
+ let leadingTrivia : Trivia
265
277
if count > 0 {
266
- self [ count - 1 ] . trailingComma = TokenSyntax . commaToken ( trailingTrivia: . space)
278
+ self [ count - 1 ] . trailingComma = TokenSyntax . commaToken ( )
279
+ leadingTrivia = . newline
280
+
281
+ // Adjust the first element to start with a newline
282
+ if count == 1 {
283
+ self [ 0 ] . leadingTrivia = . newline
284
+ }
285
+ } else {
286
+ leadingTrivia = Trivia ( )
267
287
}
268
288
269
289
// Add the new expression.
270
- append ( LabeledExprSyntax ( label: label, expression: expression) )
290
+ append (
291
+ LabeledExprSyntax (
292
+ label: label,
293
+ expression: expression
294
+ ) . with ( \. leadingTrivia, leadingTrivia)
295
+ )
271
296
}
272
297
273
298
/// Append a potentially labeled argument with a string literal.
@@ -294,7 +319,19 @@ extension Array<LabeledExprSyntax> {
294
319
elements. append ( expression: element. asSyntax ( ) )
295
320
}
296
321
297
- let array = ArrayExprSyntax ( elements: ArrayElementListSyntax ( elements) )
322
+ // When we have more than one element in the array literal, we add
323
+ // newlines at the beginning of each element. Do the same for the
324
+ // right square bracket.
325
+ let rightSquareLeadingTrivia : Trivia = elements. count > 0
326
+ ? . newline
327
+ : Trivia ( )
328
+
329
+ let array = ArrayExprSyntax (
330
+ elements: ArrayElementListSyntax ( elements) ,
331
+ rightSquare: . rightSquareToken(
332
+ leadingTrivia: rightSquareLeadingTrivia
333
+ )
334
+ )
298
335
append ( label: label, expression: ExprSyntax ( array) )
299
336
}
300
337
@@ -350,8 +387,19 @@ extension FunctionCallExprSyntax {
350
387
)
351
388
}
352
389
390
+ // Format the element appropriately for the context.
391
+ let indentation = Trivia (
392
+ pieces: arg. leadingTrivia. filter { $0. isSpaceOrTab }
393
+ )
394
+ let format = BasicFormat (
395
+ indentationWidth: [ defaultIndent ] ,
396
+ initialIndentation: indentation. appending ( defaultIndent)
397
+ )
398
+ let formattedElement = newElement. formatted ( using: format)
399
+ . cast ( ExprSyntax . self)
400
+
353
401
let updatedArgArray = argArray. appending (
354
- element: newElement ,
402
+ element: formattedElement ,
355
403
outerLeadingTrivia: arg. leadingTrivia
356
404
)
357
405
return [ . replace( argArray, with: updatedArgArray. description) ]
@@ -366,36 +414,40 @@ extension FunctionCallExprSyntax {
366
414
let newArguments = arguments. insertingArgument (
367
415
at: insertionPos
368
416
) { ( leadingTrivia, trailingComma) in
369
- // The argument is always [ element ], but if we have any newlines
370
- // in the leading trivia, then we really want to split it across
371
- // multiple lines, like this:
372
- // [
373
- // element
374
- // ]
375
- let newArgument : ExprSyntax
376
- if !leadingTrivia. hasNewlines {
377
- newArgument = " [ \( newElement) , ] "
378
- } else {
379
- let innerTrivia = leadingTrivia. appending ( defaultIndent)
380
- let arrayExpr = ArrayExprSyntax (
381
- leadingTrivia: . space,
382
- elements: [
417
+ // Format the element appropriately for the context.
418
+ let indentation = Trivia ( pieces: leadingTrivia. filter { $0. isSpaceOrTab } )
419
+ let format = BasicFormat (
420
+ indentationWidth: [ defaultIndent ] ,
421
+ initialIndentation: indentation. appending ( defaultIndent)
422
+ )
423
+ let formattedElement = newElement. formatted ( using: format)
424
+ . cast ( ExprSyntax . self)
425
+
426
+ // Form the array.
427
+ let newArgument = ArrayExprSyntax (
428
+ leadingTrivia: . space,
429
+ leftSquare: . leftSquareToken(
430
+ trailingTrivia: . newline
431
+ ) ,
432
+ elements: ArrayElementListSyntax (
433
+ [
383
434
ArrayElementSyntax (
384
- leadingTrivia: innerTrivia,
385
- expression: newElement,
435
+ expression: formattedElement,
386
436
trailingComma: . commaToken( )
387
437
)
388
- ] ,
389
- rightSquare: . rightSquareToken( leadingTrivia: leadingTrivia)
438
+ ]
439
+ ) ,
440
+ rightSquare: . rightSquareToken(
441
+ leadingTrivia: leadingTrivia
390
442
)
391
- newArgument = ExprSyntax ( arrayExpr)
392
- }
443
+ )
393
444
445
+ // Create the labeled argument for the array.
394
446
return LabeledExprSyntax (
395
447
leadingTrivia: leadingTrivia,
396
448
label: " \( raw: label) " ,
397
449
colon: . colonToken( ) ,
398
- expression: newArgument,
450
+ expression: ExprSyntax ( newArgument) ,
399
451
trailingComma: trailingComma
400
452
)
401
453
}
0 commit comments