Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: autocomplete for kernel functions #373

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/next_ls/autocomplete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,11 @@ defmodule NextLS.Autocomplete do

defp match_local(hint, exact?, runtime) do
imports = runtime |> imports_from_env() |> Enum.flat_map(&elem(&1, 1))
module_funs = get_module_funs(Kernel.SpecialForms, runtime)
special_form_funs = get_module_funs(Kernel.SpecialForms, runtime)
kernel_funs = get_module_funs(Kernel, runtime)

match_module_funs(runtime, Kernel.SpecialForms, module_funs, hint, exact?) ++
match_module_funs(runtime, Kernel.SpecialForms, special_form_funs, hint, exact?) ++
match_module_funs(runtime, Kernel, kernel_funs, hint, exact?) ++
match_module_funs(runtime, nil, imports, hint, exact?)
end

Expand Down
53 changes: 35 additions & 18 deletions test/next_ls/autocomplete_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,33 @@ defmodule NextLS.AutocompleteTest do
]} = expand(runtime, ~c"Enum.count/")
end

# TODO: locals
test "operator completion", %{runtime: runtime} do
assert {:yes,
[
%{arity: 1, name: "+", docs: _, kind: :function},
%{arity: 2, name: "+", docs: _, kind: :function},
%{arity: 2, name: "++", docs: _, kind: :function}
]} = expand(runtime, ~c"+")

# test "operator completion", %{runtime: runtime} do
# assert expand(runtime, ~c"+") == {:yes, ~c"", [~c"+/1", ~c"+/2", ~c"++/2"]}
# assert expand(runtime, ~c"+/") == {:yes, ~c"", [~c"+/1", ~c"+/2"]}
# assert expand(runtime, ~c"++/") == {:yes, ~c"", [~c"++/2"]}
# end
assert {:yes,
[
%{arity: 1, name: "+", docs: _, kind: :function},
%{arity: 2, name: "+", docs: _, kind: :function}
]} = expand(runtime, ~c"+/")

# test "sigil completion", %{runtime: runtime} do
# assert {:yes, ~c"", sigils} = expand(runtime, ~c"~")
# assert ~c"~C (sigil_C)" in sigils
# assert {:yes, ~c"", sigils} = expand(runtime, ~c"~r")
# assert ~c"\"" in sigils
# assert ~c"(" in sigils
# end
assert {:yes,
[
%{arity: 2, name: "++", docs: _, kind: :function}
]} = expand(runtime, ~c"++/")
end

test "sigil completion", %{runtime: runtime} do
assert {:yes, sigils} = expand(runtime, ~c"~")
assert %{name: "C", kind: :sigil} in sigils
assert {:yes, ~c"", sigils} = expand(runtime, ~c"~r")
assert ~c"\"" in sigils
assert ~c"(" in sigils
end

# TODO: maps
# test "map atom key completion is supported", %{runtime: runtime} do
Expand Down Expand Up @@ -391,11 +403,16 @@ defmodule NextLS.AutocompleteTest do
end)
end

# TODO: locals
# test "kernel import completion", %{runtime: runtime} do
# assert expand(runtime, ~c"defstru") == {:yes, ~c"ct", []}
# assert expand(runtime, ~c"put_") == {:yes, ~c"", [~c"put_elem/3", ~c"put_in/2", ~c"put_in/3"]}
# end
test "kernel import completion", %{runtime: runtime} do
assert {:yes, [%{name: "defstruct", kind: :function, docs: _, arity: 1}]} = expand(runtime, ~c"defstru")

assert {:yes,
[
%{arity: 3, name: "put_elem", docs: _, kind: :function},
%{arity: 2, name: "put_in", docs: _, kind: :function},
%{arity: 3, name: "put_in", docs: _, kind: :function}
]} = expand(runtime, ~c"put_")
end

# TODO: this only partially works, will not say we support for now
# test "variable name completion", %{runtime: runtime} do
Expand Down
Loading