From 96be267a6e4a4c7439bffd4c5d557ca7f20554c2 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Mon, 4 Jul 2022 13:39:33 +0200 Subject: [PATCH] ClangCodeModel: Prevent documents from getting opened in wrong clangd As per 8ad7ab2d2a7fefcd3a9ef3ff2f0ef7e5fe792417, we prefer to open non- project sources in a project-specific clangd, rather than the fallback client. However, we do *not* want clangd to open files from "foreign" projects. Checking for the existence of a client for a specific file is not enough, as we might be in the process of loading a session or the respective project might have clangd turned off. Change-Id: I2d5cb7027c6a3ad23e99606d6d6742d67fbc5384 Reviewed-by: Qt CI Bot Reviewed-by: David Schulz Reviewed-by: --- .../clangcodemodel/clangmodelmanagersupport.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp index 1437aff83f6..361bf191929 100644 --- a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp +++ b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp @@ -431,9 +431,13 @@ void ClangModelManagerSupport::updateLanguageClient( const Client * const currentClient = LanguageClientManager::clientForDocument(doc); if (!settings.sizeIsOkay(doc->filePath())) continue; - if (!currentClient || !currentClient->project() - || currentClient->state() != Client::Initialized - || project->isKnownFile(doc->filePath())) { + if (currentClient && currentClient->project() + && currentClient->project() != project) { + continue; + } + if (const Project * const docProject + = SessionManager::projectForFile(doc->filePath()); + !docProject || docProject == project) { LanguageClientManager::openDocumentWithClient(doc, client); hasDocuments = true; } @@ -546,7 +550,8 @@ void ClangModelManagerSupport::claimNonProjectSources(ClangdClient *client) } if (!ClangdSettings::instance().sizeIsOkay(doc->filePath())) continue; - client->openDocument(doc); + if (!ProjectExplorer::SessionManager::projectForFile(doc->filePath())) + LanguageClientManager::openDocumentWithClient(doc, client); } }