Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow for plugins not using the ElixirSense.Plugin @behaviour #167

Merged
merged 3 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions lib/elixir_sense/core/module_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ defmodule ElixirSense.Core.ModuleStore do
@moduledoc """
Caches the module list and a list of modules keyed by the behaviour they implement.
"""
defstruct by_behaviour: %{}, list: []
defstruct by_behaviour: %{}, list: [], plugins: []

@type t :: %__MODULE__{by_behaviour: %{optional(atom) => module}, list: list(module)}
@type t :: %__MODULE__{
by_behaviour: %{optional(atom) => module},
list: list(module),
plugins: list(module)
}

alias ElixirSense.Core.Applications

Expand All @@ -20,6 +24,13 @@ defmodule ElixirSense.Core.ModuleStore do
try do
module_store = %{module_store | list: [module | module_store.list]}

module_store =
if is_plugin?(module) do
%{module_store | plugins: [module | module_store.plugins]}
else
module_store
end

module.module_info(:attributes)
|> Enum.flat_map(fn
{:behaviour, behaviours} when is_list(behaviours) ->
Expand All @@ -36,6 +47,20 @@ defmodule ElixirSense.Core.ModuleStore do
end)
end

defp is_plugin?(module) do
module.module_info(:attributes)
|> Enum.any?(fn
{:behaviour, behaviours} when is_list(behaviours) ->
ElixirSense.Plugin in behaviours

{:is_elixir_sense_plugin, true} ->
true

_ ->
false
end)
end

defp all_loaded do
Applications.get_modules_from_applications()
|> Enum.filter(fn module ->
Expand Down
11 changes: 8 additions & 3 deletions lib/elixir_sense/providers/suggestion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ defmodule ElixirSense.Providers.Suggestion do
"""
@spec find(String.t(), State.Env.t(), Metadata.t(), cursor_context, ModuleStore.t(), keyword()) ::
[suggestion()]
def find(hint, env, buffer_metadata, cursor_context, module_store, opts \\ []) do
plugins = module_store.by_behaviour[ElixirSense.Plugin] || []

def find(
hint,
env,
buffer_metadata,
cursor_context,
%{plugins: plugins} = module_store,
opts \\ []
) do
reducers =
plugins
|> Enum.filter(&function_exported?(&1, :reduce, 5))
Expand Down