FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

New PSCi command: reload by hatashiro · Pull Request #2721 · purescript/purescript · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .hs  (7) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 4 additions & 4 deletions app/Command/REPL.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ data Backend = forall state. Backend
data BrowserCommand
= Eval (MVar String)
-- ^ Evaluate the latest JS
| Reload
-- ^ Reload the page
| Refresh
-- ^ Refresh the page

-- | State for the browser backend
data BrowserState = BrowserState
Expand Down Expand Up @@ -185,7 +185,7 @@ browserBackend serverPort = Backend setup evaluate reload shutdown
-- With many connected clients, all but one of
-- these attempts will fail.
tryPutMVar resultVar (unpack result)
Reload ->
Refresh ->
WS.sendTextData conn ("reload" :: Text)

shutdownHandler :: IO () -> IO ()
Expand Down Expand Up @@ -262,7 +262,7 @@ browserBackend serverPort = Backend setup evaluate reload shutdown
reload :: BrowserState -> IO ()
reload state = do
createBundle state
atomically $ writeTChan (browserCommands state) Reload
atomically $ writeTChan (browserCommands state) Refresh

shutdown :: BrowserState -> IO ()
shutdown state = putMVar (browserShutdownNotice state) ()
Expand Down
21 changes: 15 additions & 6 deletions src/Language/PureScript/Interactive.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ handleCommand
-> Command
-> m ()
handleCommand _ _ ShowHelp = liftIO $ putStrLn helpMessage
handleCommand _ r ResetState = handleResetState r
handleCommand _ r ReloadState = handleReloadState r
handleCommand _ r ClearState = handleClearState r
handleCommand c _ (Expression val) = handleExpression c val
handleCommand _ _ (Import im) = handleImport im
handleCommand _ _ (Decls l) = handleDecls l
Expand All @@ -106,14 +107,13 @@ handleCommand _ _ (ShowInfo QueryLoaded) = handleShowLoadedModules
handleCommand _ _ (ShowInfo QueryImport) = handleShowImportedModules
handleCommand _ _ _ = P.internalError "handleCommand: unexpected command"

-- | Reset the application state
handleResetState
-- | Reload the application state
handleReloadState
:: (MonadReader PSCiConfig m, MonadState PSCiState m, MonadIO m)
=> m ()
-> m ()
handleResetState reload = do
modify $ updateImportedModules (const [])
. updateLets (const [])
handleReloadState reload = do
modify $ updateLets (const [])
files <- asks psciLoadedFiles
e <- runExceptT $ do
modules <- ExceptT . liftIO $ loadAllModules files
Expand All @@ -125,6 +125,15 @@ handleResetState reload = do
modify (updateLoadedExterns (const (zip modules externs)))
reload

-- | Clear the application state
handleClearState
:: (MonadReader PSCiConfig m, MonadState PSCiState m, MonadIO m)
=> m ()
-> m ()
handleClearState reload = do
modify $ updateImportedModules (const [])
handleReloadState reload

