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 Aug 8, 2022
1 parent 68fe3f7 commit d2e980a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
18 changes: 12 additions & 6 deletions apps/language_server/lib/language_server/providers/completion.ex
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 Expand Up @@ -981,14 +986,15 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
insert_text_format(:snippet)
else
insert_text_format(:plain_text)
end,
end
}

json = if item.preselect do
Map.put(json, "preselect", true)
else
json
end
json =
if item.preselect do
Map.put(json, "preselect", true)
else
json
end

# deprecated as of Language Server Protocol Specification - 3.15
json =
Expand Down
58 changes: 57 additions & 1 deletion apps/language_server/test/providers/completion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,62 @@ 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
end

describe "generic suggestions" do
Expand Down Expand Up @@ -1030,7 +1086,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 d2e980a

Please sign in to comment.