|
| 1 | +defmodule NextLS.CredoExtension do |
| 2 | + @moduledoc false |
| 3 | + use GenServer |
| 4 | + |
| 5 | + alias GenLSP.Enumerations.DiagnosticSeverity |
| 6 | + alias GenLSP.Structures.CodeDescription |
| 7 | + alias GenLSP.Structures.Diagnostic |
| 8 | + alias GenLSP.Structures.Position |
| 9 | + alias GenLSP.Structures.Range |
| 10 | + alias NextLS.DiagnosticCache |
| 11 | + alias NextLS.Runtime |
| 12 | + |
| 13 | + def start_link(args) do |
| 14 | + GenServer.start_link( |
| 15 | + __MODULE__, |
| 16 | + Keyword.take(args, [:cache, :registry, :publisher, :task_supervisor]), |
| 17 | + Keyword.take(args, [:name]) |
| 18 | + ) |
| 19 | + end |
| 20 | + |
| 21 | + @impl GenServer |
| 22 | + def init(args) do |
| 23 | + cache = Keyword.fetch!(args, :cache) |
| 24 | + registry = Keyword.fetch!(args, :registry) |
| 25 | + publisher = Keyword.fetch!(args, :publisher) |
| 26 | + task_supervisor = Keyword.fetch!(args, :task_supervisor) |
| 27 | + |
| 28 | + Registry.register(registry, :extensions, :credo) |
| 29 | + |
| 30 | + {:ok, |
| 31 | + %{ |
| 32 | + runtimes: [], |
| 33 | + cache: cache, |
| 34 | + registry: registry, |
| 35 | + task_supervisor: task_supervisor, |
| 36 | + publisher: publisher, |
| 37 | + refresh_refs: Map.new() |
| 38 | + }} |
| 39 | + end |
| 40 | + |
| 41 | + def handle_info({:runtime_ready, _name, runtime_pid}, state) do |
| 42 | + state = |
| 43 | + case Runtime.call(runtime_pid, {Code, :ensure_loaded?, [Credo]}) do |
| 44 | + {:ok, true} -> |
| 45 | + :next_ls |
| 46 | + |> :code.priv_dir() |
| 47 | + |> Path.join("monkey/_next_ls_private_credo.ex") |
| 48 | + |> then(&Runtime.call(runtime_pid, {Code, :compile_file, [&1]})) |
| 49 | + |
| 50 | + Runtime.call(runtime_pid, {Application, :ensure_all_started, [:credo]}) |
| 51 | + |
| 52 | + Runtime.call(runtime_pid, {GenServer, :call, [Credo.CLI.Output.Shell, {:suppress_output, true}]}) |
| 53 | + |
| 54 | + update_in(state.runtimes, &[runtime_pid | &1]) |
| 55 | + |
| 56 | + _ -> |
| 57 | + state |
| 58 | + end |
| 59 | + |
| 60 | + {:noreply, state} |
| 61 | + end |
| 62 | + |
| 63 | + @impl GenServer |
| 64 | + def handle_info({:compiler, _diagnostics}, state) do |
| 65 | + refresh_refs = |
| 66 | + dispatch(state.registry, :runtimes, fn entries -> |
| 67 | + for {runtime, %{path: path}} <- entries, runtime in state.runtimes, into: %{} do |
| 68 | + namespace = {:credo, path} |
| 69 | + DiagnosticCache.clear(state.cache, namespace) |
| 70 | + |
| 71 | + task = |
| 72 | + Task.Supervisor.async_nolink(state.task_supervisor, fn -> |
| 73 | + case Runtime.call(runtime, {:_next_ls_private_credo, :issues, [path]}) do |
| 74 | + {:ok, issues} -> issues |
| 75 | + _error -> [] |
| 76 | + end |
| 77 | + end) |
| 78 | + |
| 79 | + {task.ref, namespace} |
| 80 | + end |
| 81 | + end) |
| 82 | + |
| 83 | + send(state.publisher, :publish) |
| 84 | + |
| 85 | + {:noreply, put_in(state.refresh_refs, refresh_refs)} |
| 86 | + end |
| 87 | + |
| 88 | + def handle_info({ref, issues}, %{refresh_refs: refs} = state) when is_map_key(refs, ref) do |
| 89 | + Process.demonitor(ref, [:flush]) |
| 90 | + {{:credo, path} = namespace, refs} = Map.pop(refs, ref) |
| 91 | + |
| 92 | + for issue <- issues do |
| 93 | + diagnostic = %Diagnostic{ |
| 94 | + range: %Range{ |
| 95 | + start: %Position{ |
| 96 | + line: issue.line_no - 1, |
| 97 | + character: (issue.column || 1) - 1 |
| 98 | + }, |
| 99 | + end: %Position{ |
| 100 | + line: issue.line_no - 1, |
| 101 | + character: issue.column || 1 |
| 102 | + } |
| 103 | + }, |
| 104 | + severity: category_to_severity(issue.category), |
| 105 | + data: %{check: issue.check, file: issue.filename}, |
| 106 | + source: "credo", |
| 107 | + code: Macro.to_string(issue.check), |
| 108 | + code_description: %CodeDescription{ |
| 109 | + href: "https://hexdocs.pm/credo/#{Macro.to_string(issue.check)}.html" |
| 110 | + }, |
| 111 | + message: issue.message |
| 112 | + } |
| 113 | + |
| 114 | + DiagnosticCache.put(state.cache, namespace, Path.join(path, issue.filename), diagnostic) |
| 115 | + end |
| 116 | + |
| 117 | + send(state.publisher, :publish) |
| 118 | + |
| 119 | + {:noreply, put_in(state.refresh_refs, refs)} |
| 120 | + end |
| 121 | + |
| 122 | + def handle_info({:DOWN, ref, :process, _pid, _reason}, %{refresh_refs: refs} = state) when is_map_key(refs, ref) do |
| 123 | + {_, refs} = Map.pop(refs, ref) |
| 124 | + |
| 125 | + {:noreply, put_in(state.refresh_refs, refs)} |
| 126 | + end |
| 127 | + |
| 128 | + defp dispatch(registry, key, callback) do |
| 129 | + ref = make_ref() |
| 130 | + me = self() |
| 131 | + |
| 132 | + Registry.dispatch(registry, key, fn entries -> |
| 133 | + result = callback.(entries) |
| 134 | + |
| 135 | + send(me, {ref, result}) |
| 136 | + end) |
| 137 | + |
| 138 | + receive do |
| 139 | + {^ref, result} -> result |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + defp category_to_severity(:refactor), do: DiagnosticSeverity.error() |
| 144 | + defp category_to_severity(:warning), do: DiagnosticSeverity.warning() |
| 145 | + defp category_to_severity(:design), do: DiagnosticSeverity.information() |
| 146 | + |
| 147 | + defp category_to_severity(:consistency), do: DiagnosticSeverity.information() |
| 148 | + |
| 149 | + defp category_to_severity(:readability), do: DiagnosticSeverity.information() |
| 150 | +end |
0 commit comments