Skip to content

Commit

Permalink
Add more tests for type arguments in object patterns and remove a TOD…
Browse files Browse the repository at this point in the history
…O. (#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.
  • Loading branch information
munificent authored Feb 22, 2024
1 parent a2075fd commit 9dd81d6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
20 changes: 12 additions & 8 deletions lib/src/back_end/code_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,19 @@ class CodeWriter {
var parentIndent = _indentStack.last.indent;
var parentCollapse = _indentStack.last.collapsible;

if (canCollapse) {
// Increase the indent and the collapsible indent.
_indentStack.add(_Indent(parentIndent + indent, parentCollapse + indent));
} else if (parentCollapse > indent) {
// All new indent is collapsed with the existing collapsible indent.
_indentStack.add(_Indent(parentIndent, parentCollapse - indent));
if (parentCollapse == indent) {
// We're indenting by the same existing collapsible amount, so collapse
// this new indentation with that existing one.
_indentStack.add(_Indent(parentIndent, 0));
} else if (canCollapse) {
// We should never get multiple levels of nested collapsible indentation.
assert(parentCollapse == 0);

// Increase the indentation and note that it can be collapsed with
// further indentation.
_indentStack.add(_Indent(parentIndent + indent, indent));
} else {
// Use up the collapsible indent (if any) and then indent by the rest.
indent -= parentCollapse;
// Regular indentation, so just increase the indent.
_indentStack.add(_Indent(parentIndent + indent, 0));
}
}
Expand Down
32 changes: 27 additions & 5 deletions test/pattern/object.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,25 @@ if (obj case Foo(
)) {
;
}
>>> Split in type argument.
>>> Split in type argument with no field subpatterns.
if (obj case LongClassName<First, Second>()) {;}
<<<
### TODO(tall): It formats like this if there's no elements. Similarly with lists, maps etc.
if (obj
case LongClassName<
First,
Second
First,
Second
>()) {
;
}
>>> Split in type argument and body.
>>> Prefer splitting in field subpatterns instead of type arguments.
if (obj case Foo<First, Second>(first: 1)) {;}
<<<
if (obj case Foo<First, Second>(
first: 1,
)) {
;
}
>>> Split in type argument but not field subpatterns.
if (obj case LongClassName<First, Second, Third>(first: 1, second: 2, third: 3)) {;}
<<<
if (obj case LongClassName<
Expand All @@ -186,3 +193,18 @@ if (obj case LongClassName<
>(first: 1, second: 2, third: 3)) {
;
}
>>> Split in type arguments and field subpatterns.
if (obj case LongClassName<First, Second, Third>(first: 1, second: 2, third: 3, fourth: 4)) {;}
<<<
if (obj case LongClassName<
First,
Second,
Third
>(
first: 1,
second: 2,
third: 3,
fourth: 4,
)) {
;
}

0 comments on commit 9dd81d6

Please sign in to comment.