@@ -151,6 +151,7 @@ import Ide.Plugin.Properties (HasProperty,
151
151
import Ide.PluginUtils (configForPlugin )
152
152
import Ide.Types (DynFlagsModifications (dynFlagsModifyGlobal , dynFlagsModifyParser ),
153
153
PluginId )
154
+ import qualified Data.HashSet as HS
154
155
155
156
-- | This is useful for rules to convert rules that can only produce errors or
156
157
-- a result into the more general IdeResult type that supports producing
@@ -311,6 +312,7 @@ getLocatedImportsRule =
311
312
define $ \ GetLocatedImports file -> do
312
313
ModSummaryResult {msrModSummary = ms} <- use_ GetModSummaryWithoutTimestamps file
313
314
targets <- useNoFile_ GetKnownTargets
315
+ let targetsMap = HM. mapWithKey const targets
314
316
let imports = [(False , imp) | imp <- ms_textual_imps ms] ++ [(True , imp) | imp <- ms_srcimps ms]
315
317
env_eq <- use_ GhcSession file
316
318
let env = hscEnvWithImportPaths env_eq
@@ -321,14 +323,24 @@ getLocatedImportsRule =
321
323
then addRelativeImport file (moduleName $ ms_mod ms) dflags
322
324
else dflags
323
325
opt <- getIdeOptions
324
- let getTargetExists modName nfp
325
- | isImplicitCradle = getFileExists nfp
326
- | HM. member (TargetModule modName) targets
327
- || HM. member (TargetFile nfp) targets
328
- = getFileExists nfp
329
- | otherwise = return False
326
+ let getTargetFor modName nfp
327
+ | isImplicitCradle = do
328
+ itExists <- getFileExists nfp
329
+ return $ if itExists then Just nfp else Nothing
330
+ | Just (TargetFile nfp') <- HM. lookup (TargetFile nfp) targetsMap = do
331
+ -- reuse the existing NormalizedFilePath in order to maximize sharing
332
+ itExists <- getFileExists nfp'
333
+ return $ if itExists then Just nfp' else Nothing
334
+ | Just tt <- HM. lookup (TargetModule modName) targets = do
335
+ -- reuse the existing NormalizedFilePath in order to maximize sharing
336
+ let ttmap = HM. mapWithKey const (HS. toMap tt)
337
+ nfp' = HM. lookupDefault nfp nfp ttmap
338
+ itExists <- getFileExists nfp'
339
+ return $ if itExists then Just nfp' else Nothing
340
+ | otherwise
341
+ = return Nothing
330
342
(diags, imports') <- fmap unzip $ forM imports $ \ (isSource, (mbPkgName, modName)) -> do
331
- diagOrImp <- locateModule dflags import_dirs (optExtensions opt) getTargetExists modName mbPkgName isSource
343
+ diagOrImp <- locateModule dflags import_dirs (optExtensions opt) getTargetFor modName mbPkgName isSource
332
344
case diagOrImp of
333
345
Left diags -> pure (diags, Just (modName, Nothing ))
334
346
Right (FileImport path) -> pure ([] , Just (modName, Just path))
0 commit comments