closed import.

This commit is contained in:
Kazu Yamamoto 2014-03-27 14:38:06 +09:00
parent d3d9eb1e2c
commit 4fd7224c9c

View File

@ -4,22 +4,23 @@ module Language.Haskell.GhcMod.Browse (
, browseAll) , browseAll)
where where
import Control.Applicative import Control.Applicative ((<$>))
import Control.Monad (void) import Control.Monad (void)
import Data.Char import Data.Char (isAlpha)
import Data.List import Data.List (sort)
import Data.Maybe (catMaybes) import Data.Maybe (catMaybes)
import FastString (mkFastString) import FastString (mkFastString)
import GHC import GHC (Ghc, GhcException(CmdLineError), ModuleInfo, Name, TyThing, DynFlags, Type, TyCon, Module)
import qualified GHC as G
import Language.Haskell.GhcMod.Doc (showUnqualifiedPage, showUnqualifiedOneLine) import Language.Haskell.GhcMod.Doc (showUnqualifiedPage, showUnqualifiedOneLine)
import Language.Haskell.GhcMod.GHCApi import Language.Haskell.GhcMod.GHCApi
import Language.Haskell.GhcMod.Gap import Language.Haskell.GhcMod.Gap
import Language.Haskell.GhcMod.Types import Language.Haskell.GhcMod.Types
import Name import Name (getOccString)
import Outputable import Outputable (ppr, Outputable)
import Panic (throwGhcException) import Panic (throwGhcException)
import TyCon import TyCon (isAlgTyCon)
import Type import Type (dropForAlls, splitFunTy_maybe, mkFunTy, isPredTy)
---------------------------------------------------------------- ----------------------------------------------------------------
@ -41,10 +42,10 @@ browse :: Options
-> Ghc [String] -> Ghc [String]
browse opt cradle mdlName = do browse opt cradle mdlName = do
void $ initializeFlagsWithCradle opt cradle [] False void $ initializeFlagsWithCradle opt cradle [] False
getModule >>= getModuleInfo >>= listExports getModule >>= G.getModuleInfo >>= listExports
where where
getModule = findModule mdlname mpkgid `gcatch` fallback getModule = G.findModule mdlname mpkgid `G.gcatch` fallback
mdlname = mkModuleName mdlName mdlname = G.mkModuleName mdlName
mpkgid = mkFastString <$> packageId opt mpkgid = mkFastString <$> packageId opt
listExports Nothing = return [] listExports Nothing = return []
listExports (Just mdinfo) = processExports opt mdinfo listExports (Just mdinfo) = processExports opt mdinfo
@ -57,11 +58,11 @@ browse opt cradle mdlName = do
fallback e = throwGhcException e fallback e = throwGhcException e
loadAndFind = do loadAndFind = do
setTargetFiles [mdlName] setTargetFiles [mdlName]
void $ load LoadAllTargets void $ G.load G.LoadAllTargets
findModule mdlname Nothing G.findModule mdlname Nothing
processExports :: Options -> ModuleInfo -> Ghc [String] processExports :: Options -> ModuleInfo -> Ghc [String]
processExports opt minfo = mapM (showExport opt minfo) $ removeOps $ modInfoExports minfo processExports opt minfo = mapM (showExport opt minfo) $ removeOps $ G.modInfoExports minfo
where where
removeOps removeOps
| operators opt = id | operators opt = id
@ -72,13 +73,13 @@ showExport opt minfo e = do
mtype' <- mtype mtype' <- mtype
return $ concat $ catMaybes [mqualified, Just $ formatOp $ getOccString e, mtype'] return $ concat $ catMaybes [mqualified, Just $ formatOp $ getOccString e, mtype']
where where
mqualified = (moduleNameString (moduleName $ nameModule e) ++ ".") `justIf` qualified opt mqualified = (G.moduleNameString (G.moduleName $ G.nameModule e) ++ ".") `justIf` qualified opt
mtype mtype
| detailed opt = do | detailed opt = do
tyInfo <- modInfoLookupName minfo e tyInfo <- G.modInfoLookupName minfo e
-- If nothing found, load dependent module and lookup global -- If nothing found, load dependent module and lookup global
tyResult <- maybe (inOtherModule e) (return . Just) tyInfo tyResult <- maybe (inOtherModule e) (return . Just) tyInfo
dflag <- getSessionDynFlags dflag <- G.getSessionDynFlags
return $ do return $ do
typeName <- tyResult >>= showThing dflag typeName <- tyResult >>= showThing dflag
(" :: " ++ typeName) `justIf` detailed opt (" :: " ++ typeName) `justIf` detailed opt
@ -88,7 +89,7 @@ showExport opt minfo e = do
| otherwise = "(" ++ nm ++ ")" | otherwise = "(" ++ nm ++ ")"
formatOp "" = error "formatOp" formatOp "" = error "formatOp"
inOtherModule :: Name -> Ghc (Maybe TyThing) inOtherModule :: Name -> Ghc (Maybe TyThing)
inOtherModule nm = getModuleInfo (nameModule nm) >> lookupGlobalName nm inOtherModule nm = G.getModuleInfo (G.nameModule nm) >> G.lookupGlobalName nm
justIf :: a -> Bool -> Maybe a justIf :: a -> Bool -> Maybe a
justIf x True = Just x justIf x True = Just x
justIf _ False = Nothing justIf _ False = Nothing
@ -100,7 +101,7 @@ showThing' :: DynFlags -> GapThing -> Maybe String
showThing' dflag (GtA a) = Just $ formatType dflag a showThing' dflag (GtA a) = Just $ formatType dflag a
showThing' _ (GtT t) = unwords . toList <$> tyType t showThing' _ (GtT t) = unwords . toList <$> tyType t
where where
toList t' = t' : getOccString t : map getOccString (tyConTyVars t) toList t' = t' : getOccString t : map getOccString (G.tyConTyVars t)
showThing' _ _ = Nothing showThing' _ _ = Nothing
formatType :: DynFlags -> Type -> String formatType :: DynFlags -> Type -> String
@ -109,12 +110,12 @@ formatType dflag a = showOutputable dflag (removeForAlls a)
tyType :: TyCon -> Maybe String tyType :: TyCon -> Maybe String
tyType typ tyType typ
| isAlgTyCon typ | isAlgTyCon typ
&& not (isNewTyCon typ) && not (G.isNewTyCon typ)
&& not (isClassTyCon typ) = Just "data" && not (G.isClassTyCon typ) = Just "data"
| isNewTyCon typ = Just "newtype" | G.isNewTyCon typ = Just "newtype"
| isClassTyCon typ = Just "class" | G.isClassTyCon typ = Just "class"
| isSynTyCon typ = Just "type" | G.isSynTyCon typ = Just "type"
| otherwise = Nothing | otherwise = Nothing
removeForAlls :: Type -> Type removeForAlls :: Type -> Type
removeForAlls ty = removeForAlls' ty' tty' removeForAlls ty = removeForAlls' ty' tty'
@ -136,14 +137,14 @@ showOutputable dflag = unwords . lines . showUnqualifiedPage dflag . ppr
-- | Browsing all functions in all system/user modules. -- | Browsing all functions in all system/user modules.
browseAll :: DynFlags -> Ghc [(String,String)] browseAll :: DynFlags -> Ghc [(String,String)]
browseAll dflag = do browseAll dflag = do
ms <- packageDbModules True ms <- G.packageDbModules True
is <- mapM getModuleInfo ms is <- mapM G.getModuleInfo ms
return $ concatMap (toNameModule dflag) (zip ms is) return $ concatMap (toNameModule dflag) (zip ms is)
toNameModule :: DynFlags -> (Module, Maybe ModuleInfo) -> [(String,String)] toNameModule :: DynFlags -> (Module, Maybe ModuleInfo) -> [(String,String)]
toNameModule _ (_,Nothing) = [] toNameModule _ (_,Nothing) = []
toNameModule dflag (m,Just inf) = map (\name -> (toStr name, mdl)) names toNameModule dflag (m,Just inf) = map (\name -> (toStr name, mdl)) names
where where
mdl = moduleNameString (moduleName m) mdl = G.moduleNameString (G.moduleName m)
names = modInfoExports inf names = G.modInfoExports inf
toStr = showUnqualifiedOneLine dflag . ppr toStr = showUnqualifiedOneLine dflag . ppr