@@ -151,6 +151,7 @@ import Ide.Plugin.Properties (HasProperty,
151151import Ide.PluginUtils (configForPlugin )
152152import Ide.Types (DynFlagsModifications (dynFlagsModifyGlobal , dynFlagsModifyParser ),
153153 PluginId )
154+ import qualified Data.HashSet as HS
154155
155156-- | This is useful for rules to convert rules that can only produce errors or
156157-- a result into the more general IdeResult type that supports producing
@@ -311,6 +312,7 @@ getLocatedImportsRule =
311312 define $ \ GetLocatedImports file -> do
312313 ModSummaryResult {msrModSummary = ms} <- use_ GetModSummaryWithoutTimestamps file
313314 targets <- useNoFile_ GetKnownTargets
315+ let targetsMap = HM. mapWithKey const targets
314316 let imports = [(False , imp) | imp <- ms_textual_imps ms] ++ [(True , imp) | imp <- ms_srcimps ms]
315317 env_eq <- use_ GhcSession file
316318 let env = hscEnvWithImportPaths env_eq
@@ -321,14 +323,24 @@ getLocatedImportsRule =
321323 then addRelativeImport file (moduleName $ ms_mod ms) dflags
322324 else dflags
323325 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
330342 (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
332344 case diagOrImp of
333345 Left diags -> pure (diags, Just (modName, Nothing ))
334346 Right (FileImport path) -> pure ([] , Just (modName, Just path))
0 commit comments