-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement callback completions (#640)
* implement new callback candidate translation
- Loading branch information
Showing
2 changed files
with
92 additions
and
8 deletions.
There are no files selected for viewing
90 changes: 86 additions & 4 deletions
90
apps/server/lib/lexical/server/code_intelligence/completion/translations/callback.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,94 @@ | ||
defmodule Lexical.Server.CodeIntelligence.Completion.Translations.Callback do | ||
alias Lexical.Ast.Env | ||
alias Lexical.Completion.Translatable | ||
alias Lexical.RemoteControl.Completion.Candidate | ||
alias Lexical.Server.CodeIntelligence.Completion.Translations | ||
alias Lexical.RemoteControl.Completion.Candidate.Callback | ||
alias Lexical.Server.CodeIntelligence.Completion | ||
|
||
defimpl Translatable, for: Candidate.Callback do | ||
defimpl Translatable, for: Callback do | ||
def translate(callback, _builder, %Env{} = env) do | ||
Translations.Callable.completion(callback, env) | ||
%Callback{ | ||
name: name, | ||
argument_names: arg_names, | ||
summary: summary | ||
} = callback | ||
|
||
%Env{line: line} = env | ||
|
||
env | ||
|> Completion.Builder.text_edit_snippet( | ||
insert_text(name, arg_names), | ||
line_range(line), | ||
label: label(name, arg_names), | ||
kind: :interface, | ||
detail: detail(callback), | ||
sort_text: sort_text(callback), | ||
filter_text: "def #{name}", | ||
documentation: summary | ||
) | ||
|> Completion.Builder.boost(5) | ||
end | ||
|
||
defp insert_text(name, arg_names) | ||
when is_binary(name) and is_list(arg_names) do | ||
impl_line(name) <> | ||
"def #{name}(#{arg_text(arg_names)}) do" <> | ||
"\n\t$0\nend" | ||
end | ||
|
||
# add tab stops and join with ", " | ||
defp arg_text(args) do | ||
args | ||
|> Enum.with_index(fn arg, i -> | ||
"${#{i + 1}:#{arg}}" | ||
end) | ||
|> Enum.join(", ") | ||
end | ||
|
||
# elixir_sense suggests child_spec/1 as a callback as it's a common idiom, | ||
# but not an actual callback of behaviours like GenServer. | ||
defp impl_line("child_spec"), do: "" | ||
|
||
# It's generally safe adding `@impl true` to callbacks as Elixir warns | ||
# of conflicting behaviours, and they're virtually non-existent anyway. | ||
defp impl_line(_), do: "@impl true\n" | ||
|
||
defp line_range(line) when is_binary(line) do | ||
start_char = | ||
case String.split(line, "def", parts: 2) do | ||
[i, _] -> String.length(i) + 1 | ||
[_] -> 0 | ||
end | ||
|
||
end_char = String.length(line) + 1 | ||
|
||
{start_char, end_char} | ||
end | ||
|
||
defp label(name, arg_names) | ||
when is_binary(name) and is_list(arg_names) do | ||
"#{name}(#{Enum.join(arg_names, ", ")})" | ||
end | ||
|
||
defp detail(%Callback{name: "child_spec"}) do | ||
"supervision specification" | ||
end | ||
|
||
defp detail(%Callback{origin: origin, metadata: %{optional: false}}) do | ||
"#{origin} callback (required)" | ||
end | ||
|
||
defp detail(%Callback{origin: origin}) do | ||
"#{origin} callback" | ||
end | ||
|
||
# cribbed from the Callable translation for now. | ||
defp sort_text(%Callback{name: name, arity: arity}) do | ||
normalized_arity = | ||
arity | ||
|> Integer.to_string() | ||
|> String.pad_leading(3, "0") | ||
|
||
"#{name}:#{normalized_arity}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters