Skip to content

Commit b3a94c5

Browse files
committed
Handle qualified imports
1 parent 25935cd commit b3a94c5

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

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

Lines changed: 7 additions & 4 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3607,19 +3607,45 @@ nonLocalCompletionTests =
36073607
, completionCommandTest
36083608
"show imports not in list - names with _"
36093609
["{-# LANGUAGE NoImplicitPrelude #-}",
3610-
"module A where", "import qualified Control.Monad as M (msum)", "f = M.mapM_"]
3610+
"module A where", "import Control.Monad as M (msum)", "f = M.mapM_"]
36113611
(Position 3 11)
36123612
"mapM_"
36133613
["{-# LANGUAGE NoImplicitPrelude #-}",
3614-
"module A where", "import qualified Control.Monad as M (msum, mapM_)", "f = M.mapM_"]
3614+
"module A where", "import Control.Monad as M (msum, mapM_)", "f = M.mapM_"]
36153615
, completionCommandTest
36163616
"show imports not in list - initial empty list"
36173617
["{-# LANGUAGE NoImplicitPrelude #-}",
3618-
"module A where", "import qualified Control.Monad as M ()", "f = M.joi"]
3618+
"module A where", "import Control.Monad as M ()", "f = M.joi"]
36193619
(Position 3 10)
36203620
"join"
36213621
["{-# LANGUAGE NoImplicitPrelude #-}",
3622-
"module A where", "import qualified Control.Monad as M (join)", "f = M.joi"]
3622+
"module A where", "import Control.Monad as M (join)", "f = M.joi"]
3623+
, testGroup "qualified imports"
3624+
[ completionCommandTest
3625+
"single"
3626+
["{-# LANGUAGE NoImplicitPrelude #-}",
3627+
"module A where", "import Control.Monad ()", "f = Control.Monad.joi"]
3628+
(Position 3 22)
3629+
"join"
3630+
["{-# LANGUAGE NoImplicitPrelude #-}",
3631+
"module A where", "import Control.Monad (join)", "f = Control.Monad.joi"]
3632+
, completionCommandTest
3633+
"as"
3634+
["{-# LANGUAGE NoImplicitPrelude #-}",
3635+
"module A where", "import Control.Monad as M ()", "f = M.joi"]
3636+
(Position 3 10)
3637+
"join"
3638+
["{-# LANGUAGE NoImplicitPrelude #-}",
3639+
"module A where", "import Control.Monad as M (join)", "f = M.joi"]
3640+
, completionCommandTest
3641+
"multiple"
3642+
["{-# LANGUAGE NoImplicitPrelude #-}",
3643+
"module A where", "import Control.Monad as M ()", "import Control.Monad as N ()", "f = N.joi"]
3644+
(Position 4 10)
3645+
"join"
3646+
["{-# LANGUAGE NoImplicitPrelude #-}",
3647+
"module A where", "import Control.Monad as M ()", "import Control.Monad as N (join)", "f = N.joi"]
3648+
]
36233649
, testGroup "Data constructor"
36243650
[ completionCommandTest
36253651
"not imported"

0 commit comments

Comments
 (0)