Skip to content

Provide completions for protocol functions #83

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

Merged
merged 2 commits into from
Dec 6, 2019
Merged
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
28 changes: 28 additions & 0 deletions apps/language_server/lib/language_server/providers/completion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,34 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
end
end

defp from_completion_item(
%{
type: :protocol_function,
args: args,
spec: _spec,
name: name,
summary: summary,
arity: arity,
origin: origin
},
context
) do
def_str = if(context[:def_before] == nil, do: "def ")

full_snippet = "#{def_str}#{snippet(name, args, arity)} do\n\t$0\nend"
label = "#{def_str}#{function_label(name, args, arity)}"

%__MODULE__{
label: label,
kind: :interface,
detail: "#{origin} protocol function",
documentation: summary,
insert_text: full_snippet,
priority: 2,
filter_text: name
}
end

defp from_completion_item(%{type: :field, name: name, origin: origin}, _context) do
%__MODULE__{
label: to_string(name),
Expand Down
4 changes: 4 additions & 0 deletions apps/language_server/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule ElixirLS.LanguageServer.Mixfile do
config_path: "config/config.exs",
deps_path: "../../deps",
lockfile: "../../mix.lock",
elixirc_paths: elixirc_paths(Mix.env()),
build_embedded: false,
start_permanent: true,
build_per_environment: false,
Expand All @@ -31,4 +32,7 @@ defmodule ElixirLS.LanguageServer.Mixfile do
{:dialyxir, "~> 1.0.0-rc.6", runtime: false}
]
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
end
22 changes: 22 additions & 0 deletions apps/language_server/test/providers/completion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,26 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
assert(Enum.any?(items, fn %{"label" => label} -> label == lfn end))
end
end

test "provides completions for protocol functions" do
text = """
defimpl ElixirLS.LanguageServer.Fixtures.ExampleProtocol, for: MyModule do

#^
end
"""

{line, char} = {1, 1}
TestUtils.assert_has_cursor_char(text, line, char)
{:ok, %{"items" => items}} = Completion.completion(text, line, char, true)

completions =
items
|> Enum.filter(&(&1["detail"] =~ "protocol function"))
|> Enum.map(& &1["label"])

assert completions == [
"def my_fun(example,arg)"
]
end
end
11 changes: 11 additions & 0 deletions apps/language_server/test/support/fixtures/example_protocol.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defprotocol ElixirLS.LanguageServer.Fixtures.ExampleProtocol do
@moduledoc """
ExampleProtocol protocol used in tests.
"""

@doc """
Does what `my_fun` does for `t`
"""
@spec my_fun(t, integer) :: binary
def my_fun(example, arg)
end