Skip to content

Commit fccd2f6

Browse files
committed
Refactor main modules to use new directive utilities
1 parent 6291fa0 commit fccd2f6

File tree

2 files changed

+46
-53
lines changed

2 files changed

+46
-53
lines changed

psci/Commands.hs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,3 @@ data Command
6565
-- Show command
6666
--
6767
| 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

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ import qualified Language.PureScript.AST as D
5454
import qualified Language.PureScript.Names as N
5555
import qualified Paths_purescript as Paths
5656

57-
import Commands as C
57+
import qualified Commands as C
58+
import qualified Directive as D
5859
import Parser
5960

6061
data PSCiOptions = PSCiOptions
@@ -170,9 +171,17 @@ expandTilde p = return p
170171
--
171172
helpMessage :: String
172173
helpMessage = "The following commands are available:\n\n " ++
173-
intercalate "\n " (map line C.help)
174-
where line :: (String, String, String) -> String
175-
line (cmd, arg, desc) = intercalate " " [cmd, arg, replicate (11 - length arg) ' ', desc]
174+
intercalate "\n " (map line D.help)
175+
where
176+
line :: (D.Directive, String, String) -> String
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)
176185

177186
-- |
178187
-- The welcome prologue.
@@ -205,19 +214,20 @@ data CompletionContext = Command String | FilePath String | Module | Identifier
205214
-- Decide what kind of completion we need based on input.
206215
completionContext :: String -> String -> Maybe CompletionContext
207216
completionContext cmd@"" _ = Just $ Multiple [Command cmd, Identifier]
208-
completionContext cmd@(':' : _ ) _
209-
| cmd `elem` C.commands || cmd == ":" = Just $ Command cmd
210-
completionContext (':' : c : _) word = case c of
211-
'i' -> Just Module
212-
'b' -> Just Module
213-
'm' -> Just $ FilePath word
214-
'q' -> Nothing
215-
'r' -> Nothing
216-
'?' -> Nothing
217-
's' -> Just $ Fixed ["import", "loaded"]
218-
't' -> Just Identifier
219-
'k' -> Just Type
220-
_ -> Nothing
217+
completionContext cmd@(":") _ = Just $ Command cmd
218+
completionContext (':' : cmd) word = do
219+
context =<< D.parseDirective (takeWhile (not . isSpace) cmd)
220+
where
221+
context :: D.Directive -> Maybe CompletionContext
222+
context D.Import = Just Module
223+
context D.Browse = Just Module
224+
context D.Load = Just $ FilePath word
225+
context D.Quit = Nothing
226+
context D.Reset = Nothing
227+
context D.Help = Nothing
228+
context D.Show = Just $ Fixed ["import", "loaded"]
229+
context D.Type = Just Identifier
230+
context D.Kind = Just Type
221231
completionContext _ _ = Just Identifier
222232

223233
-- |
@@ -238,13 +248,16 @@ completion = completeWordWithPrev Nothing " \t\n\r" findCompletions
238248
return $ sortBy sorter completions
239249

240250
getCompletion :: CompletionContext -> StateT PSCiState IO [Either String Completion]
241-
getCompletion (Command s) = return $ (map Left) $ nub $ filter (isPrefixOf s) C.commands
242251
getCompletion (FilePath f) = (map Right) <$> listFiles f
243252
getCompletion Module = (map Left) <$> getModuleNames
244253
getCompletion Identifier = (map Left) <$> ((++) <$> getIdentNames <*> getDctorNames)
245254
getCompletion Type = (map Left) <$> getTypeNames
246255
getCompletion (Fixed list) = return $ (map Left) list
247256
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
248261

249262
getLoadedModules :: StateT PSCiState IO [P.Module]
250263
getLoadedModules = map snd . psciLoadedModules <$> get
@@ -540,7 +553,7 @@ handleKindOf typ = do
540553
-- |
541554
-- Parses the input and returns either a Metacommand or an expression.
542555
--
543-
getCommand :: Bool -> InputT (StateT PSCiState IO) (Either String (Maybe Command))
556+
getCommand :: Bool -> InputT (StateT PSCiState IO) (Either String (Maybe C.Command))
544557
getCommand singleLineMode = do
545558
firstLine <- getInputLine "> "
546559
case firstLine of
@@ -555,12 +568,12 @@ getCommand singleLineMode = do
555568
-- |
556569
-- Performs an action for each meta-command given, and also for expressions..
557570
--
558-
handleCommand :: Command -> PSCI ()
559-
handleCommand (Expression val) = handleDeclaration val
560-
handleCommand Help = PSCI $ outputStrLn helpMessage
561-
handleCommand (Import moduleName) = handleImport moduleName
562-
handleCommand (Let l) = handleLet l
563-
handleCommand (LoadFile filePath) = do
571+
handleCommand :: C.Command -> PSCI ()
572+
handleCommand (C.Expression val) = handleDeclaration val
573+
handleCommand C.Help = PSCI $ outputStrLn helpMessage
574+
handleCommand (C.Import moduleName) = handleImport moduleName
575+
handleCommand (C.Let l) = handleLet l
576+
handleCommand (C.LoadFile filePath) = do
564577
absPath <- psciIO $ expandTilde filePath
565578
exists <- psciIO $ doesFileExist absPath
566579
if exists then do
@@ -571,22 +584,22 @@ handleCommand (LoadFile filePath) = do
571584
Right mods -> PSCI . lift $ modify (updateModules (map ((,) (Right absPath)) mods))
572585
else
573586
PSCI . outputStrLn $ "Couldn't locate: " ++ filePath
574-
handleCommand Reset = do
587+
handleCommand C.Reset = do
575588
files <- psciImportedFilenames <$> PSCI (lift get)
576589
PSCI . lift . modify $ \st -> st
577590
{ psciImportedFilenames = files
578591
, psciImportedModuleNames = defaultImports
579592
, psciLetBindings = []
580593
}
581594
loadAllImportedModules
582-
handleCommand (TypeOf val) = handleTypeOf val
583-
handleCommand (KindOf typ) = handleKindOf typ
584-
handleCommand (Browse moduleName) = handleBrowse moduleName
585-
handleCommand (Show "loaded") = handleShowLoadedModules
586-
handleCommand (Show "import") = handleShowImportedModules
595+
handleCommand (C.TypeOf val) = handleTypeOf val
596+
handleCommand (C.KindOf typ) = handleKindOf typ
597+
handleCommand (C.Browse moduleName) = handleBrowse moduleName
598+
handleCommand (C.Show "loaded") = handleShowLoadedModules
599+
handleCommand (C.Show "import") = handleShowImportedModules
587600
handleCommand _ = PSCI $ outputStrLn "Unknown command"
588601

589-
loadUserConfig :: IO (Maybe [Command])
602+
loadUserConfig :: IO (Maybe [C.Command])
590603
loadUserConfig = do
591604
configFile <- (</> ".psci") <$> getCurrentDirectory
592605
exists <- doesFileExist configFile
@@ -622,7 +635,7 @@ loop PSCiOptions{..} = do
622635
case c of
623636
Left err -> outputStrLn err >> go
624637
Right Nothing -> go
625-
Right (Just Quit) -> outputStrLn quitMessage
638+
Right (Just C.Quit) -> outputStrLn quitMessage
626639
Right (Just c') -> runPSCI (loadAllImportedModules >> handleCommand c') >> go
627640

628641
multiLineMode :: Parser Bool

0 commit comments

Comments
 (0)