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

Improvements to Code Lens #122

Merged
merged 2 commits into from
Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -12,9 +12,10 @@ defmodule ElixirLS.LanguageServer.Dialyzer.SuccessTypings do
file in files,
{{^mod, fun, arity} = mfa, success_typing} <- success_typings(plt, mod),
:dialyzer_plt.lookup_contract(plt, mfa) == :none,
line = SourceFile.function_line(mod, fun, arity),
{stripped_fun, stripped_arity} = SourceFile.strip_macro_prefix({fun, arity}),
line = SourceFile.function_line(mod, stripped_fun, stripped_arity),
is_integer(line),
do: {file, line, mfa, success_typing}
do: {file, line, {mod, stripped_fun, stripped_arity}, success_typing, stripped_fun != fun}
end

defp source(module) do
Expand Down
13 changes: 10 additions & 3 deletions apps/language_server/lib/language_server/providers/code_lens.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens do
import ElixirLS.LanguageServer.Protocol

defmodule ContractTranslator do
def translate_contract(fun, contract) do
def translate_contract(fun, contract, is_macro) do
{[%ExSpec{specs: [spec]} | _], _} =
"-spec foo#{contract}."
|> Parse.string()
Expand All @@ -29,6 +29,7 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens do

spec
|> Macro.postwalk(&tweak_specs/1)
|> drop_macro_env(is_macro)
|> Macro.to_string()
|> String.replace("()", "")
|> Code.format_string!(line_length: :infinity)
Expand Down Expand Up @@ -97,13 +98,19 @@ defmodule ElixirLS.LanguageServer.Providers.CodeLens do
defp tweak_specs(node) do
node
end

defp drop_macro_env(ast, false), do: ast

defp drop_macro_env({:"::", [], [{:foo, [], [_env | rest]}, res]}, true) do
{:"::", [], [{:foo, [], rest}, res]}
end
end

def code_lens(uri, text) do
resp =
for {_, line, {mod, fun, arity}, contract} <- Server.suggest_contracts(uri),
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) do
spec = ContractTranslator.translate_contract(fun, contract, is_macro) do
%{
"range" => range(line - 1, 0, line - 1, 0),
"command" => %{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
|> do_process_chunked(fn chunk ->
for {module, path} <- chunk,
{function, arity} <- module.module_info(:exports) do
{function, arity} = strip_macro_prefix({function, arity})
{function, arity} = SourceFile.strip_macro_prefix({function, arity})
line = find_function_line(module, function, arity, path)

build_result(:functions, {module, function, arity}, path, line)
Expand Down Expand Up @@ -414,7 +414,7 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
# TODO: Don't call into here directly
{{callback, arity}, [{:type, line, _, _}]} <-
ElixirSense.Core.Normalized.Typespec.get_callbacks(module) do
{callback, arity} = strip_macro_prefix({callback, arity})
{callback, arity} = SourceFile.strip_macro_prefix({callback, arity})

build_result(:callbacks, {module, callback, arity}, path, line)
end
Expand Down Expand Up @@ -525,11 +525,4 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
end: %{line: line, character: 0}
}
end

defp strip_macro_prefix({function, arity}) do
case Atom.to_string(function) do
"MACRO-" <> rest -> {String.to_atom(rest), arity - 1}
_other -> {function, arity}
end
end
end
4 changes: 3 additions & 1 deletion apps/language_server/lib/language_server/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,9 @@ defmodule ElixirLS.LanguageServer.Server do

for {from, uri} <- not_dirty do
contracts =
Enum.filter(contracts, fn {file, _, _, _} -> SourceFile.path_from_uri(uri) == file end)
Enum.filter(contracts, fn {file, _, _, _, _} ->
SourceFile.path_from_uri(uri) == file
end)

GenServer.reply(from, contracts)
end
Expand Down
18 changes: 17 additions & 1 deletion apps/language_server/lib/language_server/source_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,23 @@ defmodule ElixirLS.LanguageServer.SourceFile do
false

line_text ->
Regex.match?(Regex.compile!("^\s*def\s+#{Regex.escape(to_string(fun))}"), line_text)
# when function line is taken from docs the line points to `@doc` attribute
# or first `def`/`defp`/`defmacro`/`defmacrop`/`defquard`/`defguardp`/`defdelegate` clause line if no `@doc` attribute
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
Regex.match?(
Regex.compile!(
"^\s*def((macro)|(guard)|(delegate))?p?\s+#{Regex.escape(to_string(fun))}"
),
line_text
) or
Regex.match?(Regex.compile!("^\s*@doc"), line_text)
end
end

@spec strip_macro_prefix({atom, non_neg_integer}) :: {atom, non_neg_integer}
def strip_macro_prefix({function, arity}) do
case Atom.to_string(function) do
"MACRO-" <> rest -> {String.to_atom(rest), arity - 1}
_other -> {function, arity}
end
end
end