Skip to content
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

Provide test running code lens #389

Merged
merged 24 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
102 changes: 85 additions & 17 deletions apps/language_server/lib/language_server/providers/code_lens.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens do
"""
Blond11516 marked this conversation as resolved.
Show resolved Hide resolved

alias ElixirLS.LanguageServer.{Server, SourceFile}
alias ElixirSense.Core.Parser
alias ElixirSense.Core.State
alias Erl2ex.Convert.{Context, ErlForms}
alias Erl2ex.Pipeline.{Parse, ModuleData, ExSpec}
import ElixirLS.LanguageServer.Protocol
Expand Down Expand Up @@ -110,30 +112,96 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens do
end
end

def code_lens(server_instance_id, uri, text) do
def spec_code_lens(server_instance_id, uri, text) do
resp =
for {_, line, {mod, fun, arity}, contract, is_macro} <- Server.suggest_contracts(uri),
SourceFile.function_def_on_line?(text, line, fun),
spec = ContractTranslator.translate_contract(fun, contract, is_macro) do
%{
"range" => range(line - 1, 0, line - 1, 0),
"command" => %{
"title" => "@spec #{spec}",
"command" => "spec:#{server_instance_id}",
"arguments" => [
%{
"uri" => uri,
"mod" => to_string(mod),
"fun" => to_string(fun),
"arity" => arity,
"spec" => spec,
"line" => line
}
]
build_code_lens(
line,
"@spec #{spec}",
"spec:#{server_instance_id}",
%{
"uri" => uri,
"mod" => to_string(mod),
"fun" => to_string(fun),
"arity" => arity,
"spec" => spec,
"line" => line
}
}
)
end

{:ok, resp}
end

def test_code_lens(uri, src) do
file_path = SourceFile.path_from_uri(uri)

if imports?(src, ExUnit.Case) do
test_calls = calls_to(src, :test)
describe_calls = calls_to(src, :describe)
Blond11516 marked this conversation as resolved.
Show resolved Hide resolved

calls_lenses =
for {line, _col} <- test_calls ++ describe_calls do
test_filter = "#{file_path}:#{line}"

build_code_lens(line, "Run test", "elixir.test.run", test_filter)
Blond11516 marked this conversation as resolved.
Show resolved Hide resolved
end

file_lens = build_code_lens(1, "Run test", "elixir.test.run", file_path)
Blond11516 marked this conversation as resolved.
Show resolved Hide resolved

{:ok, [file_lens | calls_lenses]}
end
end

@spec imports?(String.t(), [atom()] | atom()) :: boolean()
defp imports?(buffer, modules) do
buffer_file_metadata =
buffer
|> Parser.parse_string(true, true, 1)

imports_set =
buffer_file_metadata.lines_to_env
|> get_imports()
|> MapSet.new()

modules
|> List.wrap()
|> MapSet.new()
|> MapSet.subset?(imports_set)
end

defp get_imports(lines_to_env) do
Blond11516 marked this conversation as resolved.
Show resolved Hide resolved
%State.Env{imports: imports} =
lines_to_env
|> Enum.max_by(fn {k, _v} -> k end)
|> elem(1)

imports
end

@spec calls_to(String.t(), atom() | {atom(), integer()}) :: [{pos_integer(), pos_integer()}]
defp calls_to(buffer, function) do
buffer_file_metadata =
buffer
|> Parser.parse_string(true, true, 1)

buffer_file_metadata.calls
|> Enum.map(fn {_k, v} -> v end)
Blond11516 marked this conversation as resolved.
Show resolved Hide resolved
|> List.flatten()
|> Enum.filter(fn call_info -> call_info.func == function end)
|> Enum.map(fn call -> call.position end)
end

def build_code_lens(line, title, command, argument) do
%{
"range" => range(line - 1, 0, line - 1, 0),
"command" => %{
"title" => title,
"command" => command,
"arguments" => [argument]
}
}
end
end
19 changes: 13 additions & 6 deletions apps/language_server/lib/language_server/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -530,13 +530,20 @@ defmodule ElixirLS.LanguageServer.Server do
end

defp handle_request(code_lens_req(_id, uri), state) do
if dialyzer_enabled?(state) and state.settings["suggestSpecs"] != false do
{:async,
fn -> CodeLens.code_lens(state.server_instance_id, uri, state.source_files[uri].text) end,
state}
else
{:ok, nil, state}
fun = fn ->
{:ok, spec_code_lens} =
Blond11516 marked this conversation as resolved.
Show resolved Hide resolved
if dialyzer_enabled?(state) and state.settings["suggestSpecs"] != false do
CodeLens.spec_code_lens(state.server_instance_id, uri, state.source_files[uri].text)
else
[]
Blond11516 marked this conversation as resolved.
Show resolved Hide resolved
end

{:ok, test_code_lens} = CodeLens.test_code_lens(uri, state.source_files[uri].text)

{:ok, spec_code_lens ++ test_code_lens}
end

{:async, fun, state}
end

defp handle_request(execute_command_req(_id, command, args), state) do
Expand Down