@@ -26,7 +26,7 @@ extension Trivia {
26
26
27
27
/// Produce trivia from the last newline to the end, dropping anything
28
28
/// prior to that.
29
- func onlyLastLine( ) -> Trivia {
29
+ var onlyLastLine : Trivia {
30
30
guard let lastNewline = pieces. lastIndex ( where: { $0. isNewline } ) else {
31
31
return self
32
32
}
@@ -122,15 +122,13 @@ extension LabeledExprListSyntax {
122
122
/// context.
123
123
func insertingArgument(
124
124
at position: SyntaxChildrenIndex ,
125
- generator: ( Trivia , TokenSyntax ? ) -> LabeledExprSyntax
125
+ generator: ( _ leadingTrivia : Trivia , _ trailingComma : TokenSyntax ? ) -> LabeledExprSyntax
126
126
) -> LabeledExprListSyntax {
127
127
// Turn the arguments into an array so we can manipulate them.
128
128
var arguments = Array ( self )
129
129
130
130
let positionIdx = distance ( from: startIndex, to: position)
131
131
132
- let commaToken = TokenSyntax . commaToken ( )
133
-
134
132
// Figure out leading trivia and adjust the prior argument (if there is
135
133
// one) by adding a comma, if necessary.
136
134
let leadingTrivia : Trivia
@@ -143,7 +141,7 @@ extension LabeledExprListSyntax {
143
141
144
142
// If the prior argument is missing a trailing comma, add one.
145
143
if priorArgument. trailingComma == nil {
146
- arguments [ positionIdx - 1 ] . trailingComma = commaToken
144
+ arguments [ positionIdx - 1 ] . trailingComma = . commaToken( )
147
145
}
148
146
} else if positionIdx + 1 < count {
149
147
leadingTrivia = arguments [ positionIdx + 1 ] . leadingTrivia
@@ -154,7 +152,7 @@ extension LabeledExprListSyntax {
154
152
// Determine whether we need a trailing comma on this argument.
155
153
let trailingComma : TokenSyntax ?
156
154
if position < endIndex {
157
- trailingComma = commaToken
155
+ trailingComma = . commaToken( )
158
156
} else {
159
157
trailingComma = nil
160
158
}
@@ -194,9 +192,7 @@ extension FunctionCallExprSyntax {
194
192
return false
195
193
}
196
194
197
- guard let stringLiteral = nameArgument. expression. as ( StringLiteralExprSyntax . self) ,
198
- let literalValue = stringLiteral. representedLiteralValue
199
- else {
195
+ guard let literalValue = nameArgument. expression. as ( StringLiteralExprSyntax . self) ? . representedLiteralValue else {
200
196
return false
201
197
}
202
198
@@ -220,24 +216,21 @@ extension ArrayExprSyntax {
220
216
) -> ArrayExprSyntax {
221
217
var elements = self . elements
222
218
223
- let commaToken = TokenSyntax . commaToken ( )
224
-
225
219
// If there are already elements, tack it on.
226
220
let leadingTrivia : Trivia
227
221
let trailingTrivia : Trivia
228
222
let leftSquareTrailingTrivia : Trivia
229
223
if let last = elements. last {
230
224
// The leading trivia of the new element should match that of the
231
225
// last element.
232
- leadingTrivia = last. leadingTrivia. onlyLastLine ( )
226
+ leadingTrivia = last. leadingTrivia. onlyLastLine
233
227
234
228
// Add a trailing comma to the last element if it isn't already
235
229
// there.
236
230
if last. trailingComma == nil {
237
231
var newElements = Array ( elements)
238
- newElements [ newElements. count - 1 ] . trailingComma = commaToken
239
- newElements [ newElements. count - 1 ] . expression. trailingTrivia =
240
- Trivia ( )
232
+ newElements [ newElements. count - 1 ] . trailingComma = . commaToken( )
233
+ newElements [ newElements. count - 1 ] . expression. trailingTrivia = Trivia ( )
241
234
newElements [ newElements. count - 1 ] . trailingTrivia = last. trailingTrivia
242
235
elements = ArrayElementListSyntax ( newElements)
243
236
}
@@ -257,7 +250,7 @@ extension ArrayExprSyntax {
257
250
elements. append (
258
251
ArrayElementSyntax (
259
252
expression: element. with ( \. leadingTrivia, leadingTrivia) ,
260
- trailingComma: commaToken. with ( \. trailingTrivia, trailingTrivia)
253
+ trailingComma: . commaToken( ) . with ( \. trailingTrivia, trailingTrivia)
261
254
)
262
255
)
263
256
@@ -351,17 +344,16 @@ extension Array<LabeledExprSyntax> {
351
344
/// Append a potentially labeled argument with a string literal, but only
352
345
/// when the string literal is not nil.
353
346
mutating func appendIf( label: String ? , stringLiteral: String ? ) {
354
- if let stringLiteral {
355
- append ( label: label, stringLiteral: stringLiteral)
356
- }
347
+ guard let stringLiteral else { return }
348
+ append ( label: label, stringLiteral: stringLiteral)
357
349
}
358
350
359
351
/// Append an array literal containing elements that can be rendered
360
352
/// into expression syntax nodes.
361
- mutating func append< T> (
353
+ mutating func append< T: ManifestSyntaxRepresentable > (
362
354
label: String ? ,
363
355
arrayLiteral: [ T ]
364
- ) where T: ManifestSyntaxRepresentable , T . PreferredSyntax == ExprSyntax {
356
+ ) where T. PreferredSyntax == ExprSyntax {
365
357
var elements : [ ArrayElementSyntax ] = [ ]
366
358
for element in arrayLiteral {
367
359
elements. append ( expression: element. asSyntax ( ) )
@@ -402,20 +394,20 @@ extension Array<LabeledExprSyntax> {
402
394
403
395
/// Append an array literal containing elements that can be rendered
404
396
/// into expression syntax nodes.
405
- mutating func appendIf< T> (
397
+ mutating func appendIf< T: ManifestSyntaxRepresentable > (
406
398
label: String ? ,
407
399
arrayLiteral: [ T ] ?
408
- ) where T: ManifestSyntaxRepresentable , T . PreferredSyntax == ExprSyntax {
400
+ ) where T. PreferredSyntax == ExprSyntax {
409
401
guard let arrayLiteral else { return }
410
402
append ( label: label, arrayLiteral: arrayLiteral)
411
403
}
412
404
413
405
/// Append an array literal containing elements that can be rendered
414
406
/// into expression syntax nodes, but only if it's not empty.
415
- mutating func appendIfNonEmpty< T> (
407
+ mutating func appendIfNonEmpty< T: ManifestSyntaxRepresentable > (
416
408
label: String ? ,
417
409
arrayLiteral: [ T ]
418
- ) where T: ManifestSyntaxRepresentable , T . PreferredSyntax == ExprSyntax {
410
+ ) where T. PreferredSyntax == ExprSyntax {
419
411
if arrayLiteral. isEmpty { return }
420
412
421
413
append ( label: label, arrayLiteral: arrayLiteral)
@@ -453,22 +445,22 @@ fileprivate extension SyntaxProtocol {
453
445
}
454
446
455
447
extension FunctionCallExprSyntax {
456
- /// Produce source edits that will add the given new element to the
448
+ /// Perform source edits that will add the given new element to the
457
449
/// array for an argument with the given label (if there is one), or
458
450
/// introduce a new argument with an array literal containing only the
459
451
/// new element.
460
452
///
461
453
/// - Parameters:
462
454
/// - label: The argument label for the argument whose array will be
463
455
/// added or modified.
464
- /// - trailingLabels : The argument labels that could follow the label,
456
+ /// - labelsAfter : The argument labels that could follow the label,
465
457
/// which helps determine where the argument should be inserted if
466
458
/// it doesn't exist yet.
467
459
/// - newElement: The new element.
468
460
/// - Returns: the function call after making this change.
469
461
func appendingToArrayArgument(
470
462
label: String ,
471
- trailingLabels : Set < String > ,
463
+ labelsAfter : Set < String > ,
472
464
newElement: ExprSyntax
473
465
) throws -> FunctionCallExprSyntax {
474
466
// If there is already an argument with this name, append to the array
@@ -483,7 +475,7 @@ extension FunctionCallExprSyntax {
483
475
484
476
// Format the element appropriately for the context.
485
477
let indentation = Trivia (
486
- pieces: arg. leadingTrivia. filter { $0 . isSpaceOrTab }
478
+ pieces: arg. leadingTrivia. filter ( \ . isSpaceOrTab)
487
479
)
488
480
let format = BasicFormat (
489
481
indentationWidth: [ defaultIndent] ,
@@ -504,13 +496,13 @@ extension FunctionCallExprSyntax {
504
496
505
497
// Insert the new argument at the appropriate place in the call.
506
498
let insertionPos = arguments. findArgumentInsertionPosition (
507
- labelsAfter: trailingLabels
499
+ labelsAfter: labelsAfter
508
500
)
509
501
let newArguments = arguments. insertingArgument (
510
502
at: insertionPos
511
503
) { ( leadingTrivia, trailingComma) in
512
504
// Format the element appropriately for the context.
513
- let indentation = Trivia ( pieces: leadingTrivia. filter { $0 . isSpaceOrTab } )
505
+ let indentation = Trivia ( pieces: leadingTrivia. filter ( \ . isSpaceOrTab) )
514
506
let format = BasicFormat (
515
507
indentationWidth: [ defaultIndent] ,
516
508
initialIndentation: indentation. appending ( defaultIndent)
@@ -521,9 +513,7 @@ extension FunctionCallExprSyntax {
521
513
// Form the array.
522
514
let newArgument = ArrayExprSyntax (
523
515
leadingTrivia: . space,
524
- leftSquare: . leftSquareToken(
525
- trailingTrivia: . newline
526
- ) ,
516
+ leftSquare: . leftSquareToken( trailingTrivia: . newline) ,
527
517
elements: ArrayElementListSyntax (
528
518
[
529
519
ArrayElementSyntax (
@@ -532,9 +522,7 @@ extension FunctionCallExprSyntax {
532
522
)
533
523
]
534
524
) ,
535
- rightSquare: . rightSquareToken(
536
- leadingTrivia: leadingTrivia
537
- )
525
+ rightSquare: . rightSquareToken( leadingTrivia: leadingTrivia)
538
526
)
539
527
540
528
// Create the labeled argument for the array.
0 commit comments