2013-05-18 21:16:37 +00:00
|
|
|
module Language.Haskell.GhcMod.Browse (browseModule, browse) where
|
2010-03-11 10:03:17 +00:00
|
|
|
|
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
|
2013-02-23 08:51:55 +00:00
|
|
|
import Data.Maybe (fromMaybe)
|
2013-03-12 13:15:23 +00:00
|
|
|
import DataCon (dataConRepType)
|
2010-04-28 06:51:30 +00:00
|
|
|
import GHC
|
2013-05-17 01:00:01 +00:00
|
|
|
import Language.Haskell.GhcMod.Doc
|
2013-07-02 08:48:44 +00:00
|
|
|
import qualified Language.Haskell.GhcMod.Gap as Gap
|
2013-05-17 01:00:01 +00:00
|
|
|
import Language.Haskell.GhcMod.GHCApi
|
|
|
|
import Language.Haskell.GhcMod.Types
|
2010-04-28 06:51:30 +00:00
|
|
|
import Name
|
2013-02-23 08:51:55 +00:00
|
|
|
import Outputable
|
|
|
|
import TyCon
|
|
|
|
import Type
|
2013-02-28 07:33:11 +00:00
|
|
|
import Var
|
2010-03-11 10:03:17 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2013-05-20 05:28:56 +00:00
|
|
|
-- | Getting functions, classes, etc from a module.
|
|
|
|
-- If 'detailed' is 'True', their types are also obtained.
|
|
|
|
browseModule :: Options
|
|
|
|
-> ModuleString -- ^ A module name. (e.g. \"Data.List\")
|
|
|
|
-> IO String
|
2013-05-20 02:29:44 +00:00
|
|
|
browseModule opt mdlName = convert opt . format <$> withGHCDummyFile (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
|
2013-02-23 08:51:55 +00:00
|
|
|
| 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
|
|
|
|
2013-05-20 05:28:56 +00:00
|
|
|
-- | Getting functions, classes, etc from a module.
|
|
|
|
-- If 'detailed' is 'True', their types are also obtained.
|
|
|
|
browse :: Options
|
|
|
|
-> ModuleString -- ^ A module name. (e.g. \"Data.List\")
|
|
|
|
-> Ghc [String]
|
2013-05-18 21:16:37 +00:00
|
|
|
browse opt mdlName = do
|
2013-03-04 04:41:56 +00:00
|
|
|
initializeFlags 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-23 08:51:55 +00:00
|
|
|
|
2013-02-28 17:24:14 +00:00
|
|
|
processExports :: ModuleInfo -> [String]
|
|
|
|
processExports = map getOccString . modInfoExports
|
2013-02-23 08:51:55 +00:00
|
|
|
|
2013-02-28 17:24:14 +00:00
|
|
|
processModule :: ModuleInfo -> Ghc [String]
|
2013-02-28 17:42:54 +00:00
|
|
|
processModule minfo = mapM processName names
|
2013-02-28 17:24:14 +00:00
|
|
|
where
|
|
|
|
names = modInfoExports minfo
|
2013-02-28 17:42:54 +00:00
|
|
|
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
|
2013-03-12 13:15:23 +00:00
|
|
|
dflag <- getSessionDynFlags
|
|
|
|
return $ fromMaybe (getOccString nm) (tyResult >>= showThing dflag)
|
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
|
2013-02-23 08:51:55 +00:00
|
|
|
|
2013-03-12 13:15:23 +00:00
|
|
|
showThing :: DynFlags -> TyThing -> Maybe String
|
|
|
|
showThing dflag (AnId i) = Just $ formatType dflag varType i
|
|
|
|
showThing dflag (ADataCon d) = Just $ formatType dflag dataConRepType d
|
|
|
|
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)
|
2013-03-12 13:15:23 +00:00
|
|
|
showThing _ _ = Nothing
|
2013-03-01 01:16:31 +00:00
|
|
|
|
2013-03-12 13:15:23 +00:00
|
|
|
formatType :: NamedThing a => DynFlags -> (a -> Type) -> a -> String
|
|
|
|
formatType dflag f x = getOccString x ++ " :: " ++ showOutputable dflag (removeForAlls $ f x)
|
2013-03-01 13:11:02 +00:00
|
|
|
|
2013-03-01 01:16:31 +00:00
|
|
|
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-23 08:51:55 +00:00
|
|
|
|
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
|
2013-02-23 08:51:55 +00:00
|
|
|
|
2013-03-12 13:15:23 +00:00
|
|
|
showOutputable :: Outputable a => DynFlags -> a -> String
|
2013-07-02 08:48:44 +00:00
|
|
|
showOutputable dflag = unwords . lines . Gap.showUnqualifiedPage dflag . ppr
|