Merge pull request #12899 from ethereum/lsp-publishDiagnostics-trace-helper

Implements $/setTrace and uses trace logging.
This commit is contained in:
Mathias L. Baumann 2022-04-05 14:24:18 +02:00 committed by GitHub
commit 0944e6853f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 0 deletions

View File

@ -76,6 +76,7 @@ LanguageServer::LanguageServer(Transport& _transport):
{"exit", [this](auto, auto) { m_state = (m_state == State::ShutdownRequested ? State::ExitRequested : State::ExitWithoutShutdown); }}, {"exit", [this](auto, auto) { m_state = (m_state == State::ShutdownRequested ? State::ExitRequested : State::ExitWithoutShutdown); }},
{"initialize", bind(&LanguageServer::handleInitialize, this, _1, _2)}, {"initialize", bind(&LanguageServer::handleInitialize, this, _1, _2)},
{"initialized", [](auto, auto) {}}, {"initialized", [](auto, auto) {}},
{"$/setTrace", bind(&LanguageServer::setTrace, this, _2)},
{"shutdown", [this](auto, auto) { m_state = State::ShutdownRequested; }}, {"shutdown", [this](auto, auto) { m_state = State::ShutdownRequested; }},
{"textDocument/definition", GotoDefinition(*this) }, {"textDocument/definition", GotoDefinition(*this) },
{"textDocument/didOpen", bind(&LanguageServer::handleTextDocumentDidOpen, this, _2)}, {"textDocument/didOpen", bind(&LanguageServer::handleTextDocumentDidOpen, this, _2)},
@ -166,6 +167,13 @@ void LanguageServer::compileAndUpdateDiagnostics()
diagnosticsBySourceUnit[*location->sourceName].append(jsonDiag); diagnosticsBySourceUnit[*location->sourceName].append(jsonDiag);
} }
if (m_client.traceValue() != TraceValue::Off)
{
Json::Value extra;
extra["openFileCount"] = Json::UInt64(diagnosticsBySourceUnit.size());
m_client.trace("Number of currently open files: " + to_string(diagnosticsBySourceUnit.size()), extra);
}
m_nonemptyDiagnostics.clear(); m_nonemptyDiagnostics.clear();
for (auto&& [sourceUnitName, diagnostics]: diagnosticsBySourceUnit) for (auto&& [sourceUnitName, diagnostics]: diagnosticsBySourceUnit)
{ {
@ -273,6 +281,21 @@ void LanguageServer::handleWorkspaceDidChangeConfiguration(Json::Value const& _a
changeConfiguration(_args["settings"]); changeConfiguration(_args["settings"]);
} }
void LanguageServer::setTrace(Json::Value const& _args)
{
if (!_args["value"].isString())
// Simply ignore invalid parameter.
return;
string const stringValue = _args["value"].asString();
if (stringValue == "off")
m_client.setTrace(TraceValue::Off);
else if (stringValue == "messages")
m_client.setTrace(TraceValue::Messages);
else if (stringValue == "verbose")
m_client.setTrace(TraceValue::Verbose);
}
void LanguageServer::handleTextDocumentDidOpen(Json::Value const& _args) void LanguageServer::handleTextDocumentDidOpen(Json::Value const& _args)
{ {
requireServerInitialized(); requireServerInitialized();

View File

@ -68,6 +68,7 @@ private:
void requireServerInitialized(); void requireServerInitialized();
void handleInitialize(MessageID _id, Json::Value const& _args); void handleInitialize(MessageID _id, Json::Value const& _args);
void handleWorkspaceDidChangeConfiguration(Json::Value const& _args); void handleWorkspaceDidChangeConfiguration(Json::Value const& _args);
void setTrace(Json::Value const& _args);
void handleTextDocumentDidOpen(Json::Value const& _args); void handleTextDocumentDidOpen(Json::Value const& _args);
void handleTextDocumentDidChange(Json::Value const& _args); void handleTextDocumentDidChange(Json::Value const& _args);
void handleTextDocumentDidClose(Json::Value const& _args); void handleTextDocumentDidClose(Json::Value const& _args);

View File

@ -98,6 +98,18 @@ void IOStreamTransport::error(MessageID _id, ErrorCode _code, string _message)
send(move(json), _id); send(move(json), _id);
} }
void Transport::trace(std::string _message, Json::Value _extra)
{
if (m_logTrace != TraceValue::Off)
{
Json::Value params;
if (_extra.isObject())
params = move(_extra);
params["message"] = move(_message);
notify("$/logTrace", move(params));
}
}
void IOStreamTransport::send(Json::Value _json, MessageID _id) void IOStreamTransport::send(Json::Value _json, MessageID _id)
{ {
solAssert(_json.isObject()); solAssert(_json.isObject());

View File

@ -34,6 +34,13 @@ namespace solidity::lsp
using MessageID = Json::Value; using MessageID = Json::Value;
enum class TraceValue
{
Off,
Messages,
Verbose
};
enum class ErrorCode enum class ErrorCode
{ {
// Defined by JSON RPC // Defined by JSON RPC
@ -89,6 +96,15 @@ public:
virtual void notify(std::string _method, Json::Value _params) = 0; virtual void notify(std::string _method, Json::Value _params) = 0;
virtual void reply(MessageID _id, Json::Value _result) = 0; virtual void reply(MessageID _id, Json::Value _result) = 0;
virtual void error(MessageID _id, ErrorCode _code, std::string _message) = 0; virtual void error(MessageID _id, ErrorCode _code, std::string _message) = 0;
void trace(std::string _message, Json::Value _extra = Json::nullValue);
TraceValue traceValue() const noexcept { return m_logTrace; }
void setTrace(TraceValue _value) noexcept { m_logTrace = _value; }
private:
TraceValue m_logTrace = TraceValue::Off;
}; };
/** /**