forked from qt-creator/qt-creator
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:
@@ -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 ResponseHandlers = std::function<void(const MessageId &, const QByteArray &, QTextCodec *)>;
|
||||||
using MethodHandler = std::function<void(const QString &, const MessageId &, const IContent *)>;
|
using MethodHandler = std::function<void(const QString &, const MessageId &, const IContent *)>;
|
||||||
|
|
||||||
@@ -121,7 +127,8 @@ public:
|
|||||||
virtual QByteArray mimeType() const = 0;
|
virtual QByteArray mimeType() const = 0;
|
||||||
virtual bool isValid(QString *errorMessage) 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
|
BaseMessage toBaseMessage() const
|
||||||
{ return BaseMessage(mimeType(), toRawData()); }
|
{ return BaseMessage(mimeType(), toRawData()); }
|
||||||
|
@@ -284,15 +284,15 @@ public:
|
|||||||
void setResponseCallback(const ResponseCallback &callback)
|
void setResponseCallback(const ResponseCallback &callback)
|
||||||
{ m_callBack = callback; }
|
{ m_callBack = callback; }
|
||||||
|
|
||||||
void registerResponseHandler(QHash<MessageId, ResponseHandler> *handlers) const final
|
Utils::optional<ResponseHandler> responseHandler() const final
|
||||||
{
|
{
|
||||||
auto callback = m_callBack;
|
auto callback = [callback = m_callBack](const QByteArray &content, QTextCodec *codec) {
|
||||||
handlers->insert(id(), [callback](const QByteArray &content, QTextCodec *codec){
|
|
||||||
if (!callback)
|
if (!callback)
|
||||||
return;
|
return;
|
||||||
QString parseError;
|
QString parseError;
|
||||||
const QJsonObject &object =
|
const QJsonObject &object = JsonRpcMessageHandler::toJsonObject(content,
|
||||||
JsonRpcMessageHandler::toJsonObject(content, codec, parseError);
|
codec,
|
||||||
|
parseError);
|
||||||
Response response(object);
|
Response response(object);
|
||||||
if (object.isEmpty()) {
|
if (object.isEmpty()) {
|
||||||
ResponseError<ErrorDataType> error;
|
ResponseError<ErrorDataType> error;
|
||||||
@@ -300,7 +300,8 @@ public:
|
|||||||
response.setError(error);
|
response.setError(error);
|
||||||
}
|
}
|
||||||
callback(Response(object));
|
callback(Response(object));
|
||||||
});
|
};
|
||||||
|
return Utils::make_optional(ResponseHandler{id(), callback});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isValid(QString *errorMessage) const override
|
bool isValid(QString *errorMessage) const override
|
||||||
|
@@ -245,7 +245,9 @@ void Client::initialize()
|
|||||||
initializeCallback(initResponse);
|
initializeCallback(initResponse);
|
||||||
});
|
});
|
||||||
// directly send data otherwise the state check would fail;
|
// 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,
|
LanguageClientManager::logBaseMessage(LspLogMessage::ClientMessage,
|
||||||
name(),
|
name(),
|
||||||
initRequest.toBaseMessage());
|
initRequest.toBaseMessage());
|
||||||
@@ -321,7 +323,8 @@ void Client::sendContent(const IContent &content)
|
|||||||
QTC_ASSERT(m_clientInterface, return);
|
QTC_ASSERT(m_clientInterface, return);
|
||||||
QTC_ASSERT(m_state == Initialized, return);
|
QTC_ASSERT(m_state == Initialized, return);
|
||||||
sendPostponedDocumentUpdates();
|
sendPostponedDocumentUpdates();
|
||||||
content.registerResponseHandler(&m_responseHandlers);
|
if (Utils::optional<ResponseHandler> responseHandler = content.responseHandler())
|
||||||
|
m_responseHandlers[responseHandler->id] = responseHandler->callback;
|
||||||
QString error;
|
QString error;
|
||||||
if (!QTC_GUARD(content.isValid(&error)))
|
if (!QTC_GUARD(content.isValid(&error)))
|
||||||
Core::MessageManager::write(error);
|
Core::MessageManager::write(error);
|
||||||
|
@@ -211,7 +211,8 @@ private:
|
|||||||
LanguageServerProtocol::MethodHandler)>;
|
LanguageServerProtocol::MethodHandler)>;
|
||||||
|
|
||||||
State m_state = Uninitialized;
|
State m_state = Uninitialized;
|
||||||
QHash<LanguageServerProtocol::MessageId, LanguageServerProtocol::ResponseHandler> m_responseHandlers;
|
QHash<LanguageServerProtocol::MessageId,
|
||||||
|
LanguageServerProtocol::ResponseHandler::Callback> m_responseHandlers;
|
||||||
QHash<QByteArray, ContentHandler> m_contentHandler;
|
QHash<QByteArray, ContentHandler> m_contentHandler;
|
||||||
QString m_displayName;
|
QString m_displayName;
|
||||||
LanguageFilter m_languagFilter;
|
LanguageFilter m_languagFilter;
|
||||||
|
Reference in New Issue
Block a user