Skip to content

Commit

Permalink
fix: fix builtin function matching
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Apr 10, 2024
1 parent 65cb793 commit 4b79f2f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 29 deletions.
2 changes: 2 additions & 0 deletions lib/spark/elixir_sense/entity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ defmodule Spark.ElixirSense.Entity do
builtins =
if builtins && !String.contains?(hint, ".") && lowercase_string?(hint) do
if function_exported?(Complete, :complete, 4) do
ElixirSense.Core.Normalized.Code.Fragment.cursor_context("#{inspect(builtins)}.#{hint}")

apply(Complete, :complete, [
to_string("#{inspect(builtins)}.#{hint}"),
apply(ElixirSense.Core.State.Env, :__struct__, []),
Expand Down
34 changes: 19 additions & 15 deletions lib/spark/elixir_sense/plugin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ defmodule Spark.ElixirSense.Plugin do
suggestions ->
suggestions
end
rescue
_ ->
:ignore

# rescue
# _ ->
# :ignore
end

def suggestions(hint, opts) do
Expand Down Expand Up @@ -109,9 +110,10 @@ defmodule Spark.ElixirSense.Plugin do
:ignore
end
end
rescue
_e ->
:ignore

# rescue
# _e ->
# :ignore
end

defp autocomplete_schema(
Expand Down Expand Up @@ -609,18 +611,18 @@ defmodule Spark.ElixirSense.Plugin do

constructors ->
suggestions =
Enum.map(constructors, fn
Enum.flat_map(constructors, fn
{:value, key, config} ->
option_values(key, config, hint, opts, value_type_path)
List.wrap(option_values(key, config, hint, opts, value_type_path))

{key, config} ->
option_suggestions(key, config, type)
List.wrap(option_suggestions(key, config, type))

%{__struct__: Spark.Dsl.Entity} = entity ->
entity_suggestions(entity)
List.wrap(entity_suggestions(entity))

%{__struct__: Spark.Dsl.Section} = section ->
section_suggestions(section)
List.wrap(section_suggestions(section))
end)
|> filter_matches(hint)

Expand All @@ -633,8 +635,12 @@ defmodule Spark.ElixirSense.Plugin do

defp filter_matches(hints, match) do
if match do
Enum.filter(hints, fn %{label: label} ->
apply(Matcher, :match?, [label, match])
Enum.filter(hints, fn
%{label: label} ->
apply(Matcher, :match?, [label, match])

%{name: name} ->
apply(Matcher, :match?, [name, match])
end)
else
hints
Expand Down Expand Up @@ -729,8 +735,6 @@ defmodule Spark.ElixirSense.Plugin do

config = Spark.Options.update_key_docs(config)



%{
type: :generic,
kind: :function,
Expand Down
38 changes: 28 additions & 10 deletions test/elixir_sense/plugin_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,41 @@ defmodule Spark.ElixirSense.PluginTest do
use Spark.Test.Contact
presets do
preset_with_snippet(
# ^
preset_with_fn_arg :foo,
# ^
end
end
end
"""

[cursor] = cursors(buffer)

refute %{
label: "thing",
type: :generic,
kind: :function,
detail: "Option",
documentation: "",
snippet: "thing: \"$0\""
} in Enum.take(suggestions(buffer, cursor), 1)
assert [
%{
label: "ExampleContacter",
type: :generic,
kind: :class,
insert_text: "ExampleContacter",
detail: "Spark.Test.Contact.Contacter",
documentation: ""
},
%{
args: "",
arity: 0,
name: "example",
type: :function,
origin: "Spark.Test.ContacterBuiltins",
spec: "",
metadata: %{app: :spark},
args_list: [],
summary: "",
snippet: nil,
def_arity: 0,
visibility: :public,
needed_import: nil,
needed_require: nil
}
] = Enum.sort(suggestions(buffer, cursor))
end

describe "using opts" do
Expand Down
8 changes: 4 additions & 4 deletions test/support/contact/contact.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Spark.Test.Contact do
doc: "A function that wil contact this person with a message",
type:
{:spark_function_behaviour, Spark.Test.Contact.Contacter,
{Spark.Test.Contact.Contacter.Function, 1}}
Spark.Test.ContacterBuiltins, {Spark.Test.Contact.Contacter.Function, 1}}
]
]
}
Expand Down Expand Up @@ -95,7 +95,7 @@ defmodule Spark.Test.Contact do
contacter: [
type:
{:spark_function_behaviour, Spark.Test.Contact.Contacter,
{Spark.Test.Contact.Contacter.Function, 1}}
Spark.Test.ContacterBuiltins, {Spark.Test.Contact.Contacter.Function, 1}}
]
]
}
Expand All @@ -119,7 +119,7 @@ defmodule Spark.Test.Contact do
contacter: [
type:
{:spark_function_behaviour, Spark.Test.Contact.Contacter,
{Spark.Test.Contact.Contacter.Function, 1}}
Spark.Test.ContacterBuiltins, {Spark.Test.Contact.Contacter.Function, 1}}
]
]
}
Expand Down Expand Up @@ -153,7 +153,7 @@ defmodule Spark.Test.Contact do
contacter: [
type:
{:spark_function_behaviour, Spark.Test.Contact.Contacter,
{Spark.Test.Contact.Contacter.Function, 1}}
Spark.Test.ContacterBuiltins, {Spark.Test.Contact.Contacter.Function, 1}}
]
]
}
Expand Down
10 changes: 10 additions & 0 deletions test/support/example_contacter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule ExampleContacter do
@moduledoc false
@behaviour Spark.Test.Contact.Contacter
def contact(_, _), do: {:ok, "contacted"}
end

defmodule Spark.Test.ContacterBuiltins do
@moduledoc false
def example, do: {ExampleContacter, []}
end

0 comments on commit 4b79f2f

Please sign in to comment.