From 02b43330f265c4ed7d2aa75a695c235d627fc20e Mon Sep 17 00:00:00 2001 From: Alex Ozdemir Date: Sat, 27 Jan 2018 17:40:10 -0500 Subject: [PATCH] Added metacommand `:list` :list allows the user to get a list of all variables they have defined. --- src/Completion.hs | 7 +------ src/Environment.hs | 9 +++++++++ src/Eval/Meta.hs | 8 ++++++++ src/Read.hs | 3 ++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Completion.hs b/src/Completion.hs index b6be699..a35cc5c 100644 --- a/src/Completion.hs +++ b/src/Completion.hs @@ -17,14 +17,9 @@ complete = lookupCompletions :: String -> Env.Task [Completion] lookupCompletions string = do env <- get - let defs = adjustDefs (Env.defs env) + let defs = Trie.unionL cmds $ Env.actualDefs env return (completions string defs) where - adjustDefs defs = - Trie.unionL cmds $ - Trie.delete Env.firstVar $ - Trie.delete Env.lastVar defs - cmds = Trie.fromList [ (":exit", "") diff --git a/src/Environment.hs b/src/Environment.hs index 9ff3a62..cce8d09 100644 --- a/src/Environment.hs +++ b/src/Environment.hs @@ -44,6 +44,7 @@ data Config | Help (Maybe String) | Exit | Reset + | List deriving (Show, Eq) @@ -66,6 +67,10 @@ needsPrint maybeDefName = Just _ -> False +getName :: DefName -> String +getName (DataDef s) = s +getName (Import s) = s +getName (VarDef s) = s -- ENVIRONMENT @@ -82,6 +87,10 @@ data Env = Env deriving Show +actualDefs :: Env -> Trie String +actualDefs env = Trie.delete firstVar $ + Trie.delete lastVar (defs env) + empty :: FilePath -> FilePath -> Env empty compiler interpreter = Env compiler diff --git a/src/Eval/Meta.hs b/src/Eval/Meta.hs index 04c22b1..efff71c 100644 --- a/src/Eval/Meta.hs +++ b/src/Eval/Meta.hs @@ -4,9 +4,12 @@ module Eval.Meta (eval) where import Control.Monad.State (get, modify) import Control.Monad.Trans (liftIO) import qualified Data.List as List +import Data.Maybe +import qualified Data.Trie as Trie import System.Exit (ExitCode(ExitSuccess)) import qualified Environment as Env +import Read (extractDefName) eval :: Env.Config -> Env.Task (Maybe ExitCode) @@ -19,6 +22,10 @@ eval config = do displayErr "Bad command\n" m display helpInfo + Env.List -> + do env <- get + display $ unlines $ map Env.getName $ concatMap (maybeToList . extractDefName) $ Trie.elems $ Env.actualDefs env + Env.InfoFlags m -> do displayErr "Bad flag\n" m display flagsInfo @@ -89,4 +96,5 @@ helpInfo = \ :help\t\t\tList available commands\n\ \ :flags\t\tManipulate flags sent to elm compiler\n\ \ :reset\t\tClears all previous imports\n\ + \ :list\t\t\tLists all defined variables\n\ \ :exit\t\t\tExits elm-repl\n" diff --git a/src/Read.hs b/src/Read.hs index 442e883..63584a4 100644 --- a/src/Read.hs +++ b/src/Read.hs @@ -1,5 +1,5 @@ {-# LANGUAGE OverloadedStrings #-} -module Read (input) where +module Read (input, extractDefName) where import qualified Data.Char as Char import qualified Data.List as List @@ -52,6 +52,7 @@ config = case flag of "exit" -> ok Env.Exit "reset" -> ok Env.Reset + "list" -> ok Env.List "help" -> ok (Env.Help Nothing) "flags" -> ok (Env.InfoFlags Nothing) <|> flags _ -> return $ Env.Help (Just flag)