@@ -1122,7 +1122,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
11221122 // When it's parenthesized, the input is a `ParameterClauseSyntax`. Otherwise, it's a
11231123 // `ClosureParamListSyntax`. The parenthesized version is wrapped in open/close breaks so that
11241124 // the parens create an extra level of indentation.
1125- if let parameterClause = input. as ( ParameterClauseSyntax . self) {
1125+ if let parameterClause = input. as ( ClosureParameterClauseSyntax . self) {
11261126 // Whether we should prioritize keeping ") throws -> <return_type>" together. We can only do
11271127 // this if the closure has arguments.
11281128 let keepOutputTogether =
@@ -1141,7 +1141,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
11411141 after ( input. lastToken, tokens: . close)
11421142 }
11431143
1144- arrangeParameterClause ( parameterClause, forcesBreakBeforeRightParen: true )
1144+ arrangeClosureParameterClause ( parameterClause, forcesBreakBeforeRightParen: true )
11451145 } else {
11461146 // Group around the arguments, but don't use open/close breaks because there are no parens
11471147 // to create a new scope.
@@ -1245,6 +1245,30 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
12451245 return . visitChildren
12461246 }
12471247
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+
12481272 override func visit( _ node: ParameterClauseSyntax ) -> SyntaxVisitorContinueKind {
12491273 // Prioritize keeping ") throws -> <return_type>" together. We can only do this if the function
12501274 // has arguments.
@@ -1257,6 +1281,37 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
12571281 return . visitChildren
12581282 }
12591283
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+
12601315 override func visit( _ node: FunctionParameterSyntax ) -> SyntaxVisitorContinueKind {
12611316 before ( node. firstToken, tokens: . open)
12621317 arrangeAttributeList ( node. attributes)
@@ -1413,7 +1468,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
14131468 after ( node. trailingComma, tokens: . break)
14141469
14151470 if let associatedValue = node. associatedValue {
1416- arrangeParameterClause ( associatedValue, forcesBreakBeforeRightParen: false )
1471+ arrangeEnumCaseParameterClause ( associatedValue, forcesBreakBeforeRightParen: false )
14171472 }
14181473
14191474 return . visitChildren
@@ -2588,6 +2643,42 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
25882643 return contentsIterator. next ( ) == nil && !commentPrecedesRightBrace
25892644 }
25902645
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+
25912682 /// Applies formatting to a collection of parameters for a decl.
25922683 ///
25932684 /// - Parameters:
0 commit comments