Skip to content

Commit 48a46d1

Browse files
committed
add sessionLoadingPreferenceConfig var to SessionState
1 parent c9926d4 commit 48a46d1

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ import Ide.Logger (Pretty (pretty),
7878
nest,
7979
toCologActionWithPrio,
8080
vcat, viaShow, (<+>))
81-
import Ide.Types (SessionLoadingPreferenceConfig (..),
81+
import Ide.Types (Config,
82+
SessionLoadingPreferenceConfig (..),
8283
sessionLoading)
8384
import Language.LSP.Protocol.Message
8485
import Language.LSP.Server
@@ -460,7 +461,8 @@ data SessionState = SessionState
460461
hscEnvs :: !(Var HieMap),
461462
fileToFlags :: !FlagsMap,
462463
filesMap :: !FilesMap,
463-
version :: !(Var Int)
464+
version :: !(Var Int),
465+
sessionLoadingPreferenceConfig :: !(Var (Maybe SessionLoadingPreferenceConfig))
464466
}
465467

466468
-- | Helper functions for SessionState management
@@ -570,6 +572,24 @@ getExtraFilesToLoad state cfp = do
570572
-- remove error files from pending files since error loading need to load one by one
571573
else (Set.delete cfp $ pendingFiles `Set.difference` errorFiles) <> old_files
572574

575+
-- | We allow users to specify a loading strategy.
576+
-- Check whether this config was changed since the last time we have loaded
577+
-- a session.
578+
--
579+
-- If the loading configuration changed, we likely should restart the session
580+
-- in its entirety.
581+
didSessionLoadingPreferenceConfigChange :: SessionState -> Config -> IO Bool
582+
didSessionLoadingPreferenceConfigChange s clientConfig = do
583+
let biosSessionLoadingVar = sessionLoadingPreferenceConfig s
584+
mLoadingConfig <- readVar biosSessionLoadingVar
585+
case mLoadingConfig of
586+
Nothing -> do
587+
writeVar biosSessionLoadingVar (Just (sessionLoading clientConfig))
588+
pure False
589+
Just loadingConfig -> do
590+
writeVar biosSessionLoadingVar (Just (sessionLoading clientConfig))
591+
pure (loadingConfig /= sessionLoading clientConfig)
592+
573593
newSessionState :: IO SessionState
574594
newSessionState = do
575595
-- Initialize SessionState
@@ -581,6 +601,7 @@ newSessionState = do
581601
<*> STM.newIO -- fileToFlags
582602
<*> STM.newIO -- filesMap
583603
<*> newVar 0 -- version
604+
<*> newVar Nothing -- sessionLoadingPreferenceConfig
584605
return sessionState
585606

586607
-- | Given a root directory, return a Shake 'Action' which setups an
@@ -602,7 +623,6 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
602623
let toAbsolutePath = toAbsolute rootDir -- see Note [Root Directory]
603624

604625
sessionState <- newSessionState
605-
biosSessionLoadingVar <- newVar Nothing :: IO (Var (Maybe SessionLoadingPreferenceConfig))
606626
let returnWithVersion fun = IdeGhcSession fun <$> liftIO (readVar (version sessionState))
607627

608628
-- This caches the mapping from Mod.hs -> hie.yaml
@@ -833,31 +853,12 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
833853
-- we are only loading this file and it failed
834854
let res = map (\err' -> renderCradleError err' cradle ncfp) err
835855
handleFileProcessingError sessionState hieYaml cfp res $ concatMap cradleErrorDependencies err
836-
837-
let
838-
-- | We allow users to specify a loading strategy.
839-
-- Check whether this config was changed since the last time we have loaded
840-
-- a session.
841-
--
842-
-- If the loading configuration changed, we likely should restart the session
843-
-- in its entirety.
844-
didSessionLoadingPreferenceConfigChange :: IO Bool
845-
didSessionLoadingPreferenceConfigChange = do
846-
mLoadingConfig <- readVar biosSessionLoadingVar
847-
case mLoadingConfig of
848-
Nothing -> do
849-
writeVar biosSessionLoadingVar (Just (sessionLoading clientConfig))
850-
pure False
851-
Just loadingConfig -> do
852-
writeVar biosSessionLoadingVar (Just (sessionLoading clientConfig))
853-
pure (loadingConfig /= sessionLoading clientConfig)
854-
855856
-- This caches the mapping from hie.yaml + Mod.hs -> [String]
856857
-- Returns the Ghc session and the cradle dependencies
857858
let sessionOpts :: (Maybe FilePath, FilePath)
858859
-> IO ()
859860
sessionOpts (hieYaml, file) = do
860-
Extra.whenM didSessionLoadingPreferenceConfigChange $ do
861+
Extra.whenM (didSessionLoadingPreferenceConfigChange sessionState clientConfig) $ do
861862
logWith recorder Info LogSessionLoadingChanged
862863
-- If the dependencies are out of date then clear both caches and start
863864
-- again.

0 commit comments

Comments
 (0)