diff --git a/src/plugins/languageclient/semantichighlightsupport.cpp b/src/plugins/languageclient/semantichighlightsupport.cpp index 24fb346ff0b..66d6742b2bc 100644 --- a/src/plugins/languageclient/semantichighlightsupport.cpp +++ b/src/plugins/languageclient/semantichighlightsupport.cpp @@ -192,8 +192,9 @@ void SemanticTokenSupport::reloadSemanticTokens(TextDocument *textDocument) return; const Utils::FilePath filePath = textDocument->filePath(); const TextDocumentIdentifier docId(DocumentUri::fromFilePath(filePath)); - auto responseCallback = [this, filePath](const SemanticTokensFullRequest::Response &response){ - handleSemanticTokens(filePath, response.result().value_or(nullptr)); + auto responseCallback = [this, filePath, documentVersion = m_client->documentVersion(filePath)]( + const SemanticTokensFullRequest::Response &response) { + handleSemanticTokens(filePath, response.result().value_or(nullptr), documentVersion); }; /*if (supportedRequests.testFlag(SemanticRequestType::Range)) { const int start = widget->firstVisibleBlockNumber(); @@ -223,15 +224,18 @@ void SemanticTokenSupport::updateSemanticTokens(TextDocument *textDocument) const SemanticRequestTypes supportedRequests = supportedSemanticRequests(textDocument); if (supportedRequests.testFlag(SemanticRequestType::FullDelta)) { const Utils::FilePath filePath = textDocument->filePath(); - const QString &previousResultId = m_tokens.value(filePath).resultId().value_or(QString()); + const QString &previousResultId = m_tokens.value(filePath).tokens.resultId().value_or(QString()); if (!previousResultId.isEmpty()) { SemanticTokensDeltaParams params; params.setTextDocument(TextDocumentIdentifier(DocumentUri::fromFilePath(filePath))); params.setPreviousResultId(previousResultId); SemanticTokensFullDeltaRequest request(params); request.setResponseCallback( - [this, filePath](const SemanticTokensFullDeltaRequest::Response &response) { - handleSemanticTokensDelta(filePath, response.result().value_or(nullptr)); + [this, filePath, documentVersion = m_client->documentVersion(filePath)]( + const SemanticTokensFullDeltaRequest::Response &response) { + handleSemanticTokensDelta(filePath, + response.result().value_or(nullptr), + documentVersion); }); m_client->sendContent(request); return; @@ -369,10 +373,11 @@ SemanticRequestTypes SemanticTokenSupport::supportedSemanticRequests(TextDocumen } void SemanticTokenSupport::handleSemanticTokens(const Utils::FilePath &filePath, - const SemanticTokensResult &result) + const SemanticTokensResult &result, + int documentVersion) { if (auto tokens = Utils::get_if(&result)) { - m_tokens[filePath] = *tokens; + m_tokens[filePath] = {*tokens, documentVersion}; highlight(filePath); } else { m_tokens.remove(filePath); @@ -380,10 +385,12 @@ void SemanticTokenSupport::handleSemanticTokens(const Utils::FilePath &filePath, } void SemanticTokenSupport::handleSemanticTokensDelta( - const Utils::FilePath &filePath, const LanguageServerProtocol::SemanticTokensDeltaResult &result) + const Utils::FilePath &filePath, + const LanguageServerProtocol::SemanticTokensDeltaResult &result, + int documentVersion) { if (auto tokens = Utils::get_if(&result)) { - m_tokens[filePath] = *tokens; + m_tokens[filePath] = {*tokens, documentVersion}; } else if (auto tokensDelta = Utils::get_if(&result)) { QList edits = tokensDelta->edits(); if (edits.isEmpty()) { @@ -393,7 +400,8 @@ void SemanticTokenSupport::handleSemanticTokensDelta( Utils::sort(edits, &SemanticTokensEdit::start); - SemanticTokens &tokens = m_tokens[filePath]; + m_tokens[filePath].version = documentVersion; + SemanticTokens &tokens = m_tokens[filePath].tokens; const QList &data = tokens.data(); int newDataSize = data.size(); @@ -443,8 +451,8 @@ void SemanticTokenSupport::highlight(const Utils::FilePath &filePath) SyntaxHighlighter *highlighter = doc->syntaxHighlighter(); if (!highlighter) return; - const QList tokens = m_tokens.value(filePath).toTokens(m_tokenTypes, - m_tokenModifiers); + const QList tokens = m_tokens.value(filePath).tokens.toTokens(m_tokenTypes, + m_tokenModifiers); if (m_tokensHandler) { int line = 1; int column = 1; diff --git a/src/plugins/languageclient/semantichighlightsupport.h b/src/plugins/languageclient/semantichighlightsupport.h index 03fc2445fa1..4a8a639f319 100644 --- a/src/plugins/languageclient/semantichighlightsupport.h +++ b/src/plugins/languageclient/semantichighlightsupport.h @@ -86,16 +86,23 @@ private: LanguageServerProtocol::SemanticRequestTypes supportedSemanticRequests( TextEditor::TextDocument *document) const; void handleSemanticTokens(const Utils::FilePath &filePath, - const LanguageServerProtocol::SemanticTokensResult &result); + const LanguageServerProtocol::SemanticTokensResult &result, + int documentVersion); void handleSemanticTokensDelta(const Utils::FilePath &filePath, - const LanguageServerProtocol::SemanticTokensDeltaResult &result); + const LanguageServerProtocol::SemanticTokensDeltaResult &result, + int documentVersion); void highlight(const Utils::FilePath &filePath); void updateFormatHash(); void currentEditorChanged(); Client *m_client = nullptr; - QHash m_tokens; + struct VersionedTokens + { + LanguageServerProtocol::SemanticTokens tokens; + int version; + }; + QHash m_tokens; QList m_tokenTypes; QList m_tokenModifiers; QHash m_formatHash;