Skip to content

Commit c19d377

Browse files
committed
Refactor command handling in startServer
1 parent b44f328 commit c19d377

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

app/Command/Ide.hs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Data.ByteString.Char8 qualified as BS8
2828
import Data.ByteString.Lazy.Char8 qualified as BSL8
2929
import GHC.IO.Exception (IOErrorType(..), IOException(..))
3030
import Language.PureScript.Ide (handleCommand)
31-
import Language.PureScript.Ide.Command (Command(..), commandName)
31+
import Language.PureScript.Ide.Command (commandName, Command(..))
3232
import Language.PureScript.Ide.Util (decodeT, displayTimeSpec, encodeT, logPerf, runLogger)
3333
import Language.PureScript.Ide.Error (IdeError(..))
3434
import Language.PureScript.Ide.State (updateCacheTimestamp)
@@ -199,14 +199,22 @@ startServer port env = Network.withSocketsDo $ do
199199
logPerf message $ do
200200
result <- runExceptT $ do
201201
updateCacheTimestamp >>= \case
202-
Nothing -> pure ()
202+
Nothing ->
203+
handleCommand cmd'
203204
Just (before, after) -> do
204205
-- If the cache db file was changed outside of the IDE
205206
-- we trigger a reset before processing the command
206207
$(logInfo) ("cachedb was changed from: " <> show before <> ", to: " <> show after)
207-
unless (isLoadAll cmd') $
208-
void (handleCommand Reset *> handleCommand (LoadSync []))
209-
handleCommand cmd'
208+
let doReload = handleCommand Reset *> handleCommand (LoadSync [])
209+
case cmd' of
210+
-- handleCommand on Load [] already resets the state.
211+
Load [] -> handleCommand cmd'
212+
-- Focus needs to fire before doReload, because we
213+
-- want to set the focused modules first before
214+
-- loading everything with LoadSync [].
215+
Focus _ -> handleCommand cmd' <* doReload
216+
-- Otherwise, just doReload and then handle.
217+
_ -> doReload *> handleCommand cmd'
210218
liftIO $ catchGoneHandle $ BSL8.hPutStrLn h $ case result of
211219
Right r -> Aeson.encode r
212220
Left err -> Aeson.encode err
@@ -219,11 +227,6 @@ startServer port env = Network.withSocketsDo $ do
219227
hFlush stdout
220228
liftIO $ catchGoneHandle (hClose h)
221229

222-
isLoadAll :: Command -> Bool
223-
isLoadAll = \case
224-
Load [] -> True
225-
_ -> False
226-
227230
catchGoneHandle :: IO () -> IO ()
228231
catchGoneHandle =
229232
handle (\e -> case e of

0 commit comments

Comments
 (0)