@@ -922,9 +922,7 @@ private 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 @@ private 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 @@ private 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 @@ private 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 @@ private 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 @@ private 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 @@ private 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)
@@ -1780,6 +1776,11 @@ private final class TokenStreamCreator: SyntaxVisitor {
17801776 return . visitChildren
17811777 }
17821778
1779+ override func visit( _ node: TuplePatternElementListSyntax ) -> SyntaxVisitorContinueKind {
1780+ markCommaDelimitedRegion ( node, isCollectionLiteral: false )
1781+ return . visitChildren
1782+ }
1783+
17831784 override func visit( _ node: TryExprSyntax ) -> SyntaxVisitorContinueKind {
17841785 before (
17851786 node. expression. firstToken ( viewMode: . sourceAccurate) ,
@@ -4291,6 +4292,32 @@ private final class TokenStreamCreator: SyntaxVisitor {
42914292 let hasCompoundExpression = !expr. is ( DeclReferenceExprSyntax . self)
42924293 return ( hasCompoundExpression, false )
42934294 }
4295+
4296+ /// Marks a comma-delimited region for the given list, inserting start/end tokens
4297+ /// and recording the last element’s trailing comma (if any) to be ignored.
4298+ ///
4299+ /// - Parameters:
4300+ /// - node: The comma-separated list syntax node.
4301+ /// - isCollectionLiteral: Indicates whether the list should be treated as a collection literal during formatting.
4302+ /// If `true`, the list is affected by the `multiElementCollectionTrailingCommas` configuration.
4303+ private func markCommaDelimitedRegion< Node: CommaSeparatedListSyntaxProtocol > (
4304+ _ node: Node ,
4305+ isCollectionLiteral: Bool
4306+ ) {
4307+ if let lastElement = node. last {
4308+ if let trailingComma = lastElement. trailingComma {
4309+ ignoredTokens. insert ( trailingComma)
4310+ }
4311+ before ( node. first? . firstToken ( viewMode: . sourceAccurate) , tokens: . commaDelimitedRegionStart)
4312+ let endToken =
4313+ Token . commaDelimitedRegionEnd (
4314+ isCollection: isCollectionLiteral,
4315+ hasTrailingComma: lastElement. trailingComma != nil ,
4316+ isSingleElement: node. first == lastElement
4317+ )
4318+ after ( node. lastNodeForTrailingComma? . lastToken ( viewMode: . sourceAccurate) , tokens: [ endToken] )
4319+ }
4320+ }
42944321}
42954322
42964323private func isNestedInPostfixIfConfig( node: Syntax ) -> Bool {
0 commit comments