Skip to content

Commit 53096f7

Browse files
committed
fix code action crash when diagnostic message is MarkupContent
1 parent 7dde1e5 commit 53096f7

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

apps/language_server/lib/language_server/providers/code_action/helpers.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ defmodule ElixirLS.LanguageServer.Providers.CodeAction.Helpers do
5050
defp maybe_recover_one_line_do(updated_text, _updated_ast) do
5151
updated_text
5252
end
53+
54+
# https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#diagnostic
55+
# message can be string or MarkupContent
56+
# string
57+
def diagnostic_to_message(diagnostic) when is_binary(diagnostic), do: diagnostic
58+
59+
# MarkupContent
60+
def diagnostic_to_message(%{"kind" => kind, "value" => value})
61+
when kind in ["plaintext", "markdown"],
62+
do: value
5363
end

apps/language_server/lib/language_server/providers/code_action/replace_remote_function.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ defmodule ElixirLS.LanguageServer.Providers.CodeAction.ReplaceRemoteFunction do
3030
end
3131

3232
defp extract_function_and_line(diagnostic) do
33-
with {:ok, module, function, arity} <- extract_function(diagnostic["message"]) do
33+
message = diagnostic_to_message(diagnostic)
34+
35+
with {:ok, module, function, arity} <- extract_function(message) do
3436
{:ok, module, function, arity, diagnostic["range"]["start"]["line"]}
3537
end
3638
end

apps/language_server/lib/language_server/providers/code_action/replace_with_underscore.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ defmodule ElixirLS.LanguageServer.Providers.CodeAction.ReplaceWithUnderscore do
2828
end
2929

3030
defp extract_variable_and_line(diagnostic) do
31-
with {:ok, variable_name} <- extract_variable_name(diagnostic["message"]) do
31+
message = diagnostic_to_message(diagnostic)
32+
33+
with {:ok, variable_name} <- extract_variable_name(message) do
3234
{:ok, variable_name, diagnostic["range"]["start"]["line"]}
3335
end
3436
end

0 commit comments

Comments
 (0)