Skip to content
Snippets Groups Projects
Commit f39494e4 authored by Erik Verbruggen's avatar Erik Verbruggen Committed by Robert Loehning
Browse files

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

(cherry picked from commit 519f1d11947ff3109850e559fae868c4a55deb43)
parent 91208c82
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -51,13 +51,13 @@ using CppEditor::CppRefactoringChanges;
namespace {
class Operation: public CppQuickFixOperation
class InsertDeclOperation: public CppQuickFixOperation
{
public:
Operation(const CppQuickFixState &state, int priority,
const QString &targetFileName, const Class *targetSymbol,
InsertionPointLocator::AccessSpec xsSpec,
const QString &decl)
InsertDeclOperation(const CppQuickFixState &state, int priority,
const QString &targetFileName, const Class *targetSymbol,
InsertionPointLocator::AccessSpec xsSpec,
const QString &decl)
: CppQuickFixOperation(state, priority)
, m_targetFileName(targetFileName)
, m_targetSymbol(targetSymbol)
......@@ -81,12 +81,12 @@ public:
void performChanges(CppRefactoringFile *, CppRefactoringChanges *refactoring)
{
CppRefactoringFile targetFile = refactoring->file(m_targetFileName);
Document::Ptr targetDoc = targetFile.cppDocument();
InsertionPointLocator locator(targetDoc);
const InsertionLocation loc = locator.methodDeclarationInClass(m_targetSymbol, m_xsSpec);
InsertionPointLocator locator(state().snapshot());
const InsertionLocation loc = locator.methodDeclarationInClass(
m_targetFileName, m_targetSymbol, m_xsSpec);
Q_ASSERT(loc.isValid());
CppRefactoringFile targetFile = refactoring->file(m_targetFileName);
int targetPosition1 = targetFile.position(loc.line(), loc.column());
int targetPosition2 = qMax(0, targetFile.position(loc.line(), 1) - 1);
......@@ -151,24 +151,36 @@ QList<CppQuickFixOperation::Ptr> DeclFromDef::match(const CppQuickFixState &stat
const QString decl = generateDeclaration(state,
method,
targetBinding);
results.append(singleResult(new Operation(state, idx, fn, clazz,
InsertionPointLocator::Public,
decl)));
results.append(singleResult(new Operation(state, idx, fn, clazz,
InsertionPointLocator::Protected,
decl)));
results.append(singleResult(new Operation(state, idx, fn, clazz,
InsertionPointLocator::Private,
decl)));
results.append(singleResult(new Operation(state, idx, fn, clazz,
InsertionPointLocator::PublicSlot,
decl)));
results.append(singleResult(new Operation(state, idx, fn, clazz,
InsertionPointLocator::ProtectedSlot,
decl)));
results.append(singleResult(new Operation(state, idx, fn, clazz,
InsertionPointLocator::PrivateSlot,
decl)));
results.append(
singleResult(
new InsertDeclOperation(state, idx, fn, clazz,
InsertionPointLocator::Public,
decl)));
results.append(
singleResult(
new InsertDeclOperation(state, idx, fn, clazz,
InsertionPointLocator::Protected,
decl)));
results.append(
singleResult(
new InsertDeclOperation(state, idx, fn, clazz,
InsertionPointLocator::Private,
decl)));
results.append(
singleResult(
new InsertDeclOperation(state, idx, fn, clazz,
InsertionPointLocator::PublicSlot,
decl)));
results.append(
singleResult(
new InsertDeclOperation(state, idx, fn, clazz,
InsertionPointLocator::ProtectedSlot,
decl)));
results.append(
singleResult(
new InsertDeclOperation(state, idx, fn, clazz,
InsertionPointLocator::PrivateSlot,
decl)));
return results;
} //! \todo support insertion of non-methods into namespaces
}
......@@ -194,3 +206,171 @@ QString DeclFromDef::generateDeclaration(const CppQuickFixState &,
return decl;
}
namespace {
static inline bool hasFunctionType(DeclarationAST *decl)
{
if (decl->asFunctionDefinition())
return true;
if (SimpleDeclarationAST *ast = decl->asSimpleDeclaration())
if (ast->symbols && ast->symbols->value && !ast->symbols->next)
if (Declaration *decl = ast->symbols->value->asDeclaration())
if (FullySpecifiedType ty = decl->type())
return ty->asFunctionType();
return false;
}
static QPair<DeclarationAST *, DeclarationAST *> findSurroundingDeclarations(
DeclarationListAST *members,
DeclarationAST *decl)
{
bool found = false;
DeclarationAST *last = 0, *next = 0, *prev = 0;
DeclarationListAST *iter = members;
for (; iter; iter = iter->next) {
DeclarationAST *value = iter->value;
if (value == decl) {
prev = last;
found = true;
} else if (hasFunctionType(value)) {
if (found) {
next = value;
break;
} else {
last = value;
}
}
}
return qMakePair(prev, next);
}
} // anonymous namespace
QList<CppQuickFixOperation::Ptr> DefFromDecl::match(const CppQuickFixState &state)
{
#if 0
qDebug() << Q_FUNC_INFO;
const QList<AST *> &path = state.path();
const CppRefactoringFile &file = state.currentFile();
DeclaratorAST *declAST = 0;
ClassSpecifierAST *classSpec = 0;
int idx = path.size() - 1;
for (; idx >= 0; --idx) {
AST *node = path.at(idx);
if (ClassSpecifierAST *clazz = node->asClassSpecifier()) {
classSpec = clazz;
continue;
}
if (idx <= 1) continue;
DeclaratorIdAST *declId = node->asDeclaratorId();
if (!declId) continue;
if (!file.isCursorOn(declId)) continue;
DeclaratorAST *candidate = path.at(idx - 1)->asDeclarator();
if (!candidate) continue;
if (!candidate->postfix_declarator_list) continue;
if (!candidate->postfix_declarator_list->value) continue;
if (candidate->postfix_declarator_list->next) continue;
FunctionDeclaratorAST *funDecl = candidate->postfix_declarator_list->value->asFunctionDeclarator();
if (!funDecl) continue;
if (funDecl->symbol->asFunctionType())
declAST = candidate;
}
if (!declAST || !classSpec || !classSpec->symbol)
return noResult();
if (!declAST->symbols || !declAST->symbols->value || declAST->symbols->next)
return noResult();
Declaration *decl = declAST->symbols->value->asDeclaration();
if (!decl)
return noResult();
Function *funTy = decl->type()->asFunctionType();
if (!funTy)
return noResult();
qDebug() << "-> Found funTy.";
QPair<DeclarationAST *, DeclarationAST *> surroundingNodes =
findSurroundingDeclarations(classSpec->member_specifier_list, declAST);
qDebug() << "->("<<surroundingNodes.first<<","<<surroundingNodes.second<<")";
if (surroundingNodes.first)
if (SimpleDeclarationAST *sd = surroundingNodes.first->asSimpleDeclaration())
qDebug()<<"-->prev@"<<sd->symbols->value->line()<<sd->symbols->value->column();
if (surroundingNodes.second)
if (SimpleDeclarationAST *sd=surroundingNodes.second->asSimpleDeclaration())
qDebug()<<"-->next@"<<sd->symbols->value->line()<<sd->symbols->value->column();
#endif
// if (ClassOrNamespace *targetBinding = state.context().lookupParent(method)) {
// foreach (Symbol *s, targetBinding->symbols()) {
// if (Class *clazz = s->asClass()) {
// QList<CppQuickFixOperation::Ptr> results;
// const QLatin1String fn(clazz->fileName());
// const QString decl = generateDeclaration(state,
// method,
// targetBinding);
// results.append(
// singleResult(
// new InsertDeclOperation(state, idx, fn, clazz,
// InsertionPointLocator::Public,
// decl)));
// results.append(
// singleResult(
// new InsertDeclOperation(state, idx, fn, clazz,
// InsertionPointLocator::Protected,
// decl)));
// results.append(
// singleResult(
// new InsertDeclOperation(state, idx, fn, clazz,
// InsertionPointLocator::Private,
// decl)));
// results.append(
// singleResult(
// new InsertDeclOperation(state, idx, fn, clazz,
// InsertionPointLocator::PublicSlot,
// decl)));
// results.append(
// singleResult(
// new InsertDeclOperation(state, idx, fn, clazz,
// InsertionPointLocator::ProtectedSlot,
// decl)));
// results.append(
// singleResult(
// new InsertDeclOperation(state, idx, fn, clazz,
// InsertionPointLocator::PrivateSlot,
// decl)));
// return results;
// } //! \todo support insertion into namespaces
// }
// }
return noResult();
}
QString DefFromDecl::generateDefinition(const CppQuickFixState &,
Function *method,
ClassOrNamespace *targetBinding)
{
Q_UNUSED(targetBinding);
Overview oo;
oo.setShowFunctionSignatures(true);
oo.setShowReturnTypes(true);
oo.setShowArgumentNames(true);
QString decl;
decl += oo(method->type(), method->unqualifiedName());
decl += QLatin1String(";\n");
return decl;
}
......@@ -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"));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment