@@ -1095,7 +1095,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1095
1095
// When it's parenthesized, the input is a `ParameterClauseSyntax`. Otherwise, it's a
1096
1096
// `ClosureParamListSyntax`. The parenthesized version is wrapped in open/close breaks so that
1097
1097
// the parens create an extra level of indentation.
1098
- if let parameterClause = input. as ( ParameterClauseSyntax . self) {
1098
+ if let parameterClause = input. as ( ClosureParameterClauseSyntax . self) {
1099
1099
// Whether we should prioritize keeping ") throws -> <return_type>" together. We can only do
1100
1100
// this if the closure has arguments.
1101
1101
let keepOutputTogether =
@@ -1114,7 +1114,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1114
1114
after ( input. lastToken, tokens: . close)
1115
1115
}
1116
1116
1117
- arrangeParameterClause ( parameterClause, forcesBreakBeforeRightParen: true )
1117
+ arrangeClosureParameterClause ( parameterClause, forcesBreakBeforeRightParen: true )
1118
1118
} else {
1119
1119
// Group around the arguments, but don't use open/close breaks because there are no parens
1120
1120
// to create a new scope.
@@ -1213,6 +1213,30 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1213
1213
return . visitChildren
1214
1214
}
1215
1215
1216
+ override func visit( _ node: ClosureParameterClauseSyntax ) -> SyntaxVisitorContinueKind {
1217
+ // Prioritize keeping ") throws -> <return_type>" together. We can only do this if the function
1218
+ // has arguments.
1219
+ if !node. parameterList. isEmpty && config. prioritizeKeepingFunctionOutputTogether {
1220
+ // Due to visitation order, this .open corresponds to a .close added in FunctionDeclSyntax
1221
+ // or SubscriptDeclSyntax.
1222
+ before ( node. rightParen, tokens: . open)
1223
+ }
1224
+
1225
+ return . visitChildren
1226
+ }
1227
+
1228
+ override func visit( _ node: EnumCaseParameterClauseSyntax ) -> SyntaxVisitorContinueKind {
1229
+ // Prioritize keeping ") throws -> <return_type>" together. We can only do this if the function
1230
+ // has arguments.
1231
+ if !node. parameterList. isEmpty && config. prioritizeKeepingFunctionOutputTogether {
1232
+ // Due to visitation order, this .open corresponds to a .close added in FunctionDeclSyntax
1233
+ // or SubscriptDeclSyntax.
1234
+ before ( node. rightParen, tokens: . open)
1235
+ }
1236
+
1237
+ return . visitChildren
1238
+ }
1239
+
1216
1240
override func visit( _ node: ParameterClauseSyntax ) -> SyntaxVisitorContinueKind {
1217
1241
// Prioritize keeping ") throws -> <return_type>" together. We can only do this if the function
1218
1242
// has arguments.
@@ -1225,6 +1249,37 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1225
1249
return . visitChildren
1226
1250
}
1227
1251
1252
+ override func visit( _ node: ClosureParameterSyntax ) -> SyntaxVisitorContinueKind {
1253
+ before ( node. firstToken, tokens: . open)
1254
+ arrangeAttributeList ( node. attributes)
1255
+ before (
1256
+ node. secondName,
1257
+ tokens: . break( . continue, newlines: . elective( ignoresDiscretionary: true ) ) )
1258
+ after ( node. colon, tokens: . break)
1259
+
1260
+ if let trailingComma = node. trailingComma {
1261
+ after ( trailingComma, tokens: . close, . break( . same) )
1262
+ } else {
1263
+ after ( node. lastToken, tokens: . close)
1264
+ }
1265
+ return . visitChildren
1266
+ }
1267
+
1268
+ override func visit( _ node: EnumCaseParameterSyntax ) -> SyntaxVisitorContinueKind {
1269
+ before ( node. firstToken, tokens: . open)
1270
+ before (
1271
+ node. secondName,
1272
+ tokens: . break( . continue, newlines: . elective( ignoresDiscretionary: true ) ) )
1273
+ after ( node. colon, tokens: . break)
1274
+
1275
+ if let trailingComma = node. trailingComma {
1276
+ after ( trailingComma, tokens: . close, . break( . same) )
1277
+ } else {
1278
+ after ( node. lastToken, tokens: . close)
1279
+ }
1280
+ return . visitChildren
1281
+ }
1282
+
1228
1283
override func visit( _ node: FunctionParameterSyntax ) -> SyntaxVisitorContinueKind {
1229
1284
before ( node. firstToken, tokens: . open)
1230
1285
arrangeAttributeList ( node. attributes)
@@ -1368,7 +1423,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1368
1423
after ( node. trailingComma, tokens: . break)
1369
1424
1370
1425
if let associatedValue = node. associatedValue {
1371
- arrangeParameterClause ( associatedValue, forcesBreakBeforeRightParen: false )
1426
+ arrangeEnumCaseParameterClause ( associatedValue, forcesBreakBeforeRightParen: false )
1372
1427
}
1373
1428
1374
1429
return . visitChildren
@@ -2645,6 +2700,42 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2645
2700
return contentsIterator. next ( ) == nil && !commentPrecedesRightBrace
2646
2701
}
2647
2702
2703
+ /// Applies formatting to a collection of parameters for a decl.
2704
+ ///
2705
+ /// - Parameters:
2706
+ /// - parameters: A node that contains the parameters that can be passed to a decl when its
2707
+ /// called.
2708
+ /// - forcesBreakBeforeRightParen: Whether a break should be required before the right paren
2709
+ /// when the right paren is on a different line than the corresponding left paren.
2710
+ private func arrangeClosureParameterClause(
2711
+ _ parameters: ClosureParameterClauseSyntax , forcesBreakBeforeRightParen: Bool
2712
+ ) {
2713
+ guard !parameters. parameterList. isEmpty else { return }
2714
+
2715
+ after ( parameters. leftParen, tokens: . break( . open, size: 0 ) , . open( argumentListConsistency ( ) ) )
2716
+ before (
2717
+ parameters. rightParen,
2718
+ tokens: . break( . close( mustBreak: forcesBreakBeforeRightParen) , size: 0 ) , . close)
2719
+ }
2720
+
2721
+ /// Applies formatting to a collection of enum case parameters for a decl.
2722
+ ///
2723
+ /// - Parameters:
2724
+ /// - parameters: A node that contains the parameters that can be passed to a decl when its
2725
+ /// called.
2726
+ /// - forcesBreakBeforeRightParen: Whether a break should be required before the right paren
2727
+ /// when the right paren is on a different line than the corresponding left paren.
2728
+ private func arrangeEnumCaseParameterClause(
2729
+ _ parameters: EnumCaseParameterClauseSyntax , forcesBreakBeforeRightParen: Bool
2730
+ ) {
2731
+ guard !parameters. parameterList. isEmpty else { return }
2732
+
2733
+ after ( parameters. leftParen, tokens: . break( . open, size: 0 ) , . open( argumentListConsistency ( ) ) )
2734
+ before (
2735
+ parameters. rightParen,
2736
+ tokens: . break( . close( mustBreak: forcesBreakBeforeRightParen) , size: 0 ) , . close)
2737
+ }
2738
+
2648
2739
/// Applies formatting to a collection of parameters for a decl.
2649
2740
///
2650
2741
/// - Parameters:
0 commit comments