Skip to content
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

Don't drop comments in collection literals with preserved newlines. #1587

Merged
merged 1 commit into from
Oct 24, 2024

Conversation

munificent
Copy link
Member

@munificent munificent commented Oct 24, 2024

A line comment in a collection literal triggers special formatting where we allow multiple elements in a single line, like:

list = [
  // Comment.
  1, 2, 3,
  4, 5,
  6,
];

The formatter models that by having a DelimitedListBuilder for the entire collection literal. Then the elements that should be packed onto a single line are formatted using a separate DelimitedListBuilder for each line.

In the very rare case where you have a comment after the last element on a line but before the subsequent comma, the comment would get dropped. This is because the comment was sitting in the inner DelimitedListBuilder for the line waiting to be put somewhere, but we then discard that builder to start a new line or when the whole list is done.

This fixes that. Whenever we're done with a line, we hoist any remaining comments up to the outer DelimitedListBuilder. That way the comment gets put after the comma at the end of the line.

Also bump the min SDK constraint to 3.4.0. I don't think dart_style works with an older version because of analyzer's SDK constraints.

Fix #1584.

A line comment in a collection literal triggers special formatting
where we allow multiple elements in a single line, like:

```
list = [
  // Comment.
  1, 2, 3
  4, 5,
  6,
];
```

The formatter models that by having a DelimitedListBuilder for the
entire collection literal. Then the elements that should be packed onto
a single line are formatted using a separate DelimitedListBuilder for
each line.

In the very rare case where you have a comment after the last element
on a line but *before* the subsequent comma, the comment would get
dropped. This is because the comment was sitting in the inner
DelimitedListBuilder for the line waiting to be put somewhere, but we
then discard that builder to start a new line or when the whole list is
done.

This fixes that. Whenever we're done with a line, we hoist any remaining
comments up to the outer DelimitedListBuilder. That way the comment gets
put after the comma at the end of the line.

Also bump the min SDK constraint to 3.4.0. I don't think dart_style
works with an older version because of analyzer's SDK constraints.

Fix #1584.
@munificent munificent merged commit 9a8a342 into main Oct 24, 2024
7 checks passed
@munificent munificent deleted the 1584-collection-comments branch October 24, 2024 18:22
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.

Formatter drops comment before comma in collection literal
2 participants