Skip to content

Commit

Permalink
fix crash in implementations when behaviour implementation is a deleg…
Browse files Browse the repository at this point in the history
…ate or guard
  • Loading branch information
lukaszsamson committed Dec 25, 2023
1 parent 2892fcf commit c315c2c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/elixir_sense/providers/implementation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,24 @@ defmodule ElixirSense.Providers.Implementation do
metadata_implementations_locations =
metadata_implementations
|> Enum.map(fn module ->
{{line, column}, type} =
{{line, column}, info} =
metadata.mods_funs_to_positions
|> Enum.find_value(fn
{{^module, ^maybe_callback, _}, info} when is_nil(maybe_callback) ->
{List.last(info.positions), info.type}
{List.last(info.positions), info}

{{^module, ^maybe_callback, a}, info} when not is_nil(a) ->
defaults = info.params |> List.last() |> Introspection.count_defaults()

if Introspection.matches_arity_with_defaults?(a, defaults, arity) do
{List.last(info.positions), info.type}
{List.last(info.positions), info}
end

_ ->
nil
end)

kind =
case type do
:defmodule -> :module
:def -> :function
:defmacro -> :macro
end
kind = ModFunInfo.get_category(info)

{module, %Location{type: kind, file: nil, line: line, column: column}}
end)
Expand Down
34 changes: 34 additions & 0 deletions test/elixir_sense/implementation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,23 @@ defmodule ElixirSense.Providers.ImplementationTest do
] = ElixirSense.implementations(buffer, 2, 19)
end

test "find implementations of metadata behaviour module macrocallback when implementation is a guard" do
buffer = """
defmodule MetadataBehaviour do
@macrocallback foo(arg :: any) :: Macro.t
end
defmodule Some do
@behaviour MetadataBehaviour
defguard foo(arg) when is_nil(arg)
end
"""

[
%Location{type: :macro, file: nil, line: 7, column: 3}
] = ElixirSense.implementations(buffer, 2, 19)
end

test "find implementations of metadata behaviour" do
buffer = """
defmodule MetadataBehaviour do
Expand Down Expand Up @@ -310,6 +327,23 @@ defmodule ElixirSense.Providers.ImplementationTest do
] = ElixirSense.implementations(buffer, 3, 8)
end

test "find metadata protocol implementation functions on function when implementation is a delegate" do
buffer = """
defprotocol MetadataProtocol do
@spec some(t) :: any
def some(t)
end
defimpl MetadataProtocol, for: String do
defdelegate some(t), to: Impl
end
"""

[
%Location{type: :function, file: nil, line: 7, column: 3}
] = ElixirSense.implementations(buffer, 3, 8)
end

test "find metadata protocol implementation" do
buffer = """
defprotocol MetadataProtocol do
Expand Down

0 comments on commit c315c2c

Please sign in to comment.