Skip to content

Commit cda5742

Browse files
authored
[feat] Loosen find_references (#831)
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.
1 parent 661aa46 commit cda5742

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

apps/remote_control/lib/lexical/remote_control/code_intelligence/references.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ defmodule Lexical.RemoteControl.CodeIntelligence.References do
3535
end
3636

3737
defp find_references(
38-
{:call, module, function_name, arity},
38+
{:call, module, function_name, _arity},
3939
_analysis,
4040
_position,
4141
include_definitions?
4242
) do
43-
subject = Subject.mfa(module, function_name, arity)
43+
subject = Subject.mfa(module, function_name, "")
4444
subtype = subtype(include_definitions?)
4545

46-
query(subject, type: {:function, :_}, subtype: subtype)
46+
case Store.prefix(subject, type: {:function, :_}, subtype: subtype) do
47+
{:ok, entries} -> Enum.map(entries, &to_location/1)
48+
_ -> []
49+
end
4750
end
4851

4952
defp find_references(

apps/remote_control/test/lexical/remote_control/code_intelligence/references_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
107107
assert [%Location{} = location] = references(project, "Functions.do_map|(a, b)", code)
108108
assert decorate(code, location.range) =~ "def func(x), do: «do_map(x, & &1 + 1)»"
109109
end
110+
111+
test "are found in function definitions with optional arguments", %{project: project} do
112+
referenced = ~q/
113+
defmodule Functions do
114+
def do_map|(a, b, c \\ 3), do: {a, b, c}
115+
def func(x), do: do_map(x, 3, 5)
116+
def func2(x), do: do_map(x, 3)
117+
end
118+
/
119+
120+
{_, code} = pop_cursor(referenced)
121+
122+
references = references(project, referenced, code)
123+
assert [first, second] = Enum.sort_by(references, & &1.range.start.line)
124+
assert decorate(code, first.range) =~ " def func(x), do: «do_map(x, 3, 5)»"
125+
assert decorate(code, second.range) =~ " def func2(x), do: «do_map(x, 3)»"
126+
end
110127
end
111128

112129
describe "module references" do

0 commit comments

Comments
 (0)