Skip to content

Commit f17af2b

Browse files
committed
turn ProgressReporting into IO
1 parent acaa7ba commit f17af2b

File tree

3 files changed

+31
-37
lines changed

3 files changed

+31
-37
lines changed

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

Lines changed: 2 additions & 2 deletions
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-
progressUpdate progress ProgressNewStarted
144+
liftIO $ 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-
progressUpdate progress ProgressCompleted
155+
liftIO $ progressUpdate progress ProgressCompleted
156156

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

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

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,41 +40,36 @@ import Language.LSP.Server (ProgressAmount (..),
4040
withProgress)
4141
import qualified Language.LSP.Server as LSP
4242
import qualified StmContainers.Map as STM
43-
import UnliftIO (Async, MonadUnliftIO, async,
44-
bracket, cancel)
43+
import UnliftIO (Async, async, bracket, cancel)
4544

4645
data ProgressEvent
4746
= ProgressNewStarted
4847
| ProgressCompleted
4948
| ProgressStarted
5049

51-
data ProgressReportingNoTrace m = ProgressReportingNoTrace
52-
{ progressUpdateI :: ProgressEvent -> m (),
50+
data ProgressReportingNoTrace = ProgressReportingNoTrace
51+
{ progressUpdateI :: ProgressEvent -> IO (),
5352
progressStopI :: IO ()
5453
-- ^ we are using IO here because creating and stopping the `ProgressReporting`
5554
-- is different from how we use it.
5655
}
5756

58-
data ProgressReporting m = ProgressReporting
57+
data ProgressReporting = ProgressReporting
5958
{
60-
inProgress :: forall a. NormalizedFilePath -> m a -> m a,
59+
inProgress :: forall a. NormalizedFilePath -> IO a -> IO a,
6160
-- ^ see Note [ProgressReporting API and InProgressState]
62-
progressReportingInner :: ProgressReportingNoTrace m
61+
progressReportingInner :: ProgressReportingNoTrace
6362
}
6463

6564
class ProgressReportingClass a where
66-
type M a :: * -> *
67-
progressUpdate :: a -> ProgressEvent -> M a ()
65+
progressUpdate :: a -> ProgressEvent -> IO ()
6866
progressStop :: a -> IO ()
6967

70-
instance ProgressReportingClass (ProgressReportingNoTrace m) where
71-
type M (ProgressReportingNoTrace m) = m
68+
instance ProgressReportingClass ProgressReportingNoTrace where
7269
progressUpdate = progressUpdateI
7370
progressStop = progressStopI
7471

75-
instance ProgressReportingClass (ProgressReporting m) where
76-
type M (ProgressReporting m) = m
77-
progressUpdate :: ProgressReporting m -> ProgressEvent -> M (ProgressReporting m) ()
72+
instance ProgressReportingClass ProgressReporting where
7873
progressUpdate = progressUpdateI . progressReportingInner
7974
progressStop = progressStopI . progressReportingInner
8075

@@ -91,12 +86,12 @@ The progress of tasks can be tracked in two ways:
9186
The `inProgress` function is only useful when we are using `ProgressReporting`.
9287
-}
9388

94-
noProgressReportingNoTrace :: (MonadUnliftIO m) => (ProgressReportingNoTrace m)
89+
noProgressReportingNoTrace :: ProgressReportingNoTrace
9590
noProgressReportingNoTrace = ProgressReportingNoTrace
9691
{ progressUpdateI = const $ pure (),
9792
progressStopI = pure ()
9893
}
99-
noProgressReporting :: (MonadUnliftIO m) => IO (ProgressReporting m)
94+
noProgressReporting :: IO ProgressReporting
10095
noProgressReporting =
10196
return $
10297
ProgressReporting
@@ -141,14 +136,13 @@ newInProgress = InProgressState <$> newTVarIO 0 <*> newTVarIO 0 <*> STM.newIO
141136
recordProgress :: InProgressState -> NormalizedFilePath -> (Int -> Int) -> IO ()
142137
recordProgress InProgressState {..} file shift = do
143138
(prev, new) <- atomicallyNamed "recordProgress" $ STM.focus alterPrevAndNew file currentVar
144-
atomicallyNamed "recordProgress2" $ do
145-
case (prev, new) of
146-
(Nothing, 0) -> modifyTVar' doneVar (+ 1) >> modifyTVar' todoVar (+ 1)
147-
(Nothing, _) -> modifyTVar' todoVar (+ 1)
148-
(Just 0, 0) -> pure ()
149-
(Just 0, _) -> modifyTVar' doneVar pred
150-
(Just _, 0) -> modifyTVar' doneVar (+ 1)
151-
(Just _, _) -> pure ()
139+
atomicallyNamed "recordProgress2" $ case (prev, new) of
140+
(Nothing, 0) -> modifyTVar' doneVar (+ 1) >> modifyTVar' todoVar (+ 1)
141+
(Nothing, _) -> modifyTVar' todoVar (+ 1)
142+
(Just 0, 0) -> pure ()
143+
(Just 0, _) -> modifyTVar' doneVar pred
144+
(Just _, 0) -> modifyTVar' doneVar (+ 1)
145+
(Just _, _) -> pure ()
152146
where
153147
alterPrevAndNew = do
154148
prev <- Focus.lookup
@@ -162,13 +156,12 @@ recordProgress InProgressState {..} file shift = do
162156
-- It functions similarly to `progressReporting`, but it utilizes an external state for progress tracking.
163157
-- Refer to Note [ProgressReporting API and InProgressState] for more details.
164158
progressReportingNoTrace ::
165-
(MonadUnliftIO m, MonadIO m) =>
166159
STM Int ->
167160
STM Int ->
168161
Maybe (LSP.LanguageContextEnv c) ->
169162
T.Text ->
170163
ProgressReportingStyle ->
171-
IO (ProgressReportingNoTrace m)
164+
IO ProgressReportingNoTrace
172165
progressReportingNoTrace _ _ Nothing _title _optProgressStyle = return noProgressReportingNoTrace
173166
progressReportingNoTrace todo done (Just lspEnv) title optProgressStyle = do
174167
progressState <- newVar NotStarted
@@ -181,19 +174,18 @@ progressReportingNoTrace todo done (Just lspEnv) title optProgressStyle = do
181174
-- It necessitates the active tracking of progress using the `inProgress` function.
182175
-- Refer to Note [ProgressReporting API and InProgressState] for more details.
183176
progressReporting ::
184-
forall c m.
185-
(MonadUnliftIO m, MonadIO m) =>
186177
Maybe (LSP.LanguageContextEnv c) ->
187178
T.Text ->
188179
ProgressReportingStyle ->
189-
IO (ProgressReporting m)
180+
IO ProgressReporting
190181
progressReporting Nothing _title _optProgressStyle = noProgressReporting
191182
progressReporting (Just lspEnv) title optProgressStyle = do
192183
inProgressState <- newInProgress
193184
progressReportingInner <- progressReportingNoTrace (readTVar $ todoVar inProgressState)
194185
(readTVar $ doneVar inProgressState) (Just lspEnv) title optProgressStyle
195-
let inProgress :: forall a. NormalizedFilePath -> m a -> m a
196-
inProgress = updateStateForFile inProgressState
186+
let
187+
inProgress :: NormalizedFilePath -> IO a -> IO a
188+
inProgress = updateStateForFile inProgressState
197189
return ProgressReporting {..}
198190
where
199191
updateStateForFile inProgress file = UnliftIO.bracket (liftIO $ f succ) (const $ liftIO $ f pred) . const
@@ -202,7 +194,7 @@ progressReporting (Just lspEnv) title optProgressStyle = do
202194
-- Do not remove the eta-expansion without profiling a session with at
203195
-- least 1000 modifications.
204196

205-
f shift = recordProgress inProgress file shift
197+
f = recordProgress inProgress file
206198

207199
-- Kill this to complete the progress session
208200
progressCounter ::

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ import qualified StmContainers.Map as STM
174174
import System.FilePath hiding (makeRelative)
175175
import System.IO.Unsafe (unsafePerformIO)
176176
import System.Time.Extra
177+
import UnliftIO (MonadUnliftIO (withRunInIO))
177178

178179

179180
data Log
@@ -244,7 +245,7 @@ data HieDbWriter
244245
{ indexQueue :: IndexQueue
245246
, indexPending :: TVar (HMap.HashMap NormalizedFilePath Fingerprint) -- ^ Avoid unnecessary/out of date indexing
246247
, indexCompleted :: TVar Int -- ^ to report progress
247-
, indexProgressReporting :: ProgressReportingNoTrace IO
248+
, indexProgressReporting :: ProgressReportingNoTrace
248249
}
249250

250251
-- | Actions to queue up on the index worker thread
@@ -294,7 +295,7 @@ data ShakeExtras = ShakeExtras
294295
-- positions in a version of that document to positions in the latest version
295296
-- First mapping is delta from previous version and second one is an
296297
-- accumulation to the current version.
297-
,progress :: ProgressReporting Action
298+
,progress :: ProgressReporting
298299
,ideTesting :: IdeTesting
299300
-- ^ Whether to enable additional lsp messages used by the test suite for checking invariants
300301
,restartShakeSession
@@ -1216,7 +1217,8 @@ defineEarlyCutoff'
12161217
defineEarlyCutoff' doDiagnostics cmp key file mbOld mode action = do
12171218
ShakeExtras{state, progress, dirtyKeys} <- getShakeExtras
12181219
options <- getIdeOptions
1219-
(if optSkipProgress options key then id else inProgress progress file) $ do
1220+
let trans g x = withRunInIO $ \run -> g (run x)
1221+
(if optSkipProgress options key then id else trans (inProgress progress file)) $ do
12201222
val <- case mbOld of
12211223
Just old | mode == RunDependenciesSame -> do
12221224
mbValue <- liftIO $ atomicallyNamed "define - read 1" $ getValues state key file

0 commit comments

Comments
 (0)