Skip to content

[Bug] Compared to Dart 3.6.2, IntelliJ "format selected text" gives different output than "format file" #1742

@Mike278

Description

@Mike278

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:

  1. Select all of line 1

  2. Code > Reformat File > check off "Selected text" > Run

  3. Each parameter should be split to a new line

  4. Select all of line 2

  5. Code > Reformat File > check off "Selected text" > Run

  6. Each record field should be split to a new line

  7. Select all of line 3

  8. Code > Reformat File > check off "Selected text" > Run

  9. 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" > Run yields 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,
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions