@@ -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
57−import Commands as C
57+import qualified Commands as C
58+import qualified Directive as D
58import Parser
59import Parser
60data PSCiOptions = PSCiOptions
61data PSCiOptions = PSCiOptions
@@ -170,9 +171,17 @@ expandTilde p = return p
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)
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 " "
179+ , replicate (11 - length cmd) ' '
181+ , replicate (11 - length arg) ' '
184+ where cmd = ":" ++ head (D.commands dir)
178-- The welcome prologue.
187-- The welcome prologue.
@@ -205,19 +214,24 @@ 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) word =
218+ case D.parseDirective dstr of
219+ Just directive | dstr `elem` D.commands directive -> context directive
209− | cmd `elem` C.commands || cmd == ":" = Just $ Command cmd
220+ _ -> Just $ Command cmd
210−completionContext (':' : c : _) word = case c of
221+ where
223+ dstr = takeWhile (not . isSpace) cmd
225+ context :: D.Directive -> Maybe CompletionContext
211− 'i' -> Just Module
226+ context D.Import = Just Module
212− 'b' -> Just Module
227+ context D.Browse = Just Module
213− 'm' -> Just $ FilePath word
228+ context D.Load = Just $ FilePath word
214− 'q' -> Nothing
229+ context D.Quit = Nothing
215− 'r' -> Nothing
230+ context D.Reset = Nothing
216− '?' -> Nothing
231+ context D.Help = Nothing
217− 's' -> Just $ Fixed ["import", "loaded"]
232+ context D.Show = Just $ Fixed ["import", "loaded"]
218− 't' -> Just Identifier
233+ context D.Type = Just Identifier
219− 'k' -> Just Type
234+ context D.Kind = Just Type
221completionContext _ _ = Just Identifier
235completionContext _ _ = Just Identifier
@@ -238,13 +252,16 @@ completion = completeWordWithPrev Nothing " \t\n\r" findCompletions
238 return $ sortBy sorter completions
252 return $ sortBy sorter completions
240 getCompletion :: CompletionContext -> StateT PSCiState IO [Either String Completion]
254 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
255 getCompletion (FilePath f) = (map Right) <$> listFiles f
243 getCompletion Module = (map Left) <$> getModuleNames
256 getCompletion Module = (map Left) <$> getModuleNames
244 getCompletion Identifier = (map Left) <$> ((++) <$> getIdentNames <*> getDctorNames)
257 getCompletion Identifier = (map Left) <$> ((++) <$> getIdentNames <*> getDctorNames)
245 getCompletion Type = (map Left) <$> getTypeNames
258 getCompletion Type = (map Left) <$> getTypeNames
246 getCompletion (Fixed list) = return $ (map Left) list
259 getCompletion (Fixed list) = return $ (map Left) list
247 getCompletion (Multiple contexts) = concat <$> mapM getCompletion contexts
260 getCompletion (Multiple contexts) = concat <$> mapM getCompletion contexts
261+ getCompletion (Command cmd) = return . map (Left . (":" ++)) . nub $ matching
263+ matching :: [String]
264+ matching = filter (isPrefixOf cmd) . concatMap (D.commands) $ D.directives
249 getLoadedModules :: StateT PSCiState IO [P.Module]
266 getLoadedModules :: StateT PSCiState IO [P.Module]
250 getLoadedModules = map snd . psciLoadedModules <$> get
267 getLoadedModules = map snd . psciLoadedModules <$> get
@@ -540,7 +557,7 @@ handleKindOf typ = do
541-- Parses the input and returns either a Metacommand or an expression.
558-- Parses the input and returns either a Metacommand or an expression.
543−getCommand :: Bool -> InputT (StateT PSCiState IO) (Either String (Maybe Command))
560+getCommand :: Bool -> InputT (StateT PSCiState IO) (Either String (Maybe C.Command))
544getCommand singleLineMode = do
561getCommand singleLineMode = do
545 firstLine <- getInputLine "> "
562 firstLine <- getInputLine "> "
546 case firstLine of
563 case firstLine of
@@ -555,12 +572,12 @@ getCommand singleLineMode = do
556-- Performs an action for each meta-command given, and also for expressions..
573-- Performs an action for each meta-command given, and also for expressions..
558−handleCommand :: Command -> PSCI ()
575+handleCommand :: C.Command -> PSCI ()
559−handleCommand (Expression val) = handleDeclaration val
576+handleCommand (C.Expression val) = handleDeclaration val
560−handleCommand Help = PSCI $ outputStrLn helpMessage
577+handleCommand C.Help = PSCI $ outputStrLn helpMessage
561−handleCommand (Import moduleName) = handleImport moduleName
578+handleCommand (C.Import moduleName) = handleImport moduleName
562−handleCommand (Let l) = handleLet l
579+handleCommand (C.Let l) = handleLet l
563−handleCommand (LoadFile filePath) = do
580+handleCommand (C.LoadFile filePath) = do
564 absPath <- psciIO $ expandTilde filePath
581 absPath <- psciIO $ expandTilde filePath
565 exists <- psciIO $ doesFileExist absPath
582 exists <- psciIO $ doesFileExist absPath
566 if exists then do
583 if exists then do
@@ -571,7 +588,7 @@ handleCommand (LoadFile filePath) = do
571 Right mods -> PSCI . lift $ modify (updateModules (map ((,) (Right absPath)) mods))
588 Right mods -> PSCI . lift $ modify (updateModules (map ((,) (Right absPath)) mods))
573 PSCI . outputStrLn $ "Couldn't locate: " ++ filePath
590 PSCI . outputStrLn $ "Couldn't locate: " ++ filePath
574−handleCommand Reset = do
591+handleCommand C.Reset = do
575 files <- psciImportedFilenames <$> PSCI (lift get)
592 files <- psciImportedFilenames <$> PSCI (lift get)
576 PSCI . lift . modify $ \st -> st
593 PSCI . lift . modify $ \st -> st
577 { psciImportedFilenames = files
594 { psciImportedFilenames = files
@@ -579,14 +596,14 @@ handleCommand Reset = do
579 , psciLetBindings = []
596 , psciLetBindings = []
581 loadAllImportedModules
598 loadAllImportedModules
582−handleCommand (TypeOf val) = handleTypeOf val
599+handleCommand (C.TypeOf val) = handleTypeOf val
583−handleCommand (KindOf typ) = handleKindOf typ
600+handleCommand (C.KindOf typ) = handleKindOf typ
584−handleCommand (Browse moduleName) = handleBrowse moduleName
601+handleCommand (C.Browse moduleName) = handleBrowse moduleName
585−handleCommand (Show "loaded") = handleShowLoadedModules
602+handleCommand (C.Show "loaded") = handleShowLoadedModules
586−handleCommand (Show "import") = handleShowImportedModules
603+handleCommand (C.Show "import") = handleShowImportedModules
587handleCommand _ = PSCI $ outputStrLn "Unknown command"
604handleCommand _ = PSCI $ outputStrLn "Unknown command"
589−loadUserConfig :: IO (Maybe [Command])
606+loadUserConfig :: IO (Maybe [C.Command])
590loadUserConfig = do
607loadUserConfig = do
591 configFile <- (</> ".psci") <$> getCurrentDirectory
608 configFile <- (</> ".psci") <$> getCurrentDirectory
592 exists <- doesFileExist configFile
609 exists <- doesFileExist configFile
@@ -622,7 +639,7 @@ loop PSCiOptions{..} = do
622 case c of
639 case c of
623 Left err -> outputStrLn err >> go
640 Left err -> outputStrLn err >> go
624 Right Nothing -> go
641 Right Nothing -> go
625− Right (Just Quit) -> outputStrLn quitMessage
642+ Right (Just C.Quit) -> outputStrLn quitMessage
626 Right (Just c') -> runPSCI (loadAllImportedModules >> handleCommand c') >> go
643 Right (Just c') -> runPSCI (loadAllImportedModules >> handleCommand c') >> go
628multiLineMode :: Parser Bool
645multiLineMode :: Parser Bool