git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Improve the edge case about invalid module
commitea21695a6633c8ca0e6a084f93734dbb57063547 authorAntoine R. Dumont <antoine.romain.dumont@gmail.com> dateFri 31 Oct 2014, 20:56 (12 years ago) parent4f2b191
1 file changed+27−17
psci/Main.hs+27−17
psci/Main.hs+27−17
@@ -354,29 +354,39 @@ handleTypeOf value = do
354354 Just (ty, _, _) -> PSCI . outputStrLn . P.prettyPrintType $ ty
355355 Nothing -> PSCI $ outputStrLn "Could not find type"
356356
357+-- |
358+-- Pretty print a module's signatures
359+--
360+printModuleSignatures :: P.ModuleName -> P.Environment -> PSCI ()
361+printModuleSignatures moduleName env =
362+ PSCI $ let namesEnv = P.names env
363+ moduleNamesIdent = (filter ((== moduleName) . fst) . M.keys) namesEnv
364+ in case moduleNamesIdent of
365+ [] -> outputStrLn $ "This module '"++ P.runModuleName moduleName ++"' does not export functions."
366+ _ -> ( outputStrLn
367+ . unlines
368+ . sort
369+ . map (showType . findType namesEnv)) moduleNamesIdent
370+ where findType :: M.Map (P.ModuleName, P.Ident) (P.Type, P.NameKind, P.NameVisibility) -> (P.ModuleName, P.Ident) -> (P.Ident, Maybe (P.Type, P.NameKind, P.NameVisibility))
371+ findType envNames m@(_, mIdent) = (mIdent, M.lookup m envNames)
372+ showType :: (P.Ident, Maybe (P.Type, P.NameKind, P.NameVisibility)) -> String
373+ showType (mIdent, Just (mType, _, _)) = show mIdent ++ " :: " ++ P.prettyPrintType mType
374+ showType _ = error "The impossible happened in printModuleSignatures."
375+
357376-- |
358377-- Browse a module and displays its signature (if module exists).
359378--
360379handleBrowse :: P.ModuleName -> PSCI ()
361380handleBrowse moduleName = do
362381 st <- PSCI $ lift get
363− e <- psciIO . runMake $ P.make modulesDir options (psciLoadedModules st) []
364− PSCI $ case e of
365− Left err -> outputStrLn err
366− Right env' ->
367− let namesEnv = P.names env'
368− moduleNamesIdent = (filter ((== moduleName) . fst) . M.keys) namesEnv
369− in case moduleNamesIdent of
370− [] -> outputStrLn $ "Either you entered an invalid module name (see `:s loaded`) or the module '"++ P.runModuleName moduleName ++"' does not export functions."
371− _ -> ( outputStrLn
372− . unlines
373− . sort
374− . map (showType . findType namesEnv)) moduleNamesIdent
375− where findType :: M.Map (P.ModuleName, P.Ident) (P.Type, P.NameKind, P.NameVisibility) -> (P.ModuleName, P.Ident) -> (P.Ident, Maybe (P.Type, P.NameKind, P.NameVisibility))
376− findType envNames m@(_, mIdent) = (mIdent, M.lookup m envNames)
377− showType :: (P.Ident, Maybe (P.Type, P.NameKind, P.NameVisibility)) -> String
378− showType (mIdent, Just (mType, _, _)) = show mIdent ++ " :: " ++ P.prettyPrintType mType
379− showType _ = error "The impossible happened in handleBrowse."
382+ let loadedModules = psciLoadedModules st
383+ env <- psciIO . runMake $ P.make modulesDir options loadedModules []
384+ case env of
385+ Left err -> PSCI $ outputStrLn err
386+ Right env' ->
387+ if moduleName `notElem` (nub . map ((\ (P.Module modName _ _ ) -> modName) . snd)) loadedModules
388+ then PSCI $ outputStrLn $ "Module '" ++ N.runModuleName moduleName ++ "' is not valid."
389+ else printModuleSignatures moduleName env'
380390
381391-- |
382392-- Takes a value and prints its kind