Commit ab8d39ac authored by Nicolas Arnaud-Cormos's avatar Nicolas Arnaud-Cormos Committed by Leandro Melo
Browse files

Fix the switch header/source action.

In case multiple files have the same name (in a complex project), find
the file with the most common path instead of the first one.

Change-Id: I75495dabe7ba8f5142d6b52e7c07d54d5d6f54bd
Reviewed-by: default avatarLeandro Melo <>
parent 2a548814
......@@ -169,24 +169,25 @@ void CppToolsPlugin::switchHeaderSource()
static QFileInfo findFileInProject(const QString &name,
static QStringList findFilesInProject(const QString &name,
const ProjectExplorer::Project *project)
if (debug)
qDebug() << Q_FUNC_INFO << name << project;
if (!project)
return QFileInfo();
return QStringList();
QString pattern = QString(1, QLatin1Char('/'));
pattern += name;
const QStringList projectFiles = project->files(ProjectExplorer::Project::AllFiles);
const QStringList::const_iterator pcend = projectFiles.constEnd();
QStringList candidateList;
for (QStringList::const_iterator it = projectFiles.constBegin(); it != pcend; ++it) {
if (it->endsWith(pattern))
return QFileInfo(*it);
return QFileInfo();
return candidateList;
// Figure out file type
......@@ -249,6 +250,15 @@ static QStringList baseNameWithAllSuffixes(const QString &baseName, const QStrin
return result;
static int commonStringLength(const QString &s1, const QString &s2)
int length = qMin(s1.length(), s2.length());
for (int i = 0; i < length; ++i)
if (s1[i] != s2[i])
return i;
return length;
QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) const
const Core::ICore *core = Core::ICore::instance();
......@@ -286,18 +296,31 @@ QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) co
const QDir absoluteDir = fi.absoluteDir();
// Try to find a file in the same directory first
foreach (const QString &fileName, candidateFileNames) {
const QFileInfo candidateFi(absoluteDir, fileName);
foreach (const QString &candidateFileName, candidateFileNames) {
const QFileInfo candidateFi(absoluteDir, candidateFileName);
if (candidateFi.isFile())
return candidateFi.absoluteFilePath();
// Find files in the project
if (project) {
foreach (const QString &fileName, candidateFileNames) {
const QFileInfo candidateFi = findFileInProject(fileName, project);
if (candidateFi.isFile())
return candidateFi.absoluteFilePath();
QString bestFileName;
int compareValue = 0;
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(fileName, projectFile);
if (value > compareValue) {
compareValue = value;
bestFileName = projectFile;
if (!bestFileName.isEmpty()) {
const QFileInfo candidateFi(bestFileName);
return candidateFi.absoluteFilePath();
Supports Markdown
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