@@ -1122,7 +1122,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1122
1122
// When it's parenthesized, the input is a `ParameterClauseSyntax`. Otherwise, it's a
1123
1123
// `ClosureParamListSyntax`. The parenthesized version is wrapped in open/close breaks so that
1124
1124
// the parens create an extra level of indentation.
1125
- if let parameterClause = input. as ( ParameterClauseSyntax . self) {
1125
+ if let parameterClause = input. as ( ClosureParameterClauseSyntax . self) {
1126
1126
// Whether we should prioritize keeping ") throws -> <return_type>" together. We can only do
1127
1127
// this if the closure has arguments.
1128
1128
let keepOutputTogether =
@@ -1141,7 +1141,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1141
1141
after ( input. lastToken, tokens: . close)
1142
1142
}
1143
1143
1144
- arrangeParameterClause ( parameterClause, forcesBreakBeforeRightParen: true )
1144
+ arrangeClosureParameterClause ( parameterClause, forcesBreakBeforeRightParen: true )
1145
1145
} else {
1146
1146
// Group around the arguments, but don't use open/close breaks because there are no parens
1147
1147
// to create a new scope.
@@ -1245,6 +1245,30 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1245
1245
return . visitChildren
1246
1246
}
1247
1247
1248
+ override func visit( _ node: ClosureParameterClauseSyntax ) -> SyntaxVisitorContinueKind {
1249
+ // Prioritize keeping ") throws -> <return_type>" together. We can only do this if the function
1250
+ // has arguments.
1251
+ if !node. parameterList. isEmpty && config. prioritizeKeepingFunctionOutputTogether {
1252
+ // Due to visitation order, this .open corresponds to a .close added in FunctionDeclSyntax
1253
+ // or SubscriptDeclSyntax.
1254
+ before ( node. rightParen, tokens: . open)
1255
+ }
1256
+
1257
+ return . visitChildren
1258
+ }
1259
+
1260
+ override func visit( _ node: EnumCaseParameterClauseSyntax ) -> SyntaxVisitorContinueKind {
1261
+ // Prioritize keeping ") throws -> <return_type>" together. We can only do this if the function
1262
+ // has arguments.
1263
+ if !node. parameterList. isEmpty && config. prioritizeKeepingFunctionOutputTogether {
1264
+ // Due to visitation order, this .open corresponds to a .close added in FunctionDeclSyntax
1265
+ // or SubscriptDeclSyntax.
1266
+ before ( node. rightParen, tokens: . open)
1267
+ }
1268
+
1269
+ return . visitChildren
1270
+ }
1271
+
1248
1272
override func visit( _ node: ParameterClauseSyntax ) -> SyntaxVisitorContinueKind {
1249
1273
// Prioritize keeping ") throws -> <return_type>" together. We can only do this if the function
1250
1274
// has arguments.
@@ -1257,6 +1281,37 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1257
1281
return . visitChildren
1258
1282
}
1259
1283
1284
+ override func visit( _ node: ClosureParameterSyntax ) -> SyntaxVisitorContinueKind {
1285
+ before ( node. firstToken, tokens: . open)
1286
+ arrangeAttributeList ( node. attributes)
1287
+ before (
1288
+ node. secondName,
1289
+ tokens: . break( . continue, newlines: . elective( ignoresDiscretionary: true ) ) )
1290
+ after ( node. colon, tokens: . break)
1291
+
1292
+ if let trailingComma = node. trailingComma {
1293
+ after ( trailingComma, tokens: . close, . break( . same) )
1294
+ } else {
1295
+ after ( node. lastToken, tokens: . close)
1296
+ }
1297
+ return . visitChildren
1298
+ }
1299
+
1300
+ override func visit( _ node: EnumCaseParameterSyntax ) -> SyntaxVisitorContinueKind {
1301
+ before ( node. firstToken, tokens: . open)
1302
+ before (
1303
+ node. secondName,
1304
+ tokens: . break( . continue, newlines: . elective( ignoresDiscretionary: true ) ) )
1305
+ after ( node. colon, tokens: . break)
1306
+
1307
+ if let trailingComma = node. trailingComma {
1308
+ after ( trailingComma, tokens: . close, . break( . same) )
1309
+ } else {
1310
+ after ( node. lastToken, tokens: . close)
1311
+ }
1312
+ return . visitChildren
1313
+ }
1314
+
1260
1315
override func visit( _ node: FunctionParameterSyntax ) -> SyntaxVisitorContinueKind {
1261
1316
before ( node. firstToken, tokens: . open)
1262
1317
arrangeAttributeList ( node. attributes)
@@ -1413,7 +1468,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1413
1468
after ( node. trailingComma, tokens: . break)
1414
1469
1415
1470
if let associatedValue = node. associatedValue {
1416
- arrangeParameterClause ( associatedValue, forcesBreakBeforeRightParen: false )
1471
+ arrangeEnumCaseParameterClause ( associatedValue, forcesBreakBeforeRightParen: false )
1417
1472
}
1418
1473
1419
1474
return . visitChildren
@@ -2588,6 +2643,42 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2588
2643
return contentsIterator. next ( ) == nil && !commentPrecedesRightBrace
2589
2644
}
2590
2645
2646
+ /// Applies formatting to a collection of parameters for a decl.
2647
+ ///
2648
+ /// - Parameters:
2649
+ /// - parameters: A node that contains the parameters that can be passed to a decl when its
2650
+ /// called.
2651
+ /// - forcesBreakBeforeRightParen: Whether a break should be required before the right paren
2652
+ /// when the right paren is on a different line than the corresponding left paren.
2653
+ private func arrangeClosureParameterClause(
2654
+ _ parameters: ClosureParameterClauseSyntax , forcesBreakBeforeRightParen: Bool
2655
+ ) {
2656
+ guard !parameters. parameterList. isEmpty else { return }
2657
+
2658
+ after ( parameters. leftParen, tokens: . break( . open, size: 0 ) , . open( argumentListConsistency ( ) ) )
2659
+ before (
2660
+ parameters. rightParen,
2661
+ tokens: . break( . close( mustBreak: forcesBreakBeforeRightParen) , size: 0 ) , . close)
2662
+ }
2663
+
2664
+ /// Applies formatting to a collection of enum case parameters for a decl.
2665
+ ///
2666
+ /// - Parameters:
2667
+ /// - parameters: A node that contains the parameters that can be passed to a decl when its
2668
+ /// called.
2669
+ /// - forcesBreakBeforeRightParen: Whether a break should be required before the right paren
2670
+ /// when the right paren is on a different line than the corresponding left paren.
2671
+ private func arrangeEnumCaseParameterClause(
2672
+ _ parameters: EnumCaseParameterClauseSyntax , forcesBreakBeforeRightParen: Bool
2673
+ ) {
2674
+ guard !parameters. parameterList. isEmpty else { return }
2675
+
2676
+ after ( parameters. leftParen, tokens: . break( . open, size: 0 ) , . open( argumentListConsistency ( ) ) )
2677
+ before (
2678
+ parameters. rightParen,
2679
+ tokens: . break( . close( mustBreak: forcesBreakBeforeRightParen) , size: 0 ) , . close)
2680
+ }
2681
+
2591
2682
/// Applies formatting to a collection of parameters for a decl.
2592
2683
///
2593
2684
/// - Parameters:
0 commit comments