Pretty printing for closures - much better. #3179
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The way the pretty printer currently handles closures is distinctly unsatisfying (the elipsis are to indicate that there is enough content so that the entire expression cannot fit on a single line; if it could, things would look fine):
What is happening is that the entire closure is wrapped in a pretty printing box; since it can't fit on the line with the do / for, it starts printing it and then has to break the line, and it indents relative to that, so the block is 4 spaces in from the beginning of the closure. A lot better, in my opinion (and how I think a person would write it), is:
That is, the indentation is keyed on the beginning of the expression. Ideally, the way the boxes would be arranged is that the closure argument would be absorbed into the "head" box that has the do/for/let, but writing the code that way seemed to involve more code complexity, as we need to be looking for closures before we actually see the AST expression for them.
What I did, and this pull request implements, fixes the formatting for do and for expressions (they look exactly like the "desired" output). It does this by eliminating the surrounding block (which involved a little bit of refactoring to allow you to close brackets without having a surrounding box to close). It does not fix the let expression - the arguments for the closure start on the following line. I'm actually not sure why this is happening; it might be something with an easy fix. The output of the code that this pull request adds is the following:
I'm not sure if this is the best approach - this is the first that I've dealt with the AST or the pretty printer, so I could be doing something non-optimally. Hopefully people can given feedback on the code / how it works.
The diff includes some changes to libsyntax/print/pp.rs to make the debug output more verbose - this was helpful to me as someone unfamiliar with the code, and so seem to make sense. The rest is changes to libsyntax/print/pprust.rs, based on the changes described.