Skip to content

Commit 1564798

Browse files
committed
typedef erl_location_t, rename line to location
1 parent 5fe8d5f commit 1564798

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

apps/language_server/lib/language_server/providers/workspace_symbols.ex

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
3434
location: location_t
3535
}
3636

37+
# Location in annotations from OTP 23 and previous contain only line number.
38+
# OTP 24 annotations may also be {line, column}, depending on compiler
39+
# options.
40+
@typep erl_location_t :: non_neg_integer | {non_neg_integer, non_neg_integer}
3741
@typep key_t :: :modules | :functions | :types | :callbacks
3842
@typep symbol_t :: module | {module, atom, non_neg_integer}
3943
@typep state_t :: %{
@@ -223,15 +227,15 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
223227

224228
## Helpers
225229

226-
defp find_module_line(module, path) do
230+
defp find_module_location(module, path) do
227231
if String.ends_with?(path, ".erl") do
228232
ErlangSourceFile.module_line(path)
229233
else
230234
SourceFile.module_line(module)
231235
end
232236
end
233237

234-
defp find_function_line(module, function, arity, path) do
238+
defp find_function_location(module, function, arity, path) do
235239
if String.ends_with?(path, ".erl") do
236240
ErlangSourceFile.function_line(path, function)
237241
else
@@ -360,8 +364,8 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
360364
chunked_module_paths
361365
|> do_process_chunked(fn chunk ->
362366
for {module, path} <- chunk do
363-
line = find_module_line(module, path)
364-
build_result(:modules, module, path, line)
367+
location = find_module_location(module, path)
368+
build_result(:modules, module, path, location)
365369
end
366370
end)
367371
end)
@@ -373,9 +377,9 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
373377
Code.ensure_loaded?(module),
374378
{function, arity} <- module.module_info(:exports) do
375379
{function, arity} = SourceFile.strip_macro_prefix({function, arity})
376-
line = find_function_line(module, function, arity, path)
380+
location = find_function_location(module, function, arity, path)
377381

378-
build_result(:functions, {module, function, arity}, path, line)
382+
build_result(:functions, {module, function, arity}, path, location)
379383
end
380384
end)
381385
end)
@@ -388,13 +392,13 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
388392
{kind, {type, type_ast, args}} <-
389393
ElixirSense.Core.Normalized.Typespec.get_types(module),
390394
kind in [:type, :opaque] do
391-
line =
395+
location =
392396
case type_ast do
393-
{_, line, _, _} -> line
394-
{_, line, _} -> line
397+
{_, location, _, _} -> location
398+
{_, location, _} -> location
395399
end
396400

397-
build_result(:types, {module, type, length(args)}, path, line)
401+
build_result(:types, {module, type, length(args)}, path, location)
398402
end
399403
end)
400404
end)
@@ -405,11 +409,11 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
405409
for {module, path} <- chunk,
406410
function_exported?(module, :behaviour_info, 1),
407411
# TODO: Don't call into here directly
408-
{{callback, arity}, [{:type, line, _, _}]} <-
412+
{{callback, arity}, [{:type, location, _, _}]} <-
409413
ElixirSense.Core.Normalized.Typespec.get_callbacks(module) do
410414
{callback, arity} = SourceFile.strip_macro_prefix({callback, arity})
411415

412-
build_result(:callbacks, {module, callback, arity}, path, line)
416+
build_result(:callbacks, {module, callback, arity}, path, location)
413417
end
414418
end)
415419
end)
@@ -477,23 +481,14 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
477481
end)
478482
end
479483

480-
@spec build_result(
481-
key_t,
482-
symbol_t,
483-
String.t(),
484-
nil | non_neg_integer | {non_neg_integer, non_neg_integer}
485-
) :: symbol_information_t
486-
defp build_result(key, symbol, path, {line, _}) do
487-
build_result(key, symbol, path, line)
488-
end
489-
490-
defp build_result(key, symbol, path, line) do
484+
@spec build_result(key_t, symbol_t, String.t(), nil | erl_location_t) :: symbol_information_t
485+
defp build_result(key, symbol, path, location) do
491486
%{
492487
kind: @symbol_codes |> Map.fetch!(key),
493488
name: symbol_name(key, symbol),
494489
location: %{
495490
uri: SourceFile.path_to_uri(path),
496-
range: build_range(line)
491+
range: build_range(location)
497492
}
498493
}
499494
end
@@ -515,14 +510,21 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
515510
"#{inspect(module)}.#{callback}/#{arity}"
516511
end
517512

518-
@spec build_range(nil | non_neg_integer) :: range_t
513+
@spec build_range(nil | erl_location_t) :: range_t
519514
defp build_range(nil) do
520515
%{
521516
start: %{line: 0, character: 0},
522517
end: %{line: 1, character: 0}
523518
}
524519
end
525520

521+
defp build_range({line, _column}) do
522+
%{
523+
start: %{line: max(line - 1, 0), character: 0},
524+
end: %{line: line, character: 0}
525+
}
526+
end
527+
526528
defp build_range(line) do
527529
%{
528530
start: %{line: max(line - 1, 0), character: 0},

0 commit comments

Comments
 (0)