dart_style icon indicating copy to clipboard operation
dart_style copied to clipboard

Confusing formatting on unrelated cascades

Open munificent opened this issue 7 years ago • 1 comments

The formatter produces this output:

main() {
  foo.bar.create(Thing()
    ..a = b
    ..c = d)
    ..method().chain((_) => lambda);
}

Note that the last cascade (..method()) is not on the same receiver as the previous ones even though it's indented the same.

munificent avatar Nov 29 '18 17:11 munificent

I spent some time looking into this, but wasn't able to come up with a fix that didn't push a lot of other formatting around in generally worse ways. The main problem is that this interacts poorly with the special rule that cascades indent deeper than a preceding method chain even though technically the method chain is a subexpression:

object
    .method()
    .method()
      ..cascade()
      ..cascade();

We can possibly lose that special rule. I ran it on a corpus and I can't honestly say I think the rule carries its weight. However, a side effect of that is that a lot of expressions preceding cascades go from +2 indentation to +4 because now they get normal expression indentation.

I'm not sure if that's a net win, so sitting on this for now.

munificent avatar Mar 21 '20 01:03 munificent

The forthcoming tall style moves ) to the next line when an argument list which happens to resolve the weird formatting here too. With the new style, you get:

main() {
  foo.bar.create(
    Thing()
      ..a = b
      ..c = d,
  )..method().chain((_) => lambda);
}

munificent avatar Aug 02 '24 16:08 munificent