From fd0bb8eb7f0fad86d23600f33b90537ad7da8eeb Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Tue, 26 Mar 2024 01:23:46 -0400 Subject: [PATCH] improvement: ensure offered completions are unique improvement: ensure completions are offered to piped in functions --- lib/spark/elixir_sense/plugin.ex | 17 ++++++++++++++--- test/elixir_sense/plugin_test.exs | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/spark/elixir_sense/plugin.ex b/lib/spark/elixir_sense/plugin.ex index a537ef6..6c2cf1d 100644 --- a/lib/spark/elixir_sense/plugin.ex +++ b/lib/spark/elixir_sense/plugin.ex @@ -64,7 +64,7 @@ defmodule Spark.ElixirSense.Plugin do :ignore completions -> - {:override, completions} + {:override, Enum.uniq(completions)} end else :ignore @@ -165,6 +165,7 @@ defmodule Spark.ElixirSense.Plugin do container |> cursor_path() |> Enum.reverse() + |> rewrite_pipes() |> collapse_do_blocks() |> collapse_lists() |> Enum.reverse() @@ -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 @@ -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 @@ -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 diff --git a/test/elixir_sense/plugin_test.exs b/test/elixir_sense/plugin_test.exs index 2194e33..d011747 100644 --- a/test/elixir_sense/plugin_test.exs +++ b/test/elixir_sense/plugin_test.exs @@ -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