Skip to content

Commit

Permalink
Provide completions for protocol functions (elixir-editors#83)
Browse files Browse the repository at this point in the history
* provide completions for protocol functions

* make test not depend on std lib
  • Loading branch information
lukaszsamson authored and axelson committed Dec 6, 2019
1 parent dfa8116 commit 9ad535f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
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

0 comments on commit 9ad535f

Please sign in to comment.