Skip to content

Commit 224c7dc

Browse files
authored
fix(manipulatePipes): avoid crashing upon errors (#576)
* fix(manipulatePipes): return 3-tuple to avoid crashes * chore: adhere to typespec
1 parent d3fda3b commit 224c7dc

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

apps/language_server/lib/language_server/providers/execute_command/manipulate_pipes.ex

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ defmodule ElixirLS.LanguageServer.Providers.ExecuteCommand.ManipulatePipes do
5151
}) do
5252
{:ok, nil}
5353
else
54-
{:error, reason} ->
55-
{:error, reason}
54+
{:error, :pipe_not_found} ->
55+
{:error, :parse_error, "Pipe operator not found at cursor"}
56+
57+
{:error, :function_call_not_found} ->
58+
{:error, :parse_error, "Function call not found at cursor"}
59+
60+
{:error, :invalid_code} ->
61+
{:error, :parse_error, "Malformed code"}
5662

5763
error ->
5864
{:error, :server_error,
59-
"cannot execute pipe conversion, workspace/applyEdit returned #{inspect(error)}"}
65+
"Cannot execute pipe conversion, workspace/applyEdit returned #{inspect(error)}"}
6066
end
6167
end
6268

apps/language_server/test/providers/execute_command/manipulate_pipes_test.exs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ defmodule ElixirLS.LanguageServer.Providers.ExecuteCommand.ManipulatePipesTest d
104104
) == edited_text
105105
end
106106

107-
test "can pipe remote calls with ulti-line args" do
107+
test "can pipe remote calls with multi-line args" do
108108
{:ok, _} =
109109
JsonRpcMock.start_link(success_reply: {:ok, %{"applied" => true}}, test_pid: self())
110110

@@ -456,7 +456,6 @@ defmodule ElixirLS.LanguageServer.Providers.ExecuteCommand.ManipulatePipesTest d
456456
assert edited_text == expected_text
457457
end
458458

459-
# Broken
460459
test "to_pipe_at_cursor at end of function (with another function after)" do
461460
text = """
462461
defmodule Demo do
@@ -562,6 +561,38 @@ defmodule ElixirLS.LanguageServer.Providers.ExecuteCommand.ManipulatePipesTest d
562561
) == edited_text
563562
end
564563

564+
test "converts function_call_not_found to 3-tuple" do
565+
{:ok, _} =
566+
JsonRpcMock.start_link(success_reply: {:ok, %{"applied" => true}}, test_pid: self())
567+
568+
uri = "file:/some_file.ex"
569+
570+
text = """
571+
test = 1
572+
%{
573+
q:
574+
if(is_nil(test),
575+
do: max(test, is_nil(test)),
576+
else: []
577+
)
578+
}
579+
"""
580+
581+
assert_never_raises(text, uri, "toPipe")
582+
583+
assert {:error, :parse_error, "Function call not found at cursor"} =
584+
ManipulatePipes.execute(
585+
["toPipe", uri, 4, 13],
586+
%Server{
587+
source_files: %{
588+
uri => %SourceFile{
589+
text: text
590+
}
591+
}
592+
}
593+
)
594+
end
595+
565596
for {line_sep, test_name_suffix} <- [{"\r\n", "\\r\\n"}, {"\n", "\\n"}] do
566597
test "can pipe correctly when the line separator is #{test_name_suffix}" do
567598
{:ok, _} =
@@ -1140,7 +1171,7 @@ defmodule ElixirLS.LanguageServer.Providers.ExecuteCommand.ManipulatePipesTest d
11401171

11411172
assert_never_raises(text, uri, "fromPipe")
11421173

1143-
assert {:error, :pipe_not_found} =
1174+
assert {:error, :parse_error, "Pipe operator not found at cursor"} =
11441175
ManipulatePipes.execute(
11451176
["fromPipe", uri, 4, 16],
11461177
%Server{

0 commit comments

Comments
 (0)