@@ -23,24 +23,31 @@ import Control.Concurrent.STM (STM)
2323import 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 )
2829import Control.Monad.Extra hiding (loop )
2930import Control.Monad.IO.Class
3031import Control.Monad.Trans.Class (lift )
32+ import qualified Data.Aeson as J
3133import Data.Functor (($>) )
3234import qualified Data.Text as T
35+ import Data.Unique (hashUnique , newUnique )
3336import Development.IDE.GHC.Orphans ()
3437import Development.IDE.Types.Location
3538import Development.IDE.Types.Options
3639import qualified Focus
40+ import Language.LSP.Protocol.Message
3741import 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 )
4147import qualified Language.LSP.Server as LSP
4248import qualified StmContainers.Map as STM
4349import UnliftIO (Async , async , bracket , cancel )
50+ import qualified UnliftIO.Exception as UE
4451
4552data 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
200226progressCounter ::
201227 LSP. LanguageContextEnv c ->
@@ -205,8 +231,12 @@ progressCounter ::
205231 STM Int ->
206232 IO ()
207233progressCounter 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
0 commit comments