alanz / ghc-exactprint

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

Don't seem to be able to re-order imports #57

Closed ocharles closed 7 years ago

ocharles commented 7 years ago

I expect the following code to re-order imports, yet the resulting printed code is in the original order.

import Data.Typeable
import Data.Function
import Data.Ord
import Data.Data
import Data.List
import qualified Data.Map as Map
import GHC hiding (Phase, parseModule)
import Language.Haskell.GHC.ExactPrint
import Language.Haskell.GHC.ExactPrint.Types

formatFile fp = do
  out <- parseModule fp
  case out of
    Left (_, e) ->
      fail $ "Could not parse file: " ++ e

    Right (annotations, parsed) ->
      let (transformed, (anns, _), log) =
            runTransform annotations (sortImports parsed)
      in do
        mapM_ print $ Map.toList annotations
        mapM_ putStrLn log
        return $ exactPrint transformed anns

sortImports :: ParsedSource -> Transform ParsedSource
sortImports lmod@(L l mod) = do
  let newOrder =
        sortBy (comparing (ideclName . unLoc)) (hsmodImports mod)
      lmod' = L (l :: SrcSpan) (mod { hsmodImports = newOrder })
  modifyAnnsT $ captureOrder lmod' newOrder
  logDataWithAnnsTr "HsModule" lmod'
  return lmod'
ocharles commented 7 years ago

Sorry, the problem here is actually that:

        sortBy (comparing (ideclName . unLoc)) (hsmodImports mod)

is sorting by Located ModuleName, not ModuleName - hence the order doesn't change.