tweag / ormolu

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

Expression-style "do" leads to strange output #417

Open david-christiansen opened 4 years ago

david-christiansen commented 4 years ago

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?

mrkkrp commented 4 years ago

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

-- 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.