-
Notifications
You must be signed in to change notification settings - Fork 129
Description
I have a code base that hasn't fully adopted the official dart formatter. To aid the incremental adoption, I often use the feature in IntelliJ that lets you apply formatting to just a selected block of code. This feature seemed deterministic in the sense that you'd get the same formatting for that block if you instead chose to format the entire file.
After upgrading from Dart 3.6.2 to 3.8.0, formatting a selected block of code no longer gives the same results as formatting the entire file. The format of the selected text seems to be influenced by the trailing_commas config, but not in an expected way.
IntelliJ version: 2024.2.5
Given the following:
void foo({required String aaa, required int bb, required int ccccccccccccccccccccccc,}) {
final a = (aaaaaaaaaaaa: 1, bbbbbbbbbbb: 2, cccccccccccccc: 3, ddddddddddddddddd: 4);
final b = (aaaaaaaaa: 1, bbbbbbbbbbb: 2, cccccccccccccccc: 3, dddddddddddddddd: 4, );
}Expected:
-
Select all of line 1
-
Code > Reformat File > check off "Selected text" > Run
-
Each parameter should be split to a new line
-
Select all of line 2
-
Code > Reformat File > check off "Selected text" > Run
-
Each record field should be split to a new line
-
Select all of line 3
-
Code > Reformat File > check off "Selected text" > Run
-
Each record field should be split to a new line
This works in Dart 3.6.2.
In Dart 3.8.0:
void foo(
{required String aaa, required int bb, required int ccccccccccccccccccccccc,}) {
final a = (aaaaaaaaaaaa: 1, bbbbbbbbbbb: 2, cccccccccccccc: 3, ddddddddddddddddd: 4);
final b = (aaaaaaaaa: 1, bbbbbbbbbbb: 2, cccccccccccccccc: 3, dddddddddddddddd: 4, );
}A couple interesting notes:
- Add a comma to the end of line 2:
void foo({required String aaa, required int bb, required int ccccccccccccccccccccccc,}) {
final a = (aaaaaaaaaaaa: 1, bbbbbbbbbbb: 2, cccccccccccccc: 3, ddddddddddddddddd: 4,);
final b = (aaaaaaaaa: 1, bbbbbbbbbbb: 2, cccccccccccccccc: 3, dddddddddddddddd: 4, );
}- Select all of line 3
- Code > Reformat File > check off "Selected text" > Run
- Each record field is split to a new line as expected:
void foo({required String aaa, required int bb, required int ccccccccccccccccccccccc,}) {
final a = (aaaaaaaaaaaa: 1, bbbbbbbbbbb: 2, cccccccccccccc: 3, ddddddddddddddddd: 4,);
final b = (
aaaaaaaaa: 1,
bbbbbbbbbbb: 2,
cccccccccccccccc: 3,
dddddddddddddddd: 4,
);
}Code > Reformat File > check off "Whole file" > Runyields the expected results:
void foo({
required String aaa,
required int bb,
required int ccccccccccccccccccccccc,
}) {
final a = (
aaaaaaaaaaaa: 1,
bbbbbbbbbbb: 2,
cccccccccccccc: 3,
ddddddddddddddddd: 4,
);
final b = (
aaaaaaaaa: 1,
bbbbbbbbbbb: 2,
cccccccccccccccc: 3,
dddddddddddddddd: 4,
);
}