Skip to content

Commit 13d4705

Browse files
committed
Import qualified in postfix when applicable
1 parent 3b05bde commit 13d4705

File tree

1 file changed

+24
-15
lines changed
  • plugins/hls-refactor-plugin/src/Development/IDE/Plugin

1 file changed

+24
-15
lines changed

plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import Development.IDE.Types.Location
6868
import Development.IDE.Types.Logger hiding
6969
(group)
7070
import Development.IDE.Types.Options
71+
import qualified GHC.Data.EnumSet as ES
7172
import GHC.Exts (fromList)
7273
import qualified GHC.LanguageExtensions as Lang
7374
#if MIN_VERSION_ghc(9,4,0)
@@ -240,7 +241,7 @@ extendImportHandler' ideState ExtendImport {..}
240241
extendImport (T.unpack <$> thingParent) (T.unpack newThing) (makeDeltaAst imp)
241242

242243
Nothing -> do
243-
let n = newImport importName sym importQual False
244+
let n = newImport importName sym importQual False (isPostQualifiedImport df)
244245
sym = if isNothing importQual then Just it else Nothing
245246
it = case thingParent of
246247
Nothing -> newThing
@@ -250,6 +251,11 @@ extendImportHandler' ideState ExtendImport {..}
250251
| otherwise =
251252
mzero
252253

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+
253259
isWantedModule :: ModuleName -> Maybe ModuleName -> GenLocated l (ImportDecl GhcPs) -> Bool
254260
isWantedModule wantedModule Nothing (L _ it@ImportDecl{ideclName, ideclHiding = Just (False, _)}) =
255261
not (isQualifiedImport it) && unLoc ideclName == wantedModule
@@ -1417,8 +1423,8 @@ suggestNewOrExtendImportForClassMethod packageExportsMap ps fileContents Diagnos
14171423
| otherwise -> []
14181424
where moduleText = moduleNameText identInfo
14191425

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}
14221428
| msg <- unifySpaces _message
14231429
, Just thingMissing <- extractNotInScopeName msg
14241430
, qual <- extractQualifiedModuleName msg
@@ -1431,15 +1437,15 @@ suggestNewImport packageExportsMap ps fileContents Diagnostic{_message}
14311437
, extendImportSuggestions <- matchRegexUnifySpaces msg
14321438
"Perhaps you want to add ‘[^’]*’ to the import list in the import of ‘([^’]*)’"
14331439
= let suggestions = nubSortBy simpleCompareImportSuggestion
1434-
(constructNewImportSuggestions packageExportsMap (qual <|> qual', thingMissing) extendImportSuggestions) in
1440+
(constructNewImportSuggestions df packageExportsMap (qual <|> qual', thingMissing) extendImportSuggestions) in
14351441
map (\(ImportSuggestion _ kind (unNewImport -> imp)) -> (imp, kind, TextEdit range (imp <> "\n" <> T.replicate indent " "))) suggestions
14361442
where
14371443
L _ HsModule {..} = astA ps
1438-
suggestNewImport _ _ _ _ = []
1444+
suggestNewImport _ _ _ _ _ = []
14391445

14401446
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
14431449
[ suggestion
14441450
| Just name <- [T.stripPrefix (maybe "" (<> ".") qual) $ notInScope thingMissing] -- strip away qualified module names from the unknown name
14451451
, 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 =
14511457
renderNewImport :: IdentInfo -> [ImportSuggestion]
14521458
renderNewImport identInfo
14531459
| Just q <- qual
1454-
= [ImportSuggestion importanceScore (quickFixImportKind "new.qualified") (newQualImport m q)]
1460+
= [ImportSuggestion importanceScore (quickFixImportKind "new.qualified") (newQualImport m q $ isPostQualifiedImport df)]
14551461
| otherwise
14561462
= [ImportSuggestion importanceScore (quickFixImportKind' "new" importStyle) (newUnqualImport m (renderImportStyle importStyle) False)
14571463
| importStyle <- NE.toList $ importStyles identInfo] ++
@@ -1631,8 +1637,9 @@ newImport
16311637
-> Maybe T.Text -- ^ the symbol
16321638
-> Maybe T.Text -- ^ qualified name
16331639
-> Bool -- ^ the symbol is to be imported or hidden
1640+
-> Bool -- ^ the qualified name is to be imported in postfix position
16341641
-> NewImport
1635-
newImport modName mSymbol mQual hiding = NewImport impStmt
1642+
newImport modName mSymbol mQual hiding postfix = NewImport impStmt
16361643
where
16371644
symImp
16381645
| Just symbol <- mSymbol
@@ -1641,20 +1648,22 @@ newImport modName mSymbol mQual hiding = NewImport impStmt
16411648
| otherwise = ""
16421649
impStmt =
16431650
"import "
1644-
<> maybe "" (const "qualified ") mQual
1645-
<> modName
1651+
<> qualifiedModName
16461652
<> (if hiding then " hiding" else "")
16471653
<> symImp
16481654
<> maybe "" (\qual -> if modName == qual then "" else " as " <> qual) mQual
1655+
qualifiedModName | isJust mQual && postfix = modName <> " qualified"
1656+
| isJust mQual = "qualified " <> modName
1657+
| otherwise = ""
16491658

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
16521661

16531662
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
16551664

16561665
newImportAll :: T.Text -> NewImport
1657-
newImportAll modName = newImport modName Nothing Nothing False
1666+
newImportAll modName = newImport modName Nothing Nothing False False
16581667

16591668
hideImplicitPreludeSymbol :: T.Text -> NewImport
16601669
hideImplicitPreludeSymbol symbol = newUnqualImport "Prelude" symbol True

0 commit comments

Comments
 (0)