Skip to content

Commit 5334775

Browse files
committed
point to function on spec
1 parent 80eb383 commit 5334775

File tree

2 files changed

+51
-44
lines changed

2 files changed

+51
-44
lines changed

apps/language_server/lib/language_server/providers/definition/locator.ex

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,19 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do
250250
if Introspection.matches_arity?(a, call_arity) do
251251
{{line, column}, {end_line, end_column}} = Location.info_to_range(spec_info)
252252

253-
%Location{
254-
file: nil,
255-
type: :spec,
256-
line: line,
257-
column: column,
258-
end_line: end_line,
259-
end_column: end_column
260-
}
253+
if spec_info.kind in [:callback, :macrocallback] do
254+
%Location{
255+
file: nil,
256+
type: :spec,
257+
line: line,
258+
column: column,
259+
end_line: end_line,
260+
end_column: end_column
261+
}
262+
else
263+
# find def location for spec
264+
find_function(module, f, a, metadata)
265+
end
261266
end
262267

263268
_ ->
@@ -268,30 +273,7 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do
268273
{line, column} = context.end
269274
call_arity = Metadata.get_call_arity(metadata, mod, fun, line, column) || :any
270275

271-
fn_definition =
272-
Location.get_function_position_using_metadata(
273-
mod,
274-
fun,
275-
call_arity,
276-
metadata.mods_funs_to_positions
277-
)
278-
279-
case fn_definition do
280-
nil ->
281-
Location.find_mod_fun_source(mod, fun, call_arity)
282-
283-
%ModFunInfo{} = info ->
284-
{{line, column}, {end_line, end_column}} = Location.info_to_range(info)
285-
286-
%Location{
287-
file: nil,
288-
type: ModFunInfo.get_category(info),
289-
line: line,
290-
column: column,
291-
end_line: end_line,
292-
end_column: end_column
293-
}
294-
end
276+
find_function(mod, fun, call_arity, metadata)
295277

296278
{mod, fun, true, :type} ->
297279
{line, column} = context.end
@@ -319,6 +301,33 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do
319301
end
320302
end
321303

304+
def find_function(mod, fun, arity, metadata) do
305+
fn_definition =
306+
Location.get_function_position_using_metadata(
307+
mod,
308+
fun,
309+
arity,
310+
metadata.mods_funs_to_positions
311+
)
312+
313+
case fn_definition do
314+
nil ->
315+
Location.find_mod_fun_source(mod, fun, arity)
316+
317+
%ModFunInfo{} = info ->
318+
{{line, column}, {end_line, end_column}} = Location.info_to_range(info)
319+
320+
%Location{
321+
file: nil,
322+
type: ModFunInfo.get_category(info),
323+
line: line,
324+
column: column,
325+
end_line: end_line,
326+
end_column: end_column
327+
}
328+
end
329+
end
330+
322331
defp get_module(module, %{end: {line, col}}, env, metadata) do
323332
with {true, module} <- get_phoenix_module(module, env),
324333
true <- Introspection.elixir_module?(module) do

apps/language_server/test/providers/definition/locator_test.exs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,14 +1920,13 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.LocatorTest do
19201920
end
19211921
"""
19221922

1923-
assert Locator.definition(buffer, 2, 10) == %Location{
1924-
type: :spec,
1923+
assert %Location{
1924+
type: :function,
19251925
file: nil,
1926-
line: 2,
1926+
line: 4,
19271927
column: 3,
1928-
end_line: 2,
1929-
end_column: 29
1930-
}
1928+
end_line: 4
1929+
} = Locator.definition(buffer, 2, 10)
19311930
end
19321931

19331932
test "find definition of local spec with guard on definition" do
@@ -1939,14 +1938,13 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.LocatorTest do
19391938
end
19401939
"""
19411940

1942-
assert Locator.definition(buffer, 2, 10) == %Location{
1943-
type: :spec,
1941+
assert %Location{
1942+
type: :function,
19441943
file: nil,
1945-
line: 2,
1944+
line: 4,
19461945
column: 3,
1947-
end_line: 2,
1948-
end_column: 44
1949-
}
1946+
end_line: 4
1947+
} = Locator.definition(buffer, 2, 10)
19501948
end
19511949

19521950
test "find definition of local callback on definition" do

0 commit comments

Comments
 (0)