Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

Commit ff1b89d

Browse files
authored
feat: autocomplete for kernel functions (#373)
1 parent 8b9a57c commit ff1b89d

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

lib/next_ls/autocomplete.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,11 @@ defmodule NextLS.Autocomplete do
278278

279279
defp match_local(hint, exact?, runtime) do
280280
imports = runtime |> imports_from_env() |> Enum.flat_map(&elem(&1, 1))
281-
module_funs = get_module_funs(Kernel.SpecialForms, runtime)
281+
special_form_funs = get_module_funs(Kernel.SpecialForms, runtime)
282+
kernel_funs = get_module_funs(Kernel, runtime)
282283

283-
match_module_funs(runtime, Kernel.SpecialForms, module_funs, hint, exact?) ++
284+
match_module_funs(runtime, Kernel.SpecialForms, special_form_funs, hint, exact?) ++
285+
match_module_funs(runtime, Kernel, kernel_funs, hint, exact?) ++
284286
match_module_funs(runtime, nil, imports, hint, exact?)
285287
end
286288

test/next_ls/autocomplete_test.exs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,21 +295,33 @@ defmodule NextLS.AutocompleteTest do
295295
]} = expand(runtime, ~c"Enum.count/")
296296
end
297297

298-
# TODO: locals
298+
test "operator completion", %{runtime: runtime} do
299+
assert {:yes,
300+
[
301+
%{arity: 1, name: "+", docs: _, kind: :function},
302+
%{arity: 2, name: "+", docs: _, kind: :function},
303+
%{arity: 2, name: "++", docs: _, kind: :function}
304+
]} = expand(runtime, ~c"+")
299305

300-
# test "operator completion", %{runtime: runtime} do
301-
# assert expand(runtime, ~c"+") == {:yes, ~c"", [~c"+/1", ~c"+/2", ~c"++/2"]}
302-
# assert expand(runtime, ~c"+/") == {:yes, ~c"", [~c"+/1", ~c"+/2"]}
303-
# assert expand(runtime, ~c"++/") == {:yes, ~c"", [~c"++/2"]}
304-
# end
306+
assert {:yes,
307+
[
308+
%{arity: 1, name: "+", docs: _, kind: :function},
309+
%{arity: 2, name: "+", docs: _, kind: :function}
310+
]} = expand(runtime, ~c"+/")
305311

306-
# test "sigil completion", %{runtime: runtime} do
307-
# assert {:yes, ~c"", sigils} = expand(runtime, ~c"~")
308-
# assert ~c"~C (sigil_C)" in sigils
309-
# assert {:yes, ~c"", sigils} = expand(runtime, ~c"~r")
310-
# assert ~c"\"" in sigils
311-
# assert ~c"(" in sigils
312-
# end
312+
assert {:yes,
313+
[
314+
%{arity: 2, name: "++", docs: _, kind: :function}
315+
]} = expand(runtime, ~c"++/")
316+
end
317+
318+
test "sigil completion", %{runtime: runtime} do
319+
assert {:yes, sigils} = expand(runtime, ~c"~")
320+
assert %{name: "C", kind: :sigil} in sigils
321+
assert {:yes, ~c"", sigils} = expand(runtime, ~c"~r")
322+
assert ~c"\"" in sigils
323+
assert ~c"(" in sigils
324+
end
313325

314326
# TODO: maps
315327
# test "map atom key completion is supported", %{runtime: runtime} do
@@ -391,11 +403,16 @@ defmodule NextLS.AutocompleteTest do
391403
end)
392404
end
393405

394-
# TODO: locals
395-
# test "kernel import completion", %{runtime: runtime} do
396-
# assert expand(runtime, ~c"defstru") == {:yes, ~c"ct", []}
397-
# assert expand(runtime, ~c"put_") == {:yes, ~c"", [~c"put_elem/3", ~c"put_in/2", ~c"put_in/3"]}
398-
# end
406+
test "kernel import completion", %{runtime: runtime} do
407+
assert {:yes, [%{name: "defstruct", kind: :function, docs: _, arity: 1}]} = expand(runtime, ~c"defstru")
408+
409+
assert {:yes,
410+
[
411+
%{arity: 3, name: "put_elem", docs: _, kind: :function},
412+
%{arity: 2, name: "put_in", docs: _, kind: :function},
413+
%{arity: 3, name: "put_in", docs: _, kind: :function}
414+
]} = expand(runtime, ~c"put_")
415+
end
399416

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

0 commit comments

Comments
 (0)