@@ -28,7 +28,7 @@ import qualified Control.Monad.Trans.State.Lazy as L
2828import Control.Monad.Error (ErrorT(..), MonadError)
2929import Control.Monad.Error.Class (MonadError(..))
3030
31−import Data.List (intercalate, isPrefixOf, nub, sortBy)
31+import Data.List (intercalate, isPrefixOf, nub, sortBy, sort)
3232import Data.Maybe (mapMaybe)
3333import Data.Foldable (traverse_)
3434import Data.Version (showVersion)
@@ -54,6 +54,8 @@ import qualified Language.PureScript as P
5454import qualified Paths_purescript as Paths
5555import qualified System.IO.UTF8 as U
5656 (writeFile, putStrLn, print, readFile)
57+import qualified Language.PureScript.Names as N
58+import qualified Language.PureScript.Declarations as D
5759
5860-- |
5961-- The PSCI state.
@@ -302,6 +304,27 @@ handleDeclaration value = do
302304 Just (ExitFailure _, _, err) -> PSCI $ outputStrLn err
303305 Nothing -> PSCI $ outputStrLn "Couldn't find node.js"
304306
307+-- |
308+-- Show actual loaded modules in psci.
309+--
310+handleShowLoadedModules :: PSCI ()
311+handleShowLoadedModules = do
312+ PSCiState { psciLoadedModules = loadedModules } <- PSCI $ lift get
313+ psciIO $ readModules loadedModules >>= putStrLn
314+ return ()
315+ where readModules = return . unlines . sort . nub . map toModuleName
316+ toModuleName = N.runModuleName . (\ (D.Module mdName _ _) -> mdName) . snd
317+
318+-- |
319+-- Show the imported modules in psci.
320+--
321+handleShowImportedModules :: PSCI ()
322+handleShowImportedModules = do
323+ PSCiState { psciImportedModuleNames = importedModuleNames } <- PSCI $ lift get
324+ psciIO $ readModules importedModuleNames >>= putStrLn
325+ return ()
326+ where readModules = return . unlines . sort . map N.runModuleName
327+
305328-- |
306329-- Imports a module, preserving the initial state on failure.
307330--
@@ -331,6 +354,40 @@ handleTypeOf value = do
331354 Just (ty, _, _) -> PSCI . outputStrLn . P.prettyPrintType $ ty
332355 Nothing -> PSCI $ outputStrLn "Could not find type"
333356
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+
376+-- |
377+-- Browse a module and displays its signature (if module exists).
378+--
379+handleBrowse :: P.ModuleName -> PSCI ()
380+handleBrowse moduleName = do
381+ st <- PSCI $ lift get
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'
390+
334391-- |
335392-- Takes a value and prints its kind
336393--
@@ -397,6 +454,9 @@ handleCommand Reset = do
397454 Right modules -> PSCI . lift $ put (PSCiState files defaultImports modules [])
398455handleCommand (TypeOf val) = handleTypeOf val
399456handleCommand (KindOf typ) = handleKindOf typ
457+handleCommand (Browse moduleName) = handleBrowse moduleName
458+handleCommand (Show "loaded") = handleShowLoadedModules
459+handleCommand (Show "import") = handleShowImportedModules
400460handleCommand _ = PSCI $ outputStrLn "Unknown command"
401461
402462singleLineFlag :: Cmd.Term Bool