Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 8d1925d

Browse files
committed
Rework MonadIde class
1 parent 1bf711f commit 8d1925d

File tree

2 files changed

+72
-88
lines changed

2 files changed

+72
-88
lines changed

hie-plugin-api/Haskell/Ide/Engine/PluginUtils.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,14 @@ clientSupportsDocumentChanges = do
269269

270270
-- ---------------------------------------------------------------------
271271

272-
readVFS :: MonadIde m => Uri -> m (Maybe T.Text)
272+
readVFS :: (MonadIde m, MonadIO m) => Uri -> m (Maybe T.Text)
273273
readVFS uri = do
274274
mvf <- getVirtualFile uri
275275
case mvf of
276276
Just (VirtualFile _ txt) -> return $ Just (Yi.toText txt)
277277
Nothing -> return Nothing
278278

279-
getRangeFromVFS :: MonadIde m => Uri -> Range -> m (Maybe T.Text)
279+
getRangeFromVFS :: (MonadIde m, MonadIO m) => Uri -> Range -> m (Maybe T.Text)
280280
getRangeFromVFS uri rg = do
281281
mvf <- getVirtualFile uri
282282
case mvf of

hie-plugin-api/Haskell/Ide/Engine/PluginsIdeMonads.hs

Lines changed: 70 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,17 @@ module Haskell.Ide.Engine.PluginsIdeMonads
4949
, IdeM
5050
, runIdeM
5151
, IdeDeferM
52-
, MonadIde(..)
52+
-- ** MonadIde and functions
53+
, MonadIde
54+
, getRootPath
55+
, getVirtualFile
56+
, getConfig
57+
, getClientCapabilities
58+
, getPlugins
59+
, withProgress
60+
, withIndefiniteProgress
5361
, Core.Progress(..)
62+
-- ** Lifting
5463
, iterT
5564
, LiftsToGhc(..)
5665
-- * IdeResult
@@ -345,94 +354,69 @@ data IdeEnv = IdeEnv
345354
}
346355

347356
-- | The class of monads that support common IDE functions, namely IdeM/IdeGhcM/IdeDeferM
348-
class MonadIO m => MonadIde m where
349-
getRootPath :: m (Maybe FilePath)
350-
getVirtualFile :: Uri -> m (Maybe VirtualFile)
351-
getConfig :: m Config
352-
getClientCapabilities :: m ClientCapabilities
353-
getPlugins :: m IdePlugins
354-
-- 'withProgress' @title f@ wraps a progress reporting session for long running tasks.
355-
-- f is passed a reporting function that can be used to give updates on the progress
356-
-- of the task.
357-
withProgress :: T.Text -> ((Core.Progress -> m ()) -> m a) -> m a
358-
-- 'withIndefiniteProgress' @title f@ is the same as the 'withProgress' but for tasks
359-
-- which do not continuously report their progress.
360-
withIndefiniteProgress :: T.Text -> m a -> m a
361-
362-
instance MonadIO m => MonadIde (ReaderT IdeEnv m) where
363-
getRootPath = do
364-
mlf <- asks ideEnvLspFuncs
365-
case mlf of
366-
Just lf -> return (Core.rootPath lf)
367-
Nothing -> return Nothing
368-
369-
getVirtualFile uri = do
370-
mlf <- asks ideEnvLspFuncs
371-
case mlf of
372-
Just lf -> liftIO $ Core.getVirtualFileFunc lf uri
373-
Nothing -> return Nothing
374-
375-
getConfig = do
376-
mlf <- asks ideEnvLspFuncs
377-
case mlf of
378-
Just lf -> fromMaybe def <$> liftIO (Core.config lf)
379-
Nothing -> return def
380-
381-
getClientCapabilities = do
382-
mlf <- asks ideEnvLspFuncs
383-
case mlf of
384-
Just lf -> return (Core.clientCapabilities lf)
385-
Nothing -> return def
386-
387-
getPlugins = asks idePlugins
388-
389-
withProgress t f = do
390-
lf <- asks ideEnvLspFuncs
391-
withProgress' lf t f
392-
393-
withIndefiniteProgress t f = do
394-
lf <- asks ideEnvLspFuncs
395-
withIndefiniteProgress' lf t f
357+
class Monad m => MonadIde m where
358+
getIdeEnv :: m IdeEnv
396359

