diff options
author | Nicolas Arnaud-Cormos <nicolas@kdab.com> | 2012-01-06 22:23:15 +0100 |
---|---|---|
committer | Leandro Melo <leandro.melo@nokia.com> | 2012-01-11 11:44:35 +0100 |
commit | ab8d39ace139881b1b6cd02c3d7b8e318ebc5e2c (patch) | |
tree | dce8abb7e14078364b542f45c1a4f7e3d3c90319 /src/plugins/cpptools/cpptoolsplugin.cpp | |
parent | 2a548814a5d416f9d8ce4761ad0fe8b188b9206b (diff) | |
download | qt-creator-ab8d39ace139881b1b6cd02c3d7b8e318ebc5e2c.tar.gz |
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: Leandro Melo <leandro.melo@nokia.com>
Diffstat (limited to 'src/plugins/cpptools/cpptoolsplugin.cpp')
-rw-r--r-- | src/plugins/cpptools/cpptoolsplugin.cpp | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/src/plugins/cpptools/cpptoolsplugin.cpp b/src/plugins/cpptools/cpptoolsplugin.cpp index 4873e8edf3..2ae47b1143 100644 --- a/src/plugins/cpptools/cpptoolsplugin.cpp +++ b/src/plugins/cpptools/cpptoolsplugin.cpp @@ -169,24 +169,25 @@ void CppToolsPlugin::switchHeaderSource() editorManager->openEditor(otherFile); } -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); + candidateList.append(*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); + Q_ASSERT(candidateFi.isFile()); + return candidateFi.absoluteFilePath(); } } |