git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #662 from ardumont/add-browse-module-in-psci
32fc5a0Phil Freeman
psci/Commands.hs+11
@@ -34,6 +34,10 @@ data Command
34 --
34 --
35 | Import ModuleName
35 | Import ModuleName
36 -- |
36 -- |
37+ -- Browse a module
38+ --
39+ | Browse ModuleName
40+ -- |
37 -- Load a file for use with importing
41 -- Load a file for use with importing
38 --
42 --
39 | LoadFile FilePath
43 | LoadFile FilePath
@@ -57,6 +61,10 @@ data Command
57 -- Find the kind of an expression
61 -- Find the kind of an expression
58 --
62 --
59 | KindOf Type
63 | KindOf Type
64+ -- |
65+ -- Show command
66+ --
67+ | Show String
60
68
61-- |
69-- |
62-- The help menu.
70-- The help menu.
@@ -65,9 +73,12 @@ help :: [[String]]
65help =
73help =
66 [ [":? ", "Show this help menu"]
74 [ [":? ", "Show this help menu"]
67 , [":i <module> ", "Import <module> for use in PSCI"]
75 , [":i <module> ", "Import <module> for use in PSCI"]
76+ , [":b <module> ", "Browse <module>"]
68 , [":m <file> ", "Load <file> for importing"]
77 , [":m <file> ", "Load <file> for importing"]
69 , [":q ", "Quit PSCi"]
78 , [":q ", "Quit PSCi"]
70 , [":r ", "Reset"]
79 , [":r ", "Reset"]
80+ , [":s import ", "Show imported modules"]
81+ , [":s loaded ", "Show loaded modules"]
71 , [":t <expr> ", "Show the type of <expr>"]
82 , [":t <expr> ", "Show the type of <expr>"]
72 , [":k <type> ", "Show the kind of <type>"]
83 , [":k <type> ", "Show the kind of <type>"]
73 ]
84 ]
psci/Main.hs+61−1
@@ -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(..))
30
30
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
57
59
58-- |
60-- |
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"
304
306
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+
305-- |
328-- |
306-- Imports a module, preserving the initial state on failure.
329-- Imports a module, preserving the initial state on failure.
307--
330--
@@ -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"
333
356
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+
334-- |
391-- |
335-- Takes a value and prints its kind
392-- Takes a value and prints its kind
336--
393--
@@ -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"
401
461
402singleLineFlag :: Cmd.Term Bool
462singleLineFlag :: Cmd.Term Bool
psci/Parser.hs+16−3
@@ -42,7 +42,7 @@ psciLet = Let <$> (P.Let <$> (P.reserved "let" *> P.indented *> C.mark (many1 (C
42--
42--
43parseCommand :: String -> Either ParseError Command
43parseCommand :: String -> Either ParseError Command
44parseCommand = P.runIndentParser "" $ choice
44parseCommand = P.runIndentParser "" $ choice
45− [ P.whiteSpace *> char ':' *> (psciHelp <|> psciImport <|> psciLoadFile <|> psciQuit <|> psciReload <|> psciTypeOf <|> psciKindOf)
45+ [ P.whiteSpace *> char ':' *> (psciHelp <|> psciImport <|> psciLoadFile <|> psciQuit <|> psciReload <|> psciTypeOf <|> psciKindOf <|> psciBrowse <|> psciShowModules)
46 , try psciLet
46 , try psciLet
47 , psciExpression
47 , psciExpression
48 ] <* eof
48 ] <* eof
@@ -70,8 +70,10 @@ psciImport = Import <$> (char 'i' *> P.whiteSpace *> P.moduleName)
70--
70--
71psciLoadFile :: Parsec String P.ParseState Command
71psciLoadFile :: Parsec String P.ParseState Command
72psciLoadFile = LoadFile . trimEnd <$> (char 'm' *> P.whiteSpace *> manyTill anyChar eof)
72psciLoadFile = LoadFile . trimEnd <$> (char 'm' *> P.whiteSpace *> manyTill anyChar eof)
73− where
73+
74+-- | Trim end of input string
75+trimEnd :: String -> String
74− trimEnd = reverse . dropWhile isSpace . reverse
76+trimEnd = reverse . dropWhile isSpace . reverse
75
77
76-- |
78-- |
77-- Parses 'Commands.Quit' command.
79-- Parses 'Commands.Quit' command.
@@ -97,3 +99,14 @@ psciTypeOf = TypeOf <$> (char 't' *> P.whiteSpace *> P.parseValue)
97--
99--
98psciKindOf :: Parsec String P.ParseState Command
100psciKindOf :: Parsec String P.ParseState Command
99psciKindOf = KindOf <$> (char 'k' *> P.whiteSpace *> P.parseType)
101psciKindOf = KindOf <$> (char 'k' *> P.whiteSpace *> P.parseType)
102+
103+-- |
104+-- Parses 'Commands.Browse' command.
105+--
106+psciBrowse :: Parsec String P.ParseState Command
107+psciBrowse = Browse <$> (char 'b' *> P.whiteSpace *> P.moduleName)
108+
109+-- |
110+-- Show Command
111+psciShowModules :: Parsec String P.ParseState Command
112+psciShowModules = Show . trimEnd <$> (char 's' *> P.whiteSpace *> manyTill anyChar eof)