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: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-05-13 12:13:07 +02:00
parent c676ec825f
commit 6a8f7ae53b
4 changed files with 13 additions and 2 deletions

View File

@@ -46,6 +46,7 @@
#include <projectexplorer/devicesupport/idevice.h> #include <projectexplorer/devicesupport/idevice.h>
#include <projectexplorer/kitaspects.h> #include <projectexplorer/kitaspects.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/projecttree.h> #include <projectexplorer/projecttree.h>
#include <projectexplorer/projectmanager.h> #include <projectexplorer/projectmanager.h>
#include <projectexplorer/target.h> #include <projectexplorer/target.h>
@@ -767,6 +768,15 @@ QList<Text::Range> ClangdClient::additionalDocumentHighlights(
qobject_cast<CppEditor::CppEditorWidget *>(editorWidget), cursor); qobject_cast<CppEditor::CppEditorWidget *>(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 RefactoringFilePtr ClangdClient::createRefactoringFile(const FilePath &filePath) const
{ {
return CppEditor::CppRefactoringChanges(CppEditor::CppModelManager::snapshot()).file(filePath); return CppEditor::CppRefactoringChanges(CppEditor::CppModelManager::snapshot()).file(filePath);

View File

@@ -148,7 +148,7 @@ private:
bool fileBelongsToProject(const Utils::FilePath &filePath) const override; bool fileBelongsToProject(const Utils::FilePath &filePath) const override;
QList<Utils::Text::Range> additionalDocumentHighlights( QList<Utils::Text::Range> additionalDocumentHighlights(
TextEditor::TextEditorWidget *editorWidget, const QTextCursor &cursor) override; TextEditor::TextEditorWidget *editorWidget, const QTextCursor &cursor) override;
bool shouldSendDidSave(const TextEditor::TextDocument *doc) const override;
class Private; class Private;
class VirtualFunctionAssistProcessor; class VirtualFunctionAssistProcessor;

View File

@@ -1173,7 +1173,7 @@ void Client::documentContentsSaved(TextEditor::TextDocument *document)
includeText = saveOptions->includeText().value_or(includeText); includeText = saveOptions->includeText().value_or(includeText);
} }
} }
if (!send) if (!send || !shouldSendDidSave(document))
return; return;
DidSaveTextDocumentParams params( DidSaveTextDocumentParams params(
TextDocumentIdentifier(hostPathToServerUri(document->filePath()))); TextDocumentIdentifier(hostPathToServerUri(document->filePath())));

View File

@@ -235,6 +235,7 @@ private:
const Utils::FilePath &candidate); const Utils::FilePath &candidate);
virtual QList<Utils::Text::Range> additionalDocumentHighlights( virtual QList<Utils::Text::Range> additionalDocumentHighlights(
TextEditor::TextEditorWidget *, const QTextCursor &) { return {}; } TextEditor::TextEditorWidget *, const QTextCursor &) { return {}; }
virtual bool shouldSendDidSave(const TextEditor::TextDocument *) const { return true; }
}; };
} // namespace LanguageClient } // namespace LanguageClient