Skip to content

Commit

Permalink
enable completions of local imports (#2190)
Browse files Browse the repository at this point in the history
* enable completions of local imports

* added unit test

* use HM.keys

* rename test

* Revert "enable completions of local imports"

This reverts commit a52894f.

* use GetKnownTargets

* clean up

* clean up

* leverage CachedCompletions over argument

* clean up

* clean

Co-authored-by: Javier Neira <[email protected]>
Co-authored-by: alexnaspoleap <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 17, 2021
1 parent 3bfb9b3 commit 6fed490
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
12 changes: 10 additions & 2 deletions ghcide/src/Development/IDE/Plugin/Completions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Development.IDE.GHC.ExactPrint (Annotated (annsA)
import Development.IDE.GHC.Util (prettyPrint)
import Development.IDE.Graph
import Development.IDE.Graph.Classes
import qualified Development.IDE.Types.KnownTargets as KT
import Development.IDE.Plugin.CodeAction (newImport,
newImportToEdit)
import Development.IDE.Plugin.CodeAction.ExactPrint
Expand Down Expand Up @@ -132,7 +133,9 @@ getCompletionsLSP ide plId
nonLocalCompls <- useWithStaleFast NonLocalCompletions npath
pm <- useWithStaleFast GetParsedModule npath
binds <- fromMaybe (mempty, zeroMapping) <$> useWithStaleFast GetBindings npath

knownTargets <- liftIO $ runAction "Completion" ide $ useNoFile GetKnownTargets
let localModules = maybe [] Map.keys knownTargets
let lModules = mempty{importableModules = map toModueNameText localModules}
-- set up the exports map including both package and project-level identifiers
packageExportsMapIO <- fmap(envPackageExports . fst) <$> useWithStaleFast GhcSession npath
packageExportsMap <- mapM liftIO packageExportsMapIO
Expand All @@ -142,7 +145,7 @@ getCompletionsLSP ide plId
let moduleExports = getModuleExportsMap exportsMap
exportsCompItems = foldMap (map (fromIdentInfo uri) . Set.toList) . Map.elems . getExportsMap $ exportsMap
exportsCompls = mempty{anyQualCompls = exportsCompItems}
let compls = (fst <$> localCompls) <> (fst <$> nonLocalCompls) <> Just exportsCompls
let compls = (fst <$> localCompls) <> (fst <$> nonLocalCompls) <> Just exportsCompls <> Just lModules

pure (opts, fmap (,pm,binds) compls, moduleExports)
case compls of
Expand All @@ -162,6 +165,11 @@ getCompletionsLSP ide plId

----------------------------------------------------------------------------------------------------

toModueNameText :: KT.Target -> T.Text
toModueNameText target = case target of
KT.TargetModule m -> T.pack $ moduleNameString m
_ -> T.empty

extendImportCommand :: PluginCommand IdeState
extendImportCommand =
PluginCommand (CommandId extendImportCommandId) "additional edits for a completion" extendImportHandler
Expand Down
20 changes: 19 additions & 1 deletion ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import Development.IDE.Test (Cursor,
import Development.IDE.Test.Runfiles
import qualified Development.IDE.Types.Diagnostics as Diagnostics
import Development.IDE.Types.Location
import qualified Language.LSP.Types.Lens as Lens (label)
import Development.Shake (getDirectoryFilesIO)
import qualified Experiments as Bench
import Ide.Plugin.Config
Expand Down Expand Up @@ -4589,7 +4590,24 @@ projectCompletionTests =
<- compls
, _label == "anidentifier"
]
liftIO $ compls' @?= ["Defined in 'A"]
liftIO $ compls' @?= ["Defined in 'A"],
testSession' "auto complete project imports" $ \dir-> do
liftIO $ writeFile (dir </> "hie.yaml")
"cradle: {direct: {arguments: [\"-Wmissing-signatures\", \"ALocalModule\", \"B\"]}}"
_ <- createDoc "ALocalModule.hs" "haskell" $ T.unlines
[ "module ALocalModule (anidentifier) where",
"anidentifier = ()"
]
_ <- waitForDiagnostics
-- Note that B does not import A
doc <- createDoc "B.hs" "haskell" $ T.unlines
[ "module B where",
"import ALocal"
]
compls <- getCompletions doc (Position 1 13)
let item = head $ filter ((== "ALocalModule") . (^. Lens.label)) compls
liftIO $ do
item ^. Lens.label @?= "ALocalModule"
]

highlightTests :: TestTree
Expand Down

0 comments on commit 6fed490

Please sign in to comment.