Skip to content

Commit 5699deb

Browse files
committed
Import qualified in postfix when applicable
1 parent 6f55ccb commit 5699deb

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
@@ -67,6 +67,7 @@ import Development.IDE.Types.Location
6767
import Development.IDE.Types.Logger hiding
6868
(group)
6969
import Development.IDE.Types.Options
70+
import qualified GHC.Data.EnumSet as ES
7071
import GHC.Exts (fromList)
7172
import qualified GHC.LanguageExtensions as Lang
7273
import Ide.PluginUtils (subRange)
@@ -243,7 +244,7 @@ extendImportHandler' ideState ExtendImport {..}
243244
extendImport (T.unpack <$> thingParent) (T.unpack newThing) (makeDeltaAst imp)
244245

245246
Nothing -> do
246-
let n = newImport importName sym importQual False
247+
let n = newImport importName sym importQual False (isPostQualifiedImport df)
247248
sym = if isNothing importQual then Just it else Nothing
248249
it = case thingParent of
249250
Nothing -> newThing
@@ -253,6 +254,11 @@ extendImportHandler' ideState ExtendImport {..}
253254
| otherwise =
254255
mzero
255256

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+
256262
isWantedModule :: ModuleName -> Maybe ModuleName -> GenLocated l (ImportDecl GhcPs) -> Bool
257263
isWantedModule wantedModule Nothing (L _ it@ImportDecl{ideclName, ideclHiding = Just (False, _)}) =
258264
not (isQualifiedImport it) && unLoc ideclName == wantedModule
@@ -1421,8 +1427,8 @@ suggestNewOrExtendImportForClassMethod packageExportsMap ps fileContents Diagnos
14211427
| otherwise -> []
14221428
#endif
14231429

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

14441450
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
14471453
[ suggestion
14481454
| Just name <- [T.stripPrefix (maybe "" (<> ".") qual) $ notInScope thingMissing] -- strip away qualified module names from the unknown name
14491455
, 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 =
14551461
renderNewImport :: IdentInfo -> [ImportSuggestion]
14561462
renderNewImport identInfo
14571463
| Just q <- qual
1458-
= [ImportSuggestion importanceScore (quickFixImportKind "new.qualified") (newQualImport m q)]
1464+
= [ImportSuggestion importanceScore (quickFixImportKind "new.qualified") (newQualImport m q $ isPostQualifiedImport df)]
14591465
| otherwise
14601466
= [ImportSuggestion importanceScore (quickFixImportKind' "new" importStyle) (newUnqualImport m (renderImportStyle importStyle) False)
14611467
| importStyle <- NE.toList $ importStyles identInfo] ++
@@ -1639,8 +1645,9 @@ newImport
16391645
-> Maybe T.Text -- ^ the symbol
16401646
-> Maybe T.Text -- ^ qualified name
16411647
-> Bool -- ^ the symbol is to be imported or hidden
1648+
-> Bool -- ^ the qualified name is to be imported in postfix position
16421649
-> NewImport
1643-
newImport modName mSymbol mQual hiding = NewImport impStmt
1650+
newImport modName mSymbol mQual hiding postfix = NewImport impStmt
16441651
where
16451652
symImp
16461653
| Just symbol <- mSymbol
@@ -1649,20 +1656,22 @@ newImport modName mSymbol mQual hiding = NewImport impStmt
16491656
| otherwise = ""
16501657
impStmt =
16511658
"import "
1652-
<> maybe "" (const "qualified ") mQual
1653-
<> modName
1659+
<> qualifiedModName
16541660
<> (if hiding then " hiding" else "")
16551661
<> symImp
16561662
<> maybe "" (\qual -> if modName == qual then "" else " as " <> qual) mQual
1663+
qualifiedModName | isJust mQual && postfix = modName <> " qualified"
1664+
| isJust mQual = "qualified " <> modName
1665+
| otherwise = ""
16571666

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
16601669

16611670
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
16631672

16641673
newImportAll :: T.Text -> NewImport
1665-
newImportAll modName = newImport modName Nothing Nothing False
1674+
newImportAll modName = newImport modName Nothing Nothing False False
16661675

16671676
hideImplicitPreludeSymbol :: T.Text -> NewImport
16681677
hideImplicitPreludeSymbol symbol = newUnqualImport "Prelude" symbol True

0 commit comments

Comments
 (0)