tweag / ormolu

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

Preserve blank lines between case branches #902

Open ocharles opened 2 years ago

ocharles commented 2 years ago

Is your feature request related to a problem? Please describe.

The following input code

readPackets s requests notifications = forever do
  packet@AMSPacket{payload, commandId} <- readPacket s
  atomically do
    case commandId of
      8 ->
        case Decode.parseOnly parseDeviceNotification payload of
          Left e ->
            throwSTM $ CouldNotParseResponse e

          Right deviceNotification ->
            writeTChan notifications deviceNotification

      _ -> do
        maybePacket <-
          stateTVar requests $
            IntMap.updateLookupWithKey (\_ _ -> Nothing) (fromIntegral (invokeId packet))

        case maybePacket of
          Nothing -> throwSTM $ UnexpectedMessage packet
          Just consume -> consume packet

Gets squashed into

readPackets s requests notifications = forever do
  packet@AMSPacket {payload, commandId} <- readPacket s
  atomically do
    case commandId of
      8 ->
        case Decode.parseOnly parseDeviceNotification payload of
          Left e ->
            throwSTM $ CouldNotParseResponse e
          Right deviceNotification ->
            writeTChan notifications deviceNotification
      _ -> do
        maybePacket <-
          stateTVar requests $
            IntMap.updateLookupWithKey (\_ _ -> Nothing) (fromIntegral (invokeId packet))

        case maybePacket of
          Nothing -> throwSTM $ UnexpectedMessage packet
          Just consume -> consume packet

I struggle to follow the flow like this, and would like Ormolu to preserve the blank lines.

Describe the solution you'd like Ormolu preserves blank lines between case branches.

Describe alternatives you've considered I could rewrite my code so each branch is a where clause. If I did this, I probably wouldn't need the branches to be separated by blank lines, and blank lines are preserved in where clauses.

ntc2 commented 2 years ago

I think this is another variant of #635 and #802. Again, I suggest that Ormolu just preserve groupings in all cases: don't insert blank lines where there were none, and don't remove blank lines where they existed.