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

Improve heuristics for match arm body placement #370

Merged
merged 3 commits into from
Sep 27, 2015
Merged

Improve heuristics for match arm body placement #370

merged 3 commits into from
Sep 27, 2015

Conversation

marcusklaas
Copy link
Contributor

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 to tests/**/match.rs.

@marcusklaas
Copy link
Contributor Author

Rebased.

@nrc
Copy link
Member

nrc commented Sep 26, 2015

What bug is the second commit addressing?

@marcusklaas
Copy link
Contributor Author

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.
Copy link
Member

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.

Copy link
Contributor Author

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.

@nrc
Copy link
Member

nrc commented Sep 26, 2015

Could you add the test case from #280 please? Also a match arm like this:

            ast::ItemConst(ref typ, ref expr) =>
                self.process_static_or_const_item(item, &typ, &expr),

@nrc
Copy link
Member

nrc commented Sep 26, 2015

r+ with that

marcusklaas added a commit that referenced this pull request Sep 27, 2015
Improve heuristics for match arm body placement
@marcusklaas marcusklaas merged commit afee62e into rust-lang:master Sep 27, 2015
@marcusklaas marcusklaas deleted the match-arm-delining branch September 27, 2015 10:00
@nrc
Copy link
Member

nrc commented Sep 29, 2015

@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.,

ast::ItemConst(ref typ, ref expr) =>
    self.process_static_or_const_item(item, &typ, &expr),

would be rewritten to

ast::ItemConst(ref typ, ref expr) => {
    self.process_static_or_const_item(item, &typ, &expr),
}

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?

@marcusklaas
Copy link
Contributor Author

Fully agreed. This is actually what I had in mind with this issue: https://github.com/nrc/rustfmt/issues/390. Let's do it!

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

Successfully merging this pull request may close these issues.

2 participants