Skip to content

Commit

Permalink
Handle qualified imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pepeiborra committed Jan 31, 2021
1 parent 284e6c9 commit 1085142
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
11 changes: 7 additions & 4 deletions ghcide/src/Development/IDE/Plugin/Completions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ extendImportHandler' ideState ExtendImport {..}
return (ms, ps, imps)
let df = ms_hspp_opts ms
wantedModule = mkModuleName (T.unpack importName)
imp <- liftMaybe $ find (isWantedModule wantedModule) imps
wantedQual = mkModuleName . T.unpack <$> importQual
imp <- liftMaybe $ find (isWantedModule wantedModule wantedQual) imps
wedit <-
liftEither $
rewriteToWEdit df doc (annsA ps) $
Expand All @@ -184,10 +185,12 @@ extendImportHandler' ideState ExtendImport {..}
| otherwise =
mzero

isWantedModule :: ModuleName -> GenLocated l (ImportDecl pass) -> Bool
isWantedModule wantedModule (L _ it@ImportDecl{ideclName, ideclHiding = Just (False, _)}) =
isWantedModule :: ModuleName -> Maybe ModuleName -> GenLocated l (ImportDecl pass) -> Bool
isWantedModule wantedModule Nothing (L _ it@ImportDecl{ideclName, ideclHiding = Just (False, _)}) =
not (isQualifiedImport it) && unLoc ideclName == wantedModule
isWantedModule _ _ = False
isWantedModule wantedModule (Just qual) (L _ ImportDecl{ideclAs, ideclName, ideclHiding = Just (False, _)}) =
unLoc ideclName == wantedModule && (wantedModule == qual || (unLoc <$> ideclAs) == Just qual)
isWantedModule _ _ _ = False

liftMaybe :: Monad m => Maybe a -> MaybeT m a
liftMaybe a = MaybeT $ pure a
Expand Down
7 changes: 7 additions & 0 deletions ghcide/src/Development/IDE/Plugin/Completions/Logic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ mkNameCompItem doc thingParent origName origMod thingType isInfix docs !imp = CI
{ doc,
thingParent,
importName = showModName $ unLoc $ ideclName $ unLoc x,
importQual = getImportQual x,
newThing = showNameWithoutUniques origName
}

Expand Down Expand Up @@ -742,6 +743,7 @@ mkRecordSnippetCompItem uri parent ctxStr compl mn docs imp = r
{ doc = uri,
thingParent = parent,
importName = showModName $ unLoc $ ideclName $ unLoc x,
importQual = getImportQual x,
newThing = ctxStr
}
}
Expand All @@ -751,3 +753,8 @@ mkRecordSnippetCompItem uri parent ctxStr compl mn docs imp = r
snippet = T.intercalate (T.pack ", ") snippet_parts
buildSnippet = ctxStr <> " {" <> snippet <> "}"
importedFrom = Right mn

getImportQual :: LImportDecl GhcPs -> Maybe T.Text
getImportQual (L _ imp)
| isQualifiedImport imp = Just $ T.pack $ moduleNameString $ fromMaybe (unLoc $ ideclName imp) (unLoc <$> ideclAs imp)
| otherwise = Nothing
3 changes: 2 additions & 1 deletion ghcide/src/Development/IDE/Plugin/Completions/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ data ExtendImport = ExtendImport
{ doc :: !Uri,
newThing :: !T.Text,
thingParent :: !(Maybe T.Text),
importName :: !T.Text
importName :: !T.Text,
importQual :: !(Maybe T.Text)
}
deriving (Eq, Show, Generic)
deriving anyclass (FromJSON, ToJSON)
Expand Down
34 changes: 30 additions & 4 deletions ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3501,19 +3501,45 @@ nonLocalCompletionTests =
, completionCommandTest
"show imports not in list - names with _"
["{-# LANGUAGE NoImplicitPrelude #-}",
"module A where", "import qualified Control.Monad as M (msum)", "f = M.mapM_"]
"module A where", "import Control.Monad as M (msum)", "f = M.mapM_"]
(Position 3 11)
"mapM_"
["{-# LANGUAGE NoImplicitPrelude #-}",
"module A where", "import qualified Control.Monad as M (msum, mapM_)", "f = M.mapM_"]
"module A where", "import Control.Monad as M (msum, mapM_)", "f = M.mapM_"]
, completionCommandTest
"show imports not in list - initial empty list"
["{-# LANGUAGE NoImplicitPrelude #-}",
"module A where", "import qualified Control.Monad as M ()", "f = M.joi"]
"module A where", "import Control.Monad as M ()", "f = M.joi"]
(Position 3 10)
"join"
["{-# LANGUAGE NoImplicitPrelude #-}",
"module A where", "import qualified Control.Monad as M (join)", "f = M.joi"]
"module A where", "import Control.Monad as M (join)", "f = M.joi"]
, testGroup "qualified imports"
[ completionCommandTest
"single"
["{-# LANGUAGE NoImplicitPrelude #-}",
"module A where", "import Control.Monad ()", "f = Control.Monad.joi"]
(Position 3 22)
"join"
["{-# LANGUAGE NoImplicitPrelude #-}",
"module A where", "import Control.Monad (join)", "f = Control.Monad.joi"]
, completionCommandTest
"as"
["{-# LANGUAGE NoImplicitPrelude #-}",
"module A where", "import Control.Monad as M ()", "f = M.joi"]
(Position 3 10)
"join"
["{-# LANGUAGE NoImplicitPrelude #-}",
"module A where", "import Control.Monad as M (join)", "f = M.joi"]
, completionCommandTest
"multiple"
["{-# LANGUAGE NoImplicitPrelude #-}",
"module A where", "import Control.Monad as M ()", "import Control.Monad as N ()", "f = N.joi"]
(Position 4 10)
"join"
["{-# LANGUAGE NoImplicitPrelude #-}",
"module A where", "import Control.Monad as M ()", "import Control.Monad as N (join)", "f = N.joi"]
]
, testGroup "Data constructor"
[ completionCommandTest
"not imported"
Expand Down

0 comments on commit 1085142

Please sign in to comment.