2014-04-21 03:46:14 +00:00
|
|
|
module Language.Haskell.GhcMod.List (listModules, modules) where
|
2010-03-11 10:03:17 +00:00
|
|
|
|
2014-03-27 07:02:16 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2014-04-24 03:51:43 +00:00
|
|
|
import Control.Exception (SomeException(..))
|
2013-09-27 03:23:53 +00:00
|
|
|
import Control.Monad (void)
|
2014-03-27 07:02:16 +00:00
|
|
|
import Data.List (nub, sort)
|
|
|
|
import GHC (Ghc)
|
|
|
|
import qualified GHC as G
|
2013-05-17 01:00:01 +00:00
|
|
|
import Language.Haskell.GhcMod.GHCApi
|
|
|
|
import Language.Haskell.GhcMod.Types
|
2014-03-27 07:02:16 +00:00
|
|
|
import Packages (pkgIdMap, exposedModules, sourcePackageId, display)
|
|
|
|
import UniqFM (eltsUFM)
|
2010-03-11 10:03:17 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-05-20 05:28:56 +00:00
|
|
|
-- | Listing installed modules.
|
2013-09-27 03:23:53 +00:00
|
|
|
listModules :: Options -> Cradle -> IO String
|
2014-04-23 07:37:24 +00:00
|
|
|
listModules opt cradle = withGHC' $ do
|
2014-04-21 07:30:31 +00:00
|
|
|
void $ initializeFlagsWithCradle opt cradle [] False
|
|
|
|
modules opt
|
2010-03-11 10:03:17 +00:00
|
|
|
|
2013-05-20 05:28:56 +00:00
|
|
|
-- | Listing installed modules.
|
2014-04-21 07:30:31 +00:00
|
|
|
modules :: Options -> Ghc String
|
2014-04-24 03:51:43 +00:00
|
|
|
modules opt = convert opt . arrange <$> (getModules `G.gcatch` handler)
|
2010-03-11 10:03:17 +00:00
|
|
|
where
|
2014-04-24 03:51:43 +00:00
|
|
|
getModules = getExposedModules <$> G.getSessionDynFlags
|
2013-10-28 08:22:18 +00:00
|
|
|
getExposedModules = concatMap exposedModules'
|
2014-03-27 07:02:16 +00:00
|
|
|
. eltsUFM . pkgIdMap . G.pkgState
|
2013-10-28 08:22:18 +00:00
|
|
|
exposedModules' p =
|
2014-03-27 07:02:16 +00:00
|
|
|
map G.moduleNameString (exposedModules p)
|
2013-10-28 08:22:18 +00:00
|
|
|
`zip`
|
2014-03-27 07:02:16 +00:00
|
|
|
repeat (display $ sourcePackageId p)
|
2014-04-24 03:51:43 +00:00
|
|
|
arrange = nub . sort . map dropPkgs
|
2014-04-21 03:46:14 +00:00
|
|
|
dropPkgs (name, pkg)
|
|
|
|
| detailed opt = name ++ " " ++ pkg
|
|
|
|
| otherwise = name
|
2014-04-24 03:51:43 +00:00
|
|
|
handler (SomeException _) = return []
|