Skip to content

Commit 9dd81d6

Browse files
authored
Add more tests for type arguments in object patterns and remove a TODO. (#1401)
* Add more tests for type arguments in object patterns and remove a TODO. I'm honestly not sure what the TODO was even trying to say. But I think the formatter behaves like we want here so there's nothing TODO as far as I can tell. But this corner did seem a little under tested, so added some more. * Fix indentation collapsing. A block indent shouldn't be able to partially eat an expression-sized collapsible indentation. The only time we want to actually collapse is when we have two equivalent expression-sized indentations.
1 parent a2075fd commit 9dd81d6

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

lib/src/back_end/code_writer.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,19 @@ class CodeWriter {
153153
var parentIndent = _indentStack.last.indent;
154154
var parentCollapse = _indentStack.last.collapsible;
155155

156-
if (canCollapse) {
157-
// Increase the indent and the collapsible indent.
158-
_indentStack.add(_Indent(parentIndent + indent, parentCollapse + indent));
159-
} else if (parentCollapse > indent) {
160-
// All new indent is collapsed with the existing collapsible indent.
161-
_indentStack.add(_Indent(parentIndent, parentCollapse - indent));
156+
if (parentCollapse == indent) {
157+
// We're indenting by the same existing collapsible amount, so collapse
158+
// this new indentation with that existing one.
159+
_indentStack.add(_Indent(parentIndent, 0));
160+
} else if (canCollapse) {
161+
// We should never get multiple levels of nested collapsible indentation.
162+
assert(parentCollapse == 0);
163+
164+
// Increase the indentation and note that it can be collapsed with
165+
// further indentation.
166+
_indentStack.add(_Indent(parentIndent + indent, indent));
162167
} else {
163-
// Use up the collapsible indent (if any) and then indent by the rest.
164-
indent -= parentCollapse;
168+
// Regular indentation, so just increase the indent.
165169
_indentStack.add(_Indent(parentIndent + indent, 0));
166170
}
167171
}

test/pattern/object.stmt

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,25 @@ if (obj case Foo(
165165
)) {
166166
;
167167
}
168-
>>> Split in type argument.
168+
>>> Split in type argument with no field subpatterns.
169169
if (obj case LongClassName<First, Second>()) {;}
170170
<<<
171-
### TODO(tall): It formats like this if there's no elements. Similarly with lists, maps etc.
172171
if (obj
173172
case LongClassName<
174-
First,
175-
Second
173+
First,
174+
Second
176175
>()) {
177176
;
178177
}
179-
>>> Split in type argument and body.
178+
>>> Prefer splitting in field subpatterns instead of type arguments.
179+
if (obj case Foo<First, Second>(first: 1)) {;}
180+
<<<
181+
if (obj case Foo<First, Second>(
182+
first: 1,
183+
)) {
184+
;
185+
}
186+
>>> Split in type argument but not field subpatterns.
180187
if (obj case LongClassName<First, Second, Third>(first: 1, second: 2, third: 3)) {;}
181188
<<<
182189
if (obj case LongClassName<
@@ -186,3 +193,18 @@ if (obj case LongClassName<
186193
>(first: 1, second: 2, third: 3)) {
187194
;
188195
}
196+
>>> Split in type arguments and field subpatterns.
197+
if (obj case LongClassName<First, Second, Third>(first: 1, second: 2, third: 3, fourth: 4)) {;}
198+
<<<
199+
if (obj case LongClassName<
200+
First,
201+
Second,
202+
Third
203+
>(
204+
first: 1,
205+
second: 2,
206+
third: 3,
207+
fourth: 4,
208+
)) {
209+
;
210+
}

0 commit comments

Comments
 (0)