diff options
author | David Schulz <david.schulz@qt.io> | 2022-05-10 11:40:56 +0200 |
---|---|---|
committer | David Schulz <david.schulz@qt.io> | 2022-05-17 04:31:06 +0000 |
commit | 0b6bc0d187363d7286e5e5be92b4939876a05178 (patch) | |
tree | 19c31330444faee1d6a04fc416a1ff0423f8154f /src/libs/languageserverprotocol/jsonrpcmessages.cpp | |
parent | 819d6ad75353e18797663ca553ce72ba56842088 (diff) | |
download | qt-creator-0b6bc0d187363d7286e5e5be92b4939876a05178.tar.gz |
LanguageClient: Move the BaseMessage to JsonRpcMessage conversion
... to the client interface. JsonRpcMessages are the only messages used
so far and no other types of messages are currently used by any of the
supported Language Servers. If a client is going to need special message
parsing it can still implement a specialized client interface and
overwrite parseCurrentMessage. This is the preparation to move receiving
and parsing data passed to and from the language server out of the GUI
thread.
Change-Id: Ibd4cd95daab7efff947273ca9e7d457de0286f47
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'src/libs/languageserverprotocol/jsonrpcmessages.cpp')
-rw-r--r-- | src/libs/languageserverprotocol/jsonrpcmessages.cpp | 87 |
1 files changed, 37 insertions, 50 deletions
diff --git a/src/libs/languageserverprotocol/jsonrpcmessages.cpp b/src/libs/languageserverprotocol/jsonrpcmessages.cpp index 517ffd5f8f..73e457547d 100644 --- a/src/libs/languageserverprotocol/jsonrpcmessages.cpp +++ b/src/libs/languageserverprotocol/jsonrpcmessages.cpp @@ -47,11 +47,16 @@ QByteArray JsonRpcMessage::toRawData() const QByteArray JsonRpcMessage::mimeType() const { - return JsonRpcMessageHandler::jsonRpcMimeType(); + return jsonRpcMimeType(); } -bool JsonRpcMessage::isValid(QString * /*errorMessage*/) const +bool JsonRpcMessage::isValid(QString *errorMessage) const { + if (!m_parseError.isEmpty()) { + if (errorMessage) + *errorMessage = m_parseError; + return false; + } return m_jsonObject[jsonRpcVersionKey] == "2.0"; } @@ -66,38 +71,6 @@ JsonRpcMessage::JsonRpcMessage() m_jsonObject[jsonRpcVersionKey] = "2.0"; } -JsonRpcMessage::JsonRpcMessage(const QJsonObject &jsonObject) - : m_jsonObject(jsonObject) -{ } - -JsonRpcMessage::JsonRpcMessage(QJsonObject &&jsonObject) - : m_jsonObject(std::move(jsonObject)) -{ } - -QByteArray JsonRpcMessageHandler::jsonRpcMimeType() -{ - return "application/vscode-jsonrpc"; -} - -void JsonRpcMessageHandler::parseContent(const QByteArray &content, - QTextCodec *codec, - QString &parseError, - const ResponseHandlers &responseHandlers, - const MethodHandler &methodHandler) -{ - const QJsonObject &jsonObject = toJsonObject(content, codec, parseError); - if (jsonObject.isEmpty()) - return; - - const MessageId id(jsonObject.value(idKey)); - const QString &method = jsonObject.value(methodKey).toString(); - const JsonRpcMessage jsonContent(jsonObject); - if (method.isEmpty()) - responseHandlers(id, jsonContent); - else - methodHandler(method, id, jsonContent); -} - constexpr int utf8mib = 106; static QString docTypeName(const QJsonDocument &doc) @@ -113,29 +86,43 @@ static QString docTypeName(const QJsonDocument &doc) return {}; } -QJsonObject JsonRpcMessageHandler::toJsonObject(const QByteArray &_content, - QTextCodec *codec, - QString &parseError) +JsonRpcMessage::JsonRpcMessage(const BaseMessage &message) { - if (_content.isEmpty()) - return QJsonObject(); + if (message.content.isEmpty()) + return; QByteArray content; - if (codec && codec->mibEnum() != utf8mib) { + if (message.codec && message.codec->mibEnum() != utf8mib) { QTextCodec *utf8 = QTextCodec::codecForMib(utf8mib); if (utf8) - content = utf8->fromUnicode(codec->toUnicode(_content)); + content = utf8->fromUnicode(message.codec->toUnicode(message.content)); } if (content.isEmpty()) - content = _content; - QJsonParseError error = {0 , QJsonParseError::NoError}; + content = message.content; + QJsonParseError error = {0, QJsonParseError::NoError}; const QJsonDocument doc = QJsonDocument::fromJson(content, &error); - if (doc.isObject()) - return doc.object(); - if (doc.isNull()) - parseError = tr("Could not parse JSON message \"%1\".").arg(error.errorString()); - else - parseError = tr("Expected a JSON object, but got a JSON \"%1\" value.").arg(docTypeName(doc)); - return QJsonObject(); + if (doc.isObject()) { + m_jsonObject = doc.object(); + } else if (doc.isNull()) { + m_parseError = tr("LanguageServerProtocol::JsonRpcMessage", + "Could not parse JSON message \"%1\".") + .arg(error.errorString()); + } else { + m_parseError = tr("Expected a JSON object, but got a JSON \"%1\" value.") + .arg(docTypeName(doc)); + } +} + +JsonRpcMessage::JsonRpcMessage(const QJsonObject &jsonObject) + : m_jsonObject(jsonObject) +{ } + +JsonRpcMessage::JsonRpcMessage(QJsonObject &&jsonObject) + : m_jsonObject(std::move(jsonObject)) +{ } + +QByteArray JsonRpcMessage::jsonRpcMimeType() +{ + return "application/vscode-jsonrpc"; } CancelRequest::CancelRequest(const CancelParameter ¶ms) |