Skip to content

Migrate indexHieFile progress notification to ProgressReporting API #4205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8981601
swtich to general progress
soulomoon Jun 13, 2024
ae83626
fix
soulomoon Jun 13, 2024
8800d2a
format
soulomoon Jun 13, 2024
f3cc5b0
add `progressReportingOutsideState`
soulomoon Jun 13, 2024
dde4c1d
format with stylish
soulomoon Jun 13, 2024
e0471c7
clean up
soulomoon Jun 13, 2024
3767e81
cleanup
soulomoon Jun 13, 2024
85c7cd0
clean up
soulomoon Jun 13, 2024
f1b14de
Merge branch 'master' into soulomoon/wait-for-token-indexHieFile
soulomoon Jun 13, 2024
2cc61bb
Merge branch 'master' into soulomoon/wait-for-token-indexHieFile
soulomoon Jun 15, 2024
9f3b396
Merge branch 'master' into soulomoon/wait-for-token-indexHieFile
soulomoon Jun 15, 2024
7934369
Merge branch 'master' into soulomoon/wait-for-token-indexHieFile
soulomoon Jun 17, 2024
43abadd
add comment
soulomoon Jun 17, 2024
0c9325e
adjust to use `progressCounter`
soulomoon Jun 17, 2024
e83f3bc
Extract progressCounter
soulomoon Jun 17, 2024
ad25018
IO switch to m in progressUpdate
soulomoon Jun 17, 2024
1a75a36
format
soulomoon Jun 17, 2024
fb44ba8
try to start at the beginning
soulomoon Jun 17, 2024
b3a4456
rename
soulomoon Jun 17, 2024
4671143
stylish
soulomoon Jun 17, 2024
477e481
add shutdown
soulomoon Jun 17, 2024
072b882
Merge branch 'master' into soulomoon/wait-for-token-indexHieFile
soulomoon Jun 20, 2024
51acba7
add note
soulomoon Jun 20, 2024
0b3808f
add Note
soulomoon Jun 20, 2024
1405cb7
Merge branch 'master' into soulomoon/wait-for-token-indexHieFile
soulomoon Jun 20, 2024
d21ba21
fix
soulomoon Jun 20, 2024
4f3828c
Merge branch 'master' into soulomoon/wait-for-token-indexHieFile
michaelpj Jun 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 4 additions & 72 deletions ghcide/src/Development/IDE/Core/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@
import qualified Data.Text as T
import Data.Time (UTCTime (..))
import Data.Tuple.Extra (dupe)
import Data.Unique as Unique
import Debug.Trace
import Development.IDE.Core.FileStore (resetInterfaceStore)

Check warning on line 71 in ghcide/src/Development/IDE/Core/Compile.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Warning in module Development.IDE.Core.Compile: Use fewer imports ▫︎ Found: "import Development.IDE.Core.FileStore ( resetInterfaceStore )\nimport Development.IDE.Core.FileStore ( shareFilePath )\n" ▫︎ Perhaps: "import Development.IDE.Core.FileStore\n ( resetInterfaceStore, shareFilePath )\n"
import Development.IDE.Core.Preprocessor
import Development.IDE.Core.RuleTypes
import Development.IDE.Core.Shake
Expand All @@ -81,6 +80,7 @@
import qualified Development.IDE.GHC.Compat as Compat
import qualified Development.IDE.GHC.Compat as GHC
import qualified Development.IDE.GHC.Compat.Util as Util
import Development.IDE.Core.ProgressReporting (ProgressReporting (..), progressReportingOutsideState)
import Development.IDE.GHC.CoreFile
import Development.IDE.GHC.Error
import Development.IDE.GHC.Orphans ()
Expand All @@ -97,7 +97,6 @@
import HieDb hiding (withHieDb)
import qualified Language.LSP.Protocol.Message as LSP
import Language.LSP.Protocol.Types (DiagnosticTag (..))
import qualified Language.LSP.Protocol.Types as LSP
import qualified Language.LSP.Server as LSP
import Prelude hiding (mod)
import System.Directory
Expand Down Expand Up @@ -293,7 +292,7 @@

