Skip to content

Commit

Permalink
test: more tests around dsl patches
Browse files Browse the repository at this point in the history
fix: honor the `add_extensions` option for DSL extensions
  • Loading branch information
zachdaniel committed Apr 21, 2024
1 parent cbba4d5 commit 8a42894
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
19 changes: 13 additions & 6 deletions lib/spark/elixir_sense/plugin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ defmodule Spark.ElixirSense.Plugin do
suggestions ->
suggestions
end

# rescue
# _ ->
# :ignore
rescue
_ ->
:ignore
end

def suggestions(hint, opts) do
Expand Down Expand Up @@ -602,7 +601,12 @@ defmodule Spark.ElixirSense.Plugin do
extension_kinds =
List.flatten(dsl_mod.module_info[:attributes][:spark_extension_kinds] || [])

extensions = default_extensions(dsl_mod) ++ parse_extensions(opts, extension_kinds)
extensions =
default_extensions(dsl_mod)
|> Enum.concat(parse_extensions(opts, extension_kinds))
|> Enum.flat_map(fn extension ->
[extension | extension.add_extensions()]
end)

case get_constructors(extensions, scope_path, hint, arg_index) do
[] ->
Expand Down Expand Up @@ -989,7 +993,10 @@ defmodule Spark.ElixirSense.Plugin do
extensions,
fn extension ->
try do
Enum.flat_map(apply_dsl_patches(sections(extension), extensions), fn section ->
extension
|> sections()
|> apply_dsl_patches(extensions)
|> Enum.flat_map(fn section ->
if section.name == first do
do_find_constructors(section, rest, hint, arg_index)
else
Expand Down
70 changes: 70 additions & 0 deletions test/elixir_sense/plugin_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,76 @@ defmodule Spark.ElixirSense.PluginTest do
] = result
end

test "suggesting patched entities two levels deep" do
buffer = """
defmodule MartyMcfly do
use Spark.Test.Contact,
extensions: [Spark.Test.ContactPatcher]
presets do
nested_preset do
# ^
end
end
end
"""

[cursor] = cursors(buffer)

result = suggestions(buffer, cursor)

assert [
%{
detail: "Option",
documentation: "",
kind: :function,
label: "name",
snippet: "name :$0",
type: :generic
},
%{
label: "special_preset",
type: :generic,
kind: :function,
detail: "Dsl Entity",
documentation: "",
snippet: "special_preset ${1:name}"
}
] = result
end

test "suggesting options inside of patched entities" do
buffer = """
defmodule MartyMcfly do
use Spark.Test.Contact,
extensions: [Spark.Test.ContactPatcher]
presets do
special_preset :thing do
f
# ^
end
end
end
"""

[cursor] = cursors(buffer)

result = suggestions(buffer, cursor)

assert [
%{
detail: "Option",
documentation: "",
kind: :function,
label: "foo",
snippet: "foo :$0",
type: :generic
}
] = result
end

test "entity snippets are correctly shown" do
buffer = """
defmodule DocBrown do
Expand Down
12 changes: 12 additions & 0 deletions test/support/contact/contact.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,20 @@ defmodule Spark.Test.Contact do
]
}

@nested_preset %Spark.Dsl.Section{
name: :nested_preset,
schema: [
name: [
type: :atom
]
]
}

@presets %Spark.Dsl.Section{
name: :presets,
sections: [
@nested_preset
],
entities: [
@preset,
@preset_with_fn_arg,
Expand Down
10 changes: 9 additions & 1 deletion test/support/contact/contact_patcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ defmodule Spark.Test.ContactPatcher do
schema: [
name: [
type: :atom
],
foo: [
type: :atom
]
],
auto_set_fields: [
Expand All @@ -19,5 +22,10 @@ defmodule Spark.Test.ContactPatcher do
entity: @special_preset
}

use Spark.Dsl.Extension, dsl_patches: [@section_patch]
@nested_section_patch %Spark.Dsl.Patch.AddEntity{
section_path: [:presets, :nested_preset],
entity: @special_preset
}

use Spark.Dsl.Extension, dsl_patches: [@section_patch, @nested_section_patch]
end

0 comments on commit 8a42894

Please sign in to comment.