Skip to content

Commit

Permalink
address todos
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Sep 27, 2024
1 parent 35b80ca commit 1bd3715
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
12 changes: 10 additions & 2 deletions lib/elixir_sense/core/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1454,14 +1454,21 @@ defmodule ElixirSense.Core.Compiler do
raise ArgumentError, "defimpl/3 expects a :for option when declared outside a module"
end)

# TODO how to look for cursor in for?
for =
__MODULE__.Macro.expand_literals(for, %{
env
| module: env.module || Elixir,
function: {:__impl__, 1}
})

{for, state} =
if is_atom(for) do
{for, state}
else
{_, state, _} = expand(for, state, env)
{:"Elixir.__UNKNOWN__", state}
end

{protocol, state, _env} = expand(name, state, env)

impl = fn protocol, for, block, state, env ->
Expand All @@ -1481,7 +1488,8 @@ defmodule ElixirSense.Core.Compiler do
block =
case opts do
[] ->
raise ArgumentError, "defimpl expects a do-end block"
# elixir raises here
nil

[do: block] ->
block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2807,4 +2807,13 @@ defmodule ElixirSense.Core.MetadataBuilder.ErrorRecoveryTest do
assert env.module == Abc
end
end

test "defimpl for" do
code = """
defimpl Enumerable, for: \
"""

assert {_, env} = get_cursor_env(code)
assert env.module == nil
end
end
4 changes: 1 addition & 3 deletions test/elixir_sense/core/metadata_builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,6 @@ defmodule ElixirSense.Core.MetadataBuilderTest do

assert Map.keys(state.lines_to_env[9].versioned_vars) == [{:k, nil}, {:kv, nil}, {:v, nil}]

# TODO should we handle unquote_slicing in arg list?
# TODO defquard on 1.18
assert [
%VarInfo{name: :k, positions: [{3, 21}, {4, 19}, {5, 19}, {6, 25}, {7, 17}]},
Expand All @@ -1009,7 +1008,6 @@ defmodule ElixirSense.Core.MetadataBuilderTest do

assert Map.keys(state.lines_to_env[18].versioned_vars) == [keys: nil, kv: nil]

# TODO should we handle unquote_slicing in arg list?
# TODO defquard on 1.18
assert [
%VarInfo{
Expand Down Expand Up @@ -8034,7 +8032,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
specs: ["@type __unknown__() :: 123"],
kind: :type,
positions: [{4, 5}],
end_positions: [{4, 30}],
end_positions: [_],
generated: [false],
doc: "",
meta: %{hidden: true}
Expand Down

0 comments on commit 1bd3715

Please sign in to comment.