Skip to content

Commit 700b316

Browse files
authored
Custom cradle loading (haskell/ghcide#928)
When using ghcide as a library, it may be desirable to host the hie.yaml file in a location other than the project root, or even avoid the file system altogether
1 parent f7334ea commit 700b316

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

ghcide/exe/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import Development.IDE.Plugin
3131
import Development.IDE.Plugin.Completions as Completions
3232
import Development.IDE.Plugin.CodeAction as CodeAction
3333
import Development.IDE.Plugin.Test as Test
34-
import Development.IDE.Session
34+
import Development.IDE.Session (loadSession)
3535
import qualified Language.Haskell.LSP.Core as LSP
3636
import Language.Haskell.LSP.Messages
3737
import Language.Haskell.LSP.Types

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
{-|
44
The logic for setting up a ghcide session by tapping into hie-bios.
55
-}
6-
module Development.IDE.Session (loadSession) where
6+
module Development.IDE.Session
7+
(SessionLoadingOptions(..)
8+
,defaultLoadingOptions
9+
,loadSession
10+
,loadSessionWithOptions
11+
) where
712

813
-- Unfortunately, we cannot use loadSession with ghc-lib since hie-bios uses
914
-- the real GHC library and the types are incompatible. Furthermore, when
@@ -44,7 +49,7 @@ import Development.IDE.Types.Logger
4449
import Development.IDE.Types.Options
4550
import Development.Shake (Action)
4651
import GHC.Check
47-
import HIE.Bios
52+
import qualified HIE.Bios as HieBios
4853
import HIE.Bios.Environment hiding (getCacheDir)
4954
import HIE.Bios.Types
5055
import Hie.Implicit.Cradle (loadImplicitHieCradle)
@@ -64,6 +69,18 @@ import Module
6469
import NameCache
6570
import Packages
6671
import Control.Exception (evaluate)
72+
import Data.Void
73+
74+
data SessionLoadingOptions = SessionLoadingOptions
75+
{ findCradle :: FilePath -> IO (Maybe FilePath)
76+
, loadCradle :: FilePath -> IO (HieBios.Cradle Void)
77+
}
78+
79+
defaultLoadingOptions :: SessionLoadingOptions
80+
defaultLoadingOptions = SessionLoadingOptions
81+
{findCradle = HieBios.findCradle
82+
,loadCradle = HieBios.loadCradle
83+
}
6784

6885
-- | Given a root directory, return a Shake 'Action' which setups an
6986
-- 'IdeGhcSession' given a file.
@@ -79,7 +96,10 @@ import Control.Exception (evaluate)
7996
-- components mapping to the same hie.yaml file are mapped to the same
8097
-- HscEnv which is updated as new components are discovered.
8198
loadSession :: FilePath -> IO (Action IdeGhcSession)
82-
loadSession dir = do
99+
loadSession = loadSessionWithOptions defaultLoadingOptions
100+
101+
loadSessionWithOptions :: SessionLoadingOptions -> FilePath -> IO (Action IdeGhcSession)
102+
loadSessionWithOptions SessionLoadingOptions{..} dir = do
83103
-- Mapping from hie.yaml file to HscEnv, one per hie.yaml file
84104
hscEnvs <- newVar Map.empty :: IO (Var HieMap)
85105
-- Mapping from a Filepath to HscEnv

0 commit comments

Comments
 (0)