Skip to content

Commit d39ea23

Browse files
committed
fix(completions): project local function calls
Closes #292
1 parent 870ac1f commit d39ea23

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

lib/next_ls/autocomplete.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ defmodule NextLS.Autocomplete do
343343
container_context_map_fields(pairs, map, hint)
344344

345345
{:struct, alias, pairs} when context == :expr ->
346-
map = Map.from_struct(alias.__struct__)
346+
map = Map.from_struct(NextLS.Runtime.execute!(runtime, do: alias.__struct__))
347347
container_context_map_fields(pairs, map, hint)
348348

349349
:bitstring_modifier ->
@@ -428,7 +428,7 @@ defmodule NextLS.Autocomplete do
428428
alias = value_from_alias(aliases, runtime),
429429
true <-
430430
Keyword.keyword?(pairs) and ensure_loaded?(alias, runtime) and
431-
function_exported?(alias, :__struct__, 1) do
431+
NextLS.Runtime.execute!(runtime, do: Kernel.function_exported?(alias, :__struct__, 1)) do
432432
{:struct, alias, pairs}
433433
else
434434
_ -> nil
@@ -619,7 +619,7 @@ defmodule NextLS.Autocomplete do
619619
{"text/markdown", []}
620620
end
621621

622-
for_result =
622+
functions =
623623
for {fun, arity} <- funs,
624624
name = Atom.to_string(fun),
625625
if(exact?, do: name == hint, else: String.starts_with?(name, hint)) do
@@ -649,7 +649,7 @@ defmodule NextLS.Autocomplete do
649649
}
650650
end
651651

652-
Enum.sort_by(for_result, &{&1.name, &1.arity})
652+
Enum.sort_by(functions, &{&1.name, &1.arity})
653653
end
654654

655655
defp match_map_fields(map, hint) do

test/next_ls/completions_test.exs

+12-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ defmodule NextLS.CompletionsTest do
2222
end
2323
""")
2424

25+
bar = Path.join(cwd, "my_proj/lib/bar.ex")
26+
27+
File.write!(bar, """
28+
defmodule Bar do
29+
defstruct [:one, :two, :three]
30+
end
31+
""")
32+
2533
[foo: foo, cwd: cwd]
2634
end
2735

@@ -185,7 +193,7 @@ defmodule NextLS.CompletionsTest do
185193
text: """
186194
defmodule Foo do
187195
def run() do
188-
IO.inspect([%URI{])
196+
IO.inspect([%Bar{])
189197
:ok
190198
end
191199
end
@@ -210,14 +218,9 @@ defmodule NextLS.CompletionsTest do
210218
}
211219

212220
assert_result 2, [
213-
%{"data" => nil, "documentation" => "", "insertText" => "port", "kind" => 5, "label" => "port"},
214-
%{"data" => nil, "documentation" => "", "insertText" => "scheme", "kind" => 5, "label" => "scheme"},
215-
%{"data" => nil, "documentation" => "", "insertText" => "path", "kind" => 5, "label" => "path"},
216-
%{"data" => nil, "documentation" => "", "insertText" => "host", "kind" => 5, "label" => "host"},
217-
%{"data" => nil, "documentation" => "", "insertText" => "userinfo", "kind" => 5, "label" => "userinfo"},
218-
%{"data" => nil, "documentation" => "", "insertText" => "fragment", "kind" => 5, "label" => "fragment"},
219-
%{"data" => nil, "documentation" => "", "insertText" => "query", "kind" => 5, "label" => "query"},
220-
%{"data" => nil, "documentation" => "", "insertText" => "authority", "kind" => 5, "label" => "authority"}
221+
%{"data" => nil, "documentation" => "", "insertText" => "one", "kind" => 5, "label" => "one"},
222+
%{"data" => nil, "documentation" => "", "insertText" => "two", "kind" => 5, "label" => "two"},
223+
%{"data" => nil, "documentation" => "", "insertText" => "three", "kind" => 5, "label" => "three"}
221224
]
222225
end
223226

0 commit comments

Comments
 (0)