Skip to content

Commit 3a65034

Browse files
committed
Add QualifiedImportStyle
1 parent 6d82231 commit 3a65034

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ extendImportHandler' ideState ExtendImport {..}
243243
extendImport (T.unpack <$> thingParent) (T.unpack newThing) (makeDeltaAst imp)
244244

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

256-
isPostQualifiedImport :: DynFlags -> Bool
257-
isPostQualifiedImport df = hasImportQualifedPostEnabled && hasPrePositiveQualifiedWarning
258-
where hasImportQualifedPostEnabled = xopt ImportQualifiedPost df
259-
hasPrePositiveQualifiedWarning = wopt Opt_WarnPrepositiveQualifiedModule df
260-
261257
isWantedModule :: ModuleName -> Maybe ModuleName -> GenLocated l (ImportDecl GhcPs) -> Bool
262258
isWantedModule wantedModule Nothing (L _ it@ImportDecl{ideclName, ideclHiding = Just (False, _)}) =
263259
not (isQualifiedImport it) && unLoc ideclName == wantedModule
@@ -1439,16 +1435,17 @@ suggestNewImport df packageExportsMap ps fileContents Diagnostic{_message}
14391435
, Just (range, indent) <- newImportInsertRange ps fileContents
14401436
, extendImportSuggestions <- matchRegexUnifySpaces msg
14411437
"Perhaps you want to add ‘[^’]*’ to the import list in the import of ‘([^’]*)’"
1442-
= let suggestions = nubSortBy simpleCompareImportSuggestion
1443-
(constructNewImportSuggestions df packageExportsMap (qual <|> qual', thingMissing) extendImportSuggestions) in
1438+
= let qis = qualifiedImportStyle df
1439+
suggestions = nubSortBy simpleCompareImportSuggestion
1440+
(constructNewImportSuggestions packageExportsMap (qual <|> qual', thingMissing) extendImportSuggestions qis) in
14441441
map (\(ImportSuggestion _ kind (unNewImport -> imp)) -> (imp, kind, TextEdit range (imp <> "\n" <> T.replicate indent " "))) suggestions
14451442
where
14461443
L _ HsModule {..} = astA ps
14471444
suggestNewImport _ _ _ _ _ = []
14481445

14491446
constructNewImportSuggestions
1450-
:: DynFlags -> ExportsMap -> (Maybe T.Text, NotInScope) -> Maybe [T.Text] -> [ImportSuggestion]
1451-
constructNewImportSuggestions df exportsMap (qual, thingMissing) notTheseModules = nubOrdBy simpleCompareImportSuggestion
1447+
:: ExportsMap -> (Maybe T.Text, NotInScope) -> Maybe [T.Text] -> QualifiedImportStyle -> [ImportSuggestion]
1448+
constructNewImportSuggestions exportsMap (qual, thingMissing) notTheseModules qis = nubOrdBy simpleCompareImportSuggestion
14521449
[ suggestion
14531450
| Just name <- [T.stripPrefix (maybe "" (<> ".") qual) $ notInScope thingMissing] -- strip away qualified module names from the unknown name
14541451
, identInfo <- maybe [] Set.toList $ Map.lookup name (getExportsMap exportsMap) -- look up the modified unknown name in the export map
@@ -1460,7 +1457,7 @@ constructNewImportSuggestions df exportsMap (qual, thingMissing) notTheseModules
14601457
renderNewImport :: IdentInfo -> [ImportSuggestion]
14611458
renderNewImport identInfo
14621459
| Just q <- qual
1463-
= [ImportSuggestion importanceScore (quickFixImportKind "new.qualified") (newQualImport m q $ isPostQualifiedImport df)]
1460+
= [ImportSuggestion importanceScore (quickFixImportKind "new.qualified") (newQualImport m q qis)]
14641461
| otherwise
14651462
= [ImportSuggestion importanceScore (quickFixImportKind' "new" importStyle) (newUnqualImport m (renderImportStyle importStyle) False)
14661463
| importStyle <- NE.toList $ importStyles identInfo] ++
@@ -1642,11 +1639,10 @@ checkPragma name = check
16421639
newImport
16431640
:: T.Text -- ^ module name
16441641
-> Maybe T.Text -- ^ the symbol
1645-
-> Maybe T.Text -- ^ qualified name
1642+
-> Maybe (T.Text, QualifiedImportStyle) -- ^ qualified name and style
16461643
-> Bool -- ^ the symbol is to be imported or hidden
1647-
-> Bool -- ^ the qualified name is to be imported in postfix position
16481644
-> NewImport
1649-
newImport modName mSymbol mQual hiding postfix = NewImport impStmt
1645+
newImport modName mSymbol mQualNameStyle hiding = NewImport impStmt
16501646
where
16511647
symImp
16521648
| Just symbol <- mSymbol
@@ -1655,22 +1651,24 @@ newImport modName mSymbol mQual hiding postfix = NewImport impStmt
16551651
| otherwise = ""
16561652
impStmt =
16571653
"import "
1658-
<> qualifiedModName
1654+
<> qualifiedModName (snd <$> mQualNameStyle)
16591655
<> (if hiding then " hiding" else "")
16601656
<> symImp
16611657
<> maybe "" (\qual -> if modName == qual then "" else " as " <> qual) mQual
1662-
qualifiedModName | isJust mQual && postfix = modName <> " qualified"
1663-
| isJust mQual = "qualified " <> modName
1664-
| otherwise = ""
1658+
mQual = fst <$> mQualNameStyle
1659+
qualifiedModName Nothing = modName
1660+
qualifiedModName (Just QualifiedImportPrefix) = "qualified " <> modName
1661+
qualifiedModName (Just QualifiedImportPostfix) = modName <> " qualified"
1662+
16651663

1666-
newQualImport :: T.Text -> T.Text -> Bool -> NewImport
1667-
newQualImport modName qual postfix = newImport modName Nothing (Just qual) False postfix
1664+
newQualImport :: T.Text -> T.Text -> QualifiedImportStyle -> NewImport
1665+
newQualImport modName qual qis = newImport modName Nothing (Just (qual, qis)) False
16681666

16691667
newUnqualImport :: T.Text -> T.Text -> Bool -> NewImport
1670-
newUnqualImport modName symbol hiding = newImport modName (Just symbol) Nothing hiding False
1668+
newUnqualImport modName symbol = newImport modName (Just symbol) Nothing
16711669

16721670
newImportAll :: T.Text -> NewImport
1673-
newImportAll modName = newImport modName Nothing Nothing False False
1671+
newImportAll modName = newImport modName Nothing Nothing False
16741672

16751673
hideImplicitPreludeSymbol :: T.Text -> NewImport
16761674
hideImplicitPreludeSymbol symbol = newUnqualImport "Prelude" symbol True

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ module Development.IDE.Plugin.Plugins.ImportUtils
44
quickFixImportKind,
55
renderImportStyle,
66
unImportStyle,
7-
importStyles
7+
importStyles,
8+
QualifiedImportStyle(..),
9+
qualifiedImportStyle
810
) where
911

1012
import Data.List.NonEmpty (NonEmpty ((:|)))
1113
import qualified Data.Text as T
14+
import Development.IDE.GHC.Compat
1215
import Development.IDE.Plugin.CodeAction.ExactPrint (wildCardSymbol)
1316
import Development.IDE.Types.Exports (IdentInfo (..))
1417
import Language.LSP.Types (CodeActionKind (..))
@@ -81,3 +84,13 @@ quickFixImportKind' x (ImportAllConstructors _) = CodeActionUnknown $ "quickfix.
8184

8285
quickFixImportKind :: T.Text -> CodeActionKind
8386
quickFixImportKind x = CodeActionUnknown $ "quickfix.import." <> x
87+
88+
-- | Possible import styles for qualified imports
89+
data QualifiedImportStyle = QualifiedImportPostfix | QualifiedImportPrefix
90+
deriving Show
91+
92+
qualifiedImportStyle :: DynFlags -> QualifiedImportStyle
93+
qualifiedImportStyle df | hasImportQualifedPostEnabled && hasPrePositiveQualifiedWarning = QualifiedImportPostfix
94+
| otherwise = QualifiedImportPrefix
95+
where hasImportQualifedPostEnabled = xopt ImportQualifiedPost df
96+
hasPrePositiveQualifiedWarning = wopt Opt_WarnPrepositiveQualifiedModule df

0 commit comments

Comments
 (0)