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

Commit bad8402

Browse files
committed
Cache CompilerOptions with Cradle
1 parent 2a2de61 commit bad8402

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

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

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
module Haskell.Ide.Engine.GhcModuleCache where
66

7+
import Control.Monad.IO.Class ( liftIO , MonadIO(..) )
8+
79
import qualified Data.Map as Map
810
import Data.Dynamic (Dynamic)
911
import Data.Typeable (TypeRep)
@@ -100,21 +102,59 @@ data LookupCradleResult = ReuseCradle | LoadCradle CachedCradle | NewCradle File
100102
lookupCradle :: FilePath -> GhcModuleCache -> LookupCradleResult
101103
lookupCradle fp gmc =
102104
case currentCradle gmc of
103-
Just (dirs, _c) | (any (\d -> d `isPrefixOf` fp) dirs) -> ReuseCradle
105+
Just (dirs, _c, _) | any (`isPrefixOf` fp) dirs -> ReuseCradle
104106
_ -> case T.match (cradleCache gmc) (B.pack fp) of
105107
Just (_k, c, _suf) -> LoadCradle c
106108
Nothing -> NewCradle fp
107109

108-
data CachedCradle = CachedCradle BIOS.Cradle HscEnv
110+
getComponentOptions
111+
:: (MonadIO m, HasGhcModuleCache m)
112+
=> FilePath
113+
-> m (Maybe BIOS.ComponentOptions)
114+
getComponentOptions fp = do
115+
mc <- getModuleCache
116+
case currentCradle mc of
117+
Just (dirs, cradle, co) | any (`isPrefixOf` fp) dirs -> case co of
118+
Just _ -> return co
119+
_ -> setCo fp cradle setCur
120+
_ -> case T.match (cradleCache mc) (B.pack fp) of
121+
Just (_, CachedCradle _ _ (Just co), _) -> return $ Just co
122+
Just (p, CachedCradle cradle _ Nothing, _) ->
123+
setCo (B.unpack p) cradle setCac
124+
_ -> return Nothing
125+
where
126+
setCo fp' cradle mod' = do
127+
res <- liftIO $ BIOS.getCompilerOptions fp' cradle
128+
case res of
129+
BIOS.CradleSuccess opts -> do
130+
modifyModuleCache $ mod' opts fp'
131+
return $ Just opts
132+
_ -> return Nothing
133+
setCur opts _ mc'@(GhcModuleCache _ _ (Just (fps, c', _))) =
134+
mc' { currentCradle = Just (fps, c', Just opts) }
135+
setCur _ _ mc' = mc'
136+
setCac opts fp' mc' = mc'
137+
{ cradleCache = T.adjust (\cc -> cc { mCompOpts = Just opts }) (B.pack fp')
138+
$ cradleCache mc'
139+
}
140+
141+
142+
143+
data CachedCradle = CachedCradle
144+
{ ccradle :: BIOS.Cradle
145+
, hscEnv :: HscEnv
146+
, mCompOpts :: Maybe BIOS.ComponentOptions
147+
}
109148

110149
instance Show CachedCradle where
111-
show (CachedCradle x _) = show x
150+
show (CachedCradle x _ _) = show x
112151

113152
data GhcModuleCache = GhcModuleCache
114153
{ cradleCache :: !(T.Trie CachedCradle)
115154
-- ^ map from FilePath to cradles
155+
-- May not include currentCradle
116156
, uriCaches :: !UriCaches
117-
, currentCradle :: Maybe ([FilePath], BIOS.Cradle)
157+
, currentCradle :: Maybe ([FilePath], BIOS.Cradle, Maybe BIOS.ComponentOptions)
118158
-- ^ The current cradle and which FilePath's it is
119159
-- responsible for
120160
} deriving (Show)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ loadCradle _ _ ReuseCradle _def action = do
131131
debugm "Reusing cradle"
132132
IdeResultOk <$> action
133133

134-
loadCradle _ _iniDynFlags (LoadCradle (CachedCradle crd env)) _def action = do
134+
loadCradle _ _iniDynFlags (LoadCradle (CachedCradle crd env _)) _def action = do
135135
-- Reloading a cradle happens on component switch
136136
logm $ "Switch to cradle: " ++ show crd
137137
-- Cache the existing cradle
@@ -245,17 +245,17 @@ setCurrentCradle cradle = do
245245
let ps = mapMaybe (GHC.ml_hs_file . GHC.ms_location) (mgModSummaries mg)
246246
debugm $ "Modules in the cradle: " ++ show ps
247247
ps' <- liftIO $ mapM canonicalizePath ps
248-
modifyCache (\s -> s { currentCradle = Just (ps', cradle) })
248+
modifyCache (\s -> s { currentCradle = Just (ps', cradle, Nothing) })
249249

250250
-- | Cache the given Cradle.
251251
-- Caches the given Cradle together with all Modules this Cradle is responsible
252252
-- for.
253253
-- Via 'lookupCradle' it can be checked if a given FilePath is managed by
254254
-- a any Cradle that has already been loaded.
255-
cacheCradle :: (HasGhcModuleCache m, GHC.GhcMonad m) => ([FilePath], Bios.Cradle) -> m ()
256-
cacheCradle (ds, c) = do
255+
cacheCradle :: (HasGhcModuleCache m, GHC.GhcMonad m) => ([FilePath], Bios.Cradle, Maybe Bios.ComponentOptions) -> m ()
256+
cacheCradle (ds, c, co) = do
257257
env <- GHC.getSession
258-
let cc = CachedCradle c env
258+
let cc = CachedCradle c env co
259259
new_map = T.fromList (map (, cc) (map B.pack ds))
260260
modifyCache (\s -> s { cradleCache = T.unionWith (\a _ -> a) new_map (cradleCache s) })
261261

0 commit comments

Comments
 (0)