Skip to content

Commit

Permalink
Fix import suggestions when dot is typed (haskell/ghcide#800)
Browse files Browse the repository at this point in the history
* Fix module suggestions

* Document PositionMapping

* Remove maybe
  • Loading branch information
lazamar committed Sep 17, 2020
1 parent 97bb599 commit c975590
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
6 changes: 2 additions & 4 deletions ghcide/src/Development/IDE/Plugin/Completions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Development.IDE.Plugin.Completions.Logic
import Development.IDE.Types.Location
import Development.IDE.Types.Options
import Development.IDE.Core.Compile
import Development.IDE.Core.PositionMapping
import Development.IDE.Core.RuleTypes
import Development.IDE.Core.Shake
import Development.IDE.GHC.Compat (hsmodExports, ParsedModule(..), ModSummary (ms_hspp_buf))
Expand Down Expand Up @@ -150,15 +149,14 @@ getCompletionsLSP lsp ide
pure (opts, liftA2 (,) compls pm)
case compls of
Just ((cci', _), (pm, mapping)) -> do
let !position' = fromCurrentPosition mapping position
pfix <- maybe (return Nothing) (flip VFS.getCompletionPrefix cnts) position'
pfix <- VFS.getCompletionPrefix position cnts
case (pfix, completionContext) of
(Just (VFS.PosPrefixInfo _ "" _ _), Just CompletionContext { _triggerCharacter = Just "."})
-> return (Completions $ List [])
(Just pfix', _) -> do
-- TODO pass the real capabilities here (or remove the logic for snippets)
let fakeClientCapabilities = ClientCapabilities Nothing Nothing Nothing Nothing
Completions . List <$> getCompletions ideOpts cci' pm pfix' fakeClientCapabilities (WithSnippets True)
Completions . List <$> getCompletions ideOpts cci' pm mapping pfix' fakeClientCapabilities (WithSnippets True)
_ -> return (Completions $ List [])
_ -> return (Completions $ List [])
_ -> return (Completions $ List [])
Expand Down
23 changes: 18 additions & 5 deletions ghcide/src/Development/IDE/Plugin/Completions/Logic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Language.Haskell.LSP.Types
import Language.Haskell.LSP.Types.Capabilities
import qualified Language.Haskell.LSP.VFS as VFS
import Development.IDE.Core.Compile
import Development.IDE.Core.PositionMapping
import Development.IDE.Plugin.Completions.Types
import Development.IDE.Spans.Documentation
import Development.IDE.GHC.Compat as GHC
Expand Down Expand Up @@ -371,10 +372,18 @@ toggleSnippets ClientCapabilities { _textDocument } (WithSnippets with) x
where supported = Just True == (_textDocument >>= _completion >>= _completionItem >>= _snippetSupport)

-- | Returns the cached completions for the given module and position.
getCompletions :: IdeOptions -> CachedCompletions -> ParsedModule -> VFS.PosPrefixInfo -> ClientCapabilities -> WithSnippets -> IO [CompletionItem]
getCompletions ideOpts CC { allModNamesAsNS, unqualCompls, qualCompls, importableModules }
pm prefixInfo caps withSnippets = do
let VFS.PosPrefixInfo { VFS.fullLine, VFS.prefixModule, VFS.prefixText } = prefixInfo
getCompletions
:: IdeOptions
-> CachedCompletions
-> ParsedModule
-> PositionMapping -- ^ map current position to position in parsed module
-> VFS.PosPrefixInfo
-> ClientCapabilities
-> WithSnippets
-> IO [CompletionItem]
getCompletions ideOpts cc pm pmapping prefixInfo caps withSnippets = do
let CC { allModNamesAsNS, unqualCompls, qualCompls, importableModules } = cc
VFS.PosPrefixInfo { VFS.fullLine, VFS.prefixModule, VFS.prefixText } = prefixInfo
enteredQual = if T.null prefixModule then "" else prefixModule <> "."
fullPrefix = enteredQual <> prefixText

Expand Down Expand Up @@ -404,8 +413,12 @@ getCompletions ideOpts CC { allModNamesAsNS, unqualCompls, qualCompls, importabl

filtCompls = map Fuzzy.original $ Fuzzy.filter prefixText ctxCompls "" "" label False
where
mcc = do
position' <- fromCurrentPosition pmapping pos
getCContext position' pm

-- completions specific to the current context
ctxCompls' = case getCContext pos pm of
ctxCompls' = case mcc of
Nothing -> compls
Just TypeContext -> filter isTypeCompl compls
Just ValueContext -> filter (not . isTypeCompl) compls
Expand Down

0 comments on commit c975590

Please sign in to comment.