Skip to content

Commit 63a4c10

Browse files
committed
report indexing in another thread
1 parent d70acdb commit 63a4c10

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

ghcide/src/Development/IDE/Core/Compile.hs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ import GHC.Core.Lint.Interactive
139139
#if MIN_VERSION_ghc(9,7,0)
140140
import Data.Foldable (toList)
141141
import GHC.Unit.Module.Warnings
142+
import Control.Concurrent.Async (async, withAsync)
142143
#else
143144
import Development.IDE.Core.FileStore (shareFilePath)
144145
#endif
@@ -923,17 +924,19 @@ indexHieFile se mod_summary srcPath !hash hf = do
923924
-- If the hash in the pending list doesn't match the current hash, then skip
924925
Just pendingHash -> pendingHash /= hash
925926
unless newerScheduled $ do
926-
-- Using bracket, so even if an exception happen during withHieDb call,
927-
-- the `post` (which clean the progress indicator) will still be called.
928-
bracket_ (pre optProgressStyle) post $
929-
withHieDb (\db -> HieDb.addRefsFromLoaded db targetPath (HieDb.RealFile $ fromNormalizedFilePath srcPath) hash hf')
930-
where
927+
-- Using `finally`, so even if an exception happen during withHieDb call,
928+
-- the the ending part of `reportProgress` (which clean the progress indicator) will still be called.
929+
indexDoneB <- liftIO newBarrier
930+
withAsync (reportProgress indexDoneB optProgressStyle) $ \preAsync ->
931+
flip finally (signalBarrier indexDoneB ()) $
932+
withHieDb (\db -> HieDb.addRefsFromLoaded db targetPath (HieDb.RealFile $ fromNormalizedFilePath srcPath) hash hf')
933+
where
931934
mod_location = ms_location mod_summary
932935
targetPath = Compat.ml_hie_file mod_location
933936
HieDbWriter{..} = hiedbWriter se
934937

935938
-- Get a progress token to report progress and update it for the current file
936-
pre style = do
939+
reportProgress indexDoneB style = do
937940
tok <- modifyVar indexProgressToken $ fmap dupe . \case
938941
x@(Just _) -> pure x
939942
-- Create a token if we don't already have one
@@ -944,15 +947,16 @@ indexHieFile se mod_summary srcPath !hash hf = do
944947
u <- LSP.ProgressToken . LSP.InR . T.pack . show . hashUnique <$> liftIO Unique.newUnique
945948
b <- liftIO newBarrier
946949
void $ LSP.sendRequest LSP.SMethod_WindowWorkDoneProgressCreate (LSP.WorkDoneProgressCreateParams u) $ liftIO . signalBarrier b
947-
ready <- liftIO $ waitBarrier b
948-
LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams u $
949-
toJSON $ LSP.WorkDoneProgressBegin
950-
{ _kind = LSP.AString @"begin"
951-
, _title = "Indexing"
952-
, _cancellable = Nothing
953-
, _message = Nothing
954-
, _percentage = Nothing
955-
}
950+
liftIO $ async $ do
951+
_ready <- waitBarrier b
952+
LSP.runLspT env $ LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams u $
953+
toJSON $ LSP.WorkDoneProgressBegin
954+
{ _kind = LSP.AString @"begin"
955+
, _title = "Indexing"
956+
, _cancellable = Nothing
957+
, _message = Nothing
958+
, _percentage = Nothing
959+
}
956960
pure (Just u)
957961

958962
(!done, !remaining) <- atomically $ do
@@ -964,7 +968,6 @@ indexHieFile se mod_summary srcPath !hash hf = do
964968
progressFrac = fromIntegral done / fromIntegral (done + remaining)
965969
progressPct :: LSP.UInt
966970
progressPct = floor $ 100 * progressFrac
967-
968971
whenJust (lspEnv se) $ \env -> whenJust tok $ \token -> LSP.runLspT env $
969972
LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams token $
970973
toJSON $
@@ -988,9 +991,9 @@ indexHieFile se mod_summary srcPath !hash hf = do
988991
, _message = Nothing
989992
, _percentage = Nothing
990993
}
994+
-- Report the progress once we are done indexing this file
995+
liftIO $ waitBarrier indexDoneB
991996

992-
-- Report the progress once we are done indexing this file
993-
post = do
994997
mdone <- atomically $ do
995998
-- Remove current element from pending
996999
pending <- stateTVar indexPending $

0 commit comments

Comments
 (0)