Skip to content

Commit a2f29e6

Browse files
authored
Merge pull request haskell#1523 from bubba/remove-maybe-in-lspfuncs
Change Maybe LspFuncs to LspFuncs inside IdeEnv
2 parents 69974d9 + 2ba1085 commit a2f29e6

File tree

6 files changed

+51
-58
lines changed

6 files changed

+51
-58
lines changed

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

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ getDiagnosticProvidersConfig c = Map.fromList [("applyrefact",hlintOn c)
344344
type IdeGhcM = GhcT IdeM
345345

346346
-- | Run an IdeGhcM with Cradle found from the current directory
347-
runIdeGhcM :: Maybe FilePath -> IdePlugins -> Maybe (Core.LspFuncs Config) -> TVar IdeState -> IdeGhcM a -> IO a
348-
runIdeGhcM mlibdir plugins mlf stateVar f = do
349-
env <- IdeEnv <$> pure mlf <*> getProcessID <*> pure plugins
347+
runIdeGhcM :: Maybe FilePath -> IdePlugins -> Core.LspFuncs Config -> TVar IdeState -> IdeGhcM a -> IO a
348+
runIdeGhcM mlibdir plugins lf stateVar f = do
349+
env <- IdeEnv <$> pure lf <*> getProcessID <*> pure plugins
350350
flip runReaderT stateVar $ flip runReaderT env $ runGhcT mlibdir f
351351

352352
-- | A computation that is deferred until the module is cached.
@@ -357,14 +357,14 @@ type IdeDeferM = FreeT Defer IdeM
357357
type IdeM = ReaderT IdeEnv (MultiThreadState IdeState)
358358

359359
-- | Run an IdeM
360-
runIdeM :: IdePlugins -> Maybe (Core.LspFuncs Config) -> TVar IdeState -> IdeM a -> IO a
361-
runIdeM plugins mlf stateVar f = do
362-
env <- IdeEnv <$> pure mlf <*> getProcessID <*> pure plugins
360+
runIdeM :: IdePlugins -> Core.LspFuncs Config -> TVar IdeState -> IdeM a -> IO a
361+
runIdeM plugins lf stateVar f = do
362+
env <- IdeEnv <$> pure lf <*> getProcessID <*> pure plugins
363363
-- TODO: AZ run a single ReaderT, with a composite R.
364364
flip runReaderT stateVar $ runReaderT f env
365365

366366
data IdeEnv = IdeEnv
367-
{ ideEnvLspFuncs :: Maybe (Core.LspFuncs Config)
367+
{ ideEnvLspFuncs :: Core.LspFuncs Config
368368
-- | The pid of this instance of hie
369369
, ideEnvPidCache :: Int
370370
, idePlugins :: IdePlugins
@@ -384,18 +384,12 @@ instance MonadIde IdeGhcM where
384384
getIdeEnv = lift ask
385385

386386
getRootPath :: MonadIde m => m (Maybe FilePath)
387-
getRootPath = do
388-
mlf <- ideEnvLspFuncs <$> getIdeEnv
389-
case mlf of
390-
Just lf -> return (Core.rootPath lf)
391-
Nothing -> return Nothing
387+
getRootPath = Core.rootPath . ideEnvLspFuncs <$> getIdeEnv
392388

393389
getVirtualFile :: (MonadIde m, MonadIO m) => Uri -> m (Maybe VirtualFile)
394390
getVirtualFile uri = do
395-
mlf <- ideEnvLspFuncs <$> getIdeEnv
396-
case mlf of
397-
Just lf -> liftIO $ Core.getVirtualFileFunc lf (toNormalizedUri uri)
398-
Nothing -> return Nothing
391+
lf <- ideEnvLspFuncs <$> getIdeEnv
392+
liftIO $ Core.getVirtualFileFunc lf (toNormalizedUri uri)
399393

400394
-- | Worker function for persistVirtualFile without monad constraints.
401395
--
@@ -407,19 +401,15 @@ persistVirtualFile' lf uri = Core.persistVirtualFileFunc lf (toNormalizedUri uri
407401

408402
reverseFileMap :: (MonadIde m, MonadIO m) => m (FilePath -> FilePath)
409403
reverseFileMap = do
410-
mlf <- ideEnvLspFuncs <$> getIdeEnv
411-
case mlf of
412-
Just lf -> liftIO $ Core.reverseFileMapFunc lf
413-
Nothing -> return id
404+
lf <- ideEnvLspFuncs <$> getIdeEnv
405+
liftIO $ Core.reverseFileMapFunc lf
414406

415407
-- | Get the location of the virtual file persisted to the file system associated
416408
-- to the given Uri.
417409
getPersistedFile :: (MonadIde m, MonadIO m) => Uri -> m (Maybe FilePath)
418410
getPersistedFile uri = do
419-
mlf <- ideEnvLspFuncs <$> getIdeEnv
420-
case mlf of
421-
Just lf -> liftIO $ persistVirtualFile' lf uri
422-
Nothing -> return $ uriToFilePath uri
411+
lf <- ideEnvLspFuncs <$> getIdeEnv
412+
liftIO $ persistVirtualFile' lf uri
423413

424414
-- | Execute an action on the temporary file associated to the given FilePath.
425415
-- If the file is not in the current Virtual File System, the given action is not executed
@@ -433,17 +423,11 @@ withMappedFile fp m k = do
433423

434424
getConfig :: (MonadIde m, MonadIO m) => m Config
435425
getConfig = do
436-
mlf <- ideEnvLspFuncs <$> getIdeEnv
437-
case mlf of
438-
Just lf -> fromMaybe def <$> liftIO (Core.config lf)
439-
Nothing -> return def
426+
lf <- ideEnvLspFuncs <$> getIdeEnv
427+
fromMaybe def <$> liftIO (Core.config lf)
440428

441429
getClientCapabilities :: MonadIde m => m ClientCapabilities
442-
getClientCapabilities = do
443-
mlf <- ideEnvLspFuncs <$> getIdeEnv
444-
case mlf of
445-
Just lf -> return (Core.clientCapabilities lf)
446-
Nothing -> return def
430+
getClientCapabilities = Core.clientCapabilities . ideEnvLspFuncs <$> getIdeEnv
447431

448432
getPlugins :: MonadIde m => m IdePlugins
449433
getPlugins = idePlugins <$> getIdeEnv
@@ -456,10 +440,7 @@ withProgress :: (MonadIde m , MonadIO m, MonadBaseControl IO m)
456440
-> ((Core.Progress -> IO ()) -> m a) -> m a
457441
withProgress t c f = do
458442
lf <- ideEnvLspFuncs <$> getIdeEnv
459-
let mWp = Core.withProgress <$> lf
460-
case mWp of
461-
Nothing -> f (const $ return ())
462-
Just wp -> control $ \run -> wp t c $ \update -> run (f update)
443+
control $ \run -> Core.withProgress lf t c $ \update -> run (f update)
463444

464445

465446
-- | 'withIndefiniteProgress' @title cancellable f@ is the same as the 'withProgress' but for tasks
@@ -468,10 +449,7 @@ withIndefiniteProgress :: (MonadIde m, MonadBaseControl IO m)
468449
=> T.Text -> Core.ProgressCancellable -> m a -> m a
469450
withIndefiniteProgress t c f = do
470451
lf <- ideEnvLspFuncs <$> getIdeEnv
471-
let mWp = Core.withIndefiniteProgress <$> lf
472-
case mWp of
473-
Nothing -> f
474-
Just wp -> control $ \run -> wp t c (run f)
452+
control $ \run -> Core.withIndefiniteProgress lf t c (run f)
475453

476454
data IdeState = IdeState
477455
{ moduleCache :: !GhcModuleCache

src/Haskell/Ide/Engine/Scheduler.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ runScheduler
143143
-- ^ A handler for any errors that the dispatcher may encounter.
144144
-> CallbackHandler m
145145
-- ^ A handler to run the requests' callback in your monad of choosing.
146-
-> Maybe (Core.LspFuncs Config)
147-
-- ^ The LspFuncs provided by haskell-lsp, if using LSP.
146+
-> Core.LspFuncs Config
147+
-- ^ The LspFuncs provided by haskell-lsp.
148148
-> Maybe Bios.Cradle
149149
-- ^ Context in which the ghc thread is executed.
150150
-- Neccessary to obtain the libdir, for example.
151151
-> IO ()
152-
runScheduler Scheduler {..} errorHandler callbackHandler mlf mcrdl = do
152+
runScheduler Scheduler {..} errorHandler callbackHandler lf mcradle = do
153153
let dEnv = DispatcherEnv
154154
{ cancelReqsTVar = requestsToCancel
155155
, wipReqsTVar = requestsInProgress
@@ -163,13 +163,13 @@ runScheduler Scheduler {..} errorHandler callbackHandler mlf mcrdl = do
163163

164164
stateVar <- STM.newTVarIO initialState
165165

166-
mlibdir <- case mcrdl of
166+
mlibdir <- case mcradle of
167167
Nothing -> return Nothing
168168
Just crdl -> Bios.getProjectGhcLibDir crdl
169169

170-
let runGhcDisp = runIdeGhcM mlibdir plugins mlf stateVar $
170+
let runGhcDisp = runIdeGhcM mlibdir plugins lf stateVar $
171171
ghcDispatcher dEnv errorHandler callbackHandler ghcChanOut
172-
runIdeDisp = runIdeM plugins mlf stateVar $
172+
runIdeDisp = runIdeM plugins lf stateVar $
173173
ideDispatcher dEnv errorHandler callbackHandler ideChanOut
174174

175175

src/Haskell/Ide/Engine/Server.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ run scheduler _origDir plugins captureFp = flip E.catches handlers $ do
193193
-- recognized properly by ghc-mod
194194
flip labelThread "scheduler" =<<
195195
(forkIO (
196-
Scheduler.runScheduler scheduler errorHandler callbackHandler (Just lf) mcradle
196+
Scheduler.runScheduler scheduler errorHandler callbackHandler lf mcradle
197197
`E.catch` \(e :: E.SomeException) ->
198198
(errorm $ "Scheduler thread exited unexpectedly: " ++ show e)
199199
))

test/dispatcher/Main.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Data.Aeson
1010
-- import qualified Data.HashMap.Strict as H
1111
import Data.Typeable
1212
import qualified Data.Text as T
13-
import Data.Default
1413
import GHC ( TypecheckedModule )
1514
import GHC.Generics
1615
import Haskell.Ide.Engine.Ghc
@@ -81,7 +80,7 @@ startServer = do
8180
scheduler
8281
(\lid errCode e -> logToChan logChan ("received an error", Left (lid, errCode, e)))
8382
(\g x -> g x)
84-
def
83+
dummyLspFuncs
8584
(Just crdl)
8685

8786
return (scheduler, logChan, dispatcher)

test/plugin-dispatcher/Main.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Control.Concurrent
66
import Control.Concurrent.STM.TChan
77
import Control.Monad.STM
88
import qualified Data.Text as T
9-
import Data.Default
109
import qualified Haskell.Ide.Engine.Cradle as Bios
1110
import Haskell.Ide.Engine.MonadTypes
1211
import Haskell.Ide.Engine.Scheduler
@@ -51,7 +50,7 @@ newPluginSpec = do
5150
pid <- forkIO $ runScheduler scheduler
5251
(\_ _ _ -> return ())
5352
(\f x -> f x)
54-
def
53+
dummyLspFuncs
5554
(Just crdl)
5655

5756
updateDocument scheduler (filePathToUri "test") 3

test/utils/TestUtils.hs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@ module TestUtils
1616
, getHspecFormattedConfig
1717
, testOptions
1818
, flushStackEnvironment
19+
, dummyLspFuncs
1920
) where
2021

2122
import Control.Concurrent.STM
2223
import Control.Monad
2324
import Data.Aeson.Types (typeMismatch)
25+
import Data.Default
2426
import Data.List (intercalate)
2527
import Data.Text (pack)
2628
import Data.Typeable
2729
import Data.Yaml
2830
import qualified Data.Map as Map
2931
import Data.Maybe
30-
-- import qualified GhcMod.Monad as GM
31-
-- import qualified GhcMod.Types as GM
32-
import qualified Language.Haskell.LSP.Core as Core
33-
import Haskell.Ide.Engine.MonadTypes
32+
import Language.Haskell.LSP.Core
33+
import Language.Haskell.LSP.Types (LspId(IdInt), fromNormalizedUri)
34+
import Haskell.Ide.Engine.MonadTypes hiding (withProgress, withIndefiniteProgress)
3435
import qualified Haskell.Ide.Engine.Cradle as Bios
3536
import System.Directory
3637
import System.Environment
@@ -77,7 +78,7 @@ runIGM testPlugins fp f = do
7778
stateVar <- newTVarIO $ IdeState emptyModuleCache Map.empty Map.empty Nothing
7879
crdl <- Bios.findLocalCradle fp
7980
mlibdir <- Bios.getProjectGhcLibDir crdl
80-
runIdeGhcM mlibdir testPlugins Nothing stateVar f
81+
runIdeGhcM mlibdir testPlugins dummyLspFuncs stateVar f
8182

8283
withFileLogging :: FilePath -> IO a -> IO a
8384
withFileLogging logFile f = do
@@ -90,7 +91,7 @@ withFileLogging logFile f = do
9091
exists <- doesFileExist logPath
9192
when exists $ removeFile logPath
9293

93-
Core.setupLogger (Just logPath) ["hie"] L.DEBUG
94+
setupLogger (Just logPath) ["hie"] L.DEBUG
9495

9596
f
9697

@@ -374,3 +375,19 @@ flushStackEnvironment = do
374375
unsetEnv "HASKELL_PACKAGE_SANDBOXES"
375376

376377
-- ---------------------------------------------------------------------
378+
379+
dummyLspFuncs :: Default a => LspFuncs a
380+
dummyLspFuncs = LspFuncs { clientCapabilities = def
381+
, config = return (Just def)
382+
, sendFunc = const (return ())
383+
, getVirtualFileFunc = const (return Nothing)
384+
, persistVirtualFileFunc = \uri -> return (uriToFilePath (fromNormalizedUri uri))
385+
, reverseFileMapFunc = return id
386+
, publishDiagnosticsFunc = mempty
387+
, flushDiagnosticsBySourceFunc = mempty
388+
, getNextReqId = pure (IdInt 0)
389+
, rootPath = Nothing
390+
, getWorkspaceFolders = return Nothing
391+
, withProgress = \_ _ f -> f (const (return ()))
392+
, withIndefiniteProgress = \_ _ f -> f
393+
}

0 commit comments

Comments
 (0)