Skip to content

Introduce multilineTrailingCommaBehavior configuration #1044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2025

Conversation

TTOzzi
Copy link
Member

@TTOzzi TTOzzi commented Jul 15, 2025

Resolve #946

Added a configuration to support the use of trailing commas extended by SE-0439.

This configuration supports three values:

  • alwaysUsed : always insert trailing commas in multiline lists
  • neverUsed: always remove trailing commas
  • keptAsWritten: preserve existing commas as-is

The default value is keptAsWritten .

The main concern was how to integrate this with the existing multiElementCollectionTrailingCommas configuration.
For now, multilineTrailingCommaBehavior takes higher precedence, but to minimize its impact on multiElementCollectionTrailingCommas, it is implemented so that when set to the default value keptAsWritten, the behavior of multiElementCollectionTrailingCommas is preserved.

I’d appreciate any suggestions or feedback you may have.

Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an abstraction we can come up to reduce the repetition in the individual visitors? Something like a helper function that accepts a generic parameter constrained to a collection of WithTrailingCommaSyntax elements and does the actual work.

@TTOzzi TTOzzi force-pushed the trailingCommasInLists branch from c5fb5d2 to a3de557 Compare August 9, 2025 16:46
@TTOzzi
Copy link
Member Author

TTOzzi commented Aug 9, 2025

Is there an abstraction we can come up to reduce the repetition in the individual visitors? Something like a helper function that accepts a generic parameter constrained to a collection of WithTrailingCommaSyntax elements and does the actual work.

I was struggling because the point where a comma needs to be inserted differs for each syntax, but as I came to know about WithTrailingCommaSyntax, I came up with an idea!
I defined an abstract type and refactored the code accordingly. Thanks!

case .ignore:
isCollection ? configuration.multiElementCollectionTrailingCommas : nil
}
if let shouldHandleCommaDelimitedRegion, shouldHandleCommaDelimitedRegion {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be if shouldHandleCommaDelimitedRegion == true. Being written twice made me have to reread it a couple times to make sure they were referring to the same thing.

protocol CommaSeparatedListSyntaxProtocol: SyntaxCollection where Element: WithTrailingCommaSyntax & Equatable {
/// Indicates whether the list should be treated as a collection for formatting.
/// If `true`, the list is affected by the `multiElementCollectionTrailingCommas` configuration.
var isCollection: Bool { get }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hate to be nitpicky, but can you change this to isCollectionLiteral? isCollection feels like it could be talking about the node itself (which are all Collections).

Or... does this have to be a protocol requirement at all? It looks like you could just pass it in as another parameter to markCommaDelimitedRegion, since it has to be called individually for each node type anyway. That might be cleaner (but the rename is still valid elsewhere that it's referenced in the pretty printer).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. It was for a cleaner implementation.
I’ll make the update along with the rename!

/// for collections only. In all other cases, existing trailing commas are preserved as-is and not modified.
/// If set to `.always` or `.never`, that behavior is applied uniformly across all list types,
/// regardless of `multiElementCollectionTrailingCommas`.
public var trailingCommasInMultilineLists: TrailingCommasInMultilineLists
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this more closely, this feels too similar to the name of the other property below. I don't know if multilineLists really communicates the bit about parameter lists, tuples, closure captures, etc.

What about multilineTrailingCommaBehavior? Then, let's rename the enum to MultilineTrailingCommaBehavior and have the cases be alwaysUsed, neverUsed, and keptAsWritten. I think that helps to better document what's going on.

Later on (not in this PR), we could add a fourth case: onlyCollectionLiterals, which could take over the behavior of the other Boolean setting, and deprecate that one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me! I’ll update the rule name and case names as you suggested.

I have a question regarding the future direction.
From my personal perspective, I thought it might be better to keep the rule simple by handling multiline trailing comma without distinguishing collection literals, and deprecating the existing rule multiElementCollectionTrailingCommas.
Are you considering defining a case like onlyCollectionLiterals to preserve the behavior that distinguishes collection literals?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a question regarding the future direction. From my personal perspective, I thought it might be better to keep the rule simple by handling multiline trailing comma without distinguishing collection literals, and deprecating the existing rule multiElementCollectionTrailingCommas. Are you considering defining a case like onlyCollectionLiterals to preserve the behavior that distinguishes collection literals?

The key word there is "preserve". We have to have that option in order to preserve the existing behavior. Minimally, that means that we always support the existing Boolean for configurations written today, and if we need to maintain it long term, it would be nice to fold it into the new enum as well so there's just a single place to define this behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. I’ll make sure to be careful when adding or changing rules. Thanks for explaining! 🙇

@TTOzzi TTOzzi force-pushed the trailingCommasInLists branch from a3de557 to 89d887a Compare August 12, 2025 17:38
@TTOzzi TTOzzi changed the title Introduce trailingCommasInMultilineLists configuration Introduce multilineTrailingCommaBehavior configuration Aug 12, 2025
@TTOzzi TTOzzi requested a review from allevato August 12, 2025 17:39
Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@allevato allevato merged commit 893c1b7 into swiftlang:main Aug 12, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Support for trailing commas in lists (SE-0439)
2 participants