Skip to content

Commit f523690

Browse files
authored
Migrate indexHieFile progress notification to ProgressReporting API (#4205)
What's done 1. Refactor ProgressReporting to allow external state management 2. Migrate `indexHieFile` progress to ProgressReporting API 3. Add Note [ProgressReporting API and InProgressState] to demonstrate the current status
1 parent 2f00507 commit f523690

File tree

4 files changed

+195
-181
lines changed

4 files changed

+195
-181
lines changed

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

+4-72
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ import Data.Proxy (Proxy (Proxy))
6767
import qualified Data.Text as T
6868
import Data.Time (UTCTime (..))
6969
import Data.Tuple.Extra (dupe)
70-
import Data.Unique as Unique
7170
import Debug.Trace
7271
import Development.IDE.Core.FileStore (resetInterfaceStore)
7372
import Development.IDE.Core.Preprocessor
@@ -81,6 +80,7 @@ import Development.IDE.GHC.Compat hiding (assert,
8180
import qualified Development.IDE.GHC.Compat as Compat
8281
import qualified Development.IDE.GHC.Compat as GHC
8382
import qualified Development.IDE.GHC.Compat.Util as Util
83+
import Development.IDE.Core.ProgressReporting (ProgressReporting (..), progressReportingOutsideState)
8484
import Development.IDE.GHC.CoreFile
8585
import Development.IDE.GHC.Error
8686
import Development.IDE.GHC.Orphans ()
@@ -97,7 +97,6 @@ import GHC.Serialized
9797
import HieDb hiding (withHieDb)
9898
import qualified Language.LSP.Protocol.Message as LSP
9999
import Language.LSP.Protocol.Types (DiagnosticTag (..))
100-
import qualified Language.LSP.Protocol.Types as LSP
101100
import qualified Language.LSP.Server as LSP
102101
import Prelude hiding (mod)
103102
import System.Directory
@@ -785,7 +784,6 @@ spliceExpressions Splices{..} =
785784
--
786785
indexHieFile :: ShakeExtras -> ModSummary -> NormalizedFilePath -> Util.Fingerprint -> Compat.HieFile -> IO ()
787786
indexHieFile se mod_summary srcPath !hash hf = do
788-
IdeOptions{optProgressStyle} <- getIdeOptionsIO se
789787
atomically $ do
790788
pending <- readTVar indexPending
791789
case HashMap.lookup srcPath pending of
@@ -806,69 +804,14 @@ indexHieFile se mod_summary srcPath !hash hf = do
806804
unless newerScheduled $ do
807805
-- Using bracket, so even if an exception happen during withHieDb call,
808806
-- the `post` (which clean the progress indicator) will still be called.
809-
bracket_ (pre optProgressStyle) post $
807+
bracket_ pre post $
810808
withHieDb (\db -> HieDb.addRefsFromLoaded db targetPath (HieDb.RealFile $ fromNormalizedFilePath srcPath) hash hf')
811809
where
812810
mod_location = ms_location mod_summary
813811
targetPath = Compat.ml_hie_file mod_location
814812
HieDbWriter{..} = hiedbWriter se
815813

816-
-- Get a progress token to report progress and update it for the current file
817-
pre style = do
818-
tok <- modifyVar indexProgressToken $ fmap dupe . \case
819-
x@(Just _) -> pure x
820-
-- Create a token if we don't already have one
821-
Nothing -> do
822-
case lspEnv se of
823-
Nothing -> pure Nothing
824-
Just env -> LSP.runLspT env $ do
825-
u <- LSP.ProgressToken . LSP.InR . T.pack . show . hashUnique <$> liftIO Unique.newUnique
826-
-- TODO: Wait for the progress create response to use the token
827-
_ <- LSP.sendRequest LSP.SMethod_WindowWorkDoneProgressCreate (LSP.WorkDoneProgressCreateParams u) (const $ pure ())
828-
LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams u $
829-
toJSON $ LSP.WorkDoneProgressBegin
830-
{ _kind = LSP.AString @"begin"
831-
, _title = "Indexing"
832-
, _cancellable = Nothing
833-
, _message = Nothing
834-
, _percentage = Nothing
835-
}
836-
pure (Just u)
837-
838-
(!done, !remaining) <- atomically $ do
839-
done <- readTVar indexCompleted
840-
remaining <- HashMap.size <$> readTVar indexPending
841-
pure (done, remaining)
842-
let
843-
progressFrac :: Double
844-
progressFrac = fromIntegral done / fromIntegral (done + remaining)
845-
progressPct :: LSP.UInt
846-
progressPct = floor $ 100 * progressFrac
847-
848-
whenJust (lspEnv se) $ \env -> whenJust tok $ \token -> LSP.runLspT env $
849-
LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams token $
850-
toJSON $
851-
case style of
852-
Percentage -> LSP.WorkDoneProgressReport
853-
{ _kind = LSP.AString @"report"
854-
, _cancellable = Nothing
855-
, _message = Nothing
856-
, _percentage = Just progressPct
857-
}
858-
Explicit -> LSP.WorkDoneProgressReport
859-
{ _kind = LSP.AString @"report"
860-
, _cancellable = Nothing
861-
, _message = Just $
862-
T.pack " (" <> T.pack (show done) <> "/" <> T.pack (show $ done + remaining) <> ")..."
863-
, _percentage = Nothing
864-
}
865-
NoProgress -> LSP.WorkDoneProgressReport
866-
{ _kind = LSP.AString @"report"
867-
, _cancellable = Nothing
868-
, _message = Nothing
869-
, _percentage = Nothing
870-
}
871-
814+
pre = progressUpdate indexProgressReporting ProgressStarted
872815
-- Report the progress once we are done indexing this file
873816
post = do
874817
mdone <- atomically $ do
@@ -883,18 +826,7 @@ indexHieFile se mod_summary srcPath !hash hf = do
883826
when (coerce $ ideTesting se) $
884827
LSP.sendNotification (LSP.SMethod_CustomMethod (Proxy @"ghcide/reference/ready")) $
885828
toJSON $ fromNormalizedFilePath srcPath
886-
whenJust mdone $ \done ->
887-
modifyVar_ indexProgressToken $ \tok -> do
888-
whenJust (lspEnv se) $ \env -> LSP.runLspT env $
889-
whenJust tok $ \token ->
890-
LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams token $
891-
toJSON $
892-
LSP.WorkDoneProgressEnd
893-
{ _kind = LSP.AString @"end"
894-
, _message = Just $ "Finished indexing " <> T.pack (show done) <> " files"
895-
}
896-
-- We are done with the current indexing cycle, so destroy the token
897-
pure Nothing
829+
whenJust mdone $ \_ -> progressUpdate indexProgressReporting ProgressCompleted
898830

899831
writeAndIndexHieFile :: HscEnv -> ShakeExtras -> ModSummary -> NormalizedFilePath -> [GHC.AvailInfo] -> HieASTs Type -> BS.ByteString -> IO [FileDiagnostic]
900832
writeAndIndexHieFile hscEnv se mod_summary srcPath exports ast source =

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ kick = do
141141
toJSON $ map fromNormalizedFilePath files
142142

143143
signal (Proxy @"kick/start")
144-
liftIO $ progressUpdate progress KickStarted
144+
progressUpdate progress ProgressNewStarted
145145

146146
-- Update the exports map
147147
results <- uses GenerateCore files
@@ -152,7 +152,7 @@ kick = do
152152
let mguts = catMaybes results
153153
void $ liftIO $ atomically $ modifyTVar' exportsMap (updateExportsMapMg mguts)
154154

155-
liftIO $ progressUpdate progress KickCompleted
155+
progressUpdate progress ProgressCompleted
156156

157157
GarbageCollectVar var <- getIdeGlobalAction
158158
garbageCollectionScheduled <- liftIO $ readVar var

0 commit comments

Comments
 (0)