Skip to content

Commit

Permalink
Suggest an appropriate module name with the 'defprotocol' snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
oo6 committed Oct 3, 2022
1 parent 9fe8ea1 commit db7b6fc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,11 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
"defmodule #{suggest_module_name(file_path)}$1 do\n\t$0\nend"
end

defp snippet_for({"Kernel", "defprotocol"}, %{file_path: file_path})
when is_binary(file_path) do
"defprotocol #{suggest_module_name(file_path)}$1 do\n\t$0\nend"
end

defp snippet_for(key, %{pipe_before?: true}) do
# Get pipe-friendly version of snippet if available, otherwise fallback to standard
Map.get(@pipe_func_snippets, key) || Map.get(@func_snippets, key)
Expand Down
86 changes: 85 additions & 1 deletion apps/language_server/test/providers/completion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,90 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
"insertText" => "defmodule $1 do\n\t$0\nend"
} = first
end

test "will suggest defprotocol with protocol_name snippet when file path matches **/lib/**/*.ex" do
text = """
defpro
# ^
"""

{line, char} = {0, 6}

TestUtils.assert_has_cursor_char(text, line, char)

assert {:ok, %{"items" => [first | _] = _items}} =
Completion.completion(
text,
line,
char,
@supports
|> Keyword.put(
:file_path,
"/some/path/my_project/lib/my_project/sub_folder/my_file.ex"
)
)

assert %{
"label" => "defprotocol",
"insertText" => "defprotocol MyProject.SubFolder.MyFile$1 do\n\t$0\nend"
} = first
end

test "will suggest defprotocol without protocol_name snippet when file path does not match expected patterns" do
text = """
defpro
# ^
"""

{line, char} = {0, 6}

TestUtils.assert_has_cursor_char(text, line, char)

assert {:ok, %{"items" => [first | _] = _items}} =
Completion.completion(
text,
line,
char,
@supports
|> Keyword.put(
:file_path,
"/some/path/my_project/lib/my_project/sub_folder/my_file.heex"
)
)

assert %{
"label" => "defprotocol",
"insertText" => "defprotocol $1 do\n\t$0\nend"
} = first
end

test "will suggest defprotocol without protocol_name snippet when file path is nil" do
text = """
defpro
# ^
"""

{line, char} = {0, 6}

TestUtils.assert_has_cursor_char(text, line, char)

assert {:ok, %{"items" => [first | _] = _items}} =
Completion.completion(
text,
line,
char,
@supports
|> Keyword.put(
:file_path
nil
)
)

assert %{
"label" => "defprotocol",
"insertText" => "defprotocol $1 do\n\t$0\nend"
} = first
end
end

describe "generic suggestions" do
Expand Down Expand Up @@ -1056,7 +1140,7 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
text = """
defmodule MyModule do
def hello do
Date.today() |>
Date.today() |>
# ^
end
end
Expand Down

0 comments on commit db7b6fc

Please sign in to comment.