@@ -28,7 +28,7 @@ import qualified Control.Monad.Trans.State.Lazy as L
28import Control.Monad.Error (ErrorT(..), MonadError)
28import Control.Monad.Error (ErrorT(..), MonadError)
29import Control.Monad.Error.Class (MonadError(..))
29import Control.Monad.Error.Class (MonadError(..))
31−import Data.List (intercalate, isPrefixOf, nub, sortBy)
31+import Data.List (intercalate, isPrefixOf, nub, sortBy, sort)
32import Data.Maybe (mapMaybe)
32import Data.Maybe (mapMaybe)
33import Data.Foldable (traverse_)
33import Data.Foldable (traverse_)
34import Data.Version (showVersion)
34import Data.Version (showVersion)
@@ -54,6 +54,8 @@ import qualified Language.PureScript as P
54import qualified Paths_purescript as Paths
54import qualified Paths_purescript as Paths
55import qualified System.IO.UTF8 as U
55import qualified System.IO.UTF8 as U
56 (writeFile, putStrLn, print, readFile)
56 (writeFile, putStrLn, print, readFile)
57+import qualified Language.PureScript.Names as N
58+import qualified Language.PureScript.Declarations as D
59-- The PSCI state.
61-- The PSCI state.
@@ -302,6 +304,27 @@ handleDeclaration value = do
302 Just (ExitFailure _, _, err) -> PSCI $ outputStrLn err
304 Just (ExitFailure _, _, err) -> PSCI $ outputStrLn err
303 Nothing -> PSCI $ outputStrLn "Couldn't find node.js"
305 Nothing -> PSCI $ outputStrLn "Couldn't find node.js"
308+-- Show actual loaded modules in psci.
310+handleShowLoadedModules :: PSCI ()
311+handleShowLoadedModules = do
312+ PSCiState { psciLoadedModules = loadedModules } <- PSCI $ lift get
313+ psciIO $ readModules loadedModules >>= putStrLn
315+ where readModules = return . unlines . sort . nub . map toModuleName
316+ toModuleName = N.runModuleName . (\ (D.Module mdName _ _) -> mdName) . snd
319+-- Show the imported modules in psci.
321+handleShowImportedModules :: PSCI ()
322+handleShowImportedModules = do
323+ PSCiState { psciImportedModuleNames = importedModuleNames } <- PSCI $ lift get
324+ psciIO $ readModules importedModuleNames >>= putStrLn
326+ where readModules = return . unlines . sort . map N.runModuleName
306-- Imports a module, preserving the initial state on failure.
329-- Imports a module, preserving the initial state on failure.
@@ -331,6 +354,40 @@ handleTypeOf value = do
331 Just (ty, _, _) -> PSCI . outputStrLn . P.prettyPrintType $ ty
354 Just (ty, _, _) -> PSCI . outputStrLn . P.prettyPrintType $ ty
332 Nothing -> PSCI $ outputStrLn "Could not find type"
355 Nothing -> PSCI $ outputStrLn "Could not find type"
358+-- Pretty print a module's signatures
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."
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."
377+-- Browse a module and displays its signature (if module exists).
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 []
385+ Left err -> PSCI $ outputStrLn err
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'
335-- Takes a value and prints its kind
392-- Takes a value and prints its kind
@@ -397,6 +454,9 @@ handleCommand Reset = do
397 Right modules -> PSCI . lift $ put (PSCiState files defaultImports modules [])
454 Right modules -> PSCI . lift $ put (PSCiState files defaultImports modules [])
398handleCommand (TypeOf val) = handleTypeOf val
455handleCommand (TypeOf val) = handleTypeOf val
399handleCommand (KindOf typ) = handleKindOf typ
456handleCommand (KindOf typ) = handleKindOf typ
457+handleCommand (Browse moduleName) = handleBrowse moduleName
458+handleCommand (Show "loaded") = handleShowLoadedModules
459+handleCommand (Show "import") = handleShowImportedModules
400handleCommand _ = PSCI $ outputStrLn "Unknown command"
460handleCommand _ = PSCI $ outputStrLn "Unknown command"
402singleLineFlag :: Cmd.Term Bool
462singleLineFlag :: Cmd.Term Bool