-
Notifications
You must be signed in to change notification settings - Fork 882
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
Improve heuristics for match arm body placement #370
Conversation
Rebased. |
What bug is the second commit addressing? |
This one: https://github.com/nrc/rustfmt/issues/228 That particular case was fixed earlier, but the bug was still present. The indentation would be off for multi-line bodies that started on the same line as the pattern. |
body_str)) | ||
} | ||
} | ||
|
||
// Takes two possible rewrites for the match arm body and chooses the "nicest". | ||
// Bool marks break line or no. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bool seems only to indicate if we took the second option, rather than actually checking for line breaks. It would be nice if we didn't need it tbh.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a very vague comment. What I meant to say is that it indicates whether the result should be placed on a new line.
Could you add the test case from #280 please? Also a match arm like this:
|
r+ with that |
Improve heuristics for match arm body placement
@marcusklaas So, having played with this, I wonder if this was a mistake (sorry). I think that perhaps what we should do is force a block if the arm body contains a new line. E.g.,
would be rewritten to
Then it doesn't matter if we screw up formatting in a tiny space, any newline forces a block, and a block always has maximal space. That saves us using a heuristic, and gives (arguably) better formatting, at the expense of an extra line per match arm (which doesn't seem too bad to me). What do you think? |
Fully agreed. This is actually what I had in mind with this issue: https://github.com/nrc/rustfmt/issues/390. Let's do it! |
Implement some extremely basic heuristic for the placement of the match body arm. We place it on the same line if it fits and contains no more line breaks compared to putting it on the next lines.
This should close https://github.com/nrc/rustfmt/issues/280.
Oh and it also moves some match tests from the
tests/**/expr.rs
totests/**/match.rs
.