@@ -22,7 +22,7 @@ import Curry.LanguageServer.Logging
2222import Curry.LanguageServer.Utils.Conversions (ppToText )
2323import Curry.LanguageServer.Utils.General (rmDupsOn , wordAtPos , guarded )
2424import Curry.LanguageServer.Utils.Env (valueInfoType , typeInfoKind )
25- import Curry.LanguageServer.Utils.Syntax (elementAt , elementContains , HasExpressions (.. ), HasDeclarations (.. ))
25+ import Curry.LanguageServer.Utils.Syntax (elementAt , elementsAt , elementContains , HasExpressions (.. ), HasDeclarations (.. ))
2626import qualified Data.Map as M
2727import Data.Maybe (maybeToList , isJust )
2828import qualified Data.Text as T
@@ -48,7 +48,7 @@ fetchCompletions store entry content pos = do
4848 $ (takeIfNonEmpty $ expressionCompletions ast' pos)
4949 <|> (takeIfNonEmpty $ declarationCompletions ast' pos)
5050 <|> (takeIfNonEmpty $ importCompletions store ast' pos)
51- <|> (takeIfNonEmpty $ generalCompletions env query)
51+ <|> (takeIfNonEmpty $ generalCompletions env ast' pos query)
5252
5353 logs INFO $ " fetchCompletions: Found " ++ show (length completions) ++ " completions with query '" ++ show query
5454 return completions
@@ -78,8 +78,14 @@ importCompletions store ast pos = do
7878moduleCompletions :: IndexStore -> [J. CompletionItem ]
7979moduleCompletions store = moduleToCompletion <$> ((maybeToList . moduleAST) =<< snd <$> storedModules store)
8080
81- generalCompletions :: Maybe CE. CompilerEnv -> T. Text -> [J. CompletionItem ]
82- generalCompletions env query = rmDupsOn (^. J. label) $ filter (matchesQuery query) $ valueCompletions env ++ typeCompletions env ++ keywordCompletions
81+ generalCompletions :: Maybe CE. CompilerEnv -> Maybe (CS. Module a ) -> J. Position -> T. Text -> [J. CompletionItem ]
82+ generalCompletions env ast pos query = rmDupsOn (^. J. label) $ filter (matchesQuery query)
83+ $ valueCompletions env ++ localCompletions ast pos
84+ ++ typeCompletions env
85+ ++ keywordCompletions
86+
87+ localCompletions :: Maybe (CS. Module a ) -> J. Position -> [J. CompletionItem ]
88+ localCompletions ast pos = declarationToCompletions =<< (elementsAt pos $ declarations =<< maybeToList ast)
8389
8490valueCompletions :: Maybe CE. CompilerEnv -> [J. CompletionItem ]
8591valueCompletions env = valueBindingToCompletion <$> ((CT. allBindings . CE. valueEnv) =<< maybeToList env)
@@ -108,6 +114,15 @@ moduleToCompletion (CS.Module _ _ _ mid _ _ _) = item
108114 doc = Nothing
109115 item = completionFrom name ciKind detail doc
110116
117+ -- | Converts a declaration to a completion item.
118+ declarationToCompletions :: CS. Decl a -> [J. CompletionItem ]
119+ declarationToCompletions decl = (\ n -> completionFrom n J. CiVariable Nothing Nothing ) <$> names
120+ where names = T. pack <$> CI. idName <$> case decl of
121+ CS. FunctionDecl _ _ ident _ -> [ident]
122+ CS. ExternalDecl _ vars -> (\ (CS. Var _ ident) -> ident) <$> vars
123+ CS. FreeDecl _ vars -> (\ (CS. Var _ ident) -> ident) <$> vars
124+ _ -> []
125+
111126-- TODO: Reimplement the following functions in terms of bindingToQualSymbols and a conversion from SymbolInformation to CompletionItem
112127
113128-- | Converts a Curry value binding to a completion item.
0 commit comments