tweag / ormolu

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

Comment placement on then/else branches #998

Closed mitchellwrosen closed 7 months ago

mitchellwrosen commented 1 year ago

Hi, I'm not sure if the AST would allow us to do this, but it would be great if this formatting of comment placement was preserved by ormolu:

if something
  -- then comment
  then do
    stuff
    stuff
  -- else comment
  else do
    stuff
    stuff

When formatted, this looks like

if something
  then -- then comment
  do
    stuff
    stuff
  else -- else comment
  do
    stuff
    stuff

One workaround is to place comments at the top of each alternative, which is okay, but seems a little less natural.

if something
  then do
    -- then comment
    stuff
    stuff
  else do
    -- else comment
    stuff
    stuff

Thanks!

brandonchinn178 commented 9 months ago

In cases where I actually do want the comment to go after the then/else, the block is de-indented:

Desired:

main =
  if x
    then
      -- asdf
      -- asdf
      case x of
        True -> _
    else
      -- asdf
      -- asdf
      case x of
        True -> _

Actual:

main =
  if x
    then -- asdf
    -- asdf
    case x of
      True -> _
    else -- asdf
    -- asdf
    case x of
      True -> _