forked from qt-creator/qt-creator
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>
This commit is contained in:
committed by
Leandro Melo
parent
2a548814a5
commit
ab8d39ace1
@@ -169,24 +169,25 @@ void CppToolsPlugin::switchHeaderSource()
|
|||||||
editorManager->openEditor(otherFile);
|
editorManager->openEditor(otherFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
static QFileInfo findFileInProject(const QString &name,
|
static QStringList findFilesInProject(const QString &name,
|
||||||
const ProjectExplorer::Project *project)
|
const ProjectExplorer::Project *project)
|
||||||
{
|
{
|
||||||
if (debug)
|
if (debug)
|
||||||
qDebug() << Q_FUNC_INFO << name << project;
|
qDebug() << Q_FUNC_INFO << name << project;
|
||||||
|
|
||||||
if (!project)
|
if (!project)
|
||||||
return QFileInfo();
|
return QStringList();
|
||||||
|
|
||||||
QString pattern = QString(1, QLatin1Char('/'));
|
QString pattern = QString(1, QLatin1Char('/'));
|
||||||
pattern += name;
|
pattern += name;
|
||||||
const QStringList projectFiles = project->files(ProjectExplorer::Project::AllFiles);
|
const QStringList projectFiles = project->files(ProjectExplorer::Project::AllFiles);
|
||||||
const QStringList::const_iterator pcend = projectFiles.constEnd();
|
const QStringList::const_iterator pcend = projectFiles.constEnd();
|
||||||
|
QStringList candidateList;
|
||||||
for (QStringList::const_iterator it = projectFiles.constBegin(); it != pcend; ++it) {
|
for (QStringList::const_iterator it = projectFiles.constBegin(); it != pcend; ++it) {
|
||||||
if (it->endsWith(pattern))
|
if (it->endsWith(pattern))
|
||||||
return QFileInfo(*it);
|
candidateList.append(*it);
|
||||||
}
|
}
|
||||||
return QFileInfo();
|
return candidateList;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Figure out file type
|
// Figure out file type
|
||||||
@@ -249,6 +250,15 @@ static QStringList baseNameWithAllSuffixes(const QString &baseName, const QStrin
|
|||||||
return result;
|
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
|
QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) const
|
||||||
{
|
{
|
||||||
const Core::ICore *core = Core::ICore::instance();
|
const Core::ICore *core = Core::ICore::instance();
|
||||||
@@ -286,18 +296,31 @@ QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) co
|
|||||||
const QDir absoluteDir = fi.absoluteDir();
|
const QDir absoluteDir = fi.absoluteDir();
|
||||||
|
|
||||||
// Try to find a file in the same directory first
|
// Try to find a file in the same directory first
|
||||||
foreach (const QString &fileName, candidateFileNames) {
|
foreach (const QString &candidateFileName, candidateFileNames) {
|
||||||
const QFileInfo candidateFi(absoluteDir, fileName);
|
const QFileInfo candidateFi(absoluteDir, candidateFileName);
|
||||||
if (candidateFi.isFile())
|
if (candidateFi.isFile())
|
||||||
return candidateFi.absoluteFilePath();
|
return candidateFi.absoluteFilePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find files in the project
|
// Find files in the project
|
||||||
if (project) {
|
if (project) {
|
||||||
foreach (const QString &fileName, candidateFileNames) {
|
QString bestFileName;
|
||||||
const QFileInfo candidateFi = findFileInProject(fileName, project);
|
int compareValue = 0;
|
||||||
if (candidateFi.isFile())
|
foreach (const QString &candidateFileName, candidateFileNames) {
|
||||||
return candidateFi.absoluteFilePath();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user