From 78274956d097603e164311db1fd448dc0df1eee9 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 29 Apr 2021 16:09:07 +0200 Subject: [PATCH] ClangCodeModel: Make open C++ files known to newly started clangd client This is relevant when opening a session, for instance. Change-Id: Iff3140296edb7b6d2b758f1f8b0b83a52fdd6f56 Reviewed-by: David Schulz --- .../clangmodelmanagersupport.cpp | 24 +++++++++++++++++-- .../clangcodemodel/clangrefactoringengine.cpp | 4 ++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp index e95bcb71d9a..32b5b31585e 100644 --- a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp +++ b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp @@ -37,6 +37,7 @@ #include "clanggloballocatorfilters.h" #include "clangoverviewmodel.h" +#include #include #include #include @@ -290,8 +291,27 @@ void ClangModelManagerSupport::updateLanguageClient(ProjectExplorer::Project *pr if (cppModelManager()->projectInfo(project) != projectInfo) return; - // TODO: We'd like to add all open editor documents for the project to the client here, - // but there doesn't seem to be such an interface. + // Acquaint the client with all open C++ documents for this project. + bool hasDocuments = false; + for (const Core::DocumentModel::Entry * const entry : Core::DocumentModel::entries()) { + const auto textDocument = qobject_cast(entry->document); + if (!textDocument) + continue; + const bool isCppDocument = Utils::contains( + Core::DocumentModel::editorsForDocument(textDocument), + [](Core::IEditor *editor) { + return CppTools::CppModelManager::isCppEditor(editor); + }); + if (!isCppDocument) + continue; + if (!project->isKnownFile(entry->fileName())) + continue; + client->openDocument(textDocument); + hasDocuments = true; + } + + if (hasDocuments) + return; // clangd oddity: Background indexing only starts after opening a random file. // TODO: changes to the compilation db do not seem to trigger re-indexing. diff --git a/src/plugins/clangcodemodel/clangrefactoringengine.cpp b/src/plugins/clangcodemodel/clangrefactoringengine.cpp index dc0e06899f7..ba418e7e032 100644 --- a/src/plugins/clangcodemodel/clangrefactoringengine.cpp +++ b/src/plugins/clangcodemodel/clangrefactoringengine.cpp @@ -97,8 +97,8 @@ void RefactoringEngine::findUsages(const CppTools::CursorInEditor &cursor, ->findUsages(cursor, std::move(callback)); return; } - if (!client->documentOpen(cursor.textDocument())) - client->openDocument(cursor.textDocument()); // TODO: Just a workaround + QTC_ASSERT(client->documentOpen(cursor.textDocument()), + client->openDocument(cursor.textDocument())); client->findUsages(cursor.textDocument(), cursor.cursor()); }