Skip to content

Commit 914d1d6

Browse files
committed
Don't drop comments in collection literals with preserved newlines.
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.
1 parent 89577e7 commit 914d1d6

File tree

5 files changed

+491
-4
lines changed

5 files changed

+491
-4
lines changed

lib/src/front_end/delimited_list_builder.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ final class DelimitedListBuilder {
138138
_commentsBeforeComma = CommentSequence.empty;
139139
}
140140

141+
/// Adds the contents of [lineBuilder] to this outer [DelimitedListBuilder].
142+
///
143+
/// This is used when preserving newlines inside a collection literal. The
144+
/// [lineBuilder] will be used for the elements that should be packed onto a
145+
/// single line, and this builder is for the rows that are each on their own
146+
/// line.
147+
void addLineBuilder(DelimitedListBuilder lineBuilder) {
148+
// Add the elements of the line to this builder.
149+
add(lineBuilder.build());
150+
151+
// Make sure that any trailing comments on the line aren't lost.
152+
_commentsBeforeComma = lineBuilder._commentsBeforeComma;
153+
}
154+
141155
/// Writes any comments appearing before [token] to the list.
142156
void addCommentsBefore(Token token) {
143157
// Handle comments between the preceding element and this one.

lib/src/front_end/piece_factory.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ mixin PieceFactory {
10821082
elements[i - 1].endToken, element.beginToken)) {
10831083
// This element begins a new line. Add the elements on the previous
10841084
// line to the list builder and start a new line.
1085-
builder.add(lineBuilder.build());
1085+
builder.addLineBuilder(lineBuilder);
10861086
lineBuilder = DelimitedListBuilder(this, lineStyle);
10871087
atLineStart = true;
10881088
}
@@ -1097,7 +1097,8 @@ mixin PieceFactory {
10971097
atLineStart = false;
10981098
}
10991099

1100-
if (!atLineStart) builder.add(lineBuilder.build());
1100+
// Finish the last line if there is anything on it.
1101+
if (!atLineStart) builder.addLineBuilder(lineBuilder);
11011102
}
11021103

11031104
/// Writes a [VariablePiece] for a named or wildcard variable pattern.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: >-
66
Provides an API and a CLI tool.
77
repository: https://github.com/dart-lang/dart_style
88
environment:
9-
sdk: "^3.0.0"
9+
sdk: "^3.4.0"
1010

1111
dependencies:
1212
analyzer: '^6.5.0'

test/tall/expression/list_comment.stmt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,26 @@ var list = [
143143
>>> Space between block comment and ",".
144144
var list = [1,/* a */ 2 /* b */ , 3];
145145
<<<
146-
var list = [1, /* a */ 2 /* b */, 3];
146+
var list = [1, /* a */ 2 /* b */, 3];
147+
>>> Comment before comma with other comments.
148+
var x = [
149+
1 // Comment 1.
150+
,
151+
2 // Comment 2.
152+
];
153+
<<<
154+
var x = [
155+
1, // Comment 1.
156+
2, // Comment 2.
157+
];
158+
>>> Comment before comma with other comments.
159+
var x = [
160+
1 // Comment 1.
161+
,
162+
// Comment 2.
163+
];
164+
<<<
165+
var x = [
166+
1, // Comment 1.
167+
// Comment 2.
168+
];

0 commit comments

Comments
 (0)