Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Go to definitions crashes on modules defined via a macro #753

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Lexical.RemoteControl.CodeIntelligence.Definition do
alias Future.Code, as: Code
alias Future.Code
alias Lexical.Ast
alias Lexical.Ast.Analysis
alias Lexical.Document
Expand Down Expand Up @@ -125,20 +125,18 @@ defmodule Lexical.RemoteControl.CodeIntelligence.Definition do
when type in [:function, :module, :macro] do
position = Position.new(document, line, column)

case Ast.zipper_at(document, position) do
{:ok, zipper} ->
zipper = %{node: {entity_name, meta, _}} = Sourceror.Zipper.next(zipper)

meta =
if entity_name == :when do
%{node: {_entity_name, meta, _}} = Sourceror.Zipper.next(zipper)
meta
else
meta
end

{meta[:line], meta[:column]}
with {:ok, zipper} <- Ast.zipper_at(document, position),
%{node: {entity_name, meta, _}} <- Sourceror.Zipper.next(zipper) do
meta =
if entity_name == :when do
%{node: {_entity_name, meta, _}} = Sourceror.Zipper.next(zipper)
meta
else
meta
end

{meta[:line], meta[:column]}
else
_ ->
{line, column}
end
Expand Down
32 changes: 32 additions & 0 deletions apps/remote_control/test/fixtures/navigations/lib/macro_struct.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule MacroStruct do
defmacro __using__(_) do
Module.register_attribute(__CALLER__.module, :field_names, accumulate: true)

quote do
import(unquote(__MODULE__), only: [field: 2, typedstruct: 2])
end
end

defmacro __before_compile__(_) do
fields = __CALLER__.module |> Module.get_attribute(:field_names) |> List.wrap()

quote do
defstruct unquote(fields)
end
end

defmacro typedstruct(opts, do: body) do
Module.put_attribute(__CALLER__.module, :opts, opts)

quote do
defmodule unquote(opts[:module]) do
@before_compile unquote(__MODULE__)
unquote(body)
end
end
end

defmacro field(name, _type) do
Module.put_attribute(__CALLER__.module, :field_names, name)
end
end
11 changes: 11 additions & 0 deletions apps/remote_control/test/fixtures/navigations/lib/struct.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule NormalStruct do
defstruct [:variant]
end

defmodule TypedStructs do
use MacroStruct

typedstruct enforce: true, module: MacroBasedStruct do
field(:contract_id, String.t())
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.DefinitionTest do
end
]

{:ok, [location1, location2]} =
definition(project, subject_module, [uri, subject_uri])
{:ok, [location1, location2]} = definition(project, subject_module, [uri, subject_uri])

{referenced_uri, definition_line} = location1
assert definition_line =~ ~S[ def «greet(name)» do]
Expand All @@ -434,6 +433,21 @@ defmodule Lexical.RemoteControl.CodeIntelligence.DefinitionTest do
end
end

describe "edge cases" do
setup [:with_referenced_file]

test "doesn't crash with structs defined with DSLs", %{project: project, uri: uri} do
subject_module = ~q[
defmodule MyTest do
def my_test(%TypedStructs.MacroBased|Struct{}) do
end
end
]

assert {:ok, _file, _definition} = definition(project, subject_module, [uri])
end
end

defp definition(project, code, referenced_uri) do
with {position, code} <- pop_cursor(code),
{:ok, document} <- subject_module(project, code),
Expand Down
Loading