mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
LSP for solcjs
This commit is contained in:
committed by
Christian Parpart
parent
c6ac1625bd
commit
e1947faa1a
@@ -68,6 +68,7 @@ int toDiagnosticSeverity(Error::Type _errorType)
|
||||
|
||||
}
|
||||
|
||||
// TODO provide constructor with custom read callback
|
||||
LanguageServer::LanguageServer(Transport& _transport):
|
||||
m_client{_transport},
|
||||
m_handlers{
|
||||
@@ -180,37 +181,9 @@ void LanguageServer::compileAndUpdateDiagnostics()
|
||||
|
||||
bool LanguageServer::run()
|
||||
{
|
||||
while (m_state != State::ExitRequested && m_state != State::ExitWithoutShutdown && !m_client.closed())
|
||||
{
|
||||
MessageID id;
|
||||
try
|
||||
{
|
||||
optional<Json::Value> const jsonMessage = m_client.receive();
|
||||
if (!jsonMessage)
|
||||
continue;
|
||||
while (isRunning())
|
||||
runIteration();
|
||||
|
||||
if ((*jsonMessage)["method"].isString())
|
||||
{
|
||||
string const methodName = (*jsonMessage)["method"].asString();
|
||||
id = (*jsonMessage)["id"];
|
||||
|
||||
if (auto handler = util::valueOrDefault(m_handlers, methodName))
|
||||
handler(id, (*jsonMessage)["params"]);
|
||||
else
|
||||
m_client.error(id, ErrorCode::MethodNotFound, "Unknown method " + methodName);
|
||||
}
|
||||
else
|
||||
m_client.error({}, ErrorCode::ParseError, "\"method\" has to be a string.");
|
||||
}
|
||||
catch (RequestError const& error)
|
||||
{
|
||||
m_client.error(id, error.code(), error.comment() ? *error.comment() : ""s);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
m_client.error(id, ErrorCode::InternalError, "Unhandled exception: "s + boost::current_exception_diagnostic_information());
|
||||
}
|
||||
}
|
||||
return m_state == State::ExitRequested;
|
||||
}
|
||||
|
||||
@@ -223,6 +196,38 @@ void LanguageServer::requireServerInitialized()
|
||||
);
|
||||
}
|
||||
|
||||
bool LanguageServer::runIteration()
|
||||
{
|
||||
MessageID id;
|
||||
try
|
||||
{
|
||||
optional<Json::Value> const jsonMessage = m_client.receive();
|
||||
if (!jsonMessage)
|
||||
return true;
|
||||
|
||||
if ((*jsonMessage)["method"].isString())
|
||||
{
|
||||
string const methodName = (*jsonMessage)["method"].asString();
|
||||
id = (*jsonMessage)["id"];
|
||||
|
||||
if (auto handler = util::valueOrDefault(m_handlers, methodName))
|
||||
handler(id, (*jsonMessage)["params"]);
|
||||
else
|
||||
m_client.error(id, ErrorCode::MethodNotFound, "Unknown method " + methodName);
|
||||
}
|
||||
}
|
||||
catch (RequestError const& error)
|
||||
{
|
||||
m_client.error(id, error.code(), error.comment() ? *error.comment() : ""s);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
m_client.error(id, ErrorCode::InternalError, "Unhandled exception: "s + boost::current_exception_diagnostic_information());
|
||||
}
|
||||
|
||||
return isRunning();
|
||||
}
|
||||
|
||||
void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
|
||||
{
|
||||
lspAssert(
|
||||
|
||||
@@ -62,6 +62,17 @@ public:
|
||||
frontend::ASTNode const* astNodeAtSourceLocation(std::string const& _sourceUnitName, langutil::LineColumn const& _filePos);
|
||||
langutil::CharStreamProvider const& charStreamProvider() const noexcept { return m_compilerStack; }
|
||||
|
||||
/// Run a single iteration of processing inputs and generating outputs.
|
||||
/// To be used when we are not in control of the event loop.
|
||||
/// @returns false if the process is supposed to terminate.
|
||||
bool runIteration();
|
||||
|
||||
/// @returns true if the server has not terminated yet, false otherwise.
|
||||
bool isRunning() const noexcept
|
||||
{
|
||||
return m_state != State::ExitRequested && m_state != State::ExitWithoutShutdown && !m_client.closed();
|
||||
}
|
||||
|
||||
private:
|
||||
/// Checks if the server is initialized (to be used by messages that need it to be initialized).
|
||||
/// Reports an error and returns false if not.
|
||||
|
||||
@@ -30,6 +30,72 @@
|
||||
using namespace std;
|
||||
using namespace solidity::lsp;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T>
|
||||
optional<T> popFromFront(std::list<T>& _queue)
|
||||
{
|
||||
if (_queue.empty())
|
||||
return nullopt;
|
||||
Json::Value message = _queue.front();
|
||||
_queue.pop_front();
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
bool MockTransport::closed() const noexcept
|
||||
{
|
||||
return m_closed;
|
||||
}
|
||||
|
||||
void MockTransport::appendInput(Json::Value _message)
|
||||
{
|
||||
solAssert(!m_closed, "");
|
||||
m_input.emplace_back(move(_message));
|
||||
}
|
||||
|
||||
optional<Json::Value> MockTransport::receive()
|
||||
{
|
||||
return popFromFront(m_input);
|
||||
}
|
||||
|
||||
optional<Json::Value> MockTransport::popOutput()
|
||||
{
|
||||
return popFromFront(m_output);
|
||||
}
|
||||
|
||||
void MockTransport::notify(string _method, Json::Value _message)
|
||||
{
|
||||
Json::Value json;
|
||||
json["method"] = move(_method);
|
||||
json["params"] = move(_message);
|
||||
send(move(json));
|
||||
}
|
||||
|
||||
void MockTransport::reply(MessageID _id, Json::Value _message)
|
||||
{
|
||||
Json::Value json;
|
||||
json["result"] = move(_message);
|
||||
send(move(json), _id);
|
||||
}
|
||||
|
||||
void MockTransport::error(MessageID _id, ErrorCode _code, string _message)
|
||||
{
|
||||
Json::Value json;
|
||||
json["error"]["code"] = static_cast<int>(_code);
|
||||
json["error"]["message"] = move(_message);
|
||||
send(move(json), _id);
|
||||
}
|
||||
|
||||
void MockTransport::send(Json::Value _json, MessageID _id)
|
||||
{
|
||||
_json["jsonrpc"] = "2.0";
|
||||
if (_id != Json::nullValue)
|
||||
_json["id"] = _id;
|
||||
|
||||
m_output.push_back(_json);
|
||||
}
|
||||
|
||||
IOStreamTransport::IOStreamTransport(istream& _in, ostream& _out):
|
||||
m_input{_in},
|
||||
m_output{_out}
|
||||
|
||||
@@ -17,12 +17,14 @@
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
#pragma once
|
||||
|
||||
#include <libsolutil/CommonIO.h>
|
||||
#include <libsolutil/Exceptions.h>
|
||||
|
||||
#include <json/value.h>
|
||||
|
||||
#include <functional>
|
||||
#include <iosfwd>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@@ -91,6 +93,26 @@ public:
|
||||
virtual void error(MessageID _id, ErrorCode _code, std::string _message) = 0;
|
||||
};
|
||||
|
||||
class MockTransport: public Transport
|
||||
{
|
||||
public:
|
||||
void close() { m_closed = true; }
|
||||
bool closed() const noexcept override;
|
||||
std::optional<Json::Value> receive() override;
|
||||
void notify(std::string _method, Json::Value _params) override;
|
||||
void reply(MessageID _id, Json::Value _result) override;
|
||||
void error(MessageID _id, ErrorCode _code, std::string _message) override;
|
||||
|
||||
void send(Json::Value _message, MessageID _id = Json::nullValue);
|
||||
std::optional<Json::Value> popOutput();
|
||||
void appendInput(Json::Value _message);
|
||||
|
||||
private:
|
||||
bool m_closed = false;
|
||||
std::list<Json::Value> m_input {};
|
||||
std::list<Json::Value> m_output {};
|
||||
};
|
||||
|
||||
/**
|
||||
* LSP Transport using JSON-RPC over iostreams.
|
||||
*/
|
||||
@@ -126,4 +148,21 @@ private:
|
||||
std::ostream& m_output;
|
||||
};
|
||||
|
||||
/**
|
||||
* LSP Transport using pure string buffers.
|
||||
* Used by solcjs.
|
||||
*/
|
||||
class BufferedTransport: public IOStreamTransport
|
||||
{
|
||||
public:
|
||||
BufferedTransport(): IOStreamTransport(m_input, m_output) {}
|
||||
|
||||
void appendInput(char const* _input) { m_input.write(_input, static_cast<std::streamsize>(strlen(_input))); }
|
||||
std::string popOutput() { return util::readUntilEnd(m_output); }
|
||||
|
||||
private:
|
||||
std::stringstream m_input;
|
||||
std::stringstream m_output;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user