From 22da0f2fd660ebd916a140d1d3a64632bca7cc35 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Mon, 9 Jan 2023 17:01:48 +0100 Subject: [PATCH] ProjectExplorer: Try harder to find a project for a source file While project files should ideally list all their source files, this is in practice not always the case. In particular, a lot of cmake projects do not bother to list their headers. Therefore, we amend SessionManager::projectForFile() such that it also considers unlisted files in a project source directory (but not in a build directory, i.e. projects with in-source builds are not considered). A user-visible effect is that when renaming C/C++ symbols, occurrences in unlisted headers are now pre-selected. Change-Id: Id9f64f4836d2bb4dba80e111489f6c14ebb322ed Reviewed-by: Christian Stenger --- src/plugins/projectexplorer/session.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/plugins/projectexplorer/session.cpp b/src/plugins/projectexplorer/session.cpp index a5d5b91c621..874870e057a 100644 --- a/src/plugins/projectexplorer/session.cpp +++ b/src/plugins/projectexplorer/session.cpp @@ -692,8 +692,20 @@ QList SessionManager::projectOrder(const Project *project) Project *SessionManager::projectForFile(const FilePath &fileName) { + if (Project * const project = Utils::findOrDefault(SessionManager::projects(), + [&fileName](const Project *p) { return p->isKnownFile(fileName); })) { + return project; + } return Utils::findOrDefault(SessionManager::projects(), - [&fileName](const Project *p) { return p->isKnownFile(fileName); }); + [&fileName](const Project *p) { + for (const Target * const target : p->targets()) { + for (const BuildConfiguration * const bc : target->buildConfigurations()) { + if (fileName.isChildOf(bc->buildDirectory())) + return false; + } + } + return fileName.isChildOf(p->projectDirectory()); + }); } Project *SessionManager::projectWithProjectFilePath(const FilePath &filePath)