tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
958 stars 83 forks source link

Comments before `else` are printed without preceding whitespace #458

Closed cblp closed 4 years ago

cblp commented 4 years ago
  if undefined
    then undefined
    -- comment
    else 
      do
        undefined

is reformatted as

  if undefined
    then undefined
    else-- comment

    do
      undefined

Introduced problems:

  1. An extra empty line.
  2. No space before inline comment.
neongreen commented 4 years ago

I'm tempted to classify this as a 'bug' – it hardly seems like this is intentional, and not an accident of the implementation.

sellout commented 4 years ago

Yeah, I haven't found a reasonable way to comment branches

-- two-line
-- comment
then
  x
-- another
-- comment
else
  y
then -- two-line
     -- comment
  x
else -- another
     -- comment
  y

and

then
  -- two-line
  -- comment
  x
else
  -- another
  -- comment
  y

all get reformatted to

then-- two-line
-- comment
  x
else-- another
-- comment
  y

which seems like it must be a bug.

I just wish I could find some workaround in the interim.

mrkkrp commented 4 years ago

Also, I looked into this recently and it is a tough one. I'd not expect it to be resolved any time soon.

mheinzel commented 4 years ago

I also ran into this.

I just wish I could find some workaround in the interim.

It's a hack and doesn't play nicely with hlint, but in some cases you might be able to introduce a do-block:

foo =
  if condition
  then do
    -- two-line
    -- comment
    x
  else do
    -- another
    -- comment
    y

This doesn't get changed by ormolu.

mrkkrp commented 4 years ago

So I fixed the lack of spacing between else (and then) and the following comment. Now you can comment branches like this:

foo =
  if undefined
    -- then comment
    then undefined
    -- else comment
    else
      do
        undefined

(I mean, you could before, it was technically correct and idempotent, just a little bit ugly.)

sellout commented 4 years ago

@mrkkrp I don't seem to be able to comment branches like you say (using 0.1.2.0). If I do

foo =
  if undefined
    -- then comment
    then undefined
    -- else comment
    else
      do
        undefined

it reformats as

foo =
  if undefined
    then -- then comment
      undefined
    else -- else comment
      do
        undefined

And multi-line comments still have the alignment issue like

foo =
  if undefined
    then -- then
    -- comment
      undefined
    else -- else
    -- comment
      do
        undefined
mrkkrp commented 4 years ago

But I think it is clear nevertheless which comment belongs to which branch of execution. I do not see a problem with current style. Re alignment—it is a matter of taste, it doesn't look wrong to me.