From 7b283ba8dec70cd4408e7dad482ab68cf3ddf930 Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Tue, 13 Feb 2024 09:49:59 +0100 Subject: [PATCH] cover more cases --- lib/elixir_sense/core/state.ex | 43 ++++++++++++ .../core/metadata_builder_test.exs | 68 +++++++++++++++++++ test/elixir_sense/docs_test.exs | 6 +- test/elixir_sense/suggestions_test.exs | 2 +- 4 files changed, 115 insertions(+), 4 deletions(-) diff --git a/lib/elixir_sense/core/state.ex b/lib/elixir_sense/core/state.ex index 8b8e6ba0..7a84dd3a 100644 --- a/lib/elixir_sense/core/state.ex +++ b/lib/elixir_sense/core/state.ex @@ -817,6 +817,34 @@ defmodule ElixirSense.Core.State do end end + meta = + if type == :defdelegate do + {target_module_expression, target_fun} = options[:target] + + Map.put( + meta, + :delegate_to, + {expand_alias(state, target_module_expression), target_fun, arity} + ) + else + meta + end + + meta = + if type in [:defguard, :defguardp] do + Map.put(meta, :guard, true) + else + meta + end + + doc = + if type in [:defp, :defmacrop] do + # documentation is discarded on private + "" + else + doc + end + current_module_variants |> Enum.reduce(state, fn variant, acc -> acc @@ -1155,6 +1183,21 @@ defmodule ElixirSense.Core.State do meta end + meta = + if kind == :opaque do + Map.put(meta, :opaque, true) + else + meta + end + + doc = + if kind == :typep do + # documentation is discarded on private + "" + else + doc + end + type_info = %TypeInfo{ name: type_name, args: [arg_names], diff --git a/test/elixir_sense/core/metadata_builder_test.exs b/test/elixir_sense/core/metadata_builder_test.exs index d9629a59..ebc2e793 100644 --- a/test/elixir_sense/core/metadata_builder_test.exs +++ b/test/elixir_sense/core/metadata_builder_test.exs @@ -4939,6 +4939,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do end_positions: [{4, 36}], generated: [false], args: [["a", "b"]], + meta: %{opaque: true}, specs: ["@opaque with_args(a, b) :: {a, b}"] }, {My, :with_args, nil} => %ElixirSense.Core.State.TypeInfo{ @@ -4948,6 +4949,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do end_positions: [{4, 36}], generated: [false], args: [["a", "b"]], + meta: %{opaque: true}, specs: ["@opaque with_args(a, b) :: {a, b}"] } } @@ -5608,6 +5610,19 @@ defmodule ElixirSense.Core.MetadataBuilderTest do assert %{doc: "", meta: %{hidden: true}} = state.mods_funs_to_positions[{Some, :fun, 0}] end + test "doc on private is discarded" do + state = + """ + defmodule Some do + @doc "Some" + defp fun(), do: :ok + end + """ + |> string_to_state + + assert %{doc: ""} = state.mods_funs_to_positions[{Some, :fun, 0}] + end + test "impl true sets hidden meta if no doc" do state = """ @@ -5734,6 +5749,19 @@ defmodule ElixirSense.Core.MetadataBuilderTest do assert %{doc: "", meta: %{hidden: true}} = state.types[{Some, :my_type, 0}] end + test "typedoc is discarded on private" do + state = + """ + defmodule Some do + @typedoc "Some" + @typep my_type() :: any() + end + """ + |> string_to_state + + assert %{doc: ""} = state.types[{Some, :my_type, 0}] + end + test "underscored type sets hidden meta when there is no typedoc" do state = """ @@ -5752,6 +5780,46 @@ defmodule ElixirSense.Core.MetadataBuilderTest do end end + describe "meta" do + test "guard" do + state = + """ + defmodule Some do + defguard fun(a) when a == 1 + end + """ + |> string_to_state + + assert %{meta: %{guard: true}} = + state.mods_funs_to_positions[{Some, :fun, 1}] + end + + test "delegate" do + state = + """ + defmodule Some do + defdelegate count(a), to: Enum + end + """ + |> string_to_state + + assert %{meta: %{delegate_to: {Enum, :count, 1}}} = + state.mods_funs_to_positions[{Some, :count, 1}] + end + + test "opaque" do + state = + """ + defmodule Some do + @opaque my_type() :: any() + end + """ + |> string_to_state + + assert %{meta: %{opaque: true}} = state.types[{Some, :my_type, 0}] + end + end + defp string_to_state(string) do string |> Code.string_to_quoted(columns: true, token_metadata: true) diff --git a/test/elixir_sense/docs_test.exs b/test/elixir_sense/docs_test.exs index 4b375aeb..067311e9 100644 --- a/test/elixir_sense/docs_test.exs +++ b/test/elixir_sense/docs_test.exs @@ -387,7 +387,7 @@ defmodule ElixirSense.DocsTest do module: MyLocalModule, metadata: %{since: "1.2.3"}, specs: ["@spec flatten(list()) :: list()"], - docs: "Sample doc", + docs: "", kind: :function } end @@ -1464,7 +1464,7 @@ defmodule ElixirSense.DocsTest do module: MyLocalModule, spec: "@typep some(a) :: {a}", metadata: %{since: "1.2.3"}, - docs: "My example type", + docs: "", kind: :type } end @@ -1524,7 +1524,7 @@ defmodule ElixirSense.DocsTest do arity: 1, module: MyLocalModule, spec: "@opaque some(a)", - metadata: %{since: "1.2.3"}, + metadata: %{since: "1.2.3", opaque: true}, docs: "My example type", kind: :type } diff --git a/test/elixir_sense/suggestions_test.exs b/test/elixir_sense/suggestions_test.exs index f84c6b00..89f89662 100644 --- a/test/elixir_sense/suggestions_test.exs +++ b/test/elixir_sense/suggestions_test.exs @@ -4191,7 +4191,7 @@ defmodule ElixirSense.SuggestionsTest do args_list: [], doc: "", spec: "@opaque my_local_op_t()", - metadata: %{} + metadata: %{opaque: true} } == suggestion1 end end