From 2d5a981b9cce68ef6250d14dee35660a272d321e Mon Sep 17 00:00:00 2001 From: Alexis Murzeau Date: Sun, 26 Apr 2020 19:31:15 +0200 Subject: [PATCH] LSP: allow non versioned text document in SemanticHighlightingParams The proposal of SemanticHighlighting uses a versioned text document identifier, but there is a suggestion to be more coherent with other existing requests and use text document identifier instead (non versionned). See here: https://github.com/microsoft/vscode-languageserver-node/pull/367/files#r225879268 The clangd language server implements the non versioned version. Existing code was returning errors with clangd because SemanticHighlightingParams::isValid would return false because of the missing version field. Change-Id: I1c36151342437adad2405118ca6b303db0936e41 Reviewed-by: David Schulz --- .../languagefeatures.cpp | 18 ++++++++++++++++-- .../languageserverprotocol/languagefeatures.h | 5 +++-- src/plugins/languageclient/client.cpp | 13 +++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/libs/languageserverprotocol/languagefeatures.cpp b/src/libs/languageserverprotocol/languagefeatures.cpp index d76f03f0b68..29cc1b01c83 100644 --- a/src/libs/languageserverprotocol/languagefeatures.cpp +++ b/src/libs/languageserverprotocol/languagefeatures.cpp @@ -491,10 +491,24 @@ void SemanticHighlightToken::appendToByteArray(QByteArray &byteArray) const byteArray.append(char((scope & 0x00ff))); } +Utils::variant +SemanticHighlightingParams::textDocument() const +{ + VersionedTextDocumentIdentifier textDocument = fromJsonValue( + value(textDocumentKey)); + ErrorHierarchy error; + if (!textDocument.isValid(&error)) { + return TextDocumentIdentifier(textDocument); + } else { + return textDocument; + } +} + bool SemanticHighlightingParams::isValid(ErrorHierarchy *error) const { - return check(error, textDocumentKey) - && checkArray(error, linesKey); + return checkVariant(error, + textDocumentKey) + && checkArray(error, linesKey); } } // namespace LanguageServerProtocol diff --git a/src/libs/languageserverprotocol/languagefeatures.h b/src/libs/languageserverprotocol/languagefeatures.h index bee7cbd04f6..e9a1bdd537e 100644 --- a/src/libs/languageserverprotocol/languagefeatures.h +++ b/src/libs/languageserverprotocol/languagefeatures.h @@ -841,8 +841,9 @@ class LANGUAGESERVERPROTOCOL_EXPORT SemanticHighlightingParams : public JsonObje public: using JsonObject::JsonObject; - VersionedTextDocumentIdentifier textDocument() const - { return typedValue(textDocumentKey); } + Utils::variant textDocument() const; + void setTextDocument(const TextDocumentIdentifier &textDocument) + { insert(textDocumentKey, textDocument); } void setTextDocument(const VersionedTextDocumentIdentifier &textDocument) { insert(textDocumentKey, textDocument); } diff --git a/src/plugins/languageclient/client.cpp b/src/plugins/languageclient/client.cpp index 851acd8668a..6cf6ac709b7 100644 --- a/src/plugins/languageclient/client.cpp +++ b/src/plugins/languageclient/client.cpp @@ -1215,9 +1215,18 @@ void Client::handleDiagnostics(const PublishDiagnosticsParams ¶ms) void Client::handleSemanticHighlight(const SemanticHighlightingParams ¶ms) { - const DocumentUri &uri = params.textDocument().uri(); + DocumentUri uri; + LanguageClientValue version; + auto textDocument = params.textDocument(); + + if (Utils::holds_alternative(textDocument)) { + uri = Utils::get(textDocument).uri(); + version = Utils::get(textDocument).version(); + } else { + uri = Utils::get(textDocument).uri(); + } + m_highlights[uri].clear(); - const LanguageClientValue &version = params.textDocument().version(); TextEditor::TextDocument *doc = TextEditor::TextDocument::textDocumentForFilePath( uri.toFilePath());