Skip to content

Commit

Permalink
improvement: ensure offered completions are unique
Browse files Browse the repository at this point in the history
improvement: ensure completions are offered to piped in functions
  • Loading branch information
zachdaniel committed Mar 26, 2024
1 parent ed653ef commit fd0bb8e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/spark/elixir_sense/plugin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule Spark.ElixirSense.Plugin do
:ignore

completions ->
{:override, completions}
{:override, Enum.uniq(completions)}
end
else
:ignore
Expand Down Expand Up @@ -165,6 +165,7 @@ defmodule Spark.ElixirSense.Plugin do
container
|> cursor_path()
|> Enum.reverse()
|> rewrite_pipes()
|> collapse_do_blocks()
|> collapse_lists()
|> Enum.reverse()
Expand All @@ -178,6 +179,16 @@ defmodule Spark.ElixirSense.Plugin do
end
end

defp rewrite_pipes([{:|>, _, [params_1, {call, meta, params_rest}]}, _ | rest]) do
rewrite_pipes([{call, meta, [params_1 | params_rest || []]} | rest])
end

defp rewrite_pipes([first | rest]) do
[first | rewrite_pipes(rest)]
end

defp rewrite_pipes([]), do: []

defp handle_dangling_do_blocks([%{value_type_path: [{:keyword_key, :do, []}]} = info | rest]) do
[%{info | value_type_path: [:do_block]} | rest]
end
Expand Down Expand Up @@ -564,7 +575,7 @@ defmodule Spark.ElixirSense.Plugin do
end)

if schema do
{:override, autocomplete_schema(schema, hint, info.value_type_path, opts)}
{:override, Enum.uniq(autocomplete_schema(schema, hint, info.value_type_path, opts))}
else
:ignore
end
Expand Down Expand Up @@ -610,7 +621,7 @@ defmodule Spark.ElixirSense.Plugin do
end)
|> filter_matches(hint)

{:override, List.flatten(suggestions)}
{:override, Enum.uniq(List.flatten(suggestions))}
end
else
:ignore
Expand Down
22 changes: 22 additions & 0 deletions test/elixir_sense/plugin_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,28 @@ defmodule Spark.ElixirSense.PluginTest do
] = suggestions(buffer, cursor)
end

test "it autocompletes the opts when piping" do
buffer = """
1
|> ExampleOptions.func(2,
opt
# ^
"""

[cursor] = cursors(buffer)

assert [
%{
label: "option",
type: :generic,
kind: :function,
snippet: "option: \"$0\"",
detail: "Option",
documentation: "An option"
}
] = suggestions(buffer, cursor)
end

test "it ignores maps" do
buffer = """
ExampleOptions.func(1, 2, %{opt
Expand Down

0 comments on commit fd0bb8e

Please sign in to comment.