Skip to content

Commit e49a9f3

Browse files
authored
Preserve one blank line between elements in delimited lists. (#1483)
This is consistent with the old formatter and lets users break groups of elements into "sections" inside basically all delimited constructs: lists, maps, objects, argument lists, enums, etc.
1 parent acbe67d commit e49a9f3

24 files changed

+404
-10
lines changed

lib/src/front_end/delimited_list_builder.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ class DelimitedListBuilder {
207207
/// before the closing delimiter.
208208
void _addComments(CommentSequence comments, {required bool hasElementAfter}) {
209209
// Early out if there's nothing to do.
210-
if (_commentsBeforeComma.isEmpty && comments.isEmpty) return;
210+
if (_commentsBeforeComma.isEmpty &&
211+
comments.isEmpty &&
212+
comments.linesBeforeNextToken <= 1) {
213+
return;
214+
}
211215

212216
if (_commentsBeforeComma.requiresNewline || comments.requiresNewline) {
213217
_mustSplit = true;
@@ -238,6 +242,11 @@ class DelimitedListBuilder {
238242
}
239243
}
240244

245+
// Preserve one blank line between successive elements.
246+
if (_elements.isNotEmpty && comments.linesBeforeNextToken > 1) {
247+
_blanksAfter.add(_elements.last);
248+
}
249+
241250
// Comments that are neither hanging nor leading are treated like their own
242251
// elements.
243252
for (var i = 0; i < separateComments.length; i++) {

test/tall/declaration/enum.unit

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,46 @@ enum SomeEnumType<
130130
a,
131131
b,
132132
c,
133-
}
133+
}
134+
>>> Remove blank lines before first and last value. Preserve one between.
135+
enum E {
136+
137+
138+
firstConstant,
139+
140+
141+
142+
secondConstant,
143+
144+
145+
146+
thirdConstant
147+
148+
149+
}
150+
<<<
151+
enum E {
152+
firstConstant,
153+
154+
secondConstant,
155+
156+
thirdConstant,
157+
}
158+
>>> Discard blank lines if doesn't need to split.
159+
enum E {
160+
161+
162+
a,
163+
164+
165+
166+
b,
167+
168+
169+
170+
c,
171+
172+
173+
}
174+
<<<
175+
enum E { a, b, c }

test/tall/declaration/enum_comment.unit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ enum A {
6363
<<<
6464
enum A {
6565
B,
66+
6667
// comment
6768
}
6869
>>> Ensure blank line above doc comments.

test/tall/declaration/enum_metadata.unit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ enum Foo { a,
2626
<<<
2727
enum Foo {
2828
a,
29+
2930
@meta
3031
b,
3132
}

test/tall/expression/list.stmt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const [1, 2, 3];
4747
fourth +
4848
fifth,
4949
];
50-
>>> Remove blank lines around elements.
50+
>>> Remove blank lines before first and last elements. Preserve one between.
5151
[
5252

5353

@@ -66,9 +66,29 @@ const [1, 2, 3];
6666
<<<
6767
[
6868
firstElement,
69+
6970
secondElement,
71+
7072
thirdElement,
7173
];
74+
>>> Discard blank lines if doesn't need to split.
75+
[
76+
77+
78+
1,
79+
80+
81+
82+
2,
83+
84+
85+
86+
3,
87+
88+
89+
];
90+
<<<
91+
[1, 2, 3];
7292
>>> With type argument.
7393
< int > [ 1 , 2 , 3 ];
7494
<<<

test/tall/expression/list_comment.stmt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ var list = [
106106
[
107107
// comment
108108
element,
109+
109110
noComment,
110111

111112
// comment

test/tall/expression/list_preserve_newlines.stmt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ var list = [
7373
element3,
7474
element4,
7575
element5,
76+
7677
element6, element7,
7778

7879
// comment

test/tall/expression/map.stmt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const {first: 1, second: 2};
7777
fifth +
7878
sixth,
7979
});
80-
>>> Remove blank lines around entries.
80+
>>> Remove blank lines before first and last entries. Preserve one between.
8181
({
8282

8383

@@ -96,9 +96,29 @@ const {first: 1, second: 2};
9696
<<<
9797
({
9898
firstElement: 1,
99+
99100
secondElement: 2,
101+
100102
thirdElement: 3,
101103
});
104+
>>> Discard blank lines if doesn't need to split.
105+
({
106+
107+
108+
1,
109+
110+
111+
112+
2,
113+
114+
115+
116+
3,
117+
118+
119+
});
120+
<<<
121+
({1, 2, 3});
102122
>>> With type arguments.
103123
< int , String > { 1 : 'one' , 2 : 'two' };
104124
<<<

test/tall/expression/map_comment.stmt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var map = {
5252
({
5353
// comment
5454
element: 1,
55+
5556
noComment: 2,
5657

5758
// comment

test/tall/expression/map_preserve_newlines.stmt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var map = {
4242
element3: 3,
4343
element4: 4,
4444
element5: 5,
45+
4546
element6: 6, element7: 7,
4647

4748
// comment

0 commit comments

Comments
 (0)