-- | Takes a value expression and evaluates it with the current state.
handleExpression
:: (MonadReader PSCiConfig m, MonadState PSCiState m, MonadIO m)
Expand Down
3 changes: 2 additions & 1 deletion src/Language/PureScript/Interactive/Completion.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ completeDirective ws w =
directiveArg :: String -> Directive -> [CompletionContext]
directiveArg _ Browse = [CtxModule]
directiveArg _ Quit = []
directiveArg _ Reset = []
directiveArg _ Reload = []
directiveArg _ Clear = []
directiveArg _ Help = []
directiveArg _ Paste = []
directiveArg _ Show = map CtxFixed replQueryStrings
Expand Down
9 changes: 6 additions & 3 deletions src/Language/PureScript/Interactive/Directive.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ directiveStrings :: [(Directive, [String])]
directiveStrings =
[ (Help , ["?", "help"])
, (Quit , ["quit"])
, (Reset , ["reset"])
, (Reload , ["reload"])
, (Clear , ["clear"])
, (Browse , ["browse"])
, (Type , ["type"])
, (Kind , ["kind"])
Expand Down Expand Up @@ -82,7 +83,8 @@ parseDirective = listToMaybe . directivesFor
hasArgument :: Directive -> Bool
hasArgument Help = False
hasArgument Quit = False
hasArgument Reset = False
hasArgument Reload = False
hasArgument Clear = False
hasArgument Paste = False
hasArgument _ = True

Expand All @@ -93,7 +95,8 @@ help :: [(Directive, String, String)]
help =
[ (Help, "", "Show this help menu")
, (Quit, "", "Quit PSCi")
, (Reset, "", "Discard all imported modules and declared bindings")
, (Reload, "", "Reload all imported modules while discarding bindings")
, (Clear, "", "Discard all imported modules and declared bindings")
, (Browse, "<module>", "See all functions in <module>")
, (Type, "<expr>", "Show the type of <expr>")
, (Kind, "<type>", "Show the kind of <type>")
Expand Down
3 changes: 2 additions & 1 deletion src/Language/PureScript/Interactive/Parser.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ parseDirective cmd =
commandFor d = case d of
Help -> return ShowHelp
Quit -> return QuitPSCi
Reset -> return ResetState
Reload -> return ReloadState
Clear -> return ClearState
Paste -> return PasteLines
Browse -> BrowseModule <$> parseRest P.moduleName arg
Show -> ShowInfo <$> parseReplQuery' (trim arg)
Expand Down
9 changes: 6 additions & 3 deletions src/Language/PureScript/Interactive/Types.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ data Command
| BrowseModule P.ModuleName
-- | Exit PSCI
| QuitPSCi
-- | Reset the state of the REPL
| ResetState
-- | Reload all the imported modules of the REPL
| ReloadState
-- | Clear the state of the REPL
| ClearState
-- | Add some declarations to the current evaluation context
| Decls [P.Declaration]
-- | Find the type of an expression
Expand Down Expand Up @@ -120,7 +122,8 @@ parseReplQuery _ = Nothing
data Directive
= Help
| Quit
| Reset
| Reload
| Clear
| Browse
| Type
| Kind
Expand Down
81 changes: 67 additions & 14 deletions tests/TestPsci.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module TestPsci where
import Prelude ()
import Prelude.Compat

import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.State.Strict (evalStateT)
import Control.Monad.Trans.RWS.Strict (evalRWST, RWST, get)
import Control.Monad (when)

import Data.List (sort)
Expand All @@ -22,9 +24,7 @@ import Test.HUnit

import qualified Language.PureScript as P

import Language.PureScript.Interactive.Module (loadAllModules)
import Language.PureScript.Interactive.Completion
import Language.PureScript.Interactive.Types
import Language.PureScript.Interactive

import TestUtils (supportModules)

Expand All @@ -34,7 +34,9 @@ main = do
when (errors + failures > 0) exitFailure

allTests :: Test
allTests = completionTests
allTests = TestList [ completionTests
, commandTests
]

completionTests :: Test
completionTests =
Expand All @@ -48,7 +50,8 @@ completionTestData :: [(String, [String])]
completionTestData =
-- basic directives
[ (":h", [":help"])
, (":re", [":reset"])
, (":r", [":reload"])
, (":c", [":clear"])
, (":q", [":quit"])
, (":b", [":browse"])

Expand All @@ -60,10 +63,11 @@ completionTestData =
, ("import Control.Monad.E", map ("import Control.Monad.Eff" ++) ["", ".Unsafe", ".Class", ".Console"])
, ("import Control.Monad.Eff.", map ("import Control.Monad.Eff" ++) [".Unsafe", ".Class", ".Console"])

-- :quit, :help, :reset should not complete
-- :quit, :help, :reload, :clear should not complete
, (":help ", [])
, (":quit ", [])
, (":reset ", [])
, (":reload ", [])
, (":clear ", [])

-- :show should complete to "loaded" and "import"
, (":show ", [":show import", ":show loaded"])
Expand Down Expand Up @@ -113,11 +117,11 @@ assertCompletedOk (line, expecteds) = do

runCM :: CompletionM a -> IO a
runCM act = do
psciState <- getPSCiState
psciState <- getPSCiStateForCompletion
evalStateT (liftCompletionM act) psciState

getPSCiState :: IO PSCiState
getPSCiState = do
initTestPSCi :: IO (PSCiState, PSCiConfig)
initTestPSCi = do
cwd <- getCurrentDirectory
let supportDir = cwd </> "tests" </> "support" </> "bower_components"
let supportFiles ext = Glob.globDir1 (Glob.compile ("purescript-*/src/**/*." ++ ext)) supportDir
Expand All @@ -127,12 +131,61 @@ getPSCiState = do
case modulesOrFirstError of
Left err ->
print err >> exitFailure
Right modules ->
let imports = [controlMonadSTasST, (P.ModuleName [P.ProperName (T.pack "Prelude")], P.Implicit, Nothing)]
dummyExterns = P.internalError "TestPsci: dummyExterns should not be used"
in return (PSCiState imports [] (zip (map snd modules) (repeat dummyExterns)))
Right modules -> do
resultOrErrors <- runMake . make $ modules
case resultOrErrors of
Left errs -> putStrLn (P.prettyPrintMultipleErrors P.defaultPPEOptions errs) >> exitFailure
Right (externs, env) ->
return (PSCiState [] [] (zip (map snd modules) externs), PSCiConfig pursFiles env)

getPSCiStateForCompletion :: IO PSCiState
getPSCiStateForCompletion = do
(PSCiState _ bs es, _) <- initTestPSCi
let imports = [controlMonadSTasST, (P.ModuleName [P.ProperName (T.pack "Prelude")], P.Implicit, Nothing)]
return $ PSCiState imports bs es

controlMonadSTasST :: ImportedModule
controlMonadSTasST = (s "Control.Monad.ST", P.Implicit, Just (s "ST"))
where
s = P.moduleNameFromString . T.pack

type TestPSCi a = RWST PSCiConfig () PSCiState IO a

runTestPSCi :: TestPSCi a -> IO a
runTestPSCi i = do
(s, c) <- initTestPSCi
fst <$> evalRWST i c s

testEval :: String -> TestPSCi ()
testEval = const $ return () -- not yet actually eval expr command

testReload :: TestPSCi ()
testReload = return ()

run :: String -> TestPSCi ()
run s = case parseCommand s of
Left errStr -> liftIO $ putStrLn errStr >> exitFailure
Right command ->
handleCommand testEval testReload command

commandTests :: Test
commandTests = TestLabel "commandTests" $ TestList $ map (TestCase . runTestPSCi)
[ do
run "import Prelude"
run "import Data.Functor"
run "import Control.Monad"
before <- psciImportedModules <$> get
liftIO $ length before @?= 3
run ":clear"
after <- psciImportedModules <$> get
liftIO $ length after @?= 0
, do
run "import Prelude"
run "import Data.Functor"
run "import Control.Monad"
before <- psciImportedModules <$> get
liftIO $ length before @?= 3
run ":reload"
after <- psciImportedModules <$> get
liftIO $ length after @?= 3
]

Back | FazBrowse Home | New Git URL