@@ -922,9 +922,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
922922 }
923923
924924 override func visit( _ node: LabeledExprListSyntax ) -> SyntaxVisitorContinueKind {
925- // Intentionally do nothing here. Since `TupleExprElement`s are used both in tuple expressions
926- // and function argument lists, which need to be formatted, differently, those nodes manually
927- // loop over the nodes and arrange them in those contexts.
925+ markCommaDelimitedRegion ( node, isCollectionLiteral: false )
928926 return . visitChildren
929927 }
930928
@@ -967,18 +965,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
967965 }
968966 }
969967
970- if let lastElement = node. last {
971- if let trailingComma = lastElement. trailingComma {
972- ignoredTokens. insert ( trailingComma)
973- }
974- before ( node. first? . firstToken ( viewMode: . sourceAccurate) , tokens: . commaDelimitedRegionStart)
975- let endToken =
976- Token . commaDelimitedRegionEnd (
977- hasTrailingComma: lastElement. trailingComma != nil ,
978- isSingleElement: node. first == lastElement
979- )
980- after ( lastElement. expression. lastToken ( viewMode: . sourceAccurate) , tokens: [ endToken] )
981- }
968+ markCommaDelimitedRegion ( node, isCollectionLiteral: true )
982969 return . visitChildren
983970 }
984971
@@ -1011,18 +998,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1011998 }
1012999 }
10131000
1014- if let lastElement = node. last {
1015- if let trailingComma = lastElement. trailingComma {
1016- ignoredTokens. insert ( trailingComma)
1017- }
1018- before ( node. first? . firstToken ( viewMode: . sourceAccurate) , tokens: . commaDelimitedRegionStart)
1019- let endToken =
1020- Token . commaDelimitedRegionEnd (
1021- hasTrailingComma: lastElement. trailingComma != nil ,
1022- isSingleElement: node. first == node. last
1023- )
1024- after ( lastElement. lastToken ( viewMode: . sourceAccurate) , tokens: endToken)
1025- }
1001+ markCommaDelimitedRegion ( node, isCollectionLiteral: true )
10261002 return . visitChildren
10271003 }
10281004
@@ -1291,6 +1267,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
12911267 return . visitChildren
12921268 }
12931269
1270+ override func visit( _ node: ClosureCaptureListSyntax ) -> SyntaxVisitorContinueKind {
1271+ markCommaDelimitedRegion ( node, isCollectionLiteral: false )
1272+ return . visitChildren
1273+ }
1274+
12941275 override func visit( _ node: ClosureCaptureSyntax ) -> SyntaxVisitorContinueKind {
12951276 before ( node. firstToken ( viewMode: . sourceAccurate) , tokens: . open)
12961277 after ( node. specifier? . lastToken ( viewMode: . sourceAccurate) , tokens: . break)
@@ -1405,6 +1386,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
14051386 return . visitChildren
14061387 }
14071388
1389+ override func visit( _ node: EnumCaseParameterListSyntax ) -> SyntaxVisitorContinueKind {
1390+ markCommaDelimitedRegion ( node, isCollectionLiteral: false )
1391+ return . visitChildren
1392+ }
1393+
14081394 override func visit( _ node: FunctionParameterClauseSyntax ) -> SyntaxVisitorContinueKind {
14091395 // Prioritize keeping ") throws -> <return_type>" together. We can only do this if the function
14101396 // has arguments.
@@ -1417,6 +1403,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
14171403 return . visitChildren
14181404 }
14191405
1406+ override func visit( _ node: FunctionParameterListSyntax ) -> SyntaxVisitorContinueKind {
1407+ markCommaDelimitedRegion ( node, isCollectionLiteral: false )
1408+ return . visitChildren
1409+ }
1410+
14201411 override func visit( _ node: ClosureParameterSyntax ) -> SyntaxVisitorContinueKind {
14211412 before ( node. firstToken ( viewMode: . sourceAccurate) , tokens: . open)
14221413 arrangeAttributeList ( node. attributes)
@@ -1722,6 +1713,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
17221713 return . visitChildren
17231714 }
17241715
1716+ override func visit( _ node: GenericParameterListSyntax ) -> SyntaxVisitorContinueKind {
1717+ markCommaDelimitedRegion ( node, isCollectionLiteral: false )
1718+ return . visitChildren
1719+ }
1720+
17251721 override func visit( _ node: PrimaryAssociatedTypeClauseSyntax ) -> SyntaxVisitorContinueKind {
17261722 after ( node. leftAngle, tokens: . break( . open, size: 0 ) , . open( argumentListConsistency ( ) ) )
17271723 before ( node. rightAngle, tokens: . break( . close, size: 0 ) , . close)
@@ -1772,6 +1768,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
17721768 return . visitChildren
17731769 }
17741770
1771+ override func visit( _ node: TuplePatternElementListSyntax ) -> SyntaxVisitorContinueKind {
1772+ markCommaDelimitedRegion ( node, isCollectionLiteral: false )
1773+ return . visitChildren
1774+ }
1775+
17751776 override func visit( _ node: TryExprSyntax ) -> SyntaxVisitorContinueKind {
17761777 before (
17771778 node. expression. firstToken ( viewMode: . sourceAccurate) ,
@@ -4283,6 +4284,32 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
42834284 let hasCompoundExpression = !expr. is ( DeclReferenceExprSyntax . self)
42844285 return ( hasCompoundExpression, false )
42854286 }
4287+
4288+ /// Marks a comma-delimited region for the given list, inserting start/end tokens
4289+ /// and recording the last element’s trailing comma (if any) to be ignored.
4290+ ///
4291+ /// - Parameters:
4292+ /// - node: The comma-separated list syntax node.
4293+ /// - isCollectionLiteral: Indicates whether the list should be treated as a collection literal during formatting.
4294+ /// If `true`, the list is affected by the `multiElementCollectionTrailingCommas` configuration.
4295+ private func markCommaDelimitedRegion< Node: CommaSeparatedListSyntaxProtocol > (
4296+ _ node: Node ,
4297+ isCollectionLiteral: Bool
4298+ ) {
4299+ if let lastElement = node. last {
4300+ if let trailingComma = lastElement. trailingComma {
4301+ ignoredTokens. insert ( trailingComma)
4302+ }
4303+ before ( node. first? . firstToken ( viewMode: . sourceAccurate) , tokens: . commaDelimitedRegionStart)
4304+ let endToken =
4305+ Token . commaDelimitedRegionEnd (
4306+ isCollection: isCollectionLiteral,
4307+ hasTrailingComma: lastElement. trailingComma != nil ,
4308+ isSingleElement: node. first == lastElement
4309+ )
4310+ after ( node. lastNodeForTrailingComma? . lastToken ( viewMode: . sourceAccurate) , tokens: [ endToken] )
4311+ }
4312+ }
42864313}
42874314
42884315private func isNestedInPostfixIfConfig( node: Syntax ) -> Bool {
0 commit comments