Skip to content

Commit f6e23cc

Browse files
committed
Refactor handle request.
1 parent 490ba63 commit f6e23cc

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

libsolidity/lsp/LanguageServer.cpp

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ vector<Declaration const*> allAnnotatedDeclarations(Identifier const* _identifie
127127
LanguageServer::LanguageServer(Logger _logger, unique_ptr<Transport> _transport):
128128
m_client{move(_transport)},
129129
m_handlers{
130-
{"$/cancelRequest", {} }, // Don't do anything for now, as we're synchronous.
131-
{"cancelRequest", {} }, // Don't do anything for now, as we're synchronous.
130+
{"$/cancelRequest", [](auto, auto) {/*nothing for now as we are synchronous */} },
131+
{"cancelRequest", [](auto, auto) {/*nothing for now as we are synchronous */} },
132132
{"initialize", bind(&LanguageServer::handleInitialize, this, _1, _2)},
133-
{"initialized", {} },
133+
{"initialized", [](auto, auto) {} },
134134
{"shutdown", [this](auto, auto) { m_shutdownRequested = true; }},
135135
{"textDocument/definition", [this](auto _id, auto _args) { handleGotoDefinition(_id, _args); }},
136136
{"textDocument/didChange", bind(&LanguageServer::handleTextDocumentDidChange, this, _1, _2)},
@@ -381,16 +381,25 @@ bool LanguageServer::run()
381381
{
382382
while (!m_exitRequested && !m_client->closed())
383383
{
384-
if (optional<Json::Value> const jsonMessage = m_client->receive(); jsonMessage.has_value())
384+
optional<Json::Value> const jsonMessage = m_client->receive();
385+
if (!jsonMessage)
386+
continue;
387+
388+
try
385389
{
386-
try
387-
{
388-
handleMessage(jsonMessage.value());
389-
}
390-
catch (exception const& e)
391-
{
392-
log("Unhandled exception caught when handling message. "s + e.what());
390+
string const methodName = _jsonMessage["method"].asString();
391+
392+
MessageID const id = _jsonMessage["id"];
393+
394+
if (auto handler = valueOrDefault(m_handlers, methodName))
395+
handler->second(id, _jsonMessage["params"]);
393396
}
397+
else
398+
m_client->error(id, ErrorCode::MethodNotFound, "Unknown method " + methodName);
399+
}
400+
catch (exception const& e)
401+
{
402+
log("Unhandled exception caught when handling message. "s + e.what());
394403
}
395404
}
396405
return m_shutdownRequested;
@@ -661,24 +670,4 @@ void LanguageServer::trace(string const& _message)
661670
m_logger(_message);
662671
}
663672

664-
void LanguageServer::handleMessage(Json::Value const& _jsonMessage)
665-
{
666-
string const methodName = _jsonMessage["method"].asString();
667-
668-
MessageID const id = _jsonMessage["id"].isInt() ?
669-
MessageID{to_string(_jsonMessage["id"].asInt())} :
670-
_jsonMessage["id"].isString() ?
671-
MessageID{_jsonMessage["id"].asString()} :
672-
MessageID{};
673-
674-
auto const handler = m_handlers.find(methodName);
675-
if (handler == m_handlers.end())
676-
m_client->error(id, ErrorCode::MethodNotFound, "Unknown method " + methodName);
677-
else if (handler->second)
678-
{
679-
Json::Value const& jsonArgs = _jsonMessage["params"];
680-
handler->second(id, jsonArgs);
681-
}
682-
}
683-
684673
} // namespace solidity

libsolidity/lsp/LanguageServer.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ class LanguageServer
7979
/// @return boolean indicating normal or abnormal termination.
8080
bool run();
8181

82-
/// Handles a single JSON-RPC message in string form.
83-
void handleMessage(std::string const& _message);
84-
85-
/// Handles a single JSON-RPC message.
86-
void handleMessage(Json::Value const& _jsonMessage);
87-
8882
protected:
8983
void handleInitialize(MessageID _id, Json::Value const& _args);
9084
void handleExit(MessageID _id, Json::Value const& _args);

0 commit comments

Comments
 (0)