{- load it -}
; (fv_hvs, lbss, pkgs) <- loadDecls (hscInterp hsc_env') hsc_env' srcspan bcos
; let hval = ((expectJust "hscCompileCoreExpr'" $ lookup (idName binding_id) fv_hvs), lbss, pkgs)

Check warning on line 295 in ghcide/src/Development/IDE/Core/Compile.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Suggestion in captureSplicesAndDeps in module Development.IDE.Core.Compile: Redundant bracket ▫︎ Found: "((expectJust \"hscCompileCoreExpr'\"\n $ lookup (idName binding_id) fv_hvs), \n lbss, pkgs)" ▫︎ Perhaps: "(expectJust \"hscCompileCoreExpr'\"\n $ lookup (idName binding_id) fv_hvs, \n lbss, pkgs)"

; modifyIORef' var (flip extendModuleEnvList [(mi_module $ hm_iface hm, linkableHash lb) | lb <- lbs, let hm = linkableHomeMod lb])
; return hval }
Expand Down Expand Up @@ -785,7 +784,6 @@
--
indexHieFile :: ShakeExtras -> ModSummary -> NormalizedFilePath -> Util.Fingerprint -> Compat.HieFile -> IO ()
indexHieFile se mod_summary srcPath !hash hf = do
IdeOptions{optProgressStyle} <- getIdeOptionsIO se
atomically $ do
pending <- readTVar indexPending
case HashMap.lookup srcPath pending of
Expand All @@ -806,69 +804,14 @@
unless newerScheduled $ do
-- Using bracket, so even if an exception happen during withHieDb call,
-- the `post` (which clean the progress indicator) will still be called.
bracket_ (pre optProgressStyle) post $
bracket_ pre post $
withHieDb (\db -> HieDb.addRefsFromLoaded db targetPath (HieDb.RealFile $ fromNormalizedFilePath srcPath) hash hf')
where
mod_location = ms_location mod_summary
targetPath = Compat.ml_hie_file mod_location
HieDbWriter{..} = hiedbWriter se

-- Get a progress token to report progress and update it for the current file
pre style = do
tok <- modifyVar indexProgressToken $ fmap dupe . \case
x@(Just _) -> pure x
-- Create a token if we don't already have one
Nothing -> do
case lspEnv se of
Nothing -> pure Nothing
Just env -> LSP.runLspT env $ do
u <- LSP.ProgressToken . LSP.InR . T.pack . show . hashUnique <$> liftIO Unique.newUnique
-- TODO: Wait for the progress create response to use the token
_ <- LSP.sendRequest LSP.SMethod_WindowWorkDoneProgressCreate (LSP.WorkDoneProgressCreateParams u) (const $ pure ())
LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams u $
toJSON $ LSP.WorkDoneProgressBegin
{ _kind = LSP.AString @"begin"
, _title = "Indexing"
, _cancellable = Nothing
, _message = Nothing
, _percentage = Nothing
}
pure (Just u)

(!done, !remaining) <- atomically $ do
done <- readTVar indexCompleted
remaining <- HashMap.size <$> readTVar indexPending
pure (done, remaining)
let
progressFrac :: Double
progressFrac = fromIntegral done / fromIntegral (done + remaining)
progressPct :: LSP.UInt
progressPct = floor $ 100 * progressFrac

whenJust (lspEnv se) $ \env -> whenJust tok $ \token -> LSP.runLspT env $
LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams token $
toJSON $
case style of
Percentage -> LSP.WorkDoneProgressReport
{ _kind = LSP.AString @"report"
, _cancellable = Nothing
, _message = Nothing
, _percentage = Just progressPct
}
Explicit -> LSP.WorkDoneProgressReport
{ _kind = LSP.AString @"report"
, _cancellable = Nothing
, _message = Just $
T.pack " (" <> T.pack (show done) <> "/" <> T.pack (show $ done + remaining) <> ")..."
, _percentage = Nothing
}
NoProgress -> LSP.WorkDoneProgressReport
{ _kind = LSP.AString @"report"
, _cancellable = Nothing
, _message = Nothing
, _percentage = Nothing
}

pre = progressUpdate indexProgressReporting ProgressStarted
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had actually thought we could just initialize a progressCounter here and use it just in the scope of this function, but this works too!

-- Report the progress once we are done indexing this file
post = do
mdone <- atomically $ do
Expand All @@ -883,18 +826,7 @@
when (coerce $ ideTesting se) $
LSP.sendNotification (LSP.SMethod_CustomMethod (Proxy @"ghcide/reference/ready")) $
toJSON $ fromNormalizedFilePath srcPath
whenJust mdone $ \done ->
modifyVar_ indexProgressToken $ \tok -> do
whenJust (lspEnv se) $ \env -> LSP.runLspT env $
whenJust tok $ \token ->
LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams token $
toJSON $
LSP.WorkDoneProgressEnd
{ _kind = LSP.AString @"end"
, _message = Just $ "Finished indexing " <> T.pack (show done) <> " files"
}
-- We are done with the current indexing cycle, so destroy the token
pure Nothing
whenJust mdone $ \_ -> progressUpdate indexProgressReporting ProgressCompleted

writeAndIndexHieFile :: HscEnv -> ShakeExtras -> ModSummary -> NormalizedFilePath -> [GHC.AvailInfo] -> HieASTs Type -> BS.ByteString -> IO [FileDiagnostic]
writeAndIndexHieFile hscEnv se mod_summary srcPath exports ast source =
Expand Down Expand Up @@ -1021,7 +953,7 @@


convImport (L _ i) = (
(ideclPkgQual i)

Check warning on line 956 in ghcide/src/Development/IDE/Core/Compile.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Suggestion in getModSummaryFromImports in module Development.IDE.Core.Compile: Redundant bracket ▫︎ Found: "((ideclPkgQual i), reLoc $ ideclName i)" ▫︎ Perhaps: "(ideclPkgQual i, reLoc $ ideclName i)"
, reLoc $ ideclName i)

msrImports = implicit_imports ++ imps
Expand Down
4 changes: 2 additions & 2 deletions ghcide/src/Development/IDE/Core/OfInterest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ kick = do
toJSON $ map fromNormalizedFilePath files

signal (Proxy @"kick/start")
liftIO $ progressUpdate progress KickStarted
progressUpdate progress ProgressNewStarted

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

liftIO $ progressUpdate progress KickCompleted
progressUpdate progress ProgressCompleted

GarbageCollectVar var <- getIdeGlobalAction
garbageCollectionScheduled <- liftIO $ readVar var
Expand Down
Loading
Loading