Remove the special reverse indentation for cascade targets.
This rule was always of dubious value and looking at how it affects a
large corpus (now that we have one), I think it's pretty clear that the
natural indentation is easier to read.
diff --git a/lib/src/call_chain_visitor.dart b/lib/src/call_chain_visitor.dart
index 4151cb7..fa4a779 100644
--- a/lib/src/call_chain_visitor.dart
+++ b/lib/src/call_chain_visitor.dart
@@ -173,14 +173,7 @@
this._blockCalls, this._hangingCall);
/// Builds chunks for the call chain.
- ///
- /// If [unnest] is `false` than this will not close the expression nesting
- /// created for the call chain and the caller must end it. Used by cascades
- /// to force a cascade after a method chain to be more deeply nested than
- /// the methods.
- void visit({bool? unnest}) {
- unnest ??= true;
-
+ void visit() {
_visitor.builder.nestExpression();
// Try to keep the entire method invocation one line.
@@ -259,8 +252,7 @@
_disableRule();
_endSpan();
-
- if (unnest) _visitor.builder.unnest();
+ _visitor.builder.unnest();
}
/// Returns `true` if the method chain should split if a split occurs inside
diff --git a/lib/src/source_visitor.dart b/lib/src/source_visitor.dart
index 0fb0ca1..bd8da7b 100644
--- a/lib/src/source_visitor.dart
+++ b/lib/src/source_visitor.dart
@@ -564,39 +564,21 @@
@override
void visitCascadeExpression(CascadeExpression node) {
+ // Whether a split in the cascade target expression forces the cascade to
+ // move to the next line. It looks weird to move the cascade down if the
+ // target expression is a collection, so we don't:
+ //
+ // var list = [
+ // stuff
+ // ]
+ // ..add(more);
var splitIfOperandsSplit =
node.cascadeSections.length > 1 || _isCollectionLike(node.target);
-
- // If the cascade sections have consistent names they can be broken
- // normally otherwise they always get their own line.
if (splitIfOperandsSplit) {
builder.startLazyRule(_allowInlineCascade(node) ? Rule() : Rule.hard());
}
- // If the target of the cascade is a method call (or chain of them), we
- // treat the nesting specially. Normally, you would end up with:
- //
- // receiver
- // .method()
- // .method()
- // ..cascade()
- // ..cascade();
- //
- // This is logical, since the method chain is an operand of the cascade
- // expression, so it's more deeply nested. But it looks wrong, so we leave
- // the method chain's nesting active until after the cascade sections to
- // force the *cascades* to be deeper because it looks better:
- //
- // receiver
- // .method()
- // .method()
- // ..cascade()
- // ..cascade();
- if (node.target is MethodInvocation) {
- CallChainVisitor(this, node.target).visit(unnest: false);
- } else {
- visit(node.target);
- }
+ visit(node.target);
builder.nestExpression(indent: Indent.cascade, now: true);
builder.startBlockArgumentNesting();
@@ -621,8 +603,6 @@
builder.endBlockArgumentNesting();
builder.unnest();
-
- if (node.target is MethodInvocation) builder.unnest();
}
/// Whether [expression] is a collection literal, or a call with a trailing
@@ -674,8 +654,8 @@
if (node.target is PrefixExpression) return false;
if (node.target is AwaitExpression) return false;
+ // Only allow a single cascade to be inline.
if (node.cascadeSections.length < 2) return true;
-
return false;
}
diff --git a/test/regression/0100/0137.stmt b/test/regression/0100/0137.stmt
index 159624f..d1c0906 100644
--- a/test/regression/0100/0137.stmt
+++ b/test/regression/0100/0137.stmt
@@ -16,11 +16,11 @@
lib.declarations.values
.where((d) => d is ClassMirror || d is MethodMirror)
.toList()
- ..sort((a, b) {
- if (a.runtimeType == b.runtimeType) {
- return _declarationName(a).compareTo(_declarationName(b));
- }
- if (a is MethodMirror && b is ClassMirror) return -1;
- if (a is ClassMirror && b is MethodMirror) return 1;
- return 0;
- });
\ No newline at end of file
+ ..sort((a, b) {
+ if (a.runtimeType == b.runtimeType) {
+ return _declarationName(a).compareTo(_declarationName(b));
+ }
+ if (a is MethodMirror && b is ClassMirror) return -1;
+ if (a is ClassMirror && b is MethodMirror) return 1;
+ return 0;
+ });
\ No newline at end of file
diff --git a/test/regression/0500/0591.unit b/test/regression/0500/0591.unit
index d91626e..f5112be 100644
--- a/test/regression/0500/0591.unit
+++ b/test/regression/0500/0591.unit
@@ -26,12 +26,11 @@
<<<
void fn80() {
// The number of characters in the `four` line is ..........................80
- var list = (one
- .two()
- .three()
+ var list =
+ (one.two().three()
..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75__'))
- ..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
+ ..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
;
}
>>>
@@ -45,12 +44,11 @@
<<<
void fn79() {
// The number of characters in the `four` line is .........................79
- var list = (one
- .two()
- .three()
+ var list =
+ (one.two().three()
..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75_'))
- ..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
+ ..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
;
}
>>>
@@ -64,12 +62,11 @@
<<<
void fn78() {
// The number of characters in the `four` line is ........................78
- var list = (one
- .two()
- .three()
+ var list =
+ (one.two().three()
..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___75'))
- ..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
+ ..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
;
}
>>>
@@ -83,12 +80,11 @@
<<<
void fn77() {
// The number of characters in the `four` line is .......................77
- var list = (one
- .two()
- .three()
+ var list =
+ (one.two().three()
..four(
'_15___20___25___30___35___40___45___50___55___60___65___70___7'))
- ..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
+ ..six('zzzzzzzzzzzzzzzzzzzzzzzzzzzz');
;
}
>>>
@@ -102,10 +98,8 @@
<<<
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');
;
}
\ No newline at end of file
diff --git a/test/splitting/invocations.stmt b/test/splitting/invocations.stmt
index f569ffa..8ce34f1 100644
--- a/test/splitting/invocations.stmt
+++ b/test/splitting/invocations.stmt
@@ -148,9 +148,9 @@
.method()
.method()
.method()
- ..cascade()
- ..cascade()
- ..cascade();
+ ..cascade()
+ ..cascade()
+ ..cascade();
>>> cascade setters on method chain
object.method().method().method().method()..x=1..y=2;
<<<
@@ -159,8 +159,8 @@
.method()
.method()
.method()
- ..x = 1
- ..y = 2;
+ ..x = 1
+ ..y = 2;
>>> cascade index
object..[index]..method()..[index]=value;
<<<