Skip to content

Commit fa65724

Browse files
committed
Move Cradle initialisation into withProgress callback
1 parent 2f006c7 commit fa65724

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

hie-plugin-api/Haskell/Ide/Engine/ModuleCache.hs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,39 +115,44 @@ loadCradle iniDynFlags (NewCradle fp) = do
115115
traceShowM cradle
116116
liftIO (GHC.newHscEnv iniDynFlags) >>= GHC.setSession
117117
liftIO $ setCurrentDirectory (BIOS.cradleRootDir cradle)
118-
res <- withProgress "Initialising Cradle" NotCancellable (\f ->
119-
BIOS.initializeFlagsWithCradleWithMessage (Just (toMessager f)) fp (fixCradle cradle)
120-
)
121-
case res of
122-
BIOS.CradleNone -> return (IdeResultOk ())
123-
BIOS.CradleFail err -> do
124-
logm $ "GhcException on cradle initialisation: " ++ show err
125-
return $ IdeResultFail $ IdeError
126-
{ ideCode = OtherError
127-
, ideMessage = Text.pack $ show err
128-
, ideInfo = Aeson.Null
129-
}
130-
BIOS.CradleSuccess init_session -> do
131-
init_res <- gcatches (Right <$> init_session)
132-
[ErrorHandler (\(ex :: GHC.GhcException)
133-
-> return $ Left (GHC.showGhcException ex ""))]
134-
case init_res of
135-
Left err -> do
136-
logm $ "GhcException on cradle initialisation: " ++ show err
137-
return $ IdeResultFail $ IdeError
118+
withProgress "Initialising Cradle" NotCancellable (initialiseCradle cradle)
119+
120+
where
121+
isStackCradle :: BIOS.Cradle -> Bool
122+
isStackCradle c = BIOS.actionName (BIOS.cradleOptsProg c) == "stack"
123+
124+
-- | Initialise the given cradle. This might fail and return an error via `IdeResultFail`.
125+
initialiseCradle :: (MonadIde m, HasGhcModuleCache m, GHC.GhcMonad m, MonadBaseControl IO m)
126+
=> BIOS.Cradle -> (Progress -> IO ()) -> m (IdeResult ())
127+
initialiseCradle cradle f = do
128+
res <- BIOS.initializeFlagsWithCradleWithMessage (Just (toMessager f)) fp (fixCradle cradle)
129+
case res of
130+
BIOS.CradleNone -> return (IdeResultOk ())
131+
BIOS.CradleFail err -> do
132+
logm $ "GhcException on cradle initialisation: " ++ show err
133+
return $ IdeResultFail $ IdeError
138134
{ ideCode = OtherError
139135
, ideMessage = Text.pack $ show err
140136
, ideInfo = Aeson.Null
141137
}
142-
-- Note: Don't setCurrentCradle because we want to try to reload
143-
-- it on a save whilst there are errors. Subsequent loads won't
144-
-- be that slow, even though the cradle isn't cached because the
145-
-- `.hi` files will be saved.
146-
Right () ->
147-
IdeResultOk <$> setCurrentCradle cradle
148-
where
149-
isStackCradle :: BIOS.Cradle -> Bool
150-
isStackCradle c = BIOS.actionName (BIOS.cradleOptsProg c) == "stack"
138+
BIOS.CradleSuccess init_session -> do
139+
init_res <- gcatches (Right <$> init_session)
140+
[ErrorHandler (\(ex :: GHC.GhcException)
141+
-> return $ Left (GHC.showGhcException ex ""))]
142+
case init_res of
143+
Left err -> do
144+
logm $ "GhcException on cradle initialisation: " ++ show err
145+
return $ IdeResultFail $ IdeError
146+
{ ideCode = OtherError
147+
, ideMessage = Text.pack $ show err
148+
, ideInfo = Aeson.Null
149+
}
150+
-- Note: Don't setCurrentCradle because we want to try to reload
151+
-- it on a save whilst there are errors. Subsequent loads won't
152+
-- be that slow, even though the cradle isn't cached because the
153+
-- `.hi` files will be saved.
154+
Right () ->
155+
IdeResultOk <$> setCurrentCradle cradle
151156

152157
-- The stack cradle doesn't return the target as well, so add the
153158
-- FilePath onto the end of the options to make sure at least one target

0 commit comments

Comments
 (0)