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 <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2023-01-09 17:01:48 +01:00
parent e78f0b5911
commit 22da0f2fd6

View File

@@ -692,8 +692,20 @@ QList<Project *> SessionManager::projectOrder(const Project *project)
Project *SessionManager::projectForFile(const FilePath &fileName) 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(), 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) Project *SessionManager::projectWithProjectFilePath(const FilePath &filePath)