Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix requestMessage doesnt seem to match spec #139

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions apps/language_server/lib/language_server/json_rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ defmodule ElixirLS.LanguageServer.JsonRpc do
end
end

defmacro request(id, method) do
quote do
%{
"id" => unquote(id),
"method" => unquote(method),
"jsonrpc" => "2.0"
}
end
end

defmacro request(id, method, params) do
quote do
%{
Expand Down
43 changes: 28 additions & 15 deletions apps/language_server/lib/language_server/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,11 @@ defmodule ElixirLS.LanguageServer.Server do

@impl GenServer
def handle_cast({:receive_packet, request(id, _, _) = packet}, state) do
state =
case handle_request(packet, state) do
{:ok, result, state} ->
JsonRpc.respond(id, result)
state

{:error, type, msg, state} ->
JsonRpc.respond_with_error(id, type, msg)
state

{:async, fun, state} ->
{pid, _ref} = handle_request_async(id, fun)
%{state | requests: Map.put(state.requests, id, pid)}
end
{:noreply, handle_request_packet(id, packet, state)}
end

{:noreply, state}
def handle_cast({:receive_packet, request(id, method)}, state) do
{:noreply, handle_request_packet(id, request(id, method, nil), state)}
end

@impl GenServer
Expand Down Expand Up @@ -317,6 +306,22 @@ defmodule ElixirLS.LanguageServer.Server do
state
end

defp handle_request_packet(id, packet, state) do
case handle_request(packet, state) do
{:ok, result, state} ->
JsonRpc.respond(id, result)
state

{:error, type, msg, state} ->
JsonRpc.respond_with_error(id, type, msg)
state

{:async, fun, state} ->
{pid, _ref} = handle_request_async(id, fun)
%{state | requests: Map.put(state.requests, id, pid)}
end
end

defp handle_request(initialize_req(_id, root_uri, client_capabilities), state) do
show_version_warnings()

Expand Down Expand Up @@ -460,7 +465,15 @@ defmodule ElixirLS.LanguageServer.Server do
{:ok, x, state}
end

defp handle_request(request(_, _) = req, state) do
handle_invalid_request(req, state)
end

defp handle_request(request(_, _, _) = req, state) do
handle_invalid_request(req, state)
end

defp handle_invalid_request(req, state) do
IO.inspect(req, label: "Unmatched request")
{:error, :invalid_request, nil, state}
end
Expand Down
10 changes: 10 additions & 0 deletions apps/language_server/test/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ defmodule ElixirLS.LanguageServer.ServerTest do
}
end

test "requests shutdown without params", %{server: server} do
Server.receive_packet(server, request(1, "shutdown"))
assert %{received_shutdown?: true} = :sys.get_state(server)
end

axelson marked this conversation as resolved.
Show resolved Hide resolved
test "requests shutdown with params", %{server: server} do
Server.receive_packet(server, request(1, "shutdown", nil))
assert %{received_shutdown?: true} = :sys.get_state(server)
end
axelson marked this conversation as resolved.
Show resolved Hide resolved

# TODO: Fix this test for the incremental formatter
@tag :pending
test "formatter", %{server: server} do
Expand Down