Skip to content

Commit 27013d3

Browse files
committed
optimize function location in workspace symbols
1 parent ea4cf65 commit 27013d3

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

apps/language_server/lib/language_server.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ defmodule ElixirLS.LanguageServer do
134134
true
135135
end
136136

137+
:persistent_term.put(:language_server_eep48, supported)
138+
137139
JsonRpc.telemetry("eep48", %{"elixir_ls.eep48" => to_string(supported)}, %{})
138140
end
139141

apps/language_server/lib/language_server/dialyzer/success_typings.ex

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
defmodule ElixirLS.LanguageServer.Dialyzer.SuccessTypings do
22
alias ElixirLS.LanguageServer.SourceFile
3+
require Logger
34

45
def suggest_contracts(plt, files) do
5-
modules =
6-
plt
7-
|> :dialyzer_plt.all_modules()
8-
|> :sets.to_list()
6+
{us, results} = :timer.tc(fn ->
7+
modules =
8+
plt
9+
|> :dialyzer_plt.all_modules()
10+
|> :sets.to_list()
911

10-
for mod <- modules,
11-
file = source(mod),
12-
file in files,
13-
{{^mod, fun, arity} = mfa, success_typing} <- success_typings(plt, mod),
14-
:dialyzer_plt.lookup_contract(plt, mfa) == :none,
15-
{stripped_fun, stripped_arity} = SourceFile.strip_macro_prefix({fun, arity}),
16-
line = SourceFile.function_line(mod, stripped_fun, stripped_arity),
17-
is_integer(line) and line > 0,
18-
do: {file, line, {mod, stripped_fun, stripped_arity}, success_typing, stripped_fun != fun}
12+
for mod <- modules,
13+
file = source(mod),
14+
file in files,
15+
docs = ElixirSense.Core.Normalized.Code.get_docs(mod, :docs),
16+
{{^mod, fun, arity} = mfa, success_typing} <- success_typings(plt, mod),
17+
:dialyzer_plt.lookup_contract(plt, mfa) == :none,
18+
{stripped_fun, stripped_arity} = SourceFile.strip_macro_prefix({fun, arity}),
19+
line = SourceFile.function_line(mod, stripped_fun, stripped_arity, docs),
20+
is_integer(line) and line > 0,
21+
do: {file, line, {mod, stripped_fun, stripped_arity}, success_typing, stripped_fun != fun}
22+
end)
23+
Logger.info("Success typings computed in #{div(us, 1000)}ms")
24+
results
1925
end
2026

2127
defp source(module) do

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,16 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
275275
end
276276
end
277277

278-
defp find_function_location(module, function, arity, path) do
278+
defp find_function_location(module, function, arity, path, docs) do
279279
line =
280280
if String.ends_with?(path, ".erl") do
281281
ErlangSourceFile.function_line(path, function)
282282
else
283-
SourceFile.function_line(module, function, arity)
283+
SourceFile.function_line(module, function, arity, docs)
284284
end
285285

286286
# both functions can return nil
287-
max(line || 1, 1)
287+
line || 1
288288
end
289289

290290
defp find_module_path(module, beam_file) do
@@ -420,9 +420,10 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
420420
|> do_process_chunked(fn chunk ->
421421
for {module, path} <- chunk,
422422
Code.ensure_loaded?(module),
423+
docs = if(not String.ends_with?(path, ".erl"), do: ElixirSense.Core.Normalized.Code.get_docs(module, :docs)),
423424
{function, arity} <- module.module_info(:exports) do
424425
{function, arity} = SourceFile.strip_macro_prefix({function, arity})
425-
location = find_function_location(module, function, arity, path)
426+
location = find_function_location(module, function, arity, path, docs)
426427

427428
build_result(:functions, {module, function, arity}, path, location, project_dir)
428429
end
@@ -467,11 +468,11 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
467468

468469
{:ok, _pid} =
469470
Task.start_link(fn ->
470-
results = fun.()
471+
{us, results} = :timer.tc(fun)
471472

472473
send(self, {:indexing_complete, key, results})
473474

474-
Logger.info("[ElixirLS WorkspaceSymbols] #{length(results)} #{key} added to index")
475+
Logger.info("[ElixirLS WorkspaceSymbols] #{length(results)} #{key} added to index in #{div(us, 1000)}ms")
475476
end)
476477

477478
:ok

apps/language_server/lib/language_server/source_file.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ defmodule ElixirLS.LanguageServer.SourceFile do
158158
end
159159
end
160160

161-
def function_line(mod, fun, arity) do
162-
case ElixirSense.Core.Normalized.Code.get_docs(mod, :docs) do
161+
def function_line(mod, fun, arity, docs \\ nil) do
162+
case docs || ElixirSense.Core.Normalized.Code.get_docs(mod, :docs) do
163163
nil ->
164164
nil
165165

0 commit comments

Comments
 (0)