Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong indent for lambda inside a method chain #755

Closed
passsy opened this issue Nov 20, 2018 · 4 comments
Closed

wrong indent for lambda inside a method chain #755

passsy opened this issue Nov 20, 2018 · 4 comments

Comments

@passsy
Copy link

passsy commented Nov 20, 2018

dartfmt makes method chains hard to read when a list operator receives a multi line lambda. Mixin multi and single line lambdas feels inconsistent.

// original input (well formatted in my opinion)
void testFormatting(List<String> list) {
  list.reversed
      .map((it) {
        if (it.contains("a")) {
          return it.toUpperCase();
        } else {
          return it.toLowerCase();
        }
      })
      .expand((it) => it.runes)
      .firstWhere((it) => it < "T".runes.first);
}
// dartfmt output
void testFormatting(List<String> list) {
  list.reversed
      .map((it) {
    if (it.contains("a")) {
      return it.toUpperCase();
    } else {
      return it.toLowerCase();
    }
  })
      .expand((it) => it.runes)
      .firstWhere((it) => it < "T".runes.first);
}
@srawlins
Copy link
Member

srawlins commented Nov 20, 2018

This is similar to the conversation in #731, especially @munificent's last example.

(Not that it's a duplicate, necessarily; just linking similar issues.)

@passsy
Copy link
Author

passsy commented Nov 20, 2018

#731 is similar but I don't request the chain to be indented even further down the chain. As @munificent pointed out it would march farther and farther to the right.

// requested solution in #731 (behaves poorly on long chains)
void testFormatting(List<String> list) {
  list.reversed
      .map((it) {
        if (it.contains("a")) {
          return it.toUpperCase();
        } else {
          return it.toLowerCase();
        }
      })
        .map((it) => it.toString())
          .map((it) => it.toString())
            .map((it) => it.toString())
        	  .map((it) => it.toString());
}

I only request the multi-line lamda to be indented based on the receiving function rather than the outer function.

@munificent
Copy link
Member

The output here is deliberate, but not great. Formatting function literals inside method chains well is really hard. For any given set of rules I've come up with, there seem to be real examples in the wild that look bad. The current set of rules — which are quite complex — are a compromise between a lot of different needs.

I'd like to do something better, but so far I haven't been able to come up with anything.

@munificent
Copy link
Member

The forthcoming tall style does what you want and gives the same formatting as the original input code:

void testFormatting(List<String> list) {
  list.reversed
      .map((it) {
        if (it.contains("a")) {
          return it.toUpperCase();
        } else {
          return it.toLowerCase();
        }
      })
      .expand((it) => it.runes)
      .firstWhere((it) => it < "T".runes.first);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants