diff --git a/src/libs/languageserverprotocol/icontent.h b/src/libs/languageserverprotocol/icontent.h index e689f500722..fb1b3b05fca 100644 --- a/src/libs/languageserverprotocol/icontent.h +++ b/src/libs/languageserverprotocol/icontent.h @@ -89,7 +89,13 @@ public: } }; -using ResponseHandler = std::function; +struct ResponseHandler +{ + MessageId id; + using Callback = std::function; + Callback callback; +}; + using ResponseHandlers = std::function; using MethodHandler = std::function; @@ -121,7 +127,8 @@ public: virtual QByteArray mimeType() const = 0; virtual bool isValid(QString *errorMessage) const = 0; - virtual void registerResponseHandler(QHash *) const { } + virtual Utils::optional 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 00caab62f60..d0d3189e00d 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 *handlers) const final + Utils::optional 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 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 1dca3e50cb8..779af127a05 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 = 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 = 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 6300c076885..b22bb56a101 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 m_responseHandlers; + QHash m_responseHandlers; QHash m_contentHandler; QString m_displayName; LanguageFilter m_languagFilter;