Commit 110f1ce5 authored by Christian Kamm's avatar Christian Kamm
Browse files

C++ switch header/source: Always prefer file in same directory.

Change-Id: I4c029919dd0bd9103bfdb1604026745d07e5535a
Reviewed-on: http://codereview.qt-project.org/5692

Reviewed-by: default avatarLeandro T. C. Melo <leandro.melo@nokia.com>
parent 65ca488c
...@@ -170,24 +170,24 @@ void CppToolsPlugin::switchHeaderSource() ...@@ -170,24 +170,24 @@ void CppToolsPlugin::switchHeaderSource()
editorManager->openEditor(otherFile); editorManager->openEditor(otherFile);
} }
QFileInfo CppToolsPlugin::findFile(const QDir &dir, const QString &name, static QFileInfo findFileInProject(const QString &name,
const ProjectExplorer::Project *project) const const ProjectExplorer::Project *project)
{ {
if (debug) if (debug)
qDebug() << Q_FUNC_INFO << dir << name; qDebug() << Q_FUNC_INFO << name << project;
QFileInfo fileInSameDir(dir, name); if (!project)
if (project && !fileInSameDir.isFile()) {
QString pattern = QString(1, QLatin1Char('/'));
pattern += name;
const QStringList projectFiles = project->files(ProjectExplorer::Project::AllFiles);
const QStringList::const_iterator pcend = projectFiles.constEnd();
for (QStringList::const_iterator it = projectFiles.constBegin(); it != pcend; ++it)
if (it->endsWith(pattern))
return QFileInfo(*it);
return QFileInfo(); return QFileInfo();
QString pattern = QString(1, QLatin1Char('/'));
pattern += name;
const QStringList projectFiles = project->files(ProjectExplorer::Project::AllFiles);
const QStringList::const_iterator pcend = projectFiles.constEnd();
for (QStringList::const_iterator it = projectFiles.constBegin(); it != pcend; ++it) {
if (it->endsWith(pattern))
return QFileInfo(*it);
} }
return fileInSameDir; return QFileInfo();
} }
// Figure out file type // Figure out file type
...@@ -237,6 +237,19 @@ static QStringList matchingCandidateSuffixes(const Core::MimeDatabase *mimeDatas ...@@ -237,6 +237,19 @@ static QStringList matchingCandidateSuffixes(const Core::MimeDatabase *mimeDatas
return QStringList(); return QStringList();
} }
static QStringList baseNameWithAllSuffixes(const QString &baseName, const QStringList &suffixes)
{
QStringList result;
const QChar dot = QLatin1Char('.');
foreach (const QString &suffix, suffixes) {
QString fileName = baseName;
fileName += dot;
fileName += suffix;
result += fileName;
}
return result;
}
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();
...@@ -254,49 +267,41 @@ QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) co ...@@ -254,49 +267,41 @@ QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) co
if (type == UnknownType) if (type == UnknownType)
return QString(); return QString();
const QDir absoluteDir = fi.absoluteDir();
const QString baseName = fi.completeBaseName(); const QString baseName = fi.completeBaseName();
const QString privateHeaderSuffix = QLatin1String("_p");
const QStringList suffixes = matchingCandidateSuffixes(mimeDatase, type); const QStringList suffixes = matchingCandidateSuffixes(mimeDatase, type);
const QString privateHeaderSuffix = QLatin1String("_p"); QStringList candidateFileNames = baseNameWithAllSuffixes(baseName, suffixes);
const QChar dot = QLatin1Char('.');
// Check base matches 'source.h'-> 'source.cpp' and vice versa
const QStringList::const_iterator scend = suffixes.constEnd();
for (QStringList::const_iterator it = suffixes.constBegin(); it != scend; ++it) {
QString candidate = baseName;
candidate += dot;
candidate += *it;
const QFileInfo candidateFi = findFile(absoluteDir, candidate, project);
if (candidateFi.isFile())
return candidateFi.absoluteFilePath();
}
if (type == HeaderFile) { if (type == HeaderFile) {
// 'source_p.h': try 'source.cpp'
if (baseName.endsWith(privateHeaderSuffix)) { if (baseName.endsWith(privateHeaderSuffix)) {
QString sourceBaseName = baseName; QString sourceBaseName = baseName;
sourceBaseName.truncate(sourceBaseName.size() - privateHeaderSuffix.size()); sourceBaseName.truncate(sourceBaseName.size() - privateHeaderSuffix.size());
for (QStringList::const_iterator it = suffixes.constBegin(); it != scend; ++it) { candidateFileNames += baseNameWithAllSuffixes(sourceBaseName, suffixes);
QString candidate = sourceBaseName;
candidate += dot;
candidate += *it;
const QFileInfo candidateFi = findFile(absoluteDir, candidate, project);
if (candidateFi.isFile())
return candidateFi.absoluteFilePath();
}
} }
} else { } else {
// 'source.cpp': try 'source_p.h' QString privateHeaderBaseName = baseName;
const QStringList::const_iterator scend = suffixes.constEnd(); privateHeaderBaseName.append(privateHeaderSuffix);
for (QStringList::const_iterator it = suffixes.constBegin(); it != scend; ++it) { candidateFileNames += baseNameWithAllSuffixes(privateHeaderBaseName, suffixes);
QString candidate = baseName; }
candidate += privateHeaderSuffix;
candidate += dot; const QDir absoluteDir = fi.absoluteDir();
candidate += *it;
const QFileInfo candidateFi = findFile(absoluteDir, candidate, project); // Try to find a file in the same directory first
foreach (const QString &fileName, candidateFileNames) {
const QFileInfo candidateFi(absoluteDir, fileName);
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()) if (candidateFi.isFile())
return candidateFi.absoluteFilePath(); return candidateFi.absoluteFilePath();
} }
} }
return QString(); return QString();
} }
......
...@@ -78,7 +78,6 @@ private slots: ...@@ -78,7 +78,6 @@ private slots:
private: private:
QString correspondingHeaderOrSourceI(const QString &fileName) const; QString correspondingHeaderOrSourceI(const QString &fileName) const;
QFileInfo findFile(const QDir &dir, const QString &name, const ProjectExplorer::Project *project) const;
CppModelManager *m_modelManager; CppModelManager *m_modelManager;
QSharedPointer<CppFileSettings> m_fileSettings; QSharedPointer<CppFileSettings> m_fileSettings;
......
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