Skip to content

Commit

Permalink
feat(extension,credo): ability to disable Credo extension (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Nov 2, 2023
1 parent 62a2b71 commit 6fda39e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
40 changes: 36 additions & 4 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule NextLS do

registry = Keyword.fetch!(args, :registry)

extensions = Keyword.get(args, :extensions, [NextLS.ElixirExtension, NextLS.CredoExtension])
extensions = Keyword.get(args, :extensions, elixir: NextLS.ElixirExtension, credo: NextLS.CredoExtension)
cache = Keyword.fetch!(args, :cache)
{:ok, logger} = DynamicSupervisor.start_child(dynamic_supervisor, {NextLS.Logger, lsp: lsp})

Expand Down Expand Up @@ -598,16 +598,23 @@ defmodule NextLS do
)
end

for extension <- lsp.assigns.extensions do
{:ok, _} =
for {id, extension} <- lsp.assigns.extensions do
child =
DynamicSupervisor.start_child(
lsp.assigns.dynamic_supervisor,
{extension,
settings: Map.fetch!(lsp.assigns.init_opts.extensions, id),
logger: lsp.assigns.logger,
cache: lsp.assigns.cache,
registry: lsp.assigns.registry,
publisher: self(),
task_supervisor: lsp.assigns.runtime_task_supervisor}
)

case child do
{:ok, _pid} -> :ok
:ignore -> :ok
end
end

with %{dynamic_registration: true} <- lsp.assigns.client_capabilities.workspace.did_change_watched_files do
Expand Down Expand Up @@ -1092,11 +1099,25 @@ defmodule NextLS do
defstruct completions: %{enable: false}
end

defmodule InitOpts.Extensions.Credo do
@moduledoc false
defstruct enable: true
end

defmodule InitOpts.Extensions do
@moduledoc false
defstruct elixir: %{enable: true},
credo: %NextLS.InitOpts.Extensions.Credo{}
end

defmodule InitOpts do
@moduledoc false
import Schematic

defstruct mix_target: "host", mix_env: "dev", experimental: %NextLS.InitOpts.Experimental{}
defstruct mix_target: "host",
mix_env: "dev",
experimental: %NextLS.InitOpts.Experimental{},
extensions: %NextLS.InitOpts.Extensions{}

def validate(opts) do
schematic =
Expand All @@ -1110,6 +1131,17 @@ defmodule NextLS do
map(%{
{"enable", :enable} => bool()
})
}),
optional(:extensions) =>
schema(NextLS.InitOpts.Extensions, %{
optional(:credo) =>
schema(NextLS.InitOpts.Extensions.Credo, %{
optional(:enable) => bool()
}),
optional(:elixir) =>
map(%{
{"enable", :enable} => bool()
})
})
})
)
Expand Down
37 changes: 24 additions & 13 deletions lib/next_ls/extensions/credo_extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule NextLS.CredoExtension do
def start_link(args) do
GenServer.start_link(
__MODULE__,
Keyword.take(args, [:cache, :registry, :publisher, :task_supervisor]),
Keyword.take(args, [:cache, :registry, :publisher, :task_supervisor, :settings, :logger]),
Keyword.take(args, [:name])
)
end
Expand All @@ -24,18 +24,29 @@ defmodule NextLS.CredoExtension do
registry = Keyword.fetch!(args, :registry)
publisher = Keyword.fetch!(args, :publisher)
task_supervisor = Keyword.fetch!(args, :task_supervisor)

Registry.register(registry, :extensions, :credo)

{:ok,
%{
runtimes: Map.new(),
cache: cache,
registry: registry,
task_supervisor: task_supervisor,
publisher: publisher,
refresh_refs: Map.new()
}}
settings = Keyword.fetch!(args, :settings)
logger = Keyword.fetch!(args, :logger)

if settings.enable do
Registry.register(registry, :extensions, :credo)

NextLS.Logger.log(logger, "[extension] Credo initializing with options #{inspect(settings)}")

{:ok,
%{
runtimes: Map.new(),
cache: cache,
registry: registry,
task_supervisor: task_supervisor,
publisher: publisher,
settings: settings,
logger: logger,
refresh_refs: Map.new()
}}
else
NextLS.Logger.log(logger, "[extension] Credo disabled")
:ignore
end
end

@impl GenServer
Expand Down
18 changes: 18 additions & 0 deletions test/next_ls/extensions/credo_extension_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,31 @@ defmodule NextLS.CredoExtensionTest do

setup :with_lsp

@tag init_options: %{"extensions" => %{"credo" => %{"enable" => false}}}
test "disables Credo", %{client: client, foo: foo} = context do

Check warning on line 42 in test/next_ls/extensions/credo_extension_test.exs

View workflow job for this annotation

GitHub Actions / Test (1.15.7/26.1.2)

variable "foo" is unused (if the variable is not meant to be used, prefix it with an underscore)
assert :ok == notify(client, %{method: "initialized", jsonrpc: "2.0", params: %{}})

assert_is_ready(context, "my_proj")
assert_compiled(context, "my_proj")

assert_notification "window/logMessage", %{
"message" => "[NextLS] [extension] Credo disabled",
"type" => 4
}
end

test "publishes credo diagnostics", %{client: client, foo: foo} = context do
assert :ok == notify(client, %{method: "initialized", jsonrpc: "2.0", params: %{}})

assert_is_ready(context, "my_proj")
assert_compiled(context, "my_proj")
assert_notification "$/progress", %{"value" => %{"kind" => "end", "message" => "Finished indexing!"}}

assert_notification "window/logMessage", %{
"message" => "[NextLS] [extension] Credo initializing with options" <> _,
"type" => 4
}

uri = uri(foo)

assert_notification "textDocument/publishDiagnostics", %{
Expand Down
4 changes: 3 additions & 1 deletion test/support/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ defmodule NextLS.Support.Utils do
r_tvisor = start_supervised!(Supervisor.child_spec(Task.Supervisor, id: :two))
rvisor = start_supervised!({DynamicSupervisor, [strategy: :one_for_one]})
start_supervised!({Registry, [keys: :duplicate, name: context.module]})
extensions = [NextLS.ElixirExtension, NextLS.CredoExtension]
extensions = [elixir: NextLS.ElixirExtension, credo: NextLS.CredoExtension]
cache = start_supervised!(NextLS.DiagnosticCache)
init_options = context[:init_options] || %{}

server =
server(NextLS,
Expand All @@ -67,6 +68,7 @@ defmodule NextLS.Support.Utils do
id: 1,
jsonrpc: "2.0",
params: %{
initializationOptions: init_options,
capabilities: %{
workspace: %{
workspaceFolders: true
Expand Down

0 comments on commit 6fda39e

Please sign in to comment.