Commit f39494e4 authored by Erik Verbruggen's avatar Erik Verbruggen Committed by Robert Loehning

C++: make InsertionPointLocator ready for re-use.

(cherry picked from commit 519f1d11947ff3109850e559fae868c4a55deb43)
parent 91208c82
......@@ -206,19 +206,23 @@ protected:
if (AccessDeclarationAST *xsDecl = decl->asAccessDeclaration()) {
const unsigned token = xsDecl->access_specifier_token;
int newXsSpec = initialXs;
bool isSlot = xsDecl->slots_token && tokenKind(xsDecl->slots_token) == T_Q_SLOTS;
bool isSlot = xsDecl->slots_token
&& tokenKind(xsDecl->slots_token) == T_Q_SLOTS;
switch (tokenKind(token)) {
case T_PUBLIC:
newXsSpec = isSlot ? InsertionPointLocator::PublicSlot : InsertionPointLocator::Public;
newXsSpec = isSlot ? InsertionPointLocator::PublicSlot
: InsertionPointLocator::Public;
break;
case T_PROTECTED:
newXsSpec = isSlot ? InsertionPointLocator::ProtectedSlot : InsertionPointLocator::Protected;
newXsSpec = isSlot ? InsertionPointLocator::ProtectedSlot
: InsertionPointLocator::Protected;
break;
case T_PRIVATE:
newXsSpec = isSlot ? InsertionPointLocator::PrivateSlot: InsertionPointLocator::Private;
newXsSpec = isSlot ? InsertionPointLocator::PrivateSlot
: InsertionPointLocator::Private;
break;
case T_Q_SIGNALS:
......@@ -268,13 +272,37 @@ InsertionLocation::InsertionLocation(const QString &prefix, const QString &suffi
, m_column(column)
{}
InsertionPointLocator::InsertionPointLocator(const Document::Ptr &doc)
: m_doc(doc)
InsertionPointLocator::InsertionPointLocator(const Snapshot &snapshot)
: m_snapshot(snapshot)
{
}
InsertionLocation InsertionPointLocator::methodDeclarationInClass(const Class *clazz, AccessSpec xsSpec) const
InsertionLocation InsertionPointLocator::methodDeclarationInClass(
const QString &fileName,
const Class *clazz,
AccessSpec xsSpec) const
{
FindInClass find(m_doc, clazz, xsSpec);
return find();
const Document::Ptr doc = m_snapshot.document(fileName);
if (doc) {
FindInClass find(doc, clazz, xsSpec);
return find();
} else {
return InsertionLocation();
}
}
QList<InsertionLocation> InsertionPointLocator::methodDefinition(
const QString &fileName, DeclarationAST *prevDecl, DeclarationAST *nextDecl) const
{
QList<InsertionLocation> result;
// option 1: after the prevDecl
// option 2: before the nextDecl
// option 3: inline in fileName (if fileName is a .h file)
// option 4: at the end of fileName.cpp (if fileName is not a .cpp file)
return result;
}
......@@ -30,6 +30,7 @@
#ifndef INSERTIONPOINTLOCATOR_H
#define INSERTIONPOINTLOCATOR_H
#include <ASTfwd.h>
#include <CPlusPlusForwardDeclarations.h>
#include <Symbols.h>
......@@ -82,19 +83,24 @@ public:
SlotBit = 1 << 2,
PublicSlot = Public | SlotBit,
PublicSlot = Public | SlotBit,
ProtectedSlot = Protected | SlotBit,
PrivateSlot = Private | SlotBit,
PrivateSlot = Private | SlotBit,
};
public:
InsertionPointLocator(const Document::Ptr &doc);
InsertionPointLocator(const Snapshot &snapshot);
InsertionLocation methodDeclarationInClass(const Class *clazz,
InsertionLocation methodDeclarationInClass(const QString &fileName,
const Class *clazz,
AccessSpec xsSpec) const;
QList<InsertionLocation> methodDefinition(const QString &fileName,
CPlusPlus::DeclarationAST *prevDecl,
CPlusPlus::DeclarationAST *nextDecl) const;
private:
Document::Ptr m_doc;
Snapshot m_snapshot;
};
} // namespace CPlusPlus
......
This diff is collapsed.
......@@ -52,6 +52,16 @@ protected:
CPlusPlus::ClassOrNamespace *targetBinding);
};
class DefFromDecl: public CppQuickFixFactory
{
public:
virtual QList<CppQuickFixOperation::Ptr> match(const CppQuickFixState &state);
protected:
static QString generateDefinition(const CppQuickFixState &state,
CPlusPlus::Function *method,
CPlusPlus::ClassOrNamespace *targetBinding);
};
} // namespace Internal
} // namespace CppEditor
......
......@@ -273,21 +273,24 @@ static inline ITextEditable *editableAt(const QString &fileName, int line, int c
return qobject_cast<ITextEditable *>(TextEditor::BaseTextEditor::openEditorAt(fileName, line, column));
}
static void addDeclaration(Document::Ptr doc, const Class *cl, const QString &functionName)
static void addDeclaration(const Snapshot &snapshot,
const QString &fileName,
const Class *cl,
const QString &functionName)
{
const QString docFileName = doc->fileName();
QString declaration = QLatin1String("void ");
declaration += functionName;
declaration += QLatin1String(";\n");
InsertionPointLocator find(doc);
const InsertionLocation loc = find.methodDeclarationInClass(cl, InsertionPointLocator::PrivateSlot);
InsertionPointLocator find(snapshot);
const InsertionLocation loc = find.methodDeclarationInClass(
fileName, cl, InsertionPointLocator::PrivateSlot);
//
//! \todo change this to use the Refactoring changes.
//
if (ITextEditable *editable = editableAt(docFileName, loc.line(), loc.column() - 1)) {
if (ITextEditable *editable = editableAt(fileName, loc.line(), loc.column() - 1)) {
BaseTextEditor *editor = qobject_cast<BaseTextEditor *>(editable->widget());
if (editor) {
QTextCursor tc = editor->textCursor();
......@@ -302,8 +305,10 @@ static void addDeclaration(Document::Ptr doc, const Class *cl, const QString &fu
}
static Document::Ptr addDefinition(const CPlusPlus::Snapshot &docTable,
const QString &headerFileName, const QString &className,
const QString &functionName, int *line)
const QString &headerFileName,
const QString &className,
const QString &functionName,
int *line)
{
QString definition = QLatin1String("\nvoid ");
definition += className;
......@@ -440,6 +445,7 @@ static Document::Ptr getParsedDocument(const QString &fileName, CppTools::CppMod
Document::Ptr doc = snapshot.documentFromSource(source, fileName);
doc->check();
snapshot.insert(doc);
return doc;
}
......@@ -541,11 +547,12 @@ bool QtCreatorIntegration::navigateToSlot(const QString &objectName,
} else {
// add function declaration to cl
CppTools::CppModelManagerInterface::WorkingCopy workingCopy = cppModelManagerInstance()->workingCopy();
Document::Ptr tmpDoc = getParsedDocument(doc->fileName(), workingCopy, docTable);
addDeclaration(tmpDoc, cl, functionNameWithParameterNames);
const QString fileName = doc->fileName();
getParsedDocument(fileName, workingCopy, docTable);
addDeclaration(docTable, fileName, cl, functionNameWithParameterNames);
// add function definition to cpp file
sourceDoc = addDefinition(docTable, doc->fileName(), className, functionNameWithParameterNames, &line);
sourceDoc = addDefinition(docTable, fileName, className, functionNameWithParameterNames, &line);
}
if (!sourceDoc) {
......
......@@ -54,14 +54,17 @@ void tst_Codegen::public_in_empty_class()
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Class *mainWindow = doc->globalSymbolAt(0)->asClass();
QVERIFY(mainWindow);
QCOMPARE(mainWindow->line(), 1U);
QCOMPARE(mainWindow->column(), 7U);
InsertionPointLocator find(doc);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
Snapshot snapshot;
snapshot.insert(doc);
InsertionPointLocator find(snapshot);
InsertionLocation loc = find.methodDeclarationInClass(
mainWindow,
doc->fileName(),
foo,
InsertionPointLocator::Public);
QVERIFY(loc.isValid());
QCOMPARE(loc.prefix(), QLatin1String("public:\n"));
......@@ -90,14 +93,17 @@ void tst_Codegen::public_in_nonempty_class()
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Class *mainWindow = doc->globalSymbolAt(0)->asClass();
QVERIFY(mainWindow);
QCOMPARE(mainWindow->line(), 1U);
QCOMPARE(mainWindow->column(), 7U);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
InsertionPointLocator find(doc);
Snapshot snapshot;
snapshot.insert(doc);
InsertionPointLocator find(snapshot);
InsertionLocation loc = find.methodDeclarationInClass(
mainWindow,
doc->fileName(),
foo,
InsertionPointLocator::Public);
QVERIFY(loc.isValid());
QVERIFY(loc.prefix().isEmpty());
......@@ -126,14 +132,17 @@ void tst_Codegen::public_before_protected()
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Class *mainWindow = doc->globalSymbolAt(0)->asClass();
QVERIFY(mainWindow);
QCOMPARE(mainWindow->line(), 1U);
QCOMPARE(mainWindow->column(), 7U);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
InsertionPointLocator find(doc);
Snapshot snapshot;
snapshot.insert(doc);
InsertionPointLocator find(snapshot);
InsertionLocation loc = find.methodDeclarationInClass(
mainWindow,
doc->fileName(),
foo,
InsertionPointLocator::Public);
QVERIFY(loc.isValid());
QCOMPARE(loc.prefix(), QLatin1String("public:\n"));
......@@ -163,14 +172,17 @@ void tst_Codegen::private_after_protected()
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Class *mainWindow = doc->globalSymbolAt(0)->asClass();
QVERIFY(mainWindow);
QCOMPARE(mainWindow->line(), 1U);
QCOMPARE(mainWindow->column(), 7U);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
InsertionPointLocator find(doc);
Snapshot snapshot;
snapshot.insert(doc);
InsertionPointLocator find(snapshot);
InsertionLocation loc = find.methodDeclarationInClass(
mainWindow,
doc->fileName(),
foo,
InsertionPointLocator::Private);
QVERIFY(loc.isValid());
QCOMPARE(loc.prefix(), QLatin1String("private:\n"));
......@@ -200,14 +212,17 @@ void tst_Codegen::protected_in_nonempty_class()
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Class *mainWindow = doc->globalSymbolAt(0)->asClass();
QVERIFY(mainWindow);
QCOMPARE(mainWindow->line(), 1U);
QCOMPARE(mainWindow->column(), 7U);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
InsertionPointLocator find(doc);
Snapshot snapshot;
snapshot.insert(doc);
InsertionPointLocator find(snapshot);
InsertionLocation loc = find.methodDeclarationInClass(
mainWindow,
doc->fileName(),
foo,
InsertionPointLocator::Protected);
QVERIFY(loc.isValid());
QCOMPARE(loc.prefix(), QLatin1String("protected:\n"));
......@@ -237,14 +252,17 @@ void tst_Codegen::protected_betwee_public_and_private()
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 1U);
Class *mainWindow = doc->globalSymbolAt(0)->asClass();
QVERIFY(mainWindow);
QCOMPARE(mainWindow->line(), 1U);
QCOMPARE(mainWindow->column(), 7U);
Class *foo = doc->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
InsertionPointLocator find(doc);
Snapshot snapshot;
snapshot.insert(doc);
InsertionPointLocator find(snapshot);
InsertionLocation loc = find.methodDeclarationInClass(
mainWindow,
doc->fileName(),
foo,
InsertionPointLocator::Protected);
QVERIFY(loc.isValid());
QCOMPARE(loc.prefix(), QLatin1String("protected:\n"));
......@@ -294,14 +312,17 @@ void tst_Codegen::qtdesigner_integration()
QCOMPARE(doc->diagnosticMessages().size(), 0);
QCOMPARE(doc->globalSymbolCount(), 2U);
Class *mainWindow = doc->globalSymbolAt(1)->asClass();
QVERIFY(mainWindow);
QCOMPARE(mainWindow->line(), 10U);
QCOMPARE(mainWindow->column(), 7U);
Class *foo = doc->globalSymbolAt(1)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 10U);
QCOMPARE(foo->column(), 7U);
InsertionPointLocator find(doc);
Snapshot snapshot;
snapshot.insert(doc);
InsertionPointLocator find(snapshot);
InsertionLocation loc = find.methodDeclarationInClass(
mainWindow,
doc->fileName(),
foo,
InsertionPointLocator::PrivateSlot);
QVERIFY(loc.isValid());
QCOMPARE(loc.prefix(), QLatin1String("private slots:\n"));
......
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