dart-lang / dart_style

An opinionated formatter/linter for Dart code
https://pub.dev/packages/dart_style
BSD 3-Clause "New" or "Revised" License
649 stars 120 forks source link

Consider how to format method call chains whose target is a split delimited expression #1583

Open loic-sharma opened 1 month ago

loic-sharma commented 1 month ago

We should consider whether method call chains should be indented if the target is a split delimited expression:

// Without indentation
Widget build(BuildContext context) {
  return Container(
    color: Colors.blue,
    padding: EdgeInsets.all(8.0),
  )
  .clipOval();
}

// With indentation - looks weird?
Widget build(BuildContext context) {
  return Container(
    color: Colors.blue,
    padding: EdgeInsets.all(8.0),
  )
    .clipOval();
}

Also, we should consider whether method call chains on single-line constructor expressions should be indented:

// With indentation
Widget build(BuildContext context) {
  return Container(color: Colors.blue)
    .clipOval()
    .center();
}

Prior art

SwiftUI indents method call chains on single-line expressions but does not indent method call chains on split delimited expressions (example).

munificent commented 1 month ago

I spent a little time looking into this. Aesthetically, I really like the proposed style. A call chain following a split delimited expression like a function call or collection literal has always looked pretty wonky and simply not indenting the chain at all so that it lines up with the closing ) or ] in the target seems like a nice fix.

I would still want to test it on a larger corpus and see if it looks weird in some contexts.

However, I haven't been able to actually implement it yet. :( It's running into, I think, the same limitation in the new formatter's IR that is getting in the way of #1465 and #1466. I have some thoughts on how to make the formatter able to handle these cases but so far I haven't been able to get anything working.

I'll try to think about it more.