ormolu icon indicating copy to clipboard operation
ormolu copied to clipboard

Expression-style "do" leads to strange output

Open david-christiansen opened this issue 4 years ago • 1 comments

It looks as though Ormolu prefers the trailing-style do instead of the expression-style do. That is,

main = do
  putStrLn "Hi!"
  putStrLn "I am a program!"

instead of

main =
  do putStrLn "Hi!"
     putStrLn "I am a program!"

However, formatting the latter results in the following:

main =
  do
    putStrLn "Hi!"
    putStrLn "I am a program!"

which seems to necessitate a manual pass to move all do keywords up to the previous line before automatically formatting. Perhaps an exemption from "respect the user's newlines" is in order in this particular case?

david-christiansen avatar Oct 22 '19 01:10 david-christiansen

I tried to implement this, but there are some bummers:

  • If do is not on the same line in the original input, there may be reasons for that. For example there may be a comment between equal sign and the do block. If we try to force hanging placement in that case, the comment would shift do to the next line and indentation will be incorrect. If we preserve the layout (that is, equal sign and do remain on different lines) everything is fine.
-- Real failure, QuickCheck, before:
     case res of
       MkResult{ok = Just True} -> -- successful test
         do continue doneTesting
              st'{ numSuccessTests           = numSuccessTests st' + 1
                 , numRecentlyDiscardedTests = 0
                 , randomSeed = rnd2
                 } f

-- After:
  case res of
    MkResult {ok = Just True} ->
    -- successful test
    do
      continue
        doneTesting
        st'
          { numSuccessTests = numSuccessTests st' + 1,
            numRecentlyDiscardedTests = 0,
            randomSeed = rnd2
          }
        f

Granted, one could argue that the way Ormolu handles comments is not perfect, but in this case it looks like respecting original placement is the best thing to do. In the end, what Ormolu does right now is good, because the do section starts at 4-th column instead of 5th.

mrkkrp avatar Nov 20 '19 09:11 mrkkrp