-
Notifications
You must be signed in to change notification settings - Fork 122
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
Indent block-like function expression bodies when split at =>
#1202
Comments
This makes them all line up as if they were empty cases in a switch statement, which is essentially how they behave. Instead of: ```dart e = switch (obj) { constant1 || constant2 || constant3 => body }; ``` Gives: ```dart e = switch (obj) { constant1 || constant2 || constant3 => body }; ``` This addresses some of the bad formatting in #1197. The rest is #1202.
…es. (#1203) * Don't indent outermost "||" pattern operands in switch expression cases. This makes them all line up as if they were empty cases in a switch statement, which is essentially how they behave. Instead of: ```dart e = switch (obj) { constant1 || constant2 || constant3 => body }; ``` Gives: ```dart e = switch (obj) { constant1 || constant2 || constant3 => body }; ``` This addresses some of the bad formatting in #1197. The rest is #1202. * Add comment.
I ran these examples through the forthcoming tall style and they look like: SomeLongFunctionName(
argument:
() => [
listElement,
listElement,
listElement,
],
);
SomeLongFunctionName(
argument:
(longParameterName______) =>
<LongTypeArgument>[
listElement,
listElement,
listElement,
],
);
SomeLongFunctionName(
argument:
(longParameterName______) =>
switch (e) {
1 => listElement,
2 => listElement,
3 => listElement,
},
); The new formatting style doesn't try to keep the parameter list and |
A function expression may have a
=>
body followed by a block-like expression, like a list, map, switch expression, or even another function expression. Right now, that inner expression's block indentation is not increased by virtue of being nested inside a function expression. That leads to output like:I think this formatting works pretty well. Most users seem to be OK with it too since lots of code is formatted this way and I haven't heard many complaints. However, the formatting looks weird if we end up needing to split after the
=>
because the parameter list is long or before the block-like expression has something long before its opening bracket, as in:This looks pretty strange because now the opening delimiter is on its own line (as opposed to be on the same line as the
=>
) but that line doesn't align with the corresponding closing delimiter. It looks particularly strange with a switch expression (#1197):I've tried changing it to always indent the block body, which gives:
This example now looks better, but the first one is worst:
From testing on a corpus of open source code, there are a lot of callsites that would be affected by this, and almost all of them ended up looking worse like the last example here.
The correct fix is to have the block indentation change based on whether we split after the
=>
. If it splits, then indent the block, otherwise don't.Unfortunately, the architecture of the formatter doesn't currently support that. Block indentation is calculated when the AST is transformed into chunks before line splitting.
I won't be able to fix that easily, so I'm filing this to track hopefully being able to do that better fix when I have more time.
The text was updated successfully, but these errors were encountered: