|
| 1 | +defmodule ElixirLS.LanguageServer.Experimental.Provider.CodeAction.ReplaceRemoteFunction do |
| 2 | + @moduledoc """ |
| 3 | + Code actions that replace unknown remote function with ones suggested by the warning message |
| 4 | + """ |
| 5 | + |
| 6 | + alias ElixirLS.LanguageServer.Experimental.CodeMod |
| 7 | + alias ElixirLS.LanguageServer.Experimental.CodeMod.Ast |
| 8 | + alias ElixirLS.LanguageServer.Experimental.Protocol.Types.CodeAction, as: CodeActionResult |
| 9 | + alias ElixirLS.LanguageServer.Experimental.Protocol.Types.Diagnostic |
| 10 | + alias ElixirLS.LanguageServer.Experimental.Protocol.Types.TextEdit |
| 11 | + alias ElixirLS.LanguageServer.Experimental.Protocol.Types.WorkspaceEdit |
| 12 | + alias ElixirLS.LanguageServer.Experimental.SourceFile |
| 13 | + |
| 14 | + @pattern ~r/(.*)\/(.*) is undefined or private. .*:\n(.*)/s |
| 15 | + |
| 16 | + @spec pattern() :: Regex.t() |
| 17 | + def pattern, do: @pattern |
| 18 | + |
| 19 | + @spec apply(SourceFile.t(), Diagnostic.t()) :: [CodeActionResult.t()] |
| 20 | + def apply(source_file, diagnostic) do |
| 21 | + with {:ok, module, name} <- extract_function(diagnostic.message), |
| 22 | + {:ok, suggestions} <- extract_suggestions(diagnostic.message), |
| 23 | + one_based_line = extract_line(diagnostic), |
| 24 | + {:ok, replies} <- |
| 25 | + build_code_actions(source_file, one_based_line, module, name, suggestions) do |
| 26 | + replies |
| 27 | + else |
| 28 | + _ -> |
| 29 | + [] |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + defp extract_function(message) do |
| 34 | + case Regex.scan(@pattern, message) do |
| 35 | + [[_, full_name, _, _]] -> |
| 36 | + {module, name} = separate_module_from_name(full_name) |
| 37 | + {:ok, module, name} |
| 38 | + |
| 39 | + _ -> |
| 40 | + :error |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + defp separate_module_from_name(full_name) do |
| 45 | + {name, module} = |
| 46 | + full_name |
| 47 | + |> String.split(".") |
| 48 | + |> Enum.map(&String.to_atom/1) |
| 49 | + |> List.pop_at(-1) |
| 50 | + |
| 51 | + {module, name} |
| 52 | + end |
| 53 | + |
| 54 | + @suggestion_pattern ~r/\* .*\/[\d]+/ |
| 55 | + defp extract_suggestions(message) do |
| 56 | + case Regex.scan(@pattern, message) do |
| 57 | + [[_, _, arity, suggestions_string]] -> |
| 58 | + suggestions = |
| 59 | + @suggestion_pattern |
| 60 | + |> Regex.scan(suggestions_string) |
| 61 | + |> Enum.flat_map(fn [suggestion] -> |
| 62 | + case String.split(suggestion, [" ", "/"]) do |
| 63 | + ["*", name, ^arity] -> [String.to_atom(name)] |
| 64 | + _ -> [] |
| 65 | + end |
| 66 | + end) |
| 67 | + |
| 68 | + {:ok, suggestions} |
| 69 | + |
| 70 | + _ -> |
| 71 | + :error |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + defp extract_line(%Diagnostic{} = diagnostic) do |
| 76 | + diagnostic.range.start.line |
| 77 | + end |
| 78 | + |
| 79 | + defp build_code_actions(%SourceFile{} = source_file, one_based_line, module, name, suggestions) do |
| 80 | + with {:ok, line_text} <- SourceFile.fetch_text_at(source_file, one_based_line), |
| 81 | + {:ok, line_ast} <- Ast.from(line_text), |
| 82 | + {:ok, edits_per_suggestion} <- |
| 83 | + text_edits_per_suggestion(line_text, line_ast, module, name, suggestions) do |
| 84 | + case edits_per_suggestion do |
| 85 | + [] -> |
| 86 | + :error |
| 87 | + |
| 88 | + [_ | _] -> |
| 89 | + edits_per_suggestion = |
| 90 | + Enum.map(edits_per_suggestion, fn {text_edits, suggestion} -> |
| 91 | + text_edits = Enum.map(text_edits, &update_line(&1, one_based_line)) |
| 92 | + {text_edits, suggestion} |
| 93 | + end) |
| 94 | + |
| 95 | + replies = |
| 96 | + Enum.map(edits_per_suggestion, fn {text_edits, function_name} -> |
| 97 | + CodeActionResult.new( |
| 98 | + title: construct_title(module, function_name), |
| 99 | + kind: :quick_fix, |
| 100 | + edit: WorkspaceEdit.new(changes: %{source_file.uri => text_edits}) |
| 101 | + ) |
| 102 | + end) |
| 103 | + |
| 104 | + {:ok, replies} |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + defp text_edits_per_suggestion(line_text, line_ast, module, name, suggestions) do |
| 110 | + Enum.reduce(suggestions, {:ok, []}, fn |
| 111 | + suggestion, {:ok, edits_per_suggestions} -> |
| 112 | + case CodeMod.ReplaceRemoteFunction.text_edits( |
| 113 | + line_text, |
| 114 | + line_ast, |
| 115 | + module, |
| 116 | + name, |
| 117 | + suggestion |
| 118 | + ) do |
| 119 | + {:ok, []} -> {:ok, edits_per_suggestions} |
| 120 | + {:ok, text_edits} -> {:ok, [{text_edits, suggestion} | edits_per_suggestions]} |
| 121 | + :error -> :error |
| 122 | + end |
| 123 | + |
| 124 | + _suggestion, :error -> |
| 125 | + :error |
| 126 | + end) |
| 127 | + end |
| 128 | + |
| 129 | + defp update_line(%TextEdit{} = text_edit, line_number) do |
| 130 | + text_edit |
| 131 | + |> put_in([:range, :start, :line], line_number - 1) |
| 132 | + |> put_in([:range, :end, :line], line_number - 1) |
| 133 | + end |
| 134 | + |
| 135 | + defp construct_title(module_list, function_name) do |
| 136 | + module_string = |
| 137 | + module_list |
| 138 | + |> Enum.map(fn module -> |
| 139 | + module |
| 140 | + |> Atom.to_string() |
| 141 | + |> String.trim_leading("Elixir.") |
| 142 | + end) |
| 143 | + |> Enum.join(".") |
| 144 | + |
| 145 | + "Replace function with #{module_string}.#{function_name}" |
| 146 | + end |
| 147 | +end |
0 commit comments