elixir-lsp / vscode-elixir-ls

Elixir language support and debugger for VS Code, powered by ElixirLS.
https://marketplace.visualstudio.com/items?itemName=JakeBecker.elixir-ls
MIT License
545 stars 105 forks source link

Autocomplete does not work for functions generated via `macros` in the same module/file. #435

Closed SergeyMosin closed 2 months ago

SergeyMosin commented 2 months ago

Environment

Troubleshooting

Description/Problem

Autocomplete does not work for functions generated via macros in the same module/file. However, it works when called from other modules/files.

Details

MyMacros.ex

defmodule MyMacros do
  defmacro enums_kv(kv) do
    Enum.map(kv, fn {k, v} ->
      quote do
        @spec unquote(k)() :: unquote(v)
        def unquote(k)(), do: unquote(v)
      end
    end)
  end

MyMod.ex (Autocomplete does NOT works when typing on or tw inside hello)

defmodule MyMod do
  require MyMacros
  MyMacros.enums_kv(one: 1, two: 2)
  # enums_kv(one: 1, two: 2) generates:
  #   def one, do: 1
  #   def two, do: 2

  def hello do
    one()
  end
end

MyOtherMod.ex (Autocomplete works when typing MyMod.t or MyMod.o inside hello)

defmodule MyOtherMod do
  def hello do
    MyMod.two()
  end
end

Demo/Video

video-demo.webm

lukaszsamson commented 2 months ago

This is the current limitation - only trivial use macros are expanded. This branch has change replacing the inference engine and rebuilding it on top of new Macro.Env API https://github.com/elixir-lsp/elixir-ls/pull/1116. With that macro expansion is possible in more places and this scenario is working.

Screenshot 2024-09-21 at 15 15 38