diff options
author | David Schulz <david.schulz@qt.io> | 2020-10-29 12:38:05 +0100 |
---|---|---|
committer | David Schulz <david.schulz@qt.io> | 2020-12-10 09:31:31 +0000 |
commit | f440dfe43a286ef1d84d4fe1e281ea8996e64148 (patch) | |
tree | 501bfd026f676a0722620ec4ef5c72449bcb0d1d /src | |
parent | 558532b45f257b58410e9b6d890729d514fdc43a (diff) | |
download | qt-creator-f440dfe43a286ef1d84d4fe1e281ea8996e64148.tar.gz |
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>
Diffstat (limited to 'src')
-rw-r--r-- | src/libs/languageserverprotocol/icontent.h | 11 | ||||
-rw-r--r-- | src/libs/languageserverprotocol/jsonrpcmessages.h | 13 | ||||
-rw-r--r-- | src/plugins/languageclient/client.cpp | 7 | ||||
-rw-r--r-- | src/plugins/languageclient/client.h | 3 |
4 files changed, 23 insertions, 11 deletions
diff --git a/src/libs/languageserverprotocol/icontent.h b/src/libs/languageserverprotocol/icontent.h index e689f50072..fb1b3b05fc 100644 --- a/src/libs/languageserverprotocol/icontent.h +++ b/src/libs/languageserverprotocol/icontent.h @@ -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()); } diff --git a/src/libs/languageserverprotocol/jsonrpcmessages.h b/src/libs/languageserverprotocol/jsonrpcmessages.h index 00caab62f6..d0d3189e00 100644 --- a/src/libs/languageserverprotocol/jsonrpcmessages.h +++ b/src/libs/languageserverprotocol/jsonrpcmessages.h @@ -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 diff --git a/src/plugins/languageclient/client.cpp b/src/plugins/languageclient/client.cpp index 1dca3e50cb..779af127a0 100644 --- a/src/plugins/languageclient/client.cpp +++ b/src/plugins/languageclient/client.cpp @@ -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); diff --git a/src/plugins/languageclient/client.h b/src/plugins/languageclient/client.h index 6300c07688..b22bb56a10 100644 --- a/src/plugins/languageclient/client.h +++ b/src/plugins/languageclient/client.h @@ -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; |