Skip to content

Commit 1976c7a

Browse files
committed
Add comments for common functions
1 parent 7c0f168 commit 1976c7a

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ module Haskell.Ide.Engine.Cradle (findLocalCradle) where
22

33
import HIE.Bios as BIOS
44

5+
-- | Find the cradle responsible for a filepath.
6+
-- If an explicit configuration is given, use it, otherwise
7+
-- try to guess a cradle based on certain heuristics, such as
8+
-- existence of stack.yaml and cabal.project.
59
findLocalCradle :: FilePath -> IO Cradle
610
findLocalCradle fp = do
711
-- Get the cabal directory from the cradle

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ captureDiagnostics rfm action = do
144144
-- will be treated with the same severity of type errors. In order to offer a more useful
145145
-- experience, we make sure warnings are always reported as warnings by setting -Wwarn
146146
unsetWErr df = unSetGeneralFlag' Opt_WarnIsError (emptyFatalWarningFlags df)
147+
-- previously, reported repeatedly about Missing Home Modules
147148
unsetMissingHomeModules = flip wopt_unset Opt_WarnMissingHomeModules
149+
-- Preserve comments
148150
setRawTokenStream = setGeneralFlag' Opt_KeepRawTokenStream
149151

150152
ghcErrRes msg = pure (mempty, [T.pack msg], Nothing)

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{-# LANGUAGE RankNTypes #-}
22
{-# LANGUAGE ScopedTypeVariables #-}
33
{-# LANGUAGE FlexibleContexts #-}
4-
{-# LANGUAGE TupleSections #-}
54

65
module Haskell.Ide.Engine.GhcModuleCache where
76

@@ -118,11 +117,11 @@ instance Show CachedCradle where
118117

119118
data GhcModuleCache = GhcModuleCache
120119
{ cradleCache :: !(T.Trie CachedCradle)
121-
-- ^ map from dirs to cradles
120+
-- ^ map from filepaths to cradles
122121
, uriCaches :: !UriCaches
123122
, currentCradle :: Maybe ([FilePath], BIOS.Cradle)
124-
-- ^ The current cradle and which directories it is
125-
-- responsible for
123+
-- ^ The current cradle and which modules/filepaths it
124+
-- is responsible for
126125
} deriving (Show)
127126

128127
-- ---------------------------------------------------------------------

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ modifyCache f = do
7979
-- in either case
8080
runActionWithContext :: (MonadIde m, GHC.GhcMonad m, HasGhcModuleCache m, MonadBaseControl IO m)
8181
=> GHC.DynFlags -> Maybe FilePath -> m a -> m (IdeResult a)
82+
-- TODO: @fendor, this currently uses a IdeResult to provide either an error
83+
-- produced by `loadCradle` or the actual result of `m a`.
84+
-- We need a way to provide diagnostics from this action instead and report them back
85+
-- to the user.
8286
runActionWithContext _df Nothing action =
8387
-- Cradle with no additional flags
8488
-- dir <- liftIO $ getCurrentDirectory
@@ -92,7 +96,11 @@ runActionWithContext df (Just uri) action = do
9296
IdeResultOk () -> fmap IdeResultOk action
9397
IdeResultFail err -> return $ IdeResultFail err
9498

95-
99+
-- | Load a cradle based on the lookup result.
100+
-- This operation may take a very long time. If a new cradle is required,
101+
-- we now set up the GHC Session and load all modules.
102+
-- If the current cradle is reused, nothing needs to be done.
103+
-- If a cached cradle can be used, set the cradle as the current cradle.
96104
loadCradle :: (MonadIde m, HasGhcModuleCache m, GHC.GhcMonad m
97105
, MonadBaseControl IO m) => GHC.DynFlags -> LookupCradleResult -> m (IdeResult ())
98106
loadCradle _ ReuseCradle = do
@@ -115,8 +123,10 @@ loadCradle iniDynFlags (NewCradle fp) = do
115123
-- Now load the new cradle
116124
cradle <- liftIO $ findLocalCradle fp
117125
traceShowM cradle
126+
-- set a new session
118127
liftIO (GHC.newHscEnv iniDynFlags) >>= GHC.setSession
119128
liftIO $ setCurrentDirectory (BIOS.cradleRootDir cradle)
129+
-- initilise the session
120130
res <- gcatches
121131
(do
122132
withProgress "Initialising Cradle" NotCancellable (initializeCradle cradle)
@@ -146,17 +156,21 @@ loadCradle iniDynFlags (NewCradle fp) = do
146156
}
147157
]
148158

149-
150159
case res of
151160
IdeResultOk () -> do
152161
setCurrentCradle cradle
153162
return (IdeResultOk ())
154163
err -> return err
155164
where
165+
-- Simple helper function to detect, whether the given cradle is a stack cradle.
166+
-- We need this, because we have to append the filepath, for which we are loading the cradle,
167+
-- to the options because of the issue described in: https://github.com/mpickering/haskell-ide-engine/issues/10
168+
-- tldr: stack does not actually load anything on initialisation, but rather on typechecking.
156169
isStackCradle :: BIOS.Cradle -> Bool
157170
isStackCradle c = BIOS.actionName (BIOS.cradleOptsProg c) == "stack"
158171

159-
-- initializeCradle ::
172+
-- | Actually initialize the cradle.
173+
-- Takes a function to report progress.
160174
initializeCradle :: GHC.GhcMonad m => BIOS.Cradle -> (Progress -> IO ()) -> m ()
161175
initializeCradle cradle f = do
162176
let msg = Just (toMessager f)
@@ -176,10 +190,12 @@ loadCradle iniDynFlags (NewCradle fp) = do
176190

177191
targets <- BIOS.initSession opts'
178192
GHC.setTargets targets
179-
-- Get the module graph using the function `getModuleGraph`
180193
mod_graph <- GHC.depanal [] True
181194
void $ GHC.load' GHC.LoadAllTargets msg mod_graph
182195

196+
-- | Set the given cradle as our current context.
197+
-- Retrieves all filepaths this cradle is responsible for from the
198+
-- GHC.moduleGraph and caches them.
183199
setCurrentCradle :: (HasGhcModuleCache m, GHC.GhcMonad m) => BIOS.Cradle -> m ()
184200
setCurrentCradle cradle = do
185201
mg <- GHC.getModuleGraph
@@ -188,23 +204,21 @@ setCurrentCradle cradle = do
188204
ps' <- liftIO $ mapM canonicalizePath ps
189205
modifyCache (\s -> s { currentCradle = Just (ps', cradle) })
190206

191-
207+
-- | Cache the current cradle the modules it is responsible for.
192208
cacheCradle :: (HasGhcModuleCache m, GHC.GhcMonad m) => ([FilePath], BIOS.Cradle) -> m ()
193209
cacheCradle (ds, c) = do
194210
env <- GHC.getSession
195211
let cc = CachedCradle c env
196212
new_map = T.fromList (map (, cc) (map B.pack ds))
197213
modifyCache (\s -> s { cradleCache = T.unionWith (\a _ -> a) new_map (cradleCache s) })
198214

199-
-- | Get the Cradle that should be used for a given URI
200-
--getCradle :: (GM.GmEnv m, MonadIO m, HasGhcModuleCache m, GM.GmLog m
201-
-- , MonadBaseControl IO m, ExceptionMonad m, GM.GmOut m)
215+
-- | Get a cradle search result for a given filepath.
216+
-- Can be used to decide whether to load a new cradle or if the current one can be reused.
202217
getCradle :: (GHC.GhcMonad m, HasGhcModuleCache m)
203218
=> FilePath -> m LookupCradleResult
204219
getCradle fp = do
205220
canon_fp <- liftIO $ canonicalizePath fp
206-
mcache <- getModuleCache
207-
return $ lookupCradle canon_fp mcache
221+
lookupCradle canon_fp <$> getModuleCache
208222

209223
ifCachedInfo :: (HasGhcModuleCache m, MonadIO m) => FilePath -> a -> (CachedInfo -> m a) -> m a
210224
ifCachedInfo fp def callback = do

0 commit comments

Comments
 (0)