-
Notifications
You must be signed in to change notification settings - Fork 125
Add an optional configuration to preserve trailing commas. #1672
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
Conversation
In the old short formatter, when a delimited construct had a trailing comma, it had two effects: 1. The construct would be formatted "Flutter style" where the closing bracket would be moved to the next line and the contents indented +2, as opposed to the earlier style where the bracket stays on the same line as the last element and the elements are indented +4). 2. The construct is forced to split even if it otherwise wouldn't. The new formatter always uses a Flutter-like style for commas-separated constructors, so trailing commas are no longer needed for point 1. When it does split a construct, it always adds a trailing comma. This means that point 2 breaks reversibility. Because of that, and because many users don't want to decide for every construct whether it should split or not, the new tall formatter no longer does point 2. It will remove a trailing comma and collapse a construct if it decides to. However, some users rely heavily on that feature and strongly prefer control over forcing argument lists and other constructs to split. This PR restores that functionality with an opt-in configuration: ``` formatter: trailing_commas: preserve ``` When enabled, if a construct has a trailing comma, the formatter will always split it. Users that preferred that behavior from the old formatter can enable the option and continue to work that way. This does not mean we are generally moving in the direction of a more configurable formatter. This was a behavior that the formatter already supported and we removed it to the detriment of some users' experience. We're restoring that existing behavior for those users who want it. Fix #1652.
Could the option also be a flag on This way the IDE could be configured to use |
Yes this PR adds command-line option syntax to enable it as well with the exactly the syntax you suggest here. |
@munificent Thank you for working on this! I can't express the amount of gratitude I feel. Just a few questions:
// original snippet
late Service service;
void main() {
service.request(Request(method: 'GET', method: 'params', path: '/products/123',));
} Current / default: // page_width: 80
// trailing_commas: automate
late Service service;
void main() {
service.request(
Request(method: 'GET', method: 'params', path: '/products/123'),
);
} Expected, when preserved: ? // page_width: 80
// trailing_commas: preserve
late Service service;
void main() {
service.request(Request(
method: 'GET',
method: 'params',
path: '/products/123',
));
}
Personally, I'm hoping this solution will stay in place indefinitely. Note: I'm in group a). For me, being able to express what I'm thinking as close as possible as I can to code, is crucially important (thus desire to control trailing commas) (it helps me be productive and more creative). But I can totally see how for code being worked on by a large number of contributors, a strict standard is more desirable. I hope Again thank you! |
Happy to help. :)
You'll get: void main() {
service.request(
Request(
method: 'GET',
method: 'params',
path: '/products/123',
),
);
}
It's not a temporary feature and there are no plans to remove it in the future unless it becomes clear that it isn't useful to users. |
This comment was marked as abuse.
This comment was marked as abuse.
In which flutter version will this be available, I don't want to automatically remove the comma |
@maxfrees Flutter 3.32.0 released with this, you can enjoy now 👍
|
In the old short formatter, when a delimited construct had a trailing comma, it had two effects:
The construct would be formatted "Flutter style" where the closing bracket would be moved to the next line and the contents indented +2, as opposed to the earlier style where the bracket stays on the same line as the last element and the elements are indented +4).
The construct is forced to split even if it otherwise wouldn't.
The new formatter always uses a Flutter-like style for commas-separated constructors, so trailing commas are no longer needed for point 1.
When it does split a construct, it always adds a trailing comma. This means that point 2 breaks reversibility. Because of that, and because many users don't want to decide for every construct whether it should split or not, the new tall formatter no longer does point 2. It will remove a trailing comma and collapse a construct if it decides to.
However, some users rely heavily on that feature and strongly prefer control over forcing argument lists and other constructs to split.
This PR restores that functionality with an opt-in configuration:
When enabled, if a construct has a trailing comma, the formatter will always split it. Users that preferred that behavior from the old formatter can enable the option and continue to work that way.
This does not mean we are generally moving in the direction of a more configurable formatter. This was a behavior that the formatter already supported and we removed it to the detriment of some users' experience. We're restoring that existing behavior for those users who want it.
Fix #1652.