-
Notifications
You must be signed in to change notification settings - Fork 37
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
RFC: A different approach to avoiding trailing whitespace #139
Conversation
Idea: In principle this behaviour could be controlled via the |
I totally agree that stripping should be done by the layouter, and not as a pass afterwards. The reason I chose the second pass is to not complicate the layouter even more (it’s quite big already), and to be able to configure options better, such as whether to strip inside annotations. Being afraid of unknown edge cases, basically. Now, years later, we have a much better idea of what those edge cases are, and I think putting it in the layouter is a good idea; if it can also be good code, I’m all in. |
Actually, I suspect that |
I benchmarked this using It seems to incur a penalty of about 3% on the timings, which seems acceptable. |
Currently, this patch would indeed remove trailing whitespace within annotations that We could add annotation accounting to Admittedly I'm not very convinced of the practical usefulness of the annotation accounting in EDIT: Also, the whitespace we're talking about here is kind of special – it's incurred by indenting things, not by trailing spaces in literals or something. |
A few preliminary tests in the REPL (using variants of One limitation is with the first line of an indented expression:
This still produces trailing whitespace on the otherwise empty first line. The problem is this whitespace is not a prettyprinter/prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs Lines 815 to 819 in 58c140a
I'm not convinced that we should start checking In any case, I don't think that this is a problem that we'll encounter a lot in practice. I'd rather wait for a bug report! |
In the same |
I've benchmarked the same changes using the
If this patch allows some users to get rid of |
@quchen This still needs some tests and polish, but the core approach seems to good to me. Could you review? |
This is nicer than I would have expected! The only question that remains is whether some users want to keep ordinary trailing whitespace, but I think that’s a corner-use-case, and the vast majority of users might actually want to have trailing whitespace stripped by default. If someone really wants to keep trailing whitespace without annotations, they can hack around this by adding a global dummy annotation around the whole document. So: LGTM, up to the lack of tests :-) |
Thanks for the review, @quchen! :) I've changed my mind about the annotation handling here though. The reason is that the whitespace that we avoid adding here is always the result of the user nesting a document in order to position it on the page. The user never intended to produce whitespace – the whitespace is just a means of positioning the document. So even when this indentation happens within an annotation, it seems implausible that the user wants to protect or even highlight this kind of whitespace. |
Also true. In general I think actually wanting trailing space of any kind is a rare use case. If this is really desirable, this may not really be the right library for it. And even when a user really really wants it, they might introduce a custom annotation with an int that stands for the amount of trailing whitespace, and then write their own renderer that respects just that. tl;dr trim all the trailing space :-) |
This PR should then also deprecate the current |
Thanks again for the feedback!
Clearly, it's more expensive than just properly stripping the comments though, so we should eventually get rid of it. |
This reverts commit 0250072.
removeTrailingWhitespace
was introduced in response to the issue of trailing whitespace on otherwise empty lines in the output ofdhall
(#46).removeTrailingWhitespace
does quite a bit more than just remove whitespace on otherwise empty lines though:SText
andSChar
of any trailing whitespace.These extra features are also useful, but they aren't needed to address the original issue.
This patch addresses the original issue more directly: When we encounter a
Line
during layout, we check how the document continues before adding any indentation.This is sufficient to address the original issue in
dhall
.What I'm currently unsure about is the importance of annotations: Would this break any documents that try to highlight whitespace?!
TODO:
rTW
indent
Travis status, since Github doesn't show it: https://travis-ci.org/github/quchen/prettyprinter/pull_requests