tweag / ormolu

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

Interesting comment placements in various scenarios #1074

Open LiamGoodacre opened 8 months ago

LiamGoodacre commented 8 months ago

Describe the bug

Here are a bunch of strange comment placements that I've noticed.

To Reproduce

Positive example

This one looks good

Input, & Output

example =
  [ -- A
    [], -- B
    -- C
    [] -- D
    -- E
  ] -- F

Using QQ in the previous example

Input (& expected output)

example =
  [ -- A
    [u||], -- B
    -- C
    [u||] -- D
    -- E
  ] -- F

Output

example =
  [ -- A
    [u||],
    -- B
    -- C
    [u||]
  ]

-- D
-- E
-- F

Record with comment on each line

Input (& expected output)

example =
  Record
    { -- A
      field = (), -- B
      -- C
      field = (), -- D
      -- E
      .. -- F
    } -- G

Output

example =
  Record
    { -- A
      field = (), -- B
      -- C
      field = (), -- D
      -- E
      ..
    }

-- F
-- G

Records with fewer comments

Notice how -- E here is also moved, which differs from the previous example.

Input (& expected output)

example =
  Record
    { -- A
      field = (),
      -- C
      field = (),
      -- E
      .. -- F
    } -- G

Output

example =
  Record
    { -- A
      field = (),
      -- C
      field = (),
      ..
    }

-- E
-- F
-- G

Environment

LiamGoodacre commented 8 months ago

Comment in import lists

It looks like ormolu interprets the comment as relating to the import Example3 line.

Input (& expected output)

import Example1
import Example2 ({- comment -})
import Example3

Output

import Example1
import Example2 ()
{- comment -}
import Example3