Skip to content

Change workspace symbols to only look at modules in the current application #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
) do
JsonRpc.log_message(:info, "[ElixirLS WorkspaceSymbols] Indexing...")

module_paths =
:code.all_loaded()
|> process_chunked(fn chunk ->
for {module, beam_file} <- chunk,
path = find_module_path(module, beam_file),
path != nil,
do: {module, path}
end)
module_paths = module_paths()

JsonRpc.log_message(:info, "[ElixirLS WorkspaceSymbols] Module discovery complete")

Expand All @@ -155,14 +148,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
) do
JsonRpc.log_message(:info, "[ElixirLS WorkspaceSymbols] Updating index...")

module_paths =
:code.all_loaded()
|> process_chunked(fn chunk ->
for {module, beam_file} <- chunk,
path = find_module_path(module, beam_file),
SourceFile.path_to_uri(path) in modified_uris,
do: {module, path}
end)
module_paths = module_paths()

JsonRpc.log_message(
:info,
Expand Down Expand Up @@ -247,28 +233,14 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
end
end

defp find_module_path(module, beam_file) do
file =
with true <- Code.ensure_loaded?(module),
path when not is_nil(path) <- module.module_info(:compile)[:source],
path_binary = List.to_string(path),
true <- File.exists?(path_binary) do
path_binary
else
_ -> nil
end

if file do
file
defp find_module_path(module) do
with true <- Code.ensure_loaded?(module),
path when not is_nil(path) <- module.module_info(:compile)[:source],
path_binary = List.to_string(path),
true <- File.exists?(path_binary) do
path_binary
else
with beam_file when not is_nil(beam_file) <-
ErlangSourceFile.get_beam_file(module, beam_file),
erl_file = ErlangSourceFile.beam_file_to_erl_file(beam_file),
true <- File.exists?(erl_file) do
erl_file
else
_ -> nil
end
_ -> nil
end
end

Expand Down Expand Up @@ -483,6 +455,31 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
end)
end

defp module_paths do
app_names = app_names()

Enum.each(app_names, &Application.load/1)

app_names
|> Enum.flat_map(fn app_name ->
Application.spec(app_name, :modules)
|> Enum.flat_map(fn app_module ->
case find_module_path(app_module) do
nil -> []
path -> [{app_module, path}]
end
end)
end)
end

defp app_names do
if Mix.Project.umbrella?() do
Mix.Project.apps_paths() |> Map.keys()
else
[Keyword.fetch!(Mix.Project.config(), :app)]
end
end

@spec build_result(key_t, symbol_t, String.t(), nil | non_neg_integer) :: symbol_information_t
defp build_result(key, symbol, path, line) do
%{
Expand Down
161 changes: 98 additions & 63 deletions apps/language_server/test/providers/workspace_symbols_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,74 +56,91 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
},
name: "ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols"
}
]} = WorkspaceSymbols.symbols("ElixirLS.LanguageServer.Fixtures.", @server_name)
]} =
WorkspaceSymbols.symbols(
"ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols",
@server_name
)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")

assert {:ok,
[
%{
kind: 2,
location: %{
range: %{end: %{character: 0, line: 1}, start: %{character: 0, line: 0}},
uri: uri
},
name: "ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols"
}
]} = WorkspaceSymbols.symbols("work", @server_name)
assert {:ok, results} = WorkspaceSymbols.symbols("work", @server_name)

assert %{
kind: 2,
location: %{
range: %{end: %{character: 0, line: 1}, start: %{character: 0, line: 0}},
uri: uri
},
name: "ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols"
} = matches(results, "ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols")

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")
end

test "returns functions" do
assert {
:ok,
[
%{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 1}, start: %{character: 0, line: 0}},
uri: uri
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.module_info/1"
},
%{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 1}, start: %{character: 0, line: 0}}
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.module_info/0"
},
%{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 1}, start: %{character: 0, line: 0}}
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.behaviour_info/1"
},
%{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 3}, start: %{character: 0, line: 2}}
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_macro/1"
},
%{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 2}, start: %{character: 0, line: 1}}
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_function/1"
},
%{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 1}, start: %{character: 0, line: 0}}
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.__info__/1"
}
]
} = WorkspaceSymbols.symbols("f ElixirLS.LanguageServer.Fixtures.", @server_name)
query = "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols"
assert {:ok, results} = WorkspaceSymbols.symbols(query, @server_name)

assert %{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 1}, start: %{character: 0, line: 0}},
uri: uri
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.module_info/1"
} =
matches(results, "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.module_info/1")

assert %{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 1}, start: %{character: 0, line: 0}}
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.module_info/0"
} =
matches(results, "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.module_info/0")

assert %{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 1}, start: %{character: 0, line: 0}}
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.behaviour_info/1"
} =
matches(
results,
"f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.behaviour_info/1"
)

assert %{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 3}, start: %{character: 0, line: 2}}
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_macro/1"
} =
matches(results, "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_macro/1")

assert %{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 2}, start: %{character: 0, line: 1}}
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_function/1"
} =
matches(
results,
"f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_function/1"
)

assert %{
kind: 12,
location: %{
range: %{end: %{character: 0, line: 1}, start: %{character: 0, line: 0}}
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.__info__/1"
} = matches(results, "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.__info__/1")

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")

Expand All @@ -137,7 +154,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
},
name: "f ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_function/1"
}
]} = WorkspaceSymbols.symbols("f fun", @server_name)
]} = WorkspaceSymbols.symbols("f some_fun", @server_name)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")
end
Expand All @@ -162,7 +179,11 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
name: "t ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_opaque_type/0"
}
]
} = WorkspaceSymbols.symbols("t ElixirLS.LanguageServer.Fixtures.", @server_name)
} =
WorkspaceSymbols.symbols(
"t ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.",
@server_name
)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")

Expand Down Expand Up @@ -203,7 +224,11 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
name: "c ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.some_macrocallback/1"
}
]
} = WorkspaceSymbols.symbols("c ElixirLS.LanguageServer.Fixtures.", @server_name)
} =
WorkspaceSymbols.symbols(
"c ElixirLS.LanguageServer.Fixtures.WorkspaceSymbols.",
@server_name
)

assert uri |> String.ends_with?("test/support/fixtures/workspace_symbols.ex")

Expand All @@ -230,4 +255,14 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbolsTest do
wait_until_indexed(pid)
end
end

defp matches(results, name) do
assert [matching_result] =
Enum.filter(
results,
&(&1.name == name)
)

matching_result
end
end