ghc-mod/Browse.hs

92 lines
2.9 KiB
Haskell
Raw Normal View History

2010-03-11 10:03:17 +00:00
module Browse (browseModule) where
2010-04-28 06:51:30 +00:00
import Control.Applicative
2010-03-11 10:03:17 +00:00
import Data.Char
import Data.List
import Data.Maybe (fromMaybe)
2010-04-28 06:51:30 +00:00
import GHC
2012-02-14 07:09:53 +00:00
import GHCApi
import Gap
2010-04-28 06:51:30 +00:00
import Name
import Outputable
import TyCon
import Type
2013-02-28 07:33:11 +00:00
import Types
import Var
2010-03-11 10:03:17 +00:00
----------------------------------------------------------------
2010-03-11 13:39:07 +00:00
browseModule :: Options -> String -> IO String
2013-02-28 07:33:11 +00:00
browseModule opt mdlName = convert opt . format <$> browse opt mdlName
2010-03-11 10:03:17 +00:00
where
2011-01-27 05:29:39 +00:00
format
| operators opt = formatOps
| otherwise = removeOps
removeOps = sort . filter (isAlpha.head)
formatOps = sort . map formatOps'
formatOps' x@(s:_)
| isAlpha s = x
| otherwise = "(" ++ name ++ ")" ++ tail_
where
(name, tail_) = break isSpace x
2011-01-27 05:29:39 +00:00
formatOps' [] = error "formatOps'"
2010-03-11 10:03:17 +00:00
browse :: Options -> String -> IO [String]
browse opt mdlName = withGHC $ do
2012-08-06 00:43:47 +00:00
_ <- initSession0 opt
2013-03-01 01:16:31 +00:00
getModule >>= getModuleInfo >>= listExports
2010-04-27 01:28:00 +00:00
where
2013-03-01 01:16:31 +00:00
getModule = findModule (mkModuleName mdlName) Nothing
listExports Nothing = return []
listExports (Just mdinfo)
| detailed opt = processModule mdinfo
| otherwise = return (processExports mdinfo)
2013-02-28 17:24:14 +00:00
processExports :: ModuleInfo -> [String]
processExports = map getOccString . modInfoExports
2013-02-28 17:24:14 +00:00
processModule :: ModuleInfo -> Ghc [String]
processModule minfo = mapM processName names
2013-02-28 17:24:14 +00:00
where
names = modInfoExports minfo
processName :: Name -> Ghc String
processName nm = do
2013-02-28 17:24:14 +00:00
tyInfo <- modInfoLookupName minfo nm
-- If nothing found, load dependent module and lookup global
tyResult <- maybe (inOtherModule nm) (return . Just) tyInfo
return $ fromMaybe (getOccString nm) (tyResult >>= showThing)
2013-02-28 17:24:14 +00:00
inOtherModule :: Name -> Ghc (Maybe TyThing)
2013-03-01 01:16:31 +00:00
inOtherModule nm = getModuleInfo (nameModule nm) >> lookupGlobalName nm
showThing :: TyThing -> Maybe String
showThing (AnId i) = Just $ getOccString i ++ " :: " ++ showOutputable (removeForAlls $ varType i)
2013-03-01 01:16:31 +00:00
showThing (ATyCon t) = unwords . toList <$> tyType t
2013-02-28 17:24:14 +00:00
where
2013-03-01 01:16:31 +00:00
toList t' = t' : getOccString t : map getOccString (tyConTyVars t)
showThing _ = Nothing
tyType :: TyCon -> Maybe String
tyType typ
| isAlgTyCon typ
&& not (isNewTyCon typ)
&& not (isClassTyCon typ) = Just "data"
| isNewTyCon typ = Just "newtype"
| isClassTyCon typ = Just "class"
| isSynTyCon typ = Just "type"
| otherwise = Nothing
2013-02-28 17:24:14 +00:00
removeForAlls :: Type -> Type
2013-03-01 01:16:31 +00:00
removeForAlls ty = removeForAlls' ty' tty'
2013-02-28 17:24:14 +00:00
where
2013-03-01 01:16:31 +00:00
ty' = dropForAlls ty
tty' = splitFunTy_maybe ty'
removeForAlls' :: Type -> Maybe (Type, Type) -> Type
removeForAlls' ty Nothing = ty
removeForAlls' ty (Just (pre, ftype))
| isPredTy pre = mkFunTy pre (dropForAlls ftype)
| otherwise = ty
showOutputable :: Outputable a => a -> String
showOutputable = unwords . lines . showDocForUser neverQualify . ppr