Skip to content

Commit 3560db4

Browse files
committed
Enhance testing progress reporting
- Introduce TestReporting style for progress reporting in IDE options.
1 parent 9b952c8 commit 3560db4

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,31 @@ import Control.Concurrent.STM (STM)
2323
import Control.Concurrent.STM.Stats (TVar, atomically,
2424
atomicallyNamed, modifyTVar',
2525
newTVarIO, readTVar, retry)
26-
import Control.Concurrent.Strict (modifyVar_, newVar,
27-
threadDelay)
26+
import Control.Concurrent.Strict (modifyVar_, newBarrier, newVar,
27+
signalBarrier, threadDelay,
28+
waitBarrier)
2829
import Control.Monad.Extra hiding (loop)
2930
import Control.Monad.IO.Class
3031
import Control.Monad.Trans.Class (lift)
32+
import qualified Data.Aeson as J
3133
import Data.Functor (($>))
3234
import qualified Data.Text as T
35+
import Data.Unique (hashUnique, newUnique)
3336
import Development.IDE.GHC.Orphans ()
3437
import Development.IDE.Types.Location
3538
import Development.IDE.Types.Options
3639
import qualified Focus
40+
import Language.LSP.Protocol.Message
3741
import Language.LSP.Protocol.Types
38-
import Language.LSP.Server (ProgressAmount (..),
42+
import qualified Language.LSP.Protocol.Types as L
43+
import Language.LSP.Server (MonadLsp, ProgressAmount (..),
3944
ProgressCancellable (..),
45+
sendNotification, sendRequest,
4046
withProgress)
4147
import qualified Language.LSP.Server as LSP
4248
import qualified StmContainers.Map as STM
4349
import UnliftIO (Async, async, bracket, cancel)
50+
import qualified UnliftIO.Exception as UE
4451

4552
data ProgressEvent
4653
= ProgressNewStarted
@@ -168,7 +175,7 @@ progressReportingNoTrace todo done (Just lspEnv) title optProgressStyle = do
168175
let _progressUpdate event = liftIO $ updateStateVar $ Event event
169176
_progressStop = updateStateVar StopProgress
170177
updateStateVar = modifyVar_ progressState . updateState (progressCounter lspEnv title optProgressStyle todo done)
171-
return ProgressReporting {..}
178+
return ProgressReporting {_progressUpdate, _progressStop}
172179

173180
-- | `progressReporting` initiates a new progress reporting session.
174181
-- It necessitates the active tracking of progress using the `inProgress` function.
@@ -196,6 +203,25 @@ progressReporting (Just lspEnv) title optProgressStyle = do
196203

197204
f = recordProgress inProgress file
198205

206+
withProgressDummy ::
207+
forall c m a.
208+
MonadLsp c m =>
209+
T.Text ->
210+
Maybe ProgressToken ->
211+
ProgressCancellable ->
212+
((ProgressAmount -> m ()) -> m a) ->
213+
m a
214+
withProgressDummy title _ _ f = do
215+
t <- L.ProgressToken . L.InR . T.pack . show . hashUnique <$> liftIO newUnique
216+
r <- liftIO newBarrier
217+
_ <- sendRequest SMethod_WindowWorkDoneProgressCreate (WorkDoneProgressCreateParams t) $
218+
\_ -> liftIO $ signalBarrier r ()
219+
-- liftIO $ waitBarrier r
220+
sendProgressReport t $ WorkDoneProgressBegin L.AString title Nothing Nothing Nothing
221+
f (const $ return ()) `UE.finally` sendProgressReport t (WorkDoneProgressEnd L.AString Nothing)
222+
where
223+
sendProgressReport token report = sendNotification SMethod_Progress $ ProgressParams token $ J.toJSON report
224+
199225
-- Kill this to complete the progress session
200226
progressCounter ::
201227
LSP.LanguageContextEnv c ->
@@ -205,8 +231,12 @@ progressCounter ::
205231
STM Int ->
206232
IO ()
207233
progressCounter lspEnv title optProgressStyle getTodo getDone =
208-
LSP.runLspT lspEnv $ withProgress title Nothing NotCancellable $ \update -> loop update 0
234+
LSP.runLspT lspEnv $ withProgressChoice title Nothing NotCancellable $ \update -> loop update 0
209235
where
236+
withProgressChoice = case optProgressStyle of
237+
TestReporting -> withProgressDummy
238+
_ -> withProgress
239+
210240
loop _ _ | optProgressStyle == NoProgress = forever $ liftIO $ threadDelay maxBound
211241
loop update prevPct = do
212242
(todo, done, nextPct) <- liftIO $ atomically $ do

ghcide/src/Development/IDE/Main.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ import Development.IDE.Types.Location (NormalizedUri,
7777
toNormalizedFilePath')
7878
import Development.IDE.Types.Monitoring (Monitoring)
7979
import Development.IDE.Types.Options (IdeGhcSession,
80-
IdeOptions (optCheckParents, optCheckProject, optReportProgress, optRunSubset),
80+
IdeOptions (..),
8181
IdeTesting (IdeTesting),
82+
ProgressReportingStyle (TestReporting),
8283
clientSupportsProgress,
8384
defaultIdeOptions,
8485
optModifyDynFlags,
@@ -276,7 +277,10 @@ testing recorder projectRoot plugins =
276277
let
277278
defOptions = argsIdeOptions config sessionLoader
278279
in
279-
defOptions{ optTesting = IdeTesting True }
280+
defOptions{
281+
optTesting = IdeTesting True
282+
, optProgressStyle = TestReporting
283+
}
280284
lspOptions = argsLspOptions { LSP.optProgressStartDelay = 0, LSP.optProgressUpdateDelay = 0 }
281285
in
282286
arguments

ghcide/src/Development/IDE/Types/Options.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ newtype IdeOTMemoryProfiling = IdeOTMemoryProfiling Bool
107107
data ProgressReportingStyle
108108
= Percentage -- ^ Report using the LSP @_percentage@ field
109109
| Explicit -- ^ Report using explicit 123/456 text
110+
| TestReporting -- ^ Special mode for testing, reports only start/stop
110111
| NoProgress -- ^ Do not report any percentage
111112
deriving Eq
112113

0 commit comments

Comments
 (0)