From 6a8f7ae53b339dca231d92fa1a2803be62a94794 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Mon, 13 May 2024 12:13:07 +0200 Subject: [PATCH] LanguageClient: Let clients opt out of sending the didSave message ... and make use of it in ClangdClient. When a file gets saved, clangd checks all open documents for whether they are affected by that. While the answer is usually "no", there is still one thread per document being started to find that out, which triggers users who keep a close watch on CPU usage. To alleviate this, we now send the didSave message only for header files. Fixes: QTCREATORBUG-28580 Change-Id: Id761cec8b04e95a795c8bd8754466bf03b6f2c28 Reviewed-by: Reviewed-by: David Schulz --- src/plugins/clangcodemodel/clangdclient.cpp | 10 ++++++++++ src/plugins/clangcodemodel/clangdclient.h | 2 +- src/plugins/languageclient/client.cpp | 2 +- src/plugins/languageclient/client.h | 1 + 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/plugins/clangcodemodel/clangdclient.cpp b/src/plugins/clangcodemodel/clangdclient.cpp index d3ede30b10a..c6cbd2185da 100644 --- a/src/plugins/clangcodemodel/clangdclient.cpp +++ b/src/plugins/clangcodemodel/clangdclient.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -767,6 +768,15 @@ QList ClangdClient::additionalDocumentHighlights( qobject_cast(editorWidget), cursor); } +bool ClangdClient::shouldSendDidSave(const TextEditor::TextDocument *doc) const +{ + for (const Project * const p : ProjectManager::projects()) { + if (const Node * const n = p->nodeForFilePath(doc->filePath())) + return n->asFileNode() && n->asFileNode()->fileType() == FileType::Header; + } + return CppEditor::ProjectFile::isHeader(doc->filePath()); +} + RefactoringFilePtr ClangdClient::createRefactoringFile(const FilePath &filePath) const { return CppEditor::CppRefactoringChanges(CppEditor::CppModelManager::snapshot()).file(filePath); diff --git a/src/plugins/clangcodemodel/clangdclient.h b/src/plugins/clangcodemodel/clangdclient.h index c0f687ea1fc..5a12c8f56d7 100644 --- a/src/plugins/clangcodemodel/clangdclient.h +++ b/src/plugins/clangcodemodel/clangdclient.h @@ -148,7 +148,7 @@ private: bool fileBelongsToProject(const Utils::FilePath &filePath) const override; QList additionalDocumentHighlights( TextEditor::TextEditorWidget *editorWidget, const QTextCursor &cursor) override; - + bool shouldSendDidSave(const TextEditor::TextDocument *doc) const override; class Private; class VirtualFunctionAssistProcessor; diff --git a/src/plugins/languageclient/client.cpp b/src/plugins/languageclient/client.cpp index f56538d6cbe..c12693e4808 100644 --- a/src/plugins/languageclient/client.cpp +++ b/src/plugins/languageclient/client.cpp @@ -1173,7 +1173,7 @@ void Client::documentContentsSaved(TextEditor::TextDocument *document) includeText = saveOptions->includeText().value_or(includeText); } } - if (!send) + if (!send || !shouldSendDidSave(document)) return; DidSaveTextDocumentParams params( TextDocumentIdentifier(hostPathToServerUri(document->filePath()))); diff --git a/src/plugins/languageclient/client.h b/src/plugins/languageclient/client.h index a881d178242..c80e458a472 100644 --- a/src/plugins/languageclient/client.h +++ b/src/plugins/languageclient/client.h @@ -235,6 +235,7 @@ private: const Utils::FilePath &candidate); virtual QList additionalDocumentHighlights( TextEditor::TextEditorWidget *, const QTextCursor &) { return {}; } + virtual bool shouldSendDidSave(const TextEditor::TextDocument *) const { return true; } }; } // namespace LanguageClient