Commit 0555837c authored by Erik Verbruggen's avatar Erik Verbruggen
Browse files

C++ Editor: prevent insertion of method definitions in header files.

parent 9deccd31
......@@ -146,7 +146,7 @@ QList<CppQuickFixOperation::Ptr> DeclFromDef::match(const CppQuickFixState &stat
foreach (Symbol *s, targetBinding->symbols()) {
if (Class *clazz = s->asClass()) {
QList<CppQuickFixOperation::Ptr> results;
const QLatin1String fn(clazz->fileName());
const QString fn = QString::fromUtf8(clazz->fileName(), clazz->fileNameLength());
const QString decl = generateDeclaration(state,
method,
targetBinding);
......
......@@ -264,7 +264,6 @@ QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) co
const QString privateHeaderSuffix = QLatin1String("_p");
const QChar dot = QLatin1Char('.');
QStringList candidates;
// 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) {
......
......@@ -35,6 +35,9 @@
#include <ASTVisitor.h>
#include <TranslationUnit.h>
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
using namespace CPlusPlus;
using namespace CppTools;
......@@ -298,6 +301,19 @@ InsertionLocation InsertionPointLocator::methodDeclarationInClass(
}
}
static bool isSourceFile(const QString &fileName)
{
const Core::MimeDatabase *mimeDb = Core::ICore::instance()->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());
}
/// Currently, we return the end of fileName.cpp
QList<InsertionLocation> InsertionPointLocator::methodDefinition(
Declaration *declaration) const
......@@ -306,10 +322,14 @@ QList<InsertionLocation> InsertionPointLocator::methodDefinition(
if (!declaration)
return result;
Internal::CppToolsPlugin *cpptools = Internal::CppToolsPlugin::instance();
const QString declFileName = QString::fromUtf8(declaration->fileName(),
declaration->fileNameLength());
QString target = declFileName;
if (!isSourceFile(declFileName)) {
Internal::CppToolsPlugin *cpptools = Internal::CppToolsPlugin::instance();
target = cpptools->correspondingHeaderOrSource(declFileName);
}
const QString declFileName = QLatin1String(declaration->fileName());
QString target = cpptools->correspondingHeaderOrSource(declFileName);
Document::Ptr doc = m_refactoringChanges->file(target).cppDocument();
if (doc.isNull())
return result;
......
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