Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@

* Format null-aware elements.

* Allow more code on the same line as a named argument or `=>` (#1536, #1545,
#1668, #1679).

```dart
// Before:
function(
name:
(param, another) =>
veryLongBody,
);

function(
name:
(param) => another(
argument1,
argument2,
argument3,
),
);

// After:
function(
name: (param, another) =>
veryLongBody,
);

function(
name: (param) => another(
argument1,
argument2,
argument3,
),
);
```

* Avoid splitting chains containing only properties.

```dart
Expand Down
32 changes: 19 additions & 13 deletions lib/src/piece/assign.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,29 @@ final class AssignPiece extends Piece {

@override
void format(CodeWriter writer, State state) {
writer.setShapeMode(ShapeMode.other);

// When splitting at the operator, both operands may split or not and
// will be indented if they do.
if (state == State.split) writer.pushIndent(Indent.expression);

writer.format(_left);
writer.splitIf(state == State.split);

if (state == State.split) {
// When splitting at the operator, indent the operands.
writer.pushIndent(Indent.expression);

// Treat a split `=` as potentially headline-shaped if the LHS doesn't
// split. Allows:
//
// variable = another =
// 'split at second "="';
writer.setShapeMode(ShapeMode.beforeHeadline);
writer.format(_left);
writer.setShapeMode(ShapeMode.afterHeadline);

writer.newline();
writer.popIndent();
writer.pushIndent(Indent.assignment);
writer.format(_right);
writer.popIndent();
} else {
writer.format(_left);
writer.space();
writer.format(_right);
}

writer.format(_right);

if (state == State.split) writer.popIndent();
}

@override
Expand Down
16 changes: 16 additions & 0 deletions lib/src/piece/chain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ final class ChainPiece extends Piece {
writer.popIndent();

case _blockFormatTrailingCall:
// Don't treat a cascade as block-shaped in the surrounding context
// even if it block splits. Prefer:
//
// variable = target
// ..cascade(argument);
//
// Over:
//
// variable = target..cascade(
// argument,
// );
//
// Note how the former makes it clearer that `variable` will be assigned
// the value `target` and that the cascade is a secondary side-effect.
if (_isCascade) writer.setShapeMode(ShapeMode.other);

writer.format(_target);

for (var i = 0; i < _calls.length; i++) {
Expand Down
15 changes: 7 additions & 8 deletions test/tall/expression/assignment.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,15 @@ target
.another
.lastOneReallyLong =
reallyLongValue;
>>> Don't block format through nested assignments.
>>> Allow block formatting through nested assignments.
outer = inner = [element1, element2, element3, element4];
<<<
outer =
inner = [
element1,
element2,
element3,
element4,
];
outer = inner = [
element1,
element2,
element3,
element4,
];
>>> Headline format unsplit target of call chain.
variable = (tar + get).method().another().third();
<<<
Expand Down
16 changes: 15 additions & 1 deletion test/tall/invocation/cascade_mixed.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,18 @@ object
>>> Chain with index target.
object..[index].method();
<<<
object..[index].method();
object..[index].method();
>>> Don't treat cascade as block-shaped in assignment.
variable = target..method(argument1, argument2);
<<<
variable = target
..method(argument1, argument2);
>>> Don't treat cascade as block-shaped even if the argument list splits.
variable = target..method(argument1, argument2, argument3);
<<<
variable = target
..method(
argument1,
argument2,
argument3,
);
17 changes: 17 additions & 0 deletions test/tall/invocation/named_argument.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,20 @@ function(
body;
},
);
>>> Headline formatting for a split `=>` function.
function(name: (param, another) => veryLongBody);
<<<
function(
name: (param, another) =>
veryLongBody,
);
>>> Block-like formatting of a `=>` body containing a function call.
function(name: (param) => another(argument1, argument2, argument3));
<<<
function(
name: (param) => another(
argument1,
argument2,
argument3,
),
);
11 changes: 6 additions & 5 deletions test/tall/regression/0000/0040.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"e"
]);
<<<
var s = new Serialization()..addRuleFor(
Various,
constructor: "Foo",
constructorFields: ["d", "e"],
);
var s = new Serialization()
..addRuleFor(
Various,
constructor: "Foo",
constructorFields: ["d", "e"],
);
6 changes: 4 additions & 2 deletions test/tall/regression/0000/0056.stmt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
>>> (indent 8)
_sources[uri] = src = new _MockSdkSource(uri, 'library dart.${uri.path};');
<<<
_sources[uri] =
src = new _MockSdkSource(uri, 'library dart.${uri.path};');
_sources[uri] = src = new _MockSdkSource(
uri,
'library dart.${uri.path};',
);
6 changes: 4 additions & 2 deletions test/tall/regression/0100/0158.unit
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ void foo() {
void foo() {
if (bar) {
if (baz) {
_sources[uri] =
src = new _MockSdkSource(uri, 'library dart.${uri.path};');
_sources[uri] = src = new _MockSdkSource(
uri,
'library dart.${uri.path};',
);
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions test/tall/regression/0200/0221.unit
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ class Foo {
static Column column(Handler onSelection) =>
(Column.defaultBuilder(videoMsg())
..id = 'VIDEO'
..segment =
((row) => row.segmentedStats
.map((s) => s.get(Stats.SEGMENTATION))
.toList())
..segment = ((row) => row.segmentedStats
.map((s) => s.get(Stats.SEGMENTATION))
.toList())
..cell = new Cell(onSelection))
.build();
}
Expand Down
17 changes: 8 additions & 9 deletions test/tall/regression/0500/0500.unit
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@
env.initialize(
names[i],
getter: (TopLevelBinding binding, ExprCont ek, ExprCont k) {
binding.getter =
(TopLevelBinding _, ExprCont ek0, ExprCont k0) => ek0(
"Reading static variable '${binding.name}' during its initialization",
);
binding
.getter = (TopLevelBinding _, ExprCont ek0, ExprCont k0) => ek0(
"Reading static variable '${binding.name}' during its initialization",
);
initializers[i](env, ek, (v) {
binding.getter =
(TopLevelBinding _, ExprCont ek1, ExprCont k1) => k1(v);
binding.getter = (TopLevelBinding _, ExprCont ek1, ExprCont k1) =>
k1(v);
return k(v);
});
},
setter:
(value, ExprCont ek, ExprCont k) =>
ek("NoSuchMethodError: method not found: '${names[i]}='"),
setter: (value, ExprCont ek, ExprCont k) =>
ek("NoSuchMethodError: method not found: '${names[i]}='"),
);
}
};
48 changes: 30 additions & 18 deletions test/tall/regression/0500/0591.unit
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ void fn81() {
<<<
void fn81() {
// The number of characters in the `four` line is ...........................81
var list = (one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75___',
))..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
var list =
(one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75___',
))
..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
;
}
>>>
Expand All @@ -25,9 +27,11 @@ void fn80() {
<<<
void fn80() {
// The number of characters in the `four` line is ..........................80
var list = (one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75__',
))..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
var list =
(one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75__',
))
..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
;
}
>>>
Expand All @@ -41,9 +45,11 @@ void fn79() {
<<<
void fn79() {
// The number of characters in the `four` line is .........................79
var list = (one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75_',
))..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
var list =
(one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75_',
))
..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
;
}
>>>
Expand All @@ -57,9 +63,11 @@ void fn78() {
<<<
void fn78() {
// The number of characters in the `four` line is ........................78
var list = (one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75',
))..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
var list =
(one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75',
))
..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
;
}
>>>
Expand All @@ -73,9 +81,11 @@ void fn77() {
<<<
void fn77() {
// The number of characters in the `four` line is .......................77
var list = (one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___7',
))..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
var list =
(one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___7',
))
..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
;
}
>>>
Expand All @@ -89,8 +99,10 @@ void fn76() {
<<<
void fn76() {
// The number of characters in the `four` line is ......................76
var list = (one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___',
))..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
var list =
(one.two().three()..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___',
))
..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
;
}
10 changes: 4 additions & 6 deletions test/tall/regression/0700/0731.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@
<<<
return Observable(_databaseService.getChatEntries(event)).map((entryList) {
entryList.forEach(
(entry) =>
entry.isFromCurrentUser =
(entry.userId == _userManager.currentUser.id),
(entry) => entry.isFromCurrentUser =
(entry.userId == _userManager.currentUser.id),
);
return entryList;
});
Expand All @@ -95,9 +94,8 @@
return Observable(_databaseService.getChatEntries(event))
.map((entryList) {
entryList.forEach(
(entry) =>
entry.isFromCurrentUser =
(entry.userId == _userManager.currentUser.id),
(entry) => entry.isFromCurrentUser =
(entry.userId == _userManager.currentUser.id),
);
return entryList;
})
Expand Down
Loading