Skip to content

Commit 09213d3

Browse files
committed
simplified
1 parent 702e367 commit 09213d3

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

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

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -437,16 +437,26 @@ On failure:
437437
This approach ensures efficient batch loading while isolating problematic files for individual handling.
438438
-}
439439

440-
handleLoadingSucc :: SessionState -> HashSet FilePath -> IO ()
441-
handleLoadingSucc sessionState files = do
442-
atomically $ forM_ (Set.toList files) $ flip S.delete (pendingFiles sessionState)
443-
mapM_ (removeErrorLoadingFile sessionState) (Set.toList files)
444-
addCradleFiles sessionState files
440+
-- SBL3
441+
handleLoadingSuccBatch :: Foldable t => Recorder (WithPriority Log) -> SessionState -> Maybe FilePath -> HashMap NormalizedFilePath (IdeResult HscEnvEq, DependencyInfo) -> t TargetDetails -> IO ()
442+
handleLoadingSuccBatch recorder sessionState hieYaml this_flags_map all_targets = do
443+
pendings <- getPendingFiles sessionState
444+
-- this_flags_map might contains files not in pendingFiles, take the intersection
445+
let newLoaded = pendings `Set.intersection` Set.fromList (fromNormalizedFilePath <$> HM.keys this_flags_map)
446+
atomically $ do
447+
STM.insert this_flags_map hieYaml (fileToFlags sessionState)
448+
insertAllFileMappings sessionState $ map ((hieYaml,) . fst) $ concatMap toFlagsMap all_targets
449+
logWith recorder Info $ LogSessionNewLoadedFiles $ Set.toList newLoaded
450+
atomically $ forM_ (Set.toList newLoaded) $ flip S.delete (pendingFiles sessionState)
451+
mapM_ (removeErrorLoadingFile sessionState) (Set.toList newLoaded)
452+
addCradleFiles sessionState newLoaded
445453

454+
-- SBL5
446455
handleLoadingFailureBatch :: SessionState -> [FilePath] -> IO ()
447456
handleLoadingFailureBatch sessionState files = do
448-
addErrorLoadingFiles sessionState files
457+
mapM_ (addErrorLoadingFile sessionState) files
449458

459+
-- SBL4
450460
handleLoadingFailureSingle :: SessionState -> FilePath -> IO ()
451461
handleLoadingFailureSingle sessionState file = do
452462
addErrorLoadingFile sessionState file
@@ -473,9 +483,6 @@ addErrorLoadingFile :: SessionState -> FilePath -> IO ()
473483
addErrorLoadingFile state file =
474484
modifyVar_' (failedFiles state) (\xs -> return $ Set.insert file xs)
475485

476-
addErrorLoadingFiles :: SessionState -> [FilePath] -> IO ()
477-
addErrorLoadingFiles = mapM_ . addErrorLoadingFile
478-
479486
-- | Remove a file from the set of files with errors during loading
480487
removeErrorLoadingFile :: SessionState -> FilePath -> IO ()
481488
removeErrorLoadingFile state file =
@@ -526,7 +533,6 @@ addToPending :: SessionState -> FilePath -> STM ()
526533
addToPending state file =
527534
S.insert file (pendingFiles state)
528535

529-
530536
-- | Insert multiple file mappings at once
531537
insertAllFileMappings :: SessionState -> [(Maybe FilePath, NormalizedFilePath)] -> STM ()
532538
insertAllFileMappings state mappings =
@@ -541,13 +547,13 @@ getPendingFiles :: SessionState -> IO (HashSet FilePath)
541547
getPendingFiles state = atomically $ Set.fromList <$> S.toUnOrderedList (pendingFiles state)
542548

