Skip to content

Commit 4538ac1

Browse files
committed
WIP: Generate completions from local declarations
1 parent 1198e53 commit 4538ac1

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

src/Curry/LanguageServer/Features/Completion.hs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Curry.LanguageServer.Logging
2222
import Curry.LanguageServer.Utils.Conversions (ppToText)
2323
import Curry.LanguageServer.Utils.General (rmDupsOn, wordAtPos, guarded)
2424
import 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 (..))
2626
import qualified Data.Map as M
2727
import Data.Maybe (maybeToList, isJust)
2828
import 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
7878
moduleCompletions :: IndexStore -> [J.CompletionItem]
7979
moduleCompletions 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

8490
valueCompletions :: Maybe CE.CompilerEnv -> [J.CompletionItem]
8591
valueCompletions 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.

src/Curry/LanguageServer/Utils/Syntax.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Curry.LanguageServer.Utils.Syntax (
88
HasIdentifier (..),
99
ModuleAST,
1010
elementAt,
11+
elementsAt,
1112
elementContains,
1213
moduleIdentifier
1314
) where
@@ -24,9 +25,13 @@ import qualified Language.Haskell.LSP.Types as J
2425

2526
type ModuleAST = CS.Module CT.PredType
2627

27-
-- | Fetches the element at the given position.
28+
-- | Fetches the innermost element at the given position.
2829
elementAt :: CSPI.HasSpanInfo e => J.Position -> [e] -> Maybe e
29-
elementAt pos = lastSafe . filter (elementContains pos)
30+
elementAt pos = lastSafe . elementsAt pos
31+
32+
-- | Fetches the elements at the given position.
33+
elementsAt :: CSPI.HasSpanInfo e => J.Position -> [e] -> [e]
34+
elementsAt pos = filter (elementContains pos)
3035

3136
-- | Tests whether the given element in the AST contains the given position.
3237
elementContains :: CSPI.HasSpanInfo e => J.Position -> e -> Bool

test/resources/Test.curry

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Test where
22

33
f x = case x of
4-
_ -> 3
4+
_ -> let y = 4 in f y
55
_ -> 4

0 commit comments

Comments
 (0)