Skip to content

Commit

Permalink
cover more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Feb 13, 2024
1 parent 4afff86 commit 7b283ba
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
43 changes: 43 additions & 0 deletions lib/elixir_sense/core/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down
68 changes: 68 additions & 0 deletions test/elixir_sense/core/metadata_builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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}"]
}
}
Expand Down Expand Up @@ -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 =
"""
Expand Down Expand Up @@ -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 =
"""
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/elixir_sense/docs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion test/elixir_sense/suggestions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4191,7 +4191,7 @@ defmodule ElixirSense.SuggestionsTest do
args_list: [],
doc: "",
spec: "@opaque my_local_op_t()",
metadata: %{}
metadata: %{opaque: true}
} == suggestion1
end
end
Expand Down

0 comments on commit 7b283ba

Please sign in to comment.