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