@@ -68,6 +68,7 @@ import Development.IDE.Types.Location
68
68
import Development.IDE.Types.Logger hiding
69
69
(group )
70
70
import Development.IDE.Types.Options
71
+ import qualified GHC.Data.EnumSet as ES
71
72
import GHC.Exts (fromList )
72
73
import qualified GHC.LanguageExtensions as Lang
73
74
#if MIN_VERSION_ghc(9,4,0)
@@ -240,7 +241,7 @@ extendImportHandler' ideState ExtendImport {..}
240
241
extendImport (T. unpack <$> thingParent) (T. unpack newThing) (makeDeltaAst imp)
241
242
242
243
Nothing -> do
243
- let n = newImport importName sym importQual False
244
+ let n = newImport importName sym importQual False (isPostQualifiedImport df)
244
245
sym = if isNothing importQual then Just it else Nothing
245
246
it = case thingParent of
246
247
Nothing -> newThing
@@ -250,6 +251,11 @@ extendImportHandler' ideState ExtendImport {..}
250
251
| otherwise =
251
252
mzero
252
253
254
+ isPostQualifiedImport :: DynFlags -> Bool
255
+ isPostQualifiedImport df = hasImportQualifedPostEnabled && hasPrePositiveQualifiedWarning
256
+ where hasImportQualifedPostEnabled = ES. member ImportQualifiedPost (extensionFlags df)
257
+ hasPrePositiveQualifiedWarning = ES. member Opt_WarnPrepositiveQualifiedModule (warningFlags df)
258
+
253
259
isWantedModule :: ModuleName -> Maybe ModuleName -> GenLocated l (ImportDecl GhcPs ) -> Bool
254
260
isWantedModule wantedModule Nothing (L _ it@ ImportDecl {ideclName, ideclHiding = Just (False , _)}) =
255
261
not (isQualifiedImport it) && unLoc ideclName == wantedModule
@@ -1417,8 +1423,8 @@ suggestNewOrExtendImportForClassMethod packageExportsMap ps fileContents Diagnos
1417
1423
| otherwise -> []
1418
1424
where moduleText = moduleNameText identInfo
1419
1425
1420
- suggestNewImport :: ExportsMap -> Annotated ParsedSource -> T. Text -> Diagnostic -> [(T. Text , CodeActionKind , TextEdit )]
1421
- suggestNewImport packageExportsMap ps fileContents Diagnostic {_message}
1426
+ suggestNewImport :: DynFlags -> ExportsMap -> Annotated ParsedSource -> T. Text -> Diagnostic -> [(T. Text , CodeActionKind , TextEdit )]
1427
+ suggestNewImport df packageExportsMap ps fileContents Diagnostic {_message}
1422
1428
| msg <- unifySpaces _message
1423
1429
, Just thingMissing <- extractNotInScopeName msg
1424
1430
, qual <- extractQualifiedModuleName msg
@@ -1431,15 +1437,15 @@ suggestNewImport packageExportsMap ps fileContents Diagnostic{_message}
1431
1437
, extendImportSuggestions <- matchRegexUnifySpaces msg
1432
1438
" Perhaps you want to add ‘[^’]*’ to the import list in the import of ‘([^’]*)’"
1433
1439
= let suggestions = nubSortBy simpleCompareImportSuggestion
1434
- (constructNewImportSuggestions packageExportsMap (qual <|> qual', thingMissing) extendImportSuggestions) in
1440
+ (constructNewImportSuggestions df packageExportsMap (qual <|> qual', thingMissing) extendImportSuggestions) in
1435
1441
map (\ (ImportSuggestion _ kind (unNewImport -> imp)) -> (imp, kind, TextEdit range (imp <> " \n " <> T. replicate indent " " ))) suggestions
1436
1442
where
1437
1443
L _ HsModule {.. } = astA ps
1438
- suggestNewImport _ _ _ _ = []
1444
+ suggestNewImport _ _ _ _ _ = []
1439
1445
1440
1446
constructNewImportSuggestions
1441
- :: ExportsMap -> (Maybe T. Text , NotInScope ) -> Maybe [T. Text ] -> [ImportSuggestion ]
1442
- constructNewImportSuggestions exportsMap (qual, thingMissing) notTheseModules = nubOrdBy simpleCompareImportSuggestion
1447
+ :: DynFlags -> ExportsMap -> (Maybe T. Text , NotInScope ) -> Maybe [T. Text ] -> [ImportSuggestion ]
1448
+ constructNewImportSuggestions df exportsMap (qual, thingMissing) notTheseModules = nubOrdBy simpleCompareImportSuggestion
1443
1449
[ suggestion
1444
1450
| Just name <- [T. stripPrefix (maybe " " (<> " ." ) qual) $ notInScope thingMissing] -- strip away qualified module names from the unknown name
1445
1451
, identInfo <- maybe [] Set. toList $ (lookupOccEnv (getExportsMap exportsMap) (mkVarOrDataOcc name)) <> (lookupOccEnv (getExportsMap exportsMap) (mkTypeOcc name)) -- look up the modified unknown name in the export map
@@ -1451,7 +1457,7 @@ constructNewImportSuggestions exportsMap (qual, thingMissing) notTheseModules =
1451
1457
renderNewImport :: IdentInfo -> [ImportSuggestion ]
1452
1458
renderNewImport identInfo
1453
1459
| Just q <- qual
1454
- = [ImportSuggestion importanceScore (quickFixImportKind " new.qualified" ) (newQualImport m q)]
1460
+ = [ImportSuggestion importanceScore (quickFixImportKind " new.qualified" ) (newQualImport m q $ isPostQualifiedImport df )]
1455
1461
| otherwise
1456
1462
= [ImportSuggestion importanceScore (quickFixImportKind' " new" importStyle) (newUnqualImport m (renderImportStyle importStyle) False )
1457
1463
| importStyle <- NE. toList $ importStyles identInfo] ++
@@ -1631,8 +1637,9 @@ newImport
1631
1637
-> Maybe T. Text -- ^ the symbol
1632
1638
-> Maybe T. Text -- ^ qualified name
1633
1639
-> Bool -- ^ the symbol is to be imported or hidden
1640
+ -> Bool -- ^ the qualified name is to be imported in postfix position
1634
1641
-> NewImport
1635
- newImport modName mSymbol mQual hiding = NewImport impStmt
1642
+ newImport modName mSymbol mQual hiding postfix = NewImport impStmt
1636
1643
where
1637
1644
symImp
1638
1645
| Just symbol <- mSymbol
@@ -1641,20 +1648,22 @@ newImport modName mSymbol mQual hiding = NewImport impStmt
1641
1648
| otherwise = " "
1642
1649
impStmt =
1643
1650
" import "
1644
- <> maybe " " (const " qualified " ) mQual
1645
- <> modName
1651
+ <> qualifiedModName
1646
1652
<> (if hiding then " hiding" else " " )
1647
1653
<> symImp
1648
1654
<> maybe " " (\ qual -> if modName == qual then " " else " as " <> qual) mQual
1655
+ qualifiedModName | isJust mQual && postfix = modName <> " qualified"
1656
+ | isJust mQual = " qualified " <> modName
1657
+ | otherwise = " "
1649
1658
1650
- newQualImport :: T. Text -> T. Text -> NewImport
1651
- newQualImport modName qual = newImport modName Nothing (Just qual) False
1659
+ newQualImport :: T. Text -> T. Text -> Bool -> NewImport
1660
+ newQualImport modName qual postfix = newImport modName Nothing (Just qual) False postfix
1652
1661
1653
1662
newUnqualImport :: T. Text -> T. Text -> Bool -> NewImport
1654
- newUnqualImport modName symbol = newImport modName (Just symbol) Nothing
1663
+ newUnqualImport modName symbol hiding = newImport modName (Just symbol) Nothing hiding False
1655
1664
1656
1665
newImportAll :: T. Text -> NewImport
1657
- newImportAll modName = newImport modName Nothing Nothing False
1666
+ newImportAll modName = newImport modName Nothing Nothing False False
1658
1667
1659
1668
hideImplicitPreludeSymbol :: T. Text -> NewImport
1660
1669
hideImplicitPreludeSymbol symbol = newUnqualImport " Prelude" symbol True
0 commit comments