2014-04-23 01:41:28 +00:00
|
|
|
{-# LANGUAGE ScopedTypeVariables, RecordWildCards #-}
|
2013-04-10 06:02:49 +00:00
|
|
|
|
2013-05-17 01:00:01 +00:00
|
|
|
module Language.Haskell.GhcMod.GHCApi (
|
2014-07-12 01:30:06 +00:00
|
|
|
ghcPkgDb
|
2014-07-11 08:43:51 +00:00
|
|
|
, package
|
|
|
|
, modules
|
|
|
|
, findModule
|
|
|
|
, moduleInfo
|
|
|
|
, localModuleInfo
|
|
|
|
, bindings
|
2013-03-04 04:41:56 +00:00
|
|
|
) where
|
2012-02-14 07:09:53 +00:00
|
|
|
|
2014-04-15 03:13:10 +00:00
|
|
|
import Language.Haskell.GhcMod.GhcPkg
|
2014-07-12 00:53:59 +00:00
|
|
|
import Language.Haskell.GhcMod.DynFlags
|
2014-07-11 01:10:37 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
2014-04-15 03:13:10 +00:00
|
|
|
|
2014-04-18 01:55:49 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2014-07-11 08:43:51 +00:00
|
|
|
import Distribution.Package (InstalledPackageId(..))
|
2014-07-12 00:53:59 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
import GHC (DynFlags(..))
|
2014-03-27 06:43:33 +00:00
|
|
|
import qualified GHC as G
|
2014-06-28 19:43:51 +00:00
|
|
|
import GhcMonad
|
2014-07-11 08:43:51 +00:00
|
|
|
import qualified Packages as G
|
|
|
|
import qualified Module as G
|
|
|
|
import qualified OccName as G
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
-- get Packages,Modules,Bindings
|
|
|
|
|
|
|
|
ghcPkgDb :: GhcMonad m => m PkgDb
|
|
|
|
ghcPkgDb = M.fromList <$>
|
|
|
|
maybe [] (map toKv . filterInternal) <$> pkgDatabase <$> G.getSessionDynFlags
|
|
|
|
where
|
|
|
|
toKv pkg = (fromInstalledPackageId $ G.installedPackageId pkg, pkg)
|
|
|
|
filterInternal =
|
|
|
|
filter ((/= InstalledPackageId "builtin_rts") . G.installedPackageId)
|
|
|
|
|
|
|
|
package :: G.PackageConfig -> Package
|
|
|
|
package = fromInstalledPackageId . G.installedPackageId
|
|
|
|
|
|
|
|
modules :: G.PackageConfig -> [ModuleString]
|
|
|
|
modules = map G.moduleNameString . G.exposedModules
|
|
|
|
|
|
|
|
findModule :: ModuleString -> PkgDb -> [Package]
|
|
|
|
findModule m db = do
|
|
|
|
M.elems $ package `M.map` (containsModule `M.filter` db)
|
|
|
|
where
|
|
|
|
containsModule :: G.PackageConfig -> Bool
|
|
|
|
containsModule pkgConf =
|
|
|
|
G.mkModuleName m `elem` G.exposedModules pkgConf
|
|
|
|
|
|
|
|
|
|
|
|
ghcPkgId :: Package -> G.PackageId
|
|
|
|
ghcPkgId (name,_,_) =
|
|
|
|
-- TODO: Adding the package version too breaks 'findModule' for some reason
|
|
|
|
-- this isn't a big deal since in the common case where we're in a cabal
|
|
|
|
-- project we just use cabal's view of package dependencies anyways so we're
|
|
|
|
-- guaranteed to only have one version of each package exposed. However when
|
|
|
|
-- we're operating without a cabal project this will probaly cause trouble.
|
|
|
|
G.stringToPackageId name
|
|
|
|
|
|
|
|
type Binding = String
|
|
|
|
|
|
|
|
-- | @moduleInfo mpkg module@. @mpkg@ should be 'Nothing' iff. moduleInfo
|
|
|
|
-- should look for @module@ in the working directory.
|
|
|
|
--
|
|
|
|
-- To map a 'ModuleString' to a package see 'findModule'
|
|
|
|
moduleInfo :: GhcMonad m
|
|
|
|
=> Maybe Package
|
|
|
|
-> ModuleString
|
|
|
|
-> m (Maybe G.ModuleInfo)
|
|
|
|
moduleInfo mpkg mdl = do
|
|
|
|
let mdlName = G.mkModuleName mdl
|
|
|
|
mfsPkgId = G.packageIdFS . ghcPkgId <$> mpkg
|
|
|
|
loadLocalModule
|
|
|
|
G.findModule mdlName mfsPkgId >>= G.getModuleInfo
|
|
|
|
where
|
|
|
|
loadLocalModule = case mpkg of
|
|
|
|
Just _ -> return ()
|
|
|
|
Nothing -> setTargetFiles [mdl]
|
|
|
|
|
|
|
|
localModuleInfo :: GhcMonad m => ModuleString -> m (Maybe G.ModuleInfo)
|
|
|
|
localModuleInfo mdl = moduleInfo Nothing mdl
|
|
|
|
|
|
|
|
bindings :: G.ModuleInfo -> [Binding]
|
|
|
|
bindings minfo = do
|
|
|
|
map (G.occNameString . G.getOccName) $ G.modInfoExports minfo
|