LanguageClient: Restructure request response handling

Instead of working on a Client member IContent now returns an optional
response handler that consists of an id, a name and a callback.

Change-Id: I5ca7dd4eaa7ae37f333f99c9fe60339db03ebf2c
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2020-10-29 12:38:05 +01:00
parent 558532b45f
commit f440dfe43a
4 changed files with 23 additions and 11 deletions

View File

@@ -89,7 +89,13 @@ public:
}
};
using ResponseHandler = std::function<void(const QByteArray &, QTextCodec *)>;
struct ResponseHandler
{
MessageId id;
using Callback = std::function<void(const QByteArray &, QTextCodec *)>;
Callback callback;
};
using ResponseHandlers = std::function<void(const MessageId &, const QByteArray &, QTextCodec *)>;
using MethodHandler = std::function<void(const QString &, const MessageId &, const IContent *)>;
@@ -121,7 +127,8 @@ public:
virtual QByteArray mimeType() const = 0;
virtual bool isValid(QString *errorMessage) const = 0;
virtual void registerResponseHandler(QHash<MessageId, ResponseHandler> *) const { }
virtual Utils::optional<ResponseHandler> responseHandler() const
{ return Utils::nullopt; }
BaseMessage toBaseMessage() const
{ return BaseMessage(mimeType(), toRawData()); }

View File

@@ -284,15 +284,15 @@ public:
void setResponseCallback(const ResponseCallback &callback)
{ m_callBack = callback; }
void registerResponseHandler(QHash<MessageId, ResponseHandler> *handlers) const final
Utils::optional<ResponseHandler> responseHandler() const final
{
auto callback = m_callBack;
handlers->insert(id(), [callback](const QByteArray &content, QTextCodec *codec){
auto callback = [callback = m_callBack](const QByteArray &content, QTextCodec *codec) {
if (!callback)
return;
QString parseError;
const QJsonObject &object =
JsonRpcMessageHandler::toJsonObject(content, codec, parseError);
const QJsonObject &object = JsonRpcMessageHandler::toJsonObject(content,
codec,
parseError);
Response response(object);
if (object.isEmpty()) {
ResponseError<ErrorDataType> error;
@@ -300,7 +300,8 @@ public:
response.setError(error);
}
callback(Response(object));
});
};
return Utils::make_optional(ResponseHandler{id(), callback});
}
bool isValid(QString *errorMessage) const override

View File

@@ -245,7 +245,9 @@ void Client::initialize()
initializeCallback(initResponse);
});
// directly send data otherwise the state check would fail;
initRequest.registerResponseHandler(&m_responseHandlers);
if (Utils::optional<ResponseHandler> responseHandler = initRequest.responseHandler())
m_responseHandlers[responseHandler->id] = responseHandler->callback;
LanguageClientManager::logBaseMessage(LspLogMessage::ClientMessage,
name(),
initRequest.toBaseMessage());
@@ -321,7 +323,8 @@ void Client::sendContent(const IContent &content)
QTC_ASSERT(m_clientInterface, return);
QTC_ASSERT(m_state == Initialized, return);
sendPostponedDocumentUpdates();
content.registerResponseHandler(&m_responseHandlers);
if (Utils::optional<ResponseHandler> responseHandler = content.responseHandler())
m_responseHandlers[responseHandler->id] = responseHandler->callback;
QString error;
if (!QTC_GUARD(content.isValid(&error)))
Core::MessageManager::write(error);

View File

@@ -211,7 +211,8 @@ private:
LanguageServerProtocol::MethodHandler)>;
State m_state = Uninitialized;
QHash<LanguageServerProtocol::MessageId, LanguageServerProtocol::ResponseHandler> m_responseHandlers;
QHash<LanguageServerProtocol::MessageId,
LanguageServerProtocol::ResponseHandler::Callback> m_responseHandlers;
QHash<QByteArray, ContentHandler> m_contentHandler;
QString m_displayName;
LanguageFilter m_languagFilter;