From ef87ad155a2af54b688a21de866f87ddbb126de4 Mon Sep 17 00:00:00 2001 From: Steve Cohen Date: Mon, 7 Oct 2024 12:46:41 -0700 Subject: [PATCH] [feat] Loosen find_references Prior, find references would take the arity into account. This was because in elixir, a functin is defined by its name and its arity. However in practice, you more than not want to see a all the calls of a function with a given name rather than a specific head. This is especially true for functions with default arguments. As a result, this change looks for functions with a given name and any arity. --- .../code_intelligence/references.ex | 9 ++++++--- .../code_intelligence/references_test.exs | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/apps/remote_control/lib/lexical/remote_control/code_intelligence/references.ex b/apps/remote_control/lib/lexical/remote_control/code_intelligence/references.ex index 3deb5b6af..878c6b37a 100644 --- a/apps/remote_control/lib/lexical/remote_control/code_intelligence/references.ex +++ b/apps/remote_control/lib/lexical/remote_control/code_intelligence/references.ex @@ -35,15 +35,18 @@ defmodule Lexical.RemoteControl.CodeIntelligence.References do end defp find_references( - {:call, module, function_name, arity}, + {:call, module, function_name, _arity}, _analysis, _position, include_definitions? ) do - subject = Subject.mfa(module, function_name, arity) + subject = Subject.mfa(module, function_name, "") subtype = subtype(include_definitions?) - query(subject, type: {:function, :_}, subtype: subtype) + case Store.prefix(subject, type: {:function, :_}, subtype: subtype) do + {:ok, entries} -> Enum.map(entries, &to_location/1) + _ -> [] + end end defp find_references( diff --git a/apps/remote_control/test/lexical/remote_control/code_intelligence/references_test.exs b/apps/remote_control/test/lexical/remote_control/code_intelligence/references_test.exs index 1d73db782..06932111d 100644 --- a/apps/remote_control/test/lexical/remote_control/code_intelligence/references_test.exs +++ b/apps/remote_control/test/lexical/remote_control/code_intelligence/references_test.exs @@ -107,6 +107,23 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do assert [%Location{} = location] = references(project, "Functions.do_map|(a, b)", code) assert decorate(code, location.range) =~ "def func(x), do: «do_map(x, & &1 + 1)»" end + + test "are found in function definitions with optional arguments", %{project: project} do + referenced = ~q/ + defmodule Functions do + def do_map|(a, b, c \\ 3), do: {a, b, c} + def func(x), do: do_map(x, 3, 5) + def func2(x), do: do_map(x, 3) + end + / + + {_, code} = pop_cursor(referenced) + + references = references(project, referenced, code) + assert [first, second] = Enum.sort_by(references, & &1.range.start.line) + assert decorate(code, first.range) =~ " def func(x), do: «do_map(x, 3, 5)»" + assert decorate(code, second.range) =~ " def func2(x), do: «do_map(x, 3)»" + end end describe "module references" do