forked from qt-creator/qt-creator
CppTools: moved some logic to ProjectFile
Simplifies code and makes it independent from mimetype names. Change-Id: Ib56a07654df4986b1916c517e3862e6c3a3dd720 Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
committed by
Erik Verbruggen
parent
d61bc4fcec
commit
0609333e02
@@ -2,6 +2,8 @@
|
||||
|
||||
#include "cppmodelmanager.h"
|
||||
#include "searchsymbols.h"
|
||||
#include "cpptoolsconstants.h"
|
||||
#include "cppprojectfile.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
#include <coreplugin/progressmanager/progressmanager.h>
|
||||
@@ -22,24 +24,12 @@ static void parse(QFutureInterface<void> &future,
|
||||
if (files.isEmpty())
|
||||
return;
|
||||
|
||||
const Core::MimeDatabase *mimeDb = Core::ICore::mimeDatabase();
|
||||
Core::MimeType cSourceTy = mimeDb->findByType(QLatin1String("text/x-csrc"));
|
||||
Core::MimeType cppSourceTy = mimeDb->findByType(QLatin1String("text/x-c++src"));
|
||||
Core::MimeType mSourceTy = mimeDb->findByType(QLatin1String("text/x-objcsrc"));
|
||||
|
||||
QStringList sources;
|
||||
QStringList headers;
|
||||
|
||||
QStringList suffixes = cSourceTy.suffixes();
|
||||
suffixes += cppSourceTy.suffixes();
|
||||
suffixes += mSourceTy.suffixes();
|
||||
|
||||
foreach (const QString &file, files) {
|
||||
QFileInfo info(file);
|
||||
|
||||
preproc->removeFromCache(file);
|
||||
|
||||
if (suffixes.contains(info.suffix()))
|
||||
if (ProjectFile::isSource(ProjectFile::classify(file)))
|
||||
sources.append(file);
|
||||
else
|
||||
headers.append(file);
|
||||
|
||||
@@ -1193,7 +1193,8 @@ bool CppCompletionAssistProcessor::objcKeywordsWanted() const
|
||||
const QString fileName = m_interface->fileName();
|
||||
|
||||
const Core::MimeDatabase *mdb = Core::ICore::mimeDatabase();
|
||||
return mdb->findByFile(fileName).type() == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE);
|
||||
const QString mt = mdb->findByFile(fileName).type();
|
||||
return mt == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE);
|
||||
}
|
||||
|
||||
int CppCompletionAssistProcessor::startCompletionInternal(const QString fileName,
|
||||
|
||||
@@ -48,6 +48,58 @@ ProjectFile::ProjectFile(const QString &file, Kind kind)
|
||||
{
|
||||
}
|
||||
|
||||
ProjectFile::Kind ProjectFile::classify(const QString &file)
|
||||
{
|
||||
const Core::MimeDatabase *mimeDatabase = Core::ICore::mimeDatabase();
|
||||
const QFileInfo fi(file);
|
||||
const Core::MimeType mimeType = mimeDatabase->findByFile(fi);
|
||||
if (!mimeType)
|
||||
return Unclassified;
|
||||
const QString mt = mimeType.type();
|
||||
if (mt == QLatin1String(CppTools::Constants::C_SOURCE_MIMETYPE))
|
||||
return CSource;
|
||||
if (mt == QLatin1String(CppTools::Constants::C_HEADER_MIMETYPE))
|
||||
return CHeader;
|
||||
if (mt == QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE))
|
||||
return CXXSource;
|
||||
if (mt == QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE))
|
||||
return CXXHeader;
|
||||
if (mt == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE))
|
||||
return ObjCXXSource;
|
||||
return Unclassified;
|
||||
}
|
||||
|
||||
/// @return True if file is header or cannot be classified (i.e has no file extension)
|
||||
bool ProjectFile::isHeader(ProjectFile::Kind kind)
|
||||
{
|
||||
switch (kind) {
|
||||
case ProjectFile::CHeader:
|
||||
case ProjectFile::CXXHeader:
|
||||
case ProjectFile::ObjCHeader:
|
||||
case ProjectFile::ObjCXXHeader:
|
||||
case ProjectFile::Unclassified:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// @return True if file is correctly classified source
|
||||
bool ProjectFile::isSource(ProjectFile::Kind kind)
|
||||
{
|
||||
switch (kind) {
|
||||
case ProjectFile::CSource:
|
||||
case ProjectFile::CXXSource:
|
||||
case ProjectFile::ObjCSource:
|
||||
case ProjectFile::ObjCXXSource:
|
||||
case ProjectFile::CudaSource:
|
||||
case ProjectFile::OpenCLSource:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
ProjectFileAdder::ProjectFileAdder(QList<ProjectFile> &files)
|
||||
: m_files(files)
|
||||
{
|
||||
|
||||
@@ -43,6 +43,7 @@ class CPPTOOLS_EXPORT ProjectFile
|
||||
public:
|
||||
// enums and types
|
||||
enum Kind {
|
||||
Unclassified = 0,
|
||||
CHeader = 1,
|
||||
CSource = 2,
|
||||
CXXHeader = 3,
|
||||
@@ -58,6 +59,10 @@ public:
|
||||
ProjectFile();
|
||||
ProjectFile(const QString &file, Kind kind);
|
||||
|
||||
static Kind classify(const QString &file);
|
||||
static bool isHeader(Kind kind);
|
||||
static bool isSource(Kind kind);
|
||||
|
||||
QString path;
|
||||
Kind kind;
|
||||
};
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#include "symbolsfindfilter.h"
|
||||
#include "cpptoolssettings.h"
|
||||
#include "cpptoolsreuse.h"
|
||||
#include "cppprojectfile.h"
|
||||
|
||||
#include <coreplugin/actionmanager/actioncontainer.h>
|
||||
#include <coreplugin/actionmanager/actionmanager.h>
|
||||
@@ -184,51 +185,31 @@ static QStringList findFilesInProject(const QString &name,
|
||||
return candidateList;
|
||||
}
|
||||
|
||||
// Figure out file type
|
||||
enum FileType {
|
||||
HeaderFile,
|
||||
C_SourceFile,
|
||||
CPP_SourceFile,
|
||||
ObjectiveCPP_SourceFile,
|
||||
UnknownType
|
||||
};
|
||||
|
||||
static inline FileType fileType(const Core::MimeDatabase *mimeDatabase, const QFileInfo & fi)
|
||||
{
|
||||
const Core::MimeType mimeType = mimeDatabase->findByFile(fi);
|
||||
if (!mimeType)
|
||||
return UnknownType;
|
||||
const QString typeName = mimeType.type();
|
||||
if (typeName == QLatin1String(CppTools::Constants::C_SOURCE_MIMETYPE))
|
||||
return C_SourceFile;
|
||||
if (typeName == QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE))
|
||||
return CPP_SourceFile;
|
||||
if (typeName == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE))
|
||||
return ObjectiveCPP_SourceFile;
|
||||
if (typeName == QLatin1String(CppTools::Constants::C_HEADER_MIMETYPE)
|
||||
|| typeName == QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE))
|
||||
return HeaderFile;
|
||||
return UnknownType;
|
||||
}
|
||||
|
||||
// Return the suffixes that should be checked when trying to find a
|
||||
// source belonging to a header and vice versa
|
||||
static QStringList matchingCandidateSuffixes(const Core::MimeDatabase *mimeDatabase, FileType type)
|
||||
static QStringList matchingCandidateSuffixes(ProjectFile::Kind kind)
|
||||
{
|
||||
switch (type) {
|
||||
case UnknownType:
|
||||
break;
|
||||
case HeaderFile: // Note that C/C++ headers are undistinguishable
|
||||
return mimeDatabase->findByType(QLatin1String(CppTools::Constants::C_SOURCE_MIMETYPE)).suffixes()
|
||||
+ mimeDatabase->findByType(QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)).suffixes()
|
||||
+ mimeDatabase->findByType(QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE)).suffixes();
|
||||
case C_SourceFile:
|
||||
return mimeDatabase->findByType(QLatin1String(CppTools::Constants::C_HEADER_MIMETYPE)).suffixes();
|
||||
case CPP_SourceFile:
|
||||
case ObjectiveCPP_SourceFile:
|
||||
return mimeDatabase->findByType(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)).suffixes();
|
||||
Core::MimeDatabase *md = Core::ICore::instance()->mimeDatabase();
|
||||
switch (kind) {
|
||||
// Note that C/C++ headers are undistinguishable
|
||||
case ProjectFile::CHeader:
|
||||
case ProjectFile::CXXHeader:
|
||||
case ProjectFile::ObjCHeader:
|
||||
case ProjectFile::ObjCXXHeader:
|
||||
return md->findByType(QLatin1String(Constants::C_SOURCE_MIMETYPE)).suffixes()
|
||||
+ md->findByType(QLatin1String(Constants::CPP_SOURCE_MIMETYPE)).suffixes()
|
||||
+ md->findByType(QLatin1String(Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE)).suffixes();
|
||||
case ProjectFile::CSource:
|
||||
case ProjectFile::ObjCSource:
|
||||
return md->findByType(QLatin1String(Constants::C_HEADER_MIMETYPE)).suffixes();
|
||||
case ProjectFile::CXXSource:
|
||||
case ProjectFile::ObjCXXSource:
|
||||
case ProjectFile::CudaSource:
|
||||
case ProjectFile::OpenCLSource:
|
||||
return md->findByType(QLatin1String(Constants::CPP_HEADER_MIMETYPE)).suffixes();
|
||||
default:
|
||||
return QStringList();
|
||||
}
|
||||
return QStringList();
|
||||
}
|
||||
|
||||
static QStringList baseNameWithAllSuffixes(const QString &baseName, const QStringList &suffixes)
|
||||
@@ -259,30 +240,26 @@ QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader)
|
||||
{
|
||||
using namespace Internal;
|
||||
|
||||
const Core::MimeDatabase *mimeDatabase = Core::ICore::mimeDatabase();
|
||||
const QFileInfo fi(fileName);
|
||||
if (m_headerSourceMapping.contains(fi.absoluteFilePath())) {
|
||||
if (wasHeader)
|
||||
*wasHeader = fileType(mimeDatabase, fi) == HeaderFile;
|
||||
ProjectFile::Kind kind = ProjectFile::classify(fileName);
|
||||
const bool isHeader = ProjectFile::isHeader(kind);
|
||||
if (*wasHeader)
|
||||
*wasHeader = isHeader;
|
||||
if (m_headerSourceMapping.contains(fi.absoluteFilePath()))
|
||||
return m_headerSourceMapping.value(fi.absoluteFilePath());
|
||||
}
|
||||
|
||||
FileType type = fileType(mimeDatabase, fi);
|
||||
if (wasHeader)
|
||||
*wasHeader = type == HeaderFile;
|
||||
|
||||
if (debug)
|
||||
qDebug() << Q_FUNC_INFO << fileName << type;
|
||||
qDebug() << Q_FUNC_INFO << fileName << kind;
|
||||
|
||||
if (type == UnknownType)
|
||||
if (kind == ProjectFile::Unclassified)
|
||||
return QString();
|
||||
|
||||
const QString baseName = fi.completeBaseName();
|
||||
const QString privateHeaderSuffix = QLatin1String("_p");
|
||||
const QStringList suffixes = matchingCandidateSuffixes(mimeDatabase, type);
|
||||
const QStringList suffixes = matchingCandidateSuffixes(kind);
|
||||
|
||||
QStringList candidateFileNames = baseNameWithAllSuffixes(baseName, suffixes);
|
||||
if (type == HeaderFile) {
|
||||
if (isHeader) {
|
||||
if (baseName.endsWith(privateHeaderSuffix)) {
|
||||
QString sourceBaseName = baseName;
|
||||
sourceBaseName.truncate(sourceBaseName.size() - privateHeaderSuffix.size());
|
||||
@@ -301,7 +278,7 @@ QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader)
|
||||
const QFileInfo candidateFi(absoluteDir, candidateFileName);
|
||||
if (candidateFi.isFile()) {
|
||||
m_headerSourceMapping[fi.absoluteFilePath()] = candidateFi.absoluteFilePath();
|
||||
if (type != HeaderFile || !baseName.endsWith(privateHeaderSuffix))
|
||||
if (!isHeader || !baseName.endsWith(privateHeaderSuffix))
|
||||
m_headerSourceMapping[candidateFi.absoluteFilePath()] = fi.absoluteFilePath();
|
||||
return candidateFi.absoluteFilePath();
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
|
||||
#include "cpptoolsreuse.h"
|
||||
#include "symbolfinder.h"
|
||||
#include "cpptoolsconstants.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
|
||||
@@ -317,19 +318,6 @@ InsertionLocation InsertionPointLocator::methodDeclarationInClass(
|
||||
}
|
||||
}
|
||||
|
||||
static bool isSourceFile(const QString &fileName)
|
||||
{
|
||||
const Core::MimeDatabase *mimeDb = Core::ICore::mimeDatabase();
|
||||
Core::MimeType cSourceTy = mimeDb->findByType(QLatin1String("text/x-csrc"));
|
||||
Core::MimeType cppSourceTy = mimeDb->findByType(QLatin1String("text/x-c++src"));
|
||||
Core::MimeType mSourceTy = mimeDb->findByType(QLatin1String("text/x-objcsrc"));
|
||||
QStringList suffixes = cSourceTy.suffixes();
|
||||
suffixes += cppSourceTy.suffixes();
|
||||
suffixes += mSourceTy.suffixes();
|
||||
QFileInfo fileInfo(fileName);
|
||||
return suffixes.contains(fileInfo.suffix());
|
||||
}
|
||||
|
||||
namespace {
|
||||
template <class Key, class Value>
|
||||
class HighestValue
|
||||
@@ -588,7 +576,7 @@ QList<InsertionLocation> InsertionPointLocator::methodDefinition(
|
||||
const QString declFileName = QString::fromUtf8(declaration->fileName(),
|
||||
declaration->fileNameLength());
|
||||
QString target = declFileName;
|
||||
if (!isSourceFile(declFileName)) {
|
||||
if (!ProjectFile::isSource(ProjectFile::classify(declFileName))) {
|
||||
QString candidate = CppTools::correspondingHeaderOrSource(declFileName);
|
||||
if (!candidate.isEmpty())
|
||||
target = candidate;
|
||||
|
||||
Reference in New Issue
Block a user