Skip to content

Commit cff2b03

Browse files
committed
Handle qualified imports
1 parent 284e6c9 commit cff2b03

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

ghcide/src/Development/IDE/Plugin/Completions.hs

+7-4
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ extendImportHandler' ideState ExtendImport {..}
175175
return (ms, ps, imps)
176176
let df = ms_hspp_opts ms
177177
wantedModule = mkModuleName (T.unpack importName)
178-
imp <- liftMaybe $ find (isWantedModule wantedModule) imps
178+
wantedQual = mkModuleName . T.unpack <$> importQual
179+
imp <- liftMaybe $ find (isWantedModule wantedModule wantedQual) imps
179180
wedit <-
180181
liftEither $
181182
rewriteToWEdit df doc (annsA ps) $
@@ -184,10 +185,12 @@ extendImportHandler' ideState ExtendImport {..}
184185
| otherwise =
185186
mzero
186187

187-
isWantedModule :: ModuleName -> GenLocated l (ImportDecl pass) -> Bool
188-
isWantedModule wantedModule (L _ it@ImportDecl{ideclName, ideclHiding = Just (False, _)}) =
188+
isWantedModule :: ModuleName -> Maybe ModuleName -> GenLocated l (ImportDecl pass) -> Bool
189+
isWantedModule wantedModule Nothing (L _ it@ImportDecl{ideclName, ideclHiding = Just (False, _)}) =
189190
not (isQualifiedImport it) && unLoc ideclName == wantedModule
190-
isWantedModule _ _ = False
191+
isWantedModule wantedModule (Just qual) (L _ ImportDecl{ideclAs, ideclName, ideclHiding = Just (False, _)}) =
192+
unLoc ideclName == wantedModule && (wantedModule == qual || (unLoc <$> ideclAs) == Just qual)
193+
isWantedModule _ _ _ = False
191194

192195
liftMaybe :: Monad m => Maybe a -> MaybeT m a
193196
liftMaybe a = MaybeT $ pure a

ghcide/src/Development/IDE/Plugin/Completions/Logic.hs

+7
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ mkNameCompItem doc thingParent origName origMod thingType isInfix docs !imp = CI
229229
{ doc,
230230
thingParent,
231231
importName = showModName $ unLoc $ ideclName $ unLoc x,
232+
importQual = getImportQual x,
232233
newThing = showNameWithoutUniques origName
233234
}
234235

@@ -742,6 +743,7 @@ mkRecordSnippetCompItem uri parent ctxStr compl mn docs imp = r
742743
{ doc = uri,
743744
thingParent = parent,
744745
importName = showModName $ unLoc $ ideclName $ unLoc x,
746+
importQual = getImportQual x,
745747
newThing = ctxStr
746748
}
747749
}
@@ -751,3 +753,8 @@ mkRecordSnippetCompItem uri parent ctxStr compl mn docs imp = r
751753
snippet = T.intercalate (T.pack ", ") snippet_parts
752754
buildSnippet = ctxStr <> " {" <> snippet <> "}"
753755
importedFrom = Right mn
756+
757+
getImportQual :: LImportDecl GhcPs -> Maybe T.Text
758+
getImportQual (L _ imp)
759+
| isQualifiedImport imp = Just $ T.pack $ moduleNameString $ maybe (unLoc $ ideclName imp) unLoc (ideclAs imp)
760+
| otherwise = Nothing

ghcide/src/Development/IDE/Plugin/Completions/Types.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ data ExtendImport = ExtendImport
2525
{ doc :: !Uri,
2626
newThing :: !T.Text,
2727
thingParent :: !(Maybe T.Text),
28-
importName :: !T.Text
28+
importName :: !T.Text,
29+
importQual :: !(Maybe T.Text)
2930
}
3031
deriving (Eq, Show, Generic)
3132
deriving anyclass (FromJSON, ToJSON)

ghcide/test/exe/Main.hs

+30-4
Original file line numberDiff line numberDiff line change
@@ -3501,19 +3501,45 @@ nonLocalCompletionTests =
35013501
, completionCommandTest
35023502
"show imports not in list - names with _"
35033503
["{-# LANGUAGE NoImplicitPrelude #-}",
3504-
"module A where", "import qualified Control.Monad as M (msum)", "f = M.mapM_"]
3504+
"module A where", "import Control.Monad as M (msum)", "f = M.mapM_"]
35053505
(Position 3 11)
35063506
"mapM_"
35073507
["{-# LANGUAGE NoImplicitPrelude #-}",
3508-
"module A where", "import qualified Control.Monad as M (msum, mapM_)", "f = M.mapM_"]
3508+
"module A where", "import Control.Monad as M (msum, mapM_)", "f = M.mapM_"]
35093509
, completionCommandTest
35103510
"show imports not in list - initial empty list"
35113511
["{-# LANGUAGE NoImplicitPrelude #-}",
3512-
"module A where", "import qualified Control.Monad as M ()", "f = M.joi"]
3512+
"module A where", "import Control.Monad as M ()", "f = M.joi"]
35133513
(Position 3 10)
35143514
"join"
35153515
["{-# LANGUAGE NoImplicitPrelude #-}",
3516-
"module A where", "import qualified Control.Monad as M (join)", "f = M.joi"]
3516+
"module A where", "import Control.Monad as M (join)", "f = M.joi"]
3517+
, testGroup "qualified imports"
3518+
[ completionCommandTest
3519+
"single"
3520+
["{-# LANGUAGE NoImplicitPrelude #-}",
3521+
"module A where", "import Control.Monad ()", "f = Control.Monad.joi"]
3522+
(Position 3 22)
3523+
"join"
3524+
["{-# LANGUAGE NoImplicitPrelude #-}",
3525+
"module A where", "import Control.Monad (join)", "f = Control.Monad.joi"]
3526+
, completionCommandTest
3527+
"as"
3528+
["{-# LANGUAGE NoImplicitPrelude #-}",
3529+
"module A where", "import Control.Monad as M ()", "f = M.joi"]
3530+
(Position 3 10)
3531+
"join"
3532+
["{-# LANGUAGE NoImplicitPrelude #-}",
3533+
"module A where", "import Control.Monad as M (join)", "f = M.joi"]
3534+
, completionCommandTest
3535+
"multiple"
3536+
["{-# LANGUAGE NoImplicitPrelude #-}",
3537+
"module A where", "import Control.Monad as M ()", "import Control.Monad as N ()", "f = N.joi"]
3538+
(Position 4 10)
3539+
"join"
3540+
["{-# LANGUAGE NoImplicitPrelude #-}",
3541+
"module A where", "import Control.Monad as M ()", "import Control.Monad as N (join)", "f = N.joi"]
3542+
]
35173543
, testGroup "Data constructor"
35183544
[ completionCommandTest
35193545
"not imported"

0 commit comments

Comments
 (0)