543549
-- | Handle errors during session loading by recording file as having error and removing from pending
544-
handleSessionError :: SessionState -> Maybe FilePath -> FilePath -> PackageSetupException -> IO ()
545-
handleSessionError state hieYaml file e = do
546-
handleFileProcessingError state hieYaml file [renderPackageSetupException file e] mempty
550+
handleSingleFileProcessingError' :: SessionState -> Maybe FilePath -> FilePath -> PackageSetupException -> IO ()
551+
handleSingleFileProcessingError' state hieYaml file e = do
552+
handleSingleFileProcessingError state hieYaml file [renderPackageSetupException file e] mempty
547553

548554
-- | Common pattern: Insert file flags, insert file mapping, and remove from pending
549-
handleFileProcessingError :: SessionState -> Maybe FilePath -> FilePath -> [FileDiagnostic] -> [FilePath] -> IO ()
550-
handleFileProcessingError state hieYaml file diags extraDepFiles = do
555+
handleSingleFileProcessingError :: SessionState -> Maybe FilePath -> FilePath -> [FileDiagnostic] -> [FilePath] -> IO ()
556+
handleSingleFileProcessingError state hieYaml file diags extraDepFiles = do
551557
dep <- getDependencyInfo $ maybeToList hieYaml <> extraDepFiles
552558
let ncfp = toNormalizedFilePath' file
553559
let flags = ((diags, Nothing), dep)
@@ -766,15 +772,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
766772
])
767773
Nothing
768774

769-
pendings <- getPendingFiles sessionState
770-
-- this_flags_map might contains files not in pendingFiles, take the intersection
771-
let newLoaded = pendings `Set.intersection` Set.fromList (fromNormalizedFilePath <$> HM.keys this_flags_map)
772-
atomically $ do
773-
STM.insert this_flags_map hieYaml (fileToFlags sessionState)
774-
insertAllFileMappings sessionState $ map ((hieYaml,) . fst) $ concatMap toFlagsMap all_targets
775-
776-
logWith recorder Info $ LogSessionNewLoadedFiles $ Set.toList newLoaded
777-
handleLoadingSucc sessionState newLoaded
775+
handleLoadingSuccBatch recorder sessionState hieYaml this_flags_map all_targets
778776
-- Typecheck all files in the project on startup
779777
checkProject <- getCheckProject
780778

@@ -828,9 +826,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
828826
[] -> error $ "GHC version could not be parsed: " <> version
829827
((runTime, _):_)
830828
| compileTime == runTime -> session (hieYaml, ncfp, opts, libDir)
831-
| otherwise -> do
832-
-- Use the common pattern here: updateFileState
833-
handleFileProcessingError sessionState hieYaml cfp [renderPackageSetupException cfp GhcVersionMismatch{..}] mempty
829+
| otherwise -> handleSingleFileProcessingError' sessionState hieYaml cfp (GhcVersionMismatch{..})
834830
-- Failure case, either a cradle error or the none cradle
835831
Left err -> do
836832
-- what if the error to load file is one of old_files ?
@@ -852,7 +848,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
852848
else do
853849
-- we are only loading this file and it failed
854850
let res = map (\err' -> renderCradleError err' cradle ncfp) err
855-
handleFileProcessingError sessionState hieYaml cfp res $ concatMap cradleErrorDependencies err
851+
handleSingleFileProcessingError sessionState hieYaml cfp res $ concatMap cradleErrorDependencies err
856852
-- This caches the mapping from hie.yaml + Mod.hs -> [String]
857853
-- Returns the Ghc session and the cradle dependencies
858854
let sessionOpts :: (Maybe FilePath, FilePath)
@@ -909,7 +905,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
909905
cachedHieYamlLocation <- join <$> atomically (STM.lookup ncfp (filesMap sessionState))
910906
hieYaml <- cradleLoc file
911907
let hieLoc = cachedHieYamlLocation <|> hieYaml
912-
sessionOpts (hieLoc, file) `Safe.catch` handleSessionError sessionState hieLoc file
908+
sessionOpts (hieLoc, file) `Safe.catch` handleSingleFileProcessingError' sessionState hieLoc file
913909
getOptionsLoop
914910

915911
-- | Given a file, this function will return the HscEnv and the dependencies

0 commit comments

Comments
 (0)