From 9796d639ab81dda467316f91764a08385678ab64 Mon Sep 17 00:00:00 2001 From: Kazu Yamamoto Date: Fri, 1 Mar 2013 02:24:14 +0900 Subject: [PATCH] refactoring Browse. --- Browse.hs | 80 +++++++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/Browse.hs b/Browse.hs index db013b0..2d4e155 100644 --- a/Browse.hs +++ b/Browse.hs @@ -38,52 +38,50 @@ browse opt mdlName = withGHC $ do where lookupModuleInfo = findModule (mkModuleName mdlName) Nothing >>= getModuleInfo - processExports :: ModuleInfo -> [String] - processExports = map getOccString . modInfoExports +processExports :: ModuleInfo -> [String] +processExports = map getOccString . modInfoExports - processModule :: ModuleInfo -> Ghc [String] - processModule minfo = do - dynFlags <- getDynFlags - let processName :: Name -> Ghc String - processName nm = do - tyInfo <- modInfoLookupName minfo nm - -- If nothing found, load dependent module and lookup global - tyResult <- maybe inOtherModule (return . Just) tyInfo - return $ fromMaybe name (tyResult >>= showThing dynFlags) - where - inOtherModule :: Ghc (Maybe TyThing) - inOtherModule = do - _ <- getModuleInfo (nameModule nm) -- FIXME - lookupGlobalName nm - name = getOccString nm - mapM processName exports - where - exports = modInfoExports minfo +processModule :: ModuleInfo -> Ghc [String] +processModule minfo = do + dynFlags <- getDynFlags + mapM (processName dynFlags) names + where + names = modInfoExports minfo + processName :: DynFlags -> Name -> Ghc String + processName dynFlags nm = do + 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 dynFlags) + inOtherModule :: Name -> Ghc (Maybe TyThing) + inOtherModule nm = do + _ <- getModuleInfo (nameModule nm) -- FIXME + lookupGlobalName nm - showThing :: DynFlags -> TyThing -> Maybe String - showThing dflags t = case t of - AnId i -> Just $ getOccString i ++ " :: " ++ showOutputable dflags (removeForAlls $ varType i) - ATyCon typ -> do +showThing :: DynFlags -> TyThing -> Maybe String +showThing dflags t = case t of + AnId i -> Just $ getOccString i ++ " :: " ++ showOutputable dflags (removeForAlls $ varType i) + ATyCon typ -> do tyType' <- tyType typ return $ unwords $ [tyType', getOccString typ] ++ map getOccString (tyConTyVars typ) - _ -> Nothing - where - 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 + _ -> Nothing + where + 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 - removeForAlls :: Type -> Type - removeForAlls ty = case splitFunTy_maybe ty' of - Nothing -> ty' - Just (pre, ftype) -> if isPredTy pre then mkFunTy pre (dropForAlls ftype) else ty' - where - ty' = dropForAlls ty +removeForAlls :: Type -> Type +removeForAlls ty = case splitFunTy_maybe ty' of + Nothing -> ty' + Just (pre, ftype) -> if isPredTy pre then mkFunTy pre (dropForAlls ftype) else ty' + where + ty' = dropForAlls ty showOutputable :: Outputable a => DynFlags -> a -> String showOutputable dflags = unwords . lines . showSDocForUser dflags neverQualify . ppr