alanz / ghc-exactprint

GHC version of haskell-src-exts exactPrint
BSD 3-Clause "New" or "Revised" License
70 stars 32 forks source link

Comments are attached to wrong import declarations #76

Open mrkkrp opened 5 years ago

mrkkrp commented 5 years ago

We have noticed an inadequate behavior with respect to how comment annotations are generated for import sections. Indeed, it's rather counter-intuitive, for example:

import Foo -- (1)
import Bar -- (2)
import Baz -- (3)

Here, contrary to what a programmer could have meant, -- (1) is saved in the annotation for import Bar, while -- (2) is saved in annotation for import Baz. import Foo doesn't have any comments associated with it, while -- (3) is a comment attached to the entire module.

One could see how wrong this is if we try to do any sort of meaningful transformation on parsed source, such as reordering of imports. Indeed, as simple as it seems, this cannot be done in a satisfactory fashion unless we manage to reassign comments somehow.

Currently we get:

-- (2)
import Bar
-- (3)
import Baz
import Foo