mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
LSP: Introduces lspAssert(condition, ErrorCode, message)
This commit is contained in:
parent
c16867cb83
commit
1bd0f9570f
@ -275,20 +275,20 @@ bool LanguageServer::run()
|
|||||||
|
|
||||||
void LanguageServer::requireServerInitialized()
|
void LanguageServer::requireServerInitialized()
|
||||||
{
|
{
|
||||||
if (m_state != State::Initialized)
|
lspAssert(
|
||||||
BOOST_THROW_EXCEPTION(
|
m_state == State::Initialized,
|
||||||
RequestError(ErrorCode::ServerNotInitialized) <<
|
ErrorCode::ServerNotInitialized,
|
||||||
errinfo_comment("Server is not properly initialized.")
|
"Server is not properly initialized."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
|
void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
|
||||||
{
|
{
|
||||||
if (m_state != State::Started)
|
lspAssert(
|
||||||
BOOST_THROW_EXCEPTION(
|
m_state == State::Started,
|
||||||
RequestError(ErrorCode::RequestFailed) <<
|
ErrorCode::RequestFailed,
|
||||||
errinfo_comment("Initialize called at the wrong time.")
|
"Initialize called at the wrong time."
|
||||||
);
|
);
|
||||||
|
|
||||||
m_state = State::Initialized;
|
m_state = State::Initialized;
|
||||||
|
|
||||||
@ -298,11 +298,11 @@ void LanguageServer::handleInitialize(MessageID _id, Json::Value const& _args)
|
|||||||
if (Json::Value uri = _args["rootUri"])
|
if (Json::Value uri = _args["rootUri"])
|
||||||
{
|
{
|
||||||
rootPath = uri.asString();
|
rootPath = uri.asString();
|
||||||
if (!boost::starts_with(rootPath, "file://"))
|
lspAssert(
|
||||||
BOOST_THROW_EXCEPTION(
|
boost::starts_with(rootPath, "file://"),
|
||||||
RequestError(ErrorCode::InvalidParams) <<
|
ErrorCode::InvalidParams,
|
||||||
errinfo_comment("rootUri only supports file URI scheme.")
|
"rootUri only supports file URI scheme."
|
||||||
);
|
);
|
||||||
|
|
||||||
rootPath = rootPath.substr(7);
|
rootPath = rootPath.substr(7);
|
||||||
}
|
}
|
||||||
@ -334,11 +334,11 @@ void LanguageServer::handleTextDocumentDidOpen(Json::Value const& _args)
|
|||||||
{
|
{
|
||||||
requireServerInitialized();
|
requireServerInitialized();
|
||||||
|
|
||||||
if (!_args["textDocument"])
|
lspAssert(
|
||||||
BOOST_THROW_EXCEPTION(
|
_args["textDocument"],
|
||||||
RequestError(ErrorCode::RequestFailed) <<
|
ErrorCode::RequestFailed,
|
||||||
errinfo_comment("Text document parameter missing.")
|
"Text document parameter missing."
|
||||||
);
|
);
|
||||||
|
|
||||||
string text = _args["textDocument"]["text"].asString();
|
string text = _args["textDocument"]["text"].asString();
|
||||||
string uri = _args["textDocument"]["uri"].asString();
|
string uri = _args["textDocument"]["uri"].asString();
|
||||||
@ -355,28 +355,28 @@ void LanguageServer::handleTextDocumentDidChange(Json::Value const& _args)
|
|||||||
|
|
||||||
for (Json::Value jsonContentChange: _args["contentChanges"])
|
for (Json::Value jsonContentChange: _args["contentChanges"])
|
||||||
{
|
{
|
||||||
if (!jsonContentChange.isObject())
|
lspAssert(
|
||||||
BOOST_THROW_EXCEPTION(
|
jsonContentChange.isObject(),
|
||||||
RequestError(ErrorCode::RequestFailed) <<
|
ErrorCode::RequestFailed,
|
||||||
errinfo_comment("Invalid content reference.")
|
"Invalid content reference."
|
||||||
);
|
);
|
||||||
|
|
||||||
string const sourceUnitName = m_fileRepository.clientPathToSourceUnitName(uri);
|
string const sourceUnitName = m_fileRepository.clientPathToSourceUnitName(uri);
|
||||||
if (!m_fileRepository.sourceUnits().count(sourceUnitName))
|
lspAssert(
|
||||||
BOOST_THROW_EXCEPTION(
|
m_fileRepository.sourceUnits().count(sourceUnitName),
|
||||||
RequestError(ErrorCode::RequestFailed) <<
|
ErrorCode::RequestFailed,
|
||||||
errinfo_comment("Unknown file: " + uri)
|
"Unknown file: " + uri
|
||||||
);
|
);
|
||||||
|
|
||||||
string text = jsonContentChange["text"].asString();
|
string text = jsonContentChange["text"].asString();
|
||||||
if (jsonContentChange["range"].isObject()) // otherwise full content update
|
if (jsonContentChange["range"].isObject()) // otherwise full content update
|
||||||
{
|
{
|
||||||
optional<SourceLocation> change = parseRange(sourceUnitName, jsonContentChange["range"]);
|
optional<SourceLocation> change = parseRange(sourceUnitName, jsonContentChange["range"]);
|
||||||
if (!change || !change->hasText())
|
lspAssert(
|
||||||
BOOST_THROW_EXCEPTION(
|
change && change->hasText(),
|
||||||
RequestError(ErrorCode::RequestFailed) <<
|
ErrorCode::RequestFailed,
|
||||||
errinfo_comment("Invalid source range: " + jsonCompactPrint(jsonContentChange["range"]))
|
"Invalid source range: " + jsonCompactPrint(jsonContentChange["range"])
|
||||||
);
|
);
|
||||||
|
|
||||||
string buffer = m_fileRepository.sourceUnits().at(sourceUnitName);
|
string buffer = m_fileRepository.sourceUnits().at(sourceUnitName);
|
||||||
buffer.replace(static_cast<size_t>(change->start), static_cast<size_t>(change->end - change->start), move(text));
|
buffer.replace(static_cast<size_t>(change->start), static_cast<size_t>(change->end - change->start), move(text));
|
||||||
@ -392,11 +392,11 @@ void LanguageServer::handleTextDocumentDidClose(Json::Value const& _args)
|
|||||||
{
|
{
|
||||||
requireServerInitialized();
|
requireServerInitialized();
|
||||||
|
|
||||||
if (!_args["textDocument"])
|
lspAssert(
|
||||||
BOOST_THROW_EXCEPTION(
|
_args["textDocument"],
|
||||||
RequestError(ErrorCode::RequestFailed) <<
|
ErrorCode::RequestFailed,
|
||||||
errinfo_comment("Text document parameter missing.")
|
"Text document parameter missing."
|
||||||
);
|
);
|
||||||
|
|
||||||
string uri = _args["textDocument"]["uri"].asString();
|
string uri = _args["textDocument"]["uri"].asString();
|
||||||
m_openFiles.erase(uri);
|
m_openFiles.erase(uri);
|
||||||
|
@ -64,6 +64,15 @@ private:
|
|||||||
ErrorCode m_code;
|
ErrorCode m_code;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define lspAssert(condition, errorCode, errorMessage) \
|
||||||
|
if (!(condition)) \
|
||||||
|
{ \
|
||||||
|
BOOST_THROW_EXCEPTION( \
|
||||||
|
RequestError(errorCode) << \
|
||||||
|
errinfo_comment(errorMessage) \
|
||||||
|
); \
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transport layer API
|
* Transport layer API
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user