Skip to content

Commit

Permalink
fix(completions): project local function calls
Browse files Browse the repository at this point in the history
Closes #292
  • Loading branch information
mhanberg committed Oct 19, 2023
1 parent 870ac1f commit d39ea23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions lib/next_ls/autocomplete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ defmodule NextLS.Autocomplete do
container_context_map_fields(pairs, map, hint)

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

:bitstring_modifier ->
Expand Down Expand Up @@ -428,7 +428,7 @@ defmodule NextLS.Autocomplete do
alias = value_from_alias(aliases, runtime),
true <-
Keyword.keyword?(pairs) and ensure_loaded?(alias, runtime) and
function_exported?(alias, :__struct__, 1) do
NextLS.Runtime.execute!(runtime, do: Kernel.function_exported?(alias, :__struct__, 1)) do
{:struct, alias, pairs}
else
_ -> nil
Expand Down Expand Up @@ -619,7 +619,7 @@ defmodule NextLS.Autocomplete do
{"text/markdown", []}
end

for_result =
functions =
for {fun, arity} <- funs,
name = Atom.to_string(fun),
if(exact?, do: name == hint, else: String.starts_with?(name, hint)) do
Expand Down Expand Up @@ -649,7 +649,7 @@ defmodule NextLS.Autocomplete do
}
end

Enum.sort_by(for_result, &{&1.name, &1.arity})
Enum.sort_by(functions, &{&1.name, &1.arity})
end

defp match_map_fields(map, hint) do
Expand Down
21 changes: 12 additions & 9 deletions test/next_ls/completions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ defmodule NextLS.CompletionsTest do
end
""")

bar = Path.join(cwd, "my_proj/lib/bar.ex")

File.write!(bar, """
defmodule Bar do
defstruct [:one, :two, :three]
end
""")

[foo: foo, cwd: cwd]
end

Expand Down Expand Up @@ -185,7 +193,7 @@ defmodule NextLS.CompletionsTest do
text: """
defmodule Foo do
def run() do
IO.inspect([%URI{])
IO.inspect([%Bar{])
:ok
end
end
Expand All @@ -210,14 +218,9 @@ defmodule NextLS.CompletionsTest do
}

assert_result 2, [
%{"data" => nil, "documentation" => "", "insertText" => "port", "kind" => 5, "label" => "port"},
%{"data" => nil, "documentation" => "", "insertText" => "scheme", "kind" => 5, "label" => "scheme"},
%{"data" => nil, "documentation" => "", "insertText" => "path", "kind" => 5, "label" => "path"},
%{"data" => nil, "documentation" => "", "insertText" => "host", "kind" => 5, "label" => "host"},
%{"data" => nil, "documentation" => "", "insertText" => "userinfo", "kind" => 5, "label" => "userinfo"},
%{"data" => nil, "documentation" => "", "insertText" => "fragment", "kind" => 5, "label" => "fragment"},
%{"data" => nil, "documentation" => "", "insertText" => "query", "kind" => 5, "label" => "query"},
%{"data" => nil, "documentation" => "", "insertText" => "authority", "kind" => 5, "label" => "authority"}
%{"data" => nil, "documentation" => "", "insertText" => "one", "kind" => 5, "label" => "one"},
%{"data" => nil, "documentation" => "", "insertText" => "two", "kind" => 5, "label" => "two"},
%{"data" => nil, "documentation" => "", "insertText" => "three", "kind" => 5, "label" => "three"}
]
end

Expand Down

0 comments on commit d39ea23

Please sign in to comment.