From 7b99c2835851c78c0986a0f8799dc751ddb22be5 Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Sun, 11 Dec 2022 21:55:26 +0100 Subject: [PATCH] return type arg list in completions --- .../suggestion/reducers/type_specs.ex | 27 +++++++++++++++++-- test/elixir_sense/suggestions_test.exs | 9 +++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/elixir_sense/providers/suggestion/reducers/type_specs.ex b/lib/elixir_sense/providers/suggestion/reducers/type_specs.ex index 7e73accf..39fc966b 100644 --- a/lib/elixir_sense/providers/suggestion/reducers/type_specs.ex +++ b/lib/elixir_sense/providers/suggestion/reducers/type_specs.ex @@ -13,7 +13,8 @@ defmodule ElixirSense.Providers.Suggestion.Reducers.TypeSpecs do type: :type_spec, name: String.t(), arity: non_neg_integer, - origin: String.t(), + origin: String.t() | nil, + args_list: list(String.t()), spec: String.t(), doc: String.t(), signature: String.t(), @@ -117,7 +118,7 @@ defmodule ElixirSense.Providers.Suggestion.Reducers.TypeSpecs do end defp type_info_to_suggestion(type_info, module) do - origin = if module, do: inspect(module), else: "" + origin = if module, do: inspect(module) case type_info do %ElixirSense.Core.State.TypeInfo{args: [args]} -> @@ -127,6 +128,7 @@ defmodule ElixirSense.Providers.Suggestion.Reducers.TypeSpecs do type: :type_spec, name: type_info.name |> Atom.to_string(), arity: length(args), + args_list: args, signature: "#{type_info.name}(#{args_stringified})", origin: origin, doc: "", @@ -136,10 +138,31 @@ defmodule ElixirSense.Providers.Suggestion.Reducers.TypeSpecs do } _ -> + args_list = + if type_info.signature do + part = + type_info.signature + |> String.split("(") + |> Enum.at(1) + + if part do + part + |> String.split(")") + |> Enum.at(0) + |> String.split(",") + |> Enum.map(&String.trim/1) + else + [] + end + else + [] + end + %{ type: :type_spec, name: type_info.name |> Atom.to_string(), arity: type_info.arity, + args_list: args_list, signature: type_info.signature, origin: origin, doc: type_info.doc, diff --git a/test/elixir_sense/suggestions_test.exs b/test/elixir_sense/suggestions_test.exs index a82058ba..e49e4e5a 100644 --- a/test/elixir_sense/suggestions_test.exs +++ b/test/elixir_sense/suggestions_test.exs @@ -3092,7 +3092,7 @@ defmodule ElixirSense.SuggestionsTest do assert suggestion.signature == "list()" assert suggestion.arity == 0 assert suggestion.doc == "A list" - assert suggestion.origin == "" + assert suggestion.origin == nil end test "builtin types - retrieve info from typespecs with params" do @@ -3104,7 +3104,7 @@ defmodule ElixirSense.SuggestionsTest do assert suggestion.signature == "list(t)" assert suggestion.arity == 1 assert suggestion.doc == "Proper list ([]-terminated)" - assert suggestion.origin == "" + assert suggestion.origin == nil end test "erlang types" do @@ -3180,6 +3180,7 @@ defmodule ElixirSense.SuggestionsTest do origin: "MyModule", type: :type_spec, signature: "my_local_t()", + args_list: [], doc: "", spec: "", metadata: %{} @@ -3191,6 +3192,7 @@ defmodule ElixirSense.SuggestionsTest do origin: "MyModule", type: :type_spec, signature: "my_local_arg_t(a, b)", + args_list: ["a", "b"], doc: "", spec: "", metadata: %{} @@ -3219,6 +3221,7 @@ defmodule ElixirSense.SuggestionsTest do origin: "MyModule", type: :type_spec, signature: "my_local_t()", + args_list: [], doc: "", spec: "", metadata: %{} @@ -3252,6 +3255,7 @@ defmodule ElixirSense.SuggestionsTest do origin: "SomeModule", type: :type_spec, signature: "my_local_pub_t(a, b)", + args_list: ["a", "b"], doc: "", spec: "", metadata: %{} @@ -3263,6 +3267,7 @@ defmodule ElixirSense.SuggestionsTest do origin: "SomeModule", type: :type_spec, signature: "my_local_op_t()", + args_list: [], doc: "", spec: "", metadata: %{}