-
Notifications
You must be signed in to change notification settings - Fork 85
Fix comments in if blocks #1092
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
Conversation
787112a
to
42b7237
Compare
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.
Thanks, this looks good to me. It introduces some complexity, but it is localized, and I think the direction of using something like locatedToken
could be promising to fix these kinds of subtle comment placement issues; there is also a movement in GHC to store the token spans in a systematic way: https://gitlab.haskell.org/ghc/ghc/-/issues/23447
I diffed our hackageTests
output before and after this PR, the results look good to me: https://gist.github.com/amesgen/88b2b92cc9e0d7bc13e7fc8357db057a (pipe through eg delta for a prettier/easier-to-read diff).
hm Thanks for the hackage diff. It's showing two diffs that I didn't expect (or want):
I'll take a look tonight |
To avoid any confusion: The diff is between the Ormolu output before and after this PR, not between the input and the Ormolu output; the original is not visible (but you can find it on Hackage ofc). Ideally, this would be a three-way diff, but I think I would have to set up a git repo for this? |
d6348a5
to
c3264ae
Compare
data/examples/declaration/value/function/if-with-comment-next-to-keyword-out.hs
Show resolved
Hide resolved
@brandonchinn178 Do you consider this ready or is there anything else you'd like to add here? |
It is nice that keyword spans are now available, it wasn't the case when I was originally implementing this. |
This is ready on my end! |
oh you added commits. I would appreciate it if you could avoid adding commits to my PRs without consulting me. It's a bit disorienting; if you just left a comment, I would have been perfectly happy to make the changes. Let me take another look |
Sorry, I usually do that when I'm about to merge, but this time around I wasn't completely sure that you were done with it. |
Sigh I wanted to see if I could improve the comment-next-to-keyword case, but it doesn't look trivial. It seems like the comments are attached to "after the I doubt this will happen often, and this PR is already a huge improvement, so let's merge. Thanks @mrkkrp! |
db0a22a
to
63f27c2
Compare
Thanks a lot! |
Resolves #998
The issue has two bugs associated with it, which are both solved in this PR.
Comments between then/else tokens and body
This bug happens if there's a comment between the
then
/else
tokens and the body of the branch:There are a few bugs here:
case
are not indentedThe issue is that the code does the following steps:
then
/else
tokenlocated
A: Spit out commentslocated
B: Set breakpoint layoutplaceHanging
Apply hanging formatBut we need to spit out the comments AFTER applying the hanging layout. Additionally, we need to never hang when there are comments. So I fixed this by moving "Apply hanging format" before "Spit out comments", and manually setting the breakpoint layout before:
then
/else
tokenswitchLayout
: Set breakpoint layoutplaceHanging
Apply hanging formatlocated
A: Spit out commentslocated
B: Set breakpoint layoutplaceHanging
Apply hanging formatComments before then/else keywords
In this bug, comments before the
then
/else
keywords get moved to after:This happens because we only spit out comments when we call
located
, but we dont calllocated
for keywords; we just do a plaintxt "then"
.The fix for this was relatively easy: I just wrap
txt "then"
in alocated
containing the SrcSpan for thethen
keyword from the annotations. I think there's future work here to always do this for all keywords, now that GHC has prettyprinting information with SrcSpans for all keywords