@@ -29,7 +29,7 @@ import qualified Data.IntMap as IM (IntMap, elems,
29
29
fromList , (!?) )
30
30
import Data.IORef (readIORef )
31
31
import qualified Data.Map.Strict as Map
32
- import Data.Maybe (mapMaybe )
32
+ import Data.Maybe (isNothing , mapMaybe )
33
33
import qualified Data.Set as S
34
34
import Data.String (fromString )
35
35
import qualified Data.Text as T
@@ -316,42 +316,32 @@ minimalImportsRule recorder modFilter = defineNoDiagnostics (cmapWithPrio LogSha
316
316
return $ mi_exports $ hirModIface imp_hir
317
317
318
318
-- Use the GHC api to extract the "minimal" imports
319
- (imports, mbMinImports) <- MaybeT $ liftIO $ extractMinimalImports hsc tmr
319
+ locationImportWithMinimal <- MaybeT $ liftIO $ extractMinimalImports hsc tmr
320
320
321
- let importsMap =
322
- Map. fromList
323
- [ (realSrcSpanStart l, printOutputable i)
324
- | L (locA -> RealSrcSpan l _) i <- mbMinImports
325
- ]
326
- minimalImportsResult =
327
- [ (range, (minImport, ExplicitImport ))
328
- | imp@ (L _ impDecl) <- imports
321
+ let minimalImportsResult =
322
+ [ (range, (printOutputable minImport, ExplicitImport ))
323
+ | (location, impDecl, minImport) <- locationImportWithMinimal
329
324
, not (isQualifiedImport impDecl)
330
325
, not (isExplicitImport impDecl)
331
326
, let L _ moduleName = ideclName impDecl
332
327
, modFilter moduleName
333
- , RealSrcSpan location _ <- [getLoc imp]
334
- , let range = realSrcSpanToRange location
335
- , Just minImport <- [Map. lookup (realSrcSpanStart location) importsMap]
336
- ]
328
+ , let range = realSrcSpanToRange location]
329
+
337
330
refineImportsResult =
338
331
[ (range, (T. intercalate " \n "
339
- . map (printOutputable . constructImport i )
332
+ . map (printOutputable . constructImport origImport minImport )
340
333
. Map. toList
341
334
$ filteredInnerImports, RefineImport ))
342
335
-- for every minimal imports
343
- | minImports <- [mbMinImports]
344
- , i@ (L _ ImportDecl {ideclName = L _ mn}) <- minImports
336
+ | (location, origImport, minImport@ (ImportDecl {ideclName = L _ mn})) <- locationImportWithMinimal
345
337
-- (almost) no one wants to see an refine import list for Prelude
346
338
, mn /= moduleName pRELUDE
347
339
-- we check for the inner imports
348
340
, Just innerImports <- [Map. lookup mn import2Map]
349
341
-- and only get those symbols used
350
- , Just filteredInnerImports <- [filterByImport i innerImports]
342
+ , Just filteredInnerImports <- [filterByImport minImport innerImports]
351
343
-- if no symbols from this modules then don't need to generate new import
352
344
, not $ null filteredInnerImports
353
- -- get the location
354
- , RealSrcSpan location _ <- [getLoc i]
355
345
-- and then convert that to a Range
356
346
, let range = realSrcSpanToRange location
357
347
]
@@ -370,7 +360,7 @@ minimalImportsRule recorder modFilter = defineNoDiagnostics (cmapWithPrio LogSha
370
360
extractMinimalImports ::
371
361
HscEnvEq ->
372
362
TcModuleResult ->
373
- IO (Maybe ([ LImportDecl GhcRn ], [ LImportDecl GhcRn ]) )
363
+ IO (Maybe [( RealSrcSpan , ImportDecl GhcRn , ImportDecl GhcRn )] )
374
364
extractMinimalImports hsc TcModuleResult {.. } = runMaybeT $ do
375
365
-- extract the original imports and the typechecking environment
376
366
let tcEnv = tmrTypechecked
@@ -391,8 +381,17 @@ extractMinimalImports hsc TcModuleResult {..} = runMaybeT $ do
391
381
(_, Just minimalImports) <- liftIO $
392
382
initTcWithGbl (hscEnv hsc) tcEnv srcSpan $ getMinimalImports usage
393
383
384
+ let minimalImportsMap =
385
+ Map. fromList
386
+ [ (realSrcSpanStart l, impDecl)
387
+ | L (locA -> RealSrcSpan l _) impDecl <- minimalImports
388
+ ]
389
+ results =
390
+ [ (location, imp, minImport)
391
+ | L (locA -> RealSrcSpan location _) imp <- imports
392
+ , Just minImport <- [Map. lookup (realSrcSpanStart location) minimalImportsMap]]
394
393
-- return both the original imports and the computed minimal ones
395
- return (imports, minimalImports)
394
+ return results
396
395
where
397
396
notExported :: [String ] -> LImportDecl GhcRn -> Bool
398
397
notExported [] _ = True
@@ -451,11 +450,11 @@ abbreviateImportTitle input =
451
450
--------------------------------------------------------------------------------
452
451
453
452
454
- filterByImport :: LImportDecl GhcRn -> Map. Map ModuleName [AvailInfo ] -> Maybe (Map. Map ModuleName [AvailInfo ])
453
+ filterByImport :: ImportDecl GhcRn -> Map. Map ModuleName [AvailInfo ] -> Maybe (Map. Map ModuleName [AvailInfo ])
455
454
#if MIN_VERSION_ghc(9,5,0)
456
- filterByImport (L _ ImportDecl {ideclImportList = Just (_, L _ names)})
455
+ filterByImport (ImportDecl {ideclImportList = Just (_, L _ names)})
457
456
#else
458
- filterByImport (L _ ImportDecl {ideclHiding = Just (_, L _ names)})
457
+ filterByImport (ImportDecl {ideclHiding = Just (_, L _ names)})
459
458
#endif
460
459
avails =
461
460
-- if there is a function defined in the current module and is used
@@ -474,18 +473,22 @@ filterByImport (L _ ImportDecl{ideclHiding = Just (_, L _ names)})
474
473
$ Map. elems res
475
474
filterByImport _ _ = Nothing
476
475
477
- constructImport :: LImportDecl GhcRn -> (ModuleName , [AvailInfo ]) -> LImportDecl GhcRn
476
+ constructImport :: ImportDecl GhcRn -> ImportDecl GhcRn -> (ModuleName , [AvailInfo ]) -> ImportDecl GhcRn
478
477
#if MIN_VERSION_ghc(9,5,0)
479
- constructImport ( L lim imd @ ImportDecl {ideclName = L _ _, ideclImportList = Just (hiding, L _ names)})
478
+ constructImport ImportDecl {ideclQualified = qualified, ideclImportList = origHiding} imd @ ImportDecl { ideclImportList = Just (hiding, L _ names)}
480
479
#else
481
- constructImport ( L lim imd @ ImportDecl {ideclName = L _ _, ideclHiding = Just (hiding, L _ names)})
480
+ constructImport ImportDecl {ideclQualified = qualified, ideclHiding = origHiding} imd @ ImportDecl { ideclHiding = Just (hiding, L _ names)}
482
481
#endif
483
- (newModuleName, avails) = L lim imd
482
+ (newModuleName, avails) = imd
484
483
{ ideclName = noLocA newModuleName
485
484
#if MIN_VERSION_ghc(9,5,0)
486
- , ideclImportList = Just (hiding, noLocA newNames)
485
+ , ideclImportList = if isNothing origHiding && qualified /= NotQualified
486
+ then Nothing
487
+ else Just (hiding, noLocA newNames)
487
488
#else
488
- , ideclHiding = Just (hiding, noLocA newNames)
489
+ , ideclHiding = if isNothing origHiding && qualified /= NotQualified
490
+ then Nothing
491
+ else Just (hiding, noLocA newNames)
489
492
#endif
490
493
}
491
494
where newNames = filter (\ n -> any (n `containsAvail` ) avails) names
@@ -495,4 +498,4 @@ constructImport (L lim imd@ImportDecl{ideclName = L _ _, ideclHiding = Just (hid
495
498
any (\ an -> printOutputable an == (printOutputable . ieName . unLoc $ name))
496
499
$ availNamesWithSelectors avail
497
500
498
- constructImport lim _ = lim
501
+ constructImport _ lim _ = lim
0 commit comments