From 1bd3715877513b6d351a27d36a86abb20470decf Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Fri, 27 Sep 2024 22:18:04 +0200 Subject: [PATCH] address todos --- lib/elixir_sense/core/compiler.ex | 12 ++++++++++-- .../core/metadata_builder/error_recovery_test.exs | 9 +++++++++ test/elixir_sense/core/metadata_builder_test.exs | 4 +--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/elixir_sense/core/compiler.ex b/lib/elixir_sense/core/compiler.ex index 90ee028e..1beb5299 100644 --- a/lib/elixir_sense/core/compiler.ex +++ b/lib/elixir_sense/core/compiler.ex @@ -1454,7 +1454,6 @@ 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 @@ -1462,6 +1461,14 @@ defmodule ElixirSense.Core.Compiler do 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 -> @@ -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 diff --git a/test/elixir_sense/core/metadata_builder/error_recovery_test.exs b/test/elixir_sense/core/metadata_builder/error_recovery_test.exs index a2b6e562..fe485472 100644 --- a/test/elixir_sense/core/metadata_builder/error_recovery_test.exs +++ b/test/elixir_sense/core/metadata_builder/error_recovery_test.exs @@ -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 diff --git a/test/elixir_sense/core/metadata_builder_test.exs b/test/elixir_sense/core/metadata_builder_test.exs index 2a90604d..4dbcdec2 100644 --- a/test/elixir_sense/core/metadata_builder_test.exs +++ b/test/elixir_sense/core/metadata_builder_test.exs @@ -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}]}, @@ -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{ @@ -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}