397-
instance MonadIde IdeGhcM where
398-
getRootPath = lift $ lift getRootPath
399-
getVirtualFile = lift . lift . getVirtualFile
400-
getConfig = lift $ lift getConfig
401-
getClientCapabilities = lift $ lift getClientCapabilities
402-
getPlugins = lift $ lift getPlugins
403-
withProgress t f = do
404-
lf <- lift $ lift $ asks ideEnvLspFuncs
405-
withProgress' lf t f
406-
withIndefiniteProgress t f = do
407-
lf <- lift $ lift $ asks ideEnvLspFuncs
408-
withIndefiniteProgress' lf t f
360+
instance MonadIde IdeM where
361+
getIdeEnv = ask
409362

410363
instance MonadIde IdeDeferM where
411-
getRootPath = lift getRootPath
412-
getVirtualFile = lift . getVirtualFile
413-
getConfig = lift getConfig
414-
getClientCapabilities = lift getClientCapabilities
415-
getPlugins = lift getPlugins
416-
withProgress t f = do
417-
lf <- lift $ asks ideEnvLspFuncs
418-
withProgress' lf t f
419-
withIndefiniteProgress t f = do
420-
lf <- lift $ asks ideEnvLspFuncs
421-
withIndefiniteProgress' lf t f
422-
423-
withProgress' :: MonadIO m => Maybe (Core.LspFuncs Config) -> T.Text -> ((Core.Progress -> m ()) -> m a) -> m a
424-
withProgress' lspFuncs t f =
425-
let mWp = Core.withProgress <$> lspFuncs
426-
in case mWp of
427-
Nothing -> f (const $ return ())
428-
Just wp -> wp t f
429-
430-
withIndefiniteProgress' :: MonadIO m => Maybe (Core.LspFuncs Config) -> T.Text -> m a -> m a
431-
withIndefiniteProgress' lspFuncs t f =
432-
let mWp = Core.withIndefiniteProgress <$> lspFuncs
433-
in case mWp of
434-
Nothing -> f
435-
Just wp -> wp t f
364+
getIdeEnv = lift ask
365+
366+
instance MonadIde IdeGhcM where
367+
getIdeEnv = lift $ lift ask
368+
369+
getRootPath :: MonadIde m => m (Maybe FilePath)
370+
getRootPath = do
371+
mlf <- ideEnvLspFuncs <$> getIdeEnv
372+
case mlf of
373+
Just lf -> return (Core.rootPath lf)
374+
Nothing -> return Nothing
375+
376+
getVirtualFile :: (MonadIde m, MonadIO m) => Uri -> m (Maybe VirtualFile)
377+
getVirtualFile uri = do
378+
mlf <- ideEnvLspFuncs <$> getIdeEnv
379+
case mlf of
380+
Just lf -> liftIO $ Core.getVirtualFileFunc lf uri
381+
Nothing -> return Nothing
382+
383+
getConfig :: (MonadIde m, MonadIO m) => m Config
384+
getConfig = do
385+
mlf <- ideEnvLspFuncs <$> getIdeEnv
386+
case mlf of
387+
Just lf -> fromMaybe def <$> liftIO (Core.config lf)
388+
Nothing -> return def
389+
390+
getClientCapabilities :: MonadIde m => m ClientCapabilities
391+
getClientCapabilities = do
392+
mlf <- ideEnvLspFuncs <$> getIdeEnv
393+
case mlf of
394+
Just lf -> return (Core.clientCapabilities lf)
395+
Nothing -> return def
396+
397+
getPlugins :: MonadIde m => m IdePlugins
398+
getPlugins = idePlugins <$> getIdeEnv
399+
400+
-- | 'withProgress' @title f@ wraps a progress reporting session for long running tasks.
401+
-- f is passed a reporting function that can be used to give updates on the progress
402+
-- of the task.
403+
withProgress :: (MonadIde m, MonadIO m) => T.Text -> ((Core.Progress -> m ()) -> m a) -> m a
404+
withProgress t f = do
405+
lf <- ideEnvLspFuncs <$> getIdeEnv
406+
let mWp = Core.withProgress <$> lf
407+
case mWp of
408+
Nothing -> f (const $ return ())
409+
Just wp -> wp t f
410+
411+
-- | 'withIndefiniteProgress' @title f@ is the same as the 'withProgress' but for tasks
412+
-- which do not continuously report their progress.
413+
withIndefiniteProgress :: (MonadIde m, MonadIO m) => T.Text -> m a -> m a
414+
withIndefiniteProgress t f = do
415+
lf <- ideEnvLspFuncs <$> getIdeEnv
416+
let mWp = Core.withIndefiniteProgress <$> lf
417+
case mWp of
418+
Nothing -> f
419+
Just wp -> wp t f
436420

437421
data IdeState = IdeState
438422
{ moduleCache :: GhcModuleCache

0 commit comments

Comments
 (0)