git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Refactor main modules to use new directive utilities
fccd2f6Łukasz Hanuszczak
psci/Commands.hs−20
@@ -65,23 +65,3 @@ data Command
65 -- Show command
65 -- Show command
66 --
66 --
67 | Show String
67 | Show String
68−
69−-- |
70−-- The help menu.
71−--
72−help :: [(String, String, String)]
73−help =
74− [ (":?", "", "Show this help menu")
75− , (":i", "<module>", "Import <module> for use in PSCI")
76− , (":b", "<module>", "Browse <module>")
77− , (":m", "<file>", "Load <file> for importing")
78− , (":q", "", "Quit PSCi")
79− , (":r", "", "Reset")
80− , (":s", "import", "Show imported modules")
81− , (":s", "loaded", "Show loaded modules")
82− , (":t", "<expr>", "Show the type of <expr>")
83− , (":k", "<type>", "Show the kind of <type>")
84− ]
85−
86−commands :: [String]
87−commands = map (\ (a, _, _) -> a) help
psci/Main.hs+46−33
@@ -54,7 +54,8 @@ import qualified Language.PureScript.AST as D
54import qualified Language.PureScript.Names as N
54import qualified Language.PureScript.Names as N
55import qualified Paths_purescript as Paths
55import qualified Paths_purescript as Paths
56
56
57−import Commands as C
57+import qualified Commands as C
58+import qualified Directive as D
58import Parser
59import Parser
59
60
60data PSCiOptions = PSCiOptions
61data PSCiOptions = PSCiOptions
@@ -170,9 +171,17 @@ expandTilde p = return p
170--
171--
171helpMessage :: String
172helpMessage :: String
172helpMessage = "The following commands are available:\n\n " ++
173helpMessage = "The following commands are available:\n\n " ++
173− intercalate "\n " (map line C.help)
174+ intercalate "\n " (map line D.help)
175+ where
174− where line :: (String, String, String) -> String
176+ line :: (D.Directive, String, String) -> String
175− line (cmd, arg, desc) = intercalate " " [cmd, arg, replicate (11 - length arg) ' ', desc]
177+ line (dir, arg, desc) = intercalate " "
178+ [ cmd
179+ , replicate (11 - length cmd) ' '
180+ , arg
181+ , replicate (11 - length arg) ' '
182+ , desc
183+ ]
184+ where cmd = ":" ++ head (D.commands dir)
176
185
177-- |
186-- |
178-- The welcome prologue.
187-- The welcome prologue.
@@ -205,19 +214,20 @@ data CompletionContext = Command String | FilePath String | Module | Identifier
205-- Decide what kind of completion we need based on input.
214-- Decide what kind of completion we need based on input.
206completionContext :: String -> String -> Maybe CompletionContext
215completionContext :: String -> String -> Maybe CompletionContext
207completionContext cmd@"" _ = Just $ Multiple [Command cmd, Identifier]
216completionContext cmd@"" _ = Just $ Multiple [Command cmd, Identifier]
208−completionContext cmd@(':' : _ ) _
217+completionContext cmd@(":") _ = Just $ Command cmd
209− | cmd `elem` C.commands || cmd == ":" = Just $ Command cmd
210−completionContext (':' : c : _) word = case c of
218+completionContext (':' : cmd) word = do
219+ context =<< D.parseDirective (takeWhile (not . isSpace) cmd)
220+ where
221+ context :: D.Directive -> Maybe CompletionContext
211− 'i' -> Just Module
222+ context D.Import = Just Module
212− 'b' -> Just Module
223+ context D.Browse = Just Module
213− 'm' -> Just $ FilePath word
224+ context D.Load = Just $ FilePath word
214− 'q' -> Nothing
225+ context D.Quit = Nothing
215− 'r' -> Nothing
226+ context D.Reset = Nothing
216− '?' -> Nothing
227+ context D.Help = Nothing
217− 's' -> Just $ Fixed ["import", "loaded"]
228+ context D.Show = Just $ Fixed ["import", "loaded"]
218− 't' -> Just Identifier
229+ context D.Type = Just Identifier
219− 'k' -> Just Type
230+ context D.Kind = Just Type
220− _ -> Nothing
221completionContext _ _ = Just Identifier
231completionContext _ _ = Just Identifier
222
232
223-- |
233-- |
@@ -238,13 +248,16 @@ completion = completeWordWithPrev Nothing " \t\n\r" findCompletions
238 return $ sortBy sorter completions
248 return $ sortBy sorter completions
239
249
240 getCompletion :: CompletionContext -> StateT PSCiState IO [Either String Completion]
250 getCompletion :: CompletionContext -> StateT PSCiState IO [Either String Completion]
241− getCompletion (Command s) = return $ (map Left) $ nub $ filter (isPrefixOf s) C.commands
242 getCompletion (FilePath f) = (map Right) <$> listFiles f
251 getCompletion (FilePath f) = (map Right) <$> listFiles f
243 getCompletion Module = (map Left) <$> getModuleNames
252 getCompletion Module = (map Left) <$> getModuleNames
244 getCompletion Identifier = (map Left) <$> ((++) <$> getIdentNames <*> getDctorNames)
253 getCompletion Identifier = (map Left) <$> ((++) <$> getIdentNames <*> getDctorNames)
245 getCompletion Type = (map Left) <$> getTypeNames
254 getCompletion Type = (map Left) <$> getTypeNames
246 getCompletion (Fixed list) = return $ (map Left) list
255 getCompletion (Fixed list) = return $ (map Left) list
247 getCompletion (Multiple contexts) = concat <$> mapM getCompletion contexts
256 getCompletion (Multiple contexts) = concat <$> mapM getCompletion contexts
257+ getCompletion (Command cmd) = return . map Left . nub $ matching
258+ where
259+ matching :: [String]
260+ matching = filter (isPrefixOf cmd) . concatMap (D.commands) $ D.directives
248
261
249 getLoadedModules :: StateT PSCiState IO [P.Module]
262 getLoadedModules :: StateT PSCiState IO [P.Module]
250 getLoadedModules = map snd . psciLoadedModules <$> get
263 getLoadedModules = map snd . psciLoadedModules <$> get
@@ -540,7 +553,7 @@ handleKindOf typ = do
540-- |
553-- |
541-- Parses the input and returns either a Metacommand or an expression.
554-- Parses the input and returns either a Metacommand or an expression.
542--
555--
543−getCommand :: Bool -> InputT (StateT PSCiState IO) (Either String (Maybe Command))
556+getCommand :: Bool -> InputT (StateT PSCiState IO) (Either String (Maybe C.Command))
544getCommand singleLineMode = do
557getCommand singleLineMode = do
545 firstLine <- getInputLine "> "
558 firstLine <- getInputLine "> "
546 case firstLine of
559 case firstLine of
@@ -555,12 +568,12 @@ getCommand singleLineMode = do
555-- |
568-- |
556-- Performs an action for each meta-command given, and also for expressions..
569-- Performs an action for each meta-command given, and also for expressions..
557--
570--
558−handleCommand :: Command -> PSCI ()
571+handleCommand :: C.Command -> PSCI ()
559−handleCommand (Expression val) = handleDeclaration val
572+handleCommand (C.Expression val) = handleDeclaration val
560−handleCommand Help = PSCI $ outputStrLn helpMessage
573+handleCommand C.Help = PSCI $ outputStrLn helpMessage
561−handleCommand (Import moduleName) = handleImport moduleName
574+handleCommand (C.Import moduleName) = handleImport moduleName
562−handleCommand (Let l) = handleLet l
575+handleCommand (C.Let l) = handleLet l
563−handleCommand (LoadFile filePath) = do
576+handleCommand (C.LoadFile filePath) = do
564 absPath <- psciIO $ expandTilde filePath
577 absPath <- psciIO $ expandTilde filePath
565 exists <- psciIO $ doesFileExist absPath
578 exists <- psciIO $ doesFileExist absPath
566 if exists then do
579 if exists then do
@@ -571,7 +584,7 @@ handleCommand (LoadFile filePath) = do
571 Right mods -> PSCI . lift $ modify (updateModules (map ((,) (Right absPath)) mods))
584 Right mods -> PSCI . lift $ modify (updateModules (map ((,) (Right absPath)) mods))
572 else
585 else
573 PSCI . outputStrLn $ "Couldn't locate: " ++ filePath
586 PSCI . outputStrLn $ "Couldn't locate: " ++ filePath
574−handleCommand Reset = do
587+handleCommand C.Reset = do
575 files <- psciImportedFilenames <$> PSCI (lift get)
588 files <- psciImportedFilenames <$> PSCI (lift get)
576 PSCI . lift . modify $ \st -> st
589 PSCI . lift . modify $ \st -> st
577 { psciImportedFilenames = files
590 { psciImportedFilenames = files
@@ -579,14 +592,14 @@ handleCommand Reset = do
579 , psciLetBindings = []
592 , psciLetBindings = []
580 }
593 }
581 loadAllImportedModules
594 loadAllImportedModules
582−handleCommand (TypeOf val) = handleTypeOf val
595+handleCommand (C.TypeOf val) = handleTypeOf val
583−handleCommand (KindOf typ) = handleKindOf typ
596+handleCommand (C.KindOf typ) = handleKindOf typ
584−handleCommand (Browse moduleName) = handleBrowse moduleName
597+handleCommand (C.Browse moduleName) = handleBrowse moduleName
585−handleCommand (Show "loaded") = handleShowLoadedModules
598+handleCommand (C.Show "loaded") = handleShowLoadedModules
586−handleCommand (Show "import") = handleShowImportedModules
599+handleCommand (C.Show "import") = handleShowImportedModules
587handleCommand _ = PSCI $ outputStrLn "Unknown command"
600handleCommand _ = PSCI $ outputStrLn "Unknown command"
588
601
589−loadUserConfig :: IO (Maybe [Command])
602+loadUserConfig :: IO (Maybe [C.Command])
590loadUserConfig = do
603loadUserConfig = do
591 configFile <- (</> ".psci") <$> getCurrentDirectory
604 configFile <- (</> ".psci") <$> getCurrentDirectory
592 exists <- doesFileExist configFile
605 exists <- doesFileExist configFile
@@ -622,7 +635,7 @@ loop PSCiOptions{..} = do
622 case c of
635 case c of
623 Left err -> outputStrLn err >> go
636 Left err -> outputStrLn err >> go
624 Right Nothing -> go
637 Right Nothing -> go
625− Right (Just Quit) -> outputStrLn quitMessage
638+ Right (Just C.Quit) -> outputStrLn quitMessage
626 Right (Just c') -> runPSCI (loadAllImportedModules >> handleCommand c') >> go
639 Right (Just c') -> runPSCI (loadAllImportedModules >> handleCommand c') >> go
627
640
628multiLineMode :: Parser Bool
641multiLineMode :: Parser Bool