Skip to content

Commit dc27803

Browse files
committed
CodeAction for postfix qualified actions
1 parent 6f55ccb commit dc27803

File tree

1 file changed

+16
-7
lines changed
  • plugins/hls-refactor-plugin/src/Development/IDE/Plugin

1 file changed

+16
-7
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import GHC (AddEpAnn (Ad
104104
EpaLocation (..),
105105
LEpaComment,
106106
LocatedA)
107+
import qualified GHC.Data.EnumSet as ES
107108
#else
108109
import Language.Haskell.GHC.ExactPrint.Types (Annotation (annsDP),
109110
DeltaPos,
@@ -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
@@ -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

16581667
newQualImport :: T.Text -> T.Text -> NewImport
1659-
newQualImport modName qual = newImport modName Nothing (Just qual) False
1668+
newQualImport modName qual = newImport modName Nothing (Just qual) False False
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)