Commit 195d3c6f authored by Nikolai Kosjar's avatar Nikolai Kosjar

CppTools: "Switch Header/Source" checks also other projects

...and not only the current one as fallback.

E.g. if the projects qtcreator and qtbase are open and you navigate into
qdir.h of your include path from the qtcreator project and trigger
"Switch Header/Source", then "qdir.cpp" will be found in the qtbase
source dir.

Task-number: QTCREATORBUG-3789
Change-Id: Ibd73c37bb626f8f7ee80cd8be3ef1244883ccf5e
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 012924ec
...@@ -237,6 +237,35 @@ static int commonStringLength(const QString &s1, const QString &s2) ...@@ -237,6 +237,35 @@ static int commonStringLength(const QString &s1, const QString &s2)
return length; return length;
} }
static QString correspondingHeaderOrSourceInProject(const QFileInfo &fileInfo,
const QStringList &candidateFileNames,
const ProjectExplorer::Project *project)
{
QString bestFileName;
int compareValue = 0;
const QString filePath = fileInfo.filePath();
foreach (const QString &candidateFileName, candidateFileNames) {
const QStringList projectFiles = findFilesInProject(candidateFileName, project);
// Find the file having the most common path with fileName
foreach (const QString &projectFile, projectFiles) {
int value = commonStringLength(filePath, projectFile);
if (value > compareValue) {
compareValue = value;
bestFileName = projectFile;
}
}
}
if (!bestFileName.isEmpty()) {
const QFileInfo candidateFi(bestFileName);
QTC_ASSERT(candidateFi.isFile(), return QString());
m_headerSourceMapping[fileInfo.absoluteFilePath()] = candidateFi.absoluteFilePath();
m_headerSourceMapping[candidateFi.absoluteFilePath()] = fileInfo.absoluteFilePath();
return candidateFi.absoluteFilePath();
}
return QString();
}
} // namespace Internal } // namespace Internal
QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader) QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader)
...@@ -287,29 +316,26 @@ QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader) ...@@ -287,29 +316,26 @@ QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader)
} }
} }
// Find files in the project // Find files in the current project
ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::currentProject(); ProjectExplorer::Project *currentProject = ProjectExplorer::ProjectExplorerPlugin::currentProject();
if (project) { if (currentProject) {
QString bestFileName; const QString path = correspondingHeaderOrSourceInProject(fi, candidateFileNames,
int compareValue = 0; currentProject);
foreach (const QString &candidateFileName, candidateFileNames) { if (!path.isEmpty())
const QStringList projectFiles = findFilesInProject(candidateFileName, project); return path;
// Find the file having the most common path with fileName
foreach (const QString &projectFile, projectFiles) {
int value = commonStringLength(fileName, projectFile);
if (value > compareValue) {
compareValue = value;
bestFileName = projectFile;
}
}
}
if (!bestFileName.isEmpty()) {
const QFileInfo candidateFi(bestFileName);
QTC_ASSERT(candidateFi.isFile(), return QString());
m_headerSourceMapping[fi.absoluteFilePath()] = candidateFi.absoluteFilePath();
m_headerSourceMapping[candidateFi.absoluteFilePath()] = fi.absoluteFilePath();
return candidateFi.absoluteFilePath();
} }
// Find files in other projects
CppModelManager *modelManager = CppModelManager::instance();
QList<CppModelManagerInterface::ProjectInfo> projectInfos = modelManager->projectInfos();
foreach (const CppModelManagerInterface::ProjectInfo &projectInfo, projectInfos) {
const ProjectExplorer::Project *project = projectInfo.project().data();
if (project == currentProject)
continue; // We have already checked the current project.
const QString path = correspondingHeaderOrSourceInProject(fi, candidateFileNames, project);
if (!path.isEmpty())
return path;
} }
return QString(); return QString();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment