Skip to content

Commit

Permalink
don't stringify complex types when building parameter list
Browse files Browse the repository at this point in the history
Fixes #279
  • Loading branch information
lukaszsamson committed Sep 26, 2023
1 parent e8a7003 commit 2c4b352
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/elixir_sense/core/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ defmodule ElixirSense.Core.Introspection do

results =
for {{_name, _arity}, [params | _]} = spec <- specs do
params = TypeInfo.extract_params(params) |> Enum.map(&Atom.to_string/1)
params = TypeInfo.extract_params(params)

%{
name: Atom.to_string(fun),
Expand Down Expand Up @@ -527,7 +527,7 @@ defmodule ElixirSense.Core.Introspection do

results =
for {{_name, arity}, [params | _]} <- specs do
fun_args_text = TypeInfo.extract_params(params) |> Enum.map_join(", ", &Atom.to_string/1)
fun_args_text = TypeInfo.extract_params(params) |> Enum.join(", ")

%{
module: mod,
Expand Down Expand Up @@ -1846,7 +1846,7 @@ defmodule ElixirSense.Core.Introspection do
# we don't expect macros here
case TypeInfo.get_callback(behaviour, f, arity) do
{_, [params | _]} ->
TypeInfo.extract_params(params) |> Enum.map(&Atom.to_string/1)
TypeInfo.extract_params(params)

_ ->
# provide dummy
Expand All @@ -1860,11 +1860,11 @@ defmodule ElixirSense.Core.Introspection do

[{:attribute, _, :spec, {{^f, ^arity}, [params | _]}}] ->
# erlang doc with signature meta - moduleless form
TypeInfo.extract_params(params) |> Enum.map(&Atom.to_string/1)
TypeInfo.extract_params(params)

[{:attribute, _, :spec, {{^mod, ^f, ^arity}, [params | _]}}] ->
# erlang doc with signature meta - form with module
TypeInfo.extract_params(params) |> Enum.map(&Atom.to_string/1)
TypeInfo.extract_params(params)
end
end
end
16 changes: 11 additions & 5 deletions lib/elixir_sense/core/type_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -685,17 +685,23 @@ defmodule ElixirSense.Core.TypeInfo do
end

def extract_params(type) do
case Typespec.spec_to_quoted(:dummy, type) do
quoted = Typespec.spec_to_quoted(:dummy, type)

case quoted do
{:when, _, [{:"::", _, [{:dummy, _, args}, _res]}, _var_args]} -> args
{:"::", _, [{:dummy, _, args}, _res]} -> args
end
|> Enum.map(fn arg ->
case arg do
{:"::", _, [left, _right]} -> left
other -> other
{:"::", _, [{atom, _, _}, _right]} when is_atom(atom) -> to_string(atom)
{:|, _, [_left, _right]} -> "term"
[_ | _] -> "list"
{_, _} -> "tuple"
{:{}, _, _} -> "tuple"
%{} -> "map"
{atom, _, _} when is_atom(atom) -> to_string(atom)
_other -> "term"
end
|> Macro.to_string()
|> String.to_atom()
end)
end

Expand Down
1 change: 0 additions & 1 deletion lib/elixir_sense/providers/suggestion/complete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,6 @@ defmodule ElixirSense.Providers.Suggestion.Complete do

defp format_params({{_name, _arity}, [params | _]}, _arity_1) do
TypeInfo.extract_params(params)
|> Enum.map(&Atom.to_string/1)
end

defp format_params(nil, 0), do: []
Expand Down
21 changes: 19 additions & 2 deletions test/elixir_sense/providers/suggestion/complete_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ defmodule ElixirSense.Providers.Suggestion.CompleteTest do
name: "or",
spec: "@spec boolean() or boolean() :: boolean()",
type: :function,
args: "boolean(), boolean()",
args: "boolean, boolean",
origin: ":erlang",
summary: ""
},
Expand All @@ -1964,7 +1964,7 @@ defmodule ElixirSense.Providers.Suggestion.CompleteTest do
name: "and",
spec: "@spec boolean() and boolean() :: boolean()",
type: :function,
args: "boolean(), boolean()",
args: "boolean, boolean",
origin: ":erlang",
summary: ""
},
Expand Down Expand Up @@ -2020,6 +2020,23 @@ defmodule ElixirSense.Providers.Suggestion.CompleteTest do
end
end

test "provide doc and specs for erlang functions with args from typespec" do

Check failure on line 2023 in test/elixir_sense/providers/suggestion/complete_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.13.x | Erlang/OTP 22.x)

test provide doc and specs for erlang functions with args from typespec (ElixirSense.Providers.Suggestion.CompleteTest)

Check failure on line 2023 in test/elixir_sense/providers/suggestion/complete_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.14.x | Erlang/OTP 25.x)

test provide doc and specs for erlang functions with args from typespec (ElixirSense.Providers.Suggestion.CompleteTest)

Check failure on line 2023 in test/elixir_sense/providers/suggestion/complete_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.13.x | Erlang/OTP 25.x)

test provide doc and specs for erlang functions with args from typespec (ElixirSense.Providers.Suggestion.CompleteTest)

Check failure on line 2023 in test/elixir_sense/providers/suggestion/complete_test.exs

View workflow job for this annotation

GitHub Actions / mix test (Elixir 1.15.x | Erlang/OTP 25.x)

test provide doc and specs for erlang functions with args from typespec (ElixirSense.Providers.Suggestion.CompleteTest)

Check failure on line 2023 in test/elixir_sense/providers/suggestion/complete_test.exs

View workflow job for this annotation

GitHub Actions / mix test windows (Elixir 1.13.x | Erlang/OTP 25.x)

test provide doc and specs for erlang functions with args from typespec (ElixirSense.Providers.Suggestion.CompleteTest)

Check failure on line 2023 in test/elixir_sense/providers/suggestion/complete_test.exs

View workflow job for this annotation

GitHub Actions / mix test windows (Elixir 1.13.x | Erlang/OTP 22.x)

test provide doc and specs for erlang functions with args from typespec (ElixirSense.Providers.Suggestion.CompleteTest)

Check failure on line 2023 in test/elixir_sense/providers/suggestion/complete_test.exs

View workflow job for this annotation

GitHub Actions / mix test windows (Elixir 1.15.x | Erlang/OTP 25.x)

test provide doc and specs for erlang functions with args from typespec (ElixirSense.Providers.Suggestion.CompleteTest)

Check failure on line 2023 in test/elixir_sense/providers/suggestion/complete_test.exs

View workflow job for this annotation

GitHub Actions / mix test windows (Elixir 1.14.x | Erlang/OTP 25.x)

test provide doc and specs for erlang functions with args from typespec (ElixirSense.Providers.Suggestion.CompleteTest)
assert [
%{
name: "handle_call",
args_list: ["call", "from", "state"]
},
%{
name: "handle_cast",
args_list: ["tuple", "state"]
},
%{
name: "handle_info",
args_list: ["term", "state"]
}
] = expand(~c":pg.handle_")

This comment has been minimized.

Copy link
@VictorGaiva

VictorGaiva Sep 27, 2023

end

test "complete after ! operator" do
assert [%{name: "is_binary"}] = expand(~c"!is_bina")
end
Expand Down

0 comments on commit 2c4b352

Please sign in to comment.