Commit d980e37e authored by Erik Verbruggen's avatar Erik Verbruggen

Reworked the QuickFix infrastructure.

parent 5bf0ba0b
......@@ -45,6 +45,7 @@
#include <QtCore/QCoreApplication>
using namespace CPlusPlus;
using namespace CppEditor;
using namespace CppEditor::Internal;
using namespace CppTools;
......@@ -146,22 +147,54 @@ QString prettyMinimalType(const FullySpecifiedType &ty,
return oo(ty);
}
} // anonymous namespace
class Operation: public CppQuickFixOperation
{
public:
Operation(const CppQuickFixState &state, int priority,
const QString &targetFileName, const QString &targetSymbolName,
const QString &decl)
: CppQuickFixOperation(state, priority)
, m_targetFileName(targetFileName)
, m_targetSymbolName(targetSymbolName)
, m_decl(decl)
{
setDescription(QCoreApplication::tr("Create Declaration from Definition",
"CppEditor::DeclFromDef"));
}
DeclFromDef::DeclFromDef(TextEditor::BaseTextEditor *editor)
: CppQuickFixOperation(editor)
{}
void createChanges()
{
CppRefactoringChanges *changes = refactoringChanges();
QString DeclFromDef::description() const
{
return QCoreApplication::tr("Create Declaration from Definition", "DeclFromDef");
}
Document::Ptr targetDoc = changes->document(m_targetFileName);
InsertionPointFinder findInsertionPoint(targetDoc, m_targetSymbolName);
int line = 0, column = 0;
findInsertionPoint(&line, &column);
int targetPosition1 = changes->positionInFile(m_targetFileName, line, column);
int targetPosition2 = changes->positionInFile(m_targetFileName, line + 1, 0) - 1;
Utils::ChangeSet target;
target.insert(targetPosition1, m_decl);
changes->changeFile(m_targetFileName, target);
changes->reindent(m_targetFileName,
Utils::ChangeSet::Range(targetPosition1, targetPosition2));
changes->openEditor(m_targetFileName, line, column);
}
private:
QString m_targetFileName;
QString m_targetSymbolName;
QString m_decl;
};
} // anonymous namespace
int DeclFromDef::match(const QList<CPlusPlus::AST *> &path)
QList<CppQuickFixOperation::Ptr> DeclFromDef::match(const CppQuickFixState &state)
{
m_targetFileName.clear();
m_targetSymbolName.clear();
m_decl.clear();
const QList<AST *> &path = state.path();
FunctionDefinitionAST *funDef = 0;
int idx = 0;
......@@ -171,62 +204,40 @@ int DeclFromDef::match(const QList<CPlusPlus::AST *> &path)
if (!funDef)
funDef = candidate;
} else if (node->asClassSpecifier()) {
return -1;
return noResult();
}
}
if (!funDef || !funDef->symbol)
return -1;
return noResult();
Function *method = funDef->symbol;
LookupContext context(document(), snapshot());
if (ClassOrNamespace *targetBinding = context.lookupParent(method)) {
if (ClassOrNamespace *targetBinding = state.context().lookupParent(method)) {
foreach (Symbol *s, targetBinding->symbols()) {
if (Class *clazz = s->asClass()) {
m_targetFileName = QLatin1String(clazz->fileName());
m_targetSymbolName = QLatin1String(clazz->identifier()->chars());
m_decl = generateDeclaration(method, targetBinding);
return idx;
return singleResult(new Operation(state, idx,
QLatin1String(clazz->fileName()),
QLatin1String(clazz->identifier()->chars()),
generateDeclaration(state,
method,
targetBinding)));
} // ### TODO: support insertion into namespaces
}
}
return -1;
}
void DeclFromDef::createChanges()
{
CppRefactoringChanges *changes = refactoringChanges();
Document::Ptr targetDoc = changes->document(m_targetFileName);
InsertionPointFinder findInsertionPoint(targetDoc, m_targetSymbolName);
int line = 0, column = 0;
findInsertionPoint(&line, &column);
int targetPosition1 = changes->positionInFile(m_targetFileName, line, column);
int targetPosition2 = changes->positionInFile(m_targetFileName, line + 1, 0) - 1;
Utils::ChangeSet target;
target.insert(targetPosition1, m_decl);
changes->changeFile(m_targetFileName, target);
changes->reindent(m_targetFileName,
Utils::ChangeSet::Range(targetPosition1, targetPosition2));
changes->openEditor(m_targetFileName, line, column);
return noResult();
}
QString DeclFromDef::generateDeclaration(Function *method, ClassOrNamespace *targetBinding)
QString DeclFromDef::generateDeclaration(const CppQuickFixState &state,
Function *method,
ClassOrNamespace *targetBinding)
{
LookupContext context(document(), snapshot());
Overview oo;
QString decl;
decl.append(prettyMinimalType(method->returnType(),
context,
state.context(),
method->scope(),
targetBinding));
......@@ -238,7 +249,7 @@ QString DeclFromDef::generateDeclaration(Function *method, ClassOrNamespace *tar
decl.append(QLatin1String(", "));
Argument *arg = method->argumentAt(argIdx)->asArgument();
decl.append(prettyMinimalType(arg->type(),
context,
state.context(),
method->members(),
targetBinding));
decl.append(QLatin1Char(' '));
......
......@@ -41,23 +41,15 @@ class ClassOrNamespace;
namespace CppEditor {
namespace Internal {
class DeclFromDef: public CppQuickFixOperation
class DeclFromDef: public CppQuickFixFactory
{
public:
DeclFromDef(TextEditor::BaseTextEditor *editor);
virtual int match(const QList<CPlusPlus::AST *> &path);
virtual QString description() const;
virtual void createChanges();
virtual QList<CppQuickFixOperation::Ptr> match(const CppQuickFixState &state);
protected:
virtual QString generateDeclaration(CPlusPlus::Function *method,
CPlusPlus::ClassOrNamespace *targetBinding);
private:
QString m_targetFileName;
QString m_targetSymbolName;
QString m_decl;
static QString generateDeclaration(const CppQuickFixState &state,
CPlusPlus::Function *method,
CPlusPlus::ClassOrNamespace *targetBinding);
};
......
......@@ -30,6 +30,7 @@ SOURCES += cppplugin.cpp \
cppfilewizard.cpp \
cppclasswizard.cpp \
cppquickfix.cpp \
cppquickfixes.cpp \
cpprefactoringchanges.cpp \
cppchecksymbols.cpp \
cppsemanticinfo.cpp \
......
......@@ -63,6 +63,7 @@
#include <QtGui/QMenu>
using namespace CppEditor;
using namespace CppEditor::Internal;
enum { QUICKFIX_INTERVAL = 20 };
......@@ -208,8 +209,7 @@ bool CppPlugin::initialize(const QStringList & /*arguments*/, QString *errorMess
m_quickFixCollector = new CppQuickFixCollector;
addAutoReleasedObject(m_quickFixCollector);
addAutoReleasedObject(new CppQuickFixFactory);
CppQuickFixCollector::registerQuickFixes(this);
CppFileWizard::BaseFileWizardParameters wizardParameters(Core::IWizard::FileWizard);
......
This diff is collapsed.
......@@ -30,100 +30,142 @@
#ifndef CPPQUICKFIX_H
#define CPPQUICKFIX_H
#include <texteditor/icompletioncollector.h>
#include <texteditor/quickfix.h>
#include "cpprefactoringchanges.h"
#include "cppsemanticinfo.h"
#include <cplusplus/CppDocument.h>
#include <ASTfwd.h>
#include <cplusplus/CppDocument.h>
#include <texteditor/icompletioncollector.h>
#include <texteditor/quickfix.h>
#include <utils/changeset.h>
#include <QtCore/QSharedPointer>
#include <QtGui/QTextCursor>
#include "cpprefactoringchanges.h"
namespace CppTools {
class CppModelManagerInterface;
} // end of namespace CppTools
namespace ExtensionSystem {
class IPlugin;
}
namespace CppEditor {
namespace Internal {
class CppQuickFixCollector;
} // end of namespace Internal
class CppQuickFixOperation: public TextEditor::QuickFixOperation
class CPPEDITOR_EXPORT CppQuickFixState: public TextEditor::QuickFixState
{
Q_DISABLE_COPY(CppQuickFixOperation)
friend class Internal::CppQuickFixCollector;
public:
CppQuickFixOperation(TextEditor::BaseTextEditor *editor);
virtual ~CppQuickFixOperation();
virtual int match(const QList<CPlusPlus::AST *> &path) = 0;
CppQuickFixState(TextEditor::BaseTextEditor *editor);
typedef Utils::ChangeSet::Range Range;
const QList<CPlusPlus::AST *> &path() const;
CPlusPlus::Snapshot snapshot() const;
CPlusPlus::Document::Ptr document() const;
const CPlusPlus::Snapshot &snapshot() const;
const CPlusPlus::LookupContext &context() const;
CppEditor::Internal::SemanticInfo semanticInfo() const;
CPlusPlus::LookupContext context() const;
virtual int match(TextEditor::QuickFixState *state);
using TextEditor::QuickFixState::range;
using TextEditor::QuickFixState::textOf;
using TextEditor::QuickFixState::charAt;
protected:
using TextEditor::QuickFixOperation::range;
using TextEditor::QuickFixOperation::textOf;
using TextEditor::QuickFixOperation::charAt;
Utils::ChangeSet::Range range(unsigned tokenIndex) const;
Utils::ChangeSet::Range range(CPlusPlus::AST *ast) const;
CPlusPlus::Scope *scopeAt(unsigned index) const;
QString fileName() const;
bool isCursorOn(unsigned tokenIndex) const;
bool isCursorOn(const CPlusPlus::AST *ast) const;
virtual void apply();
virtual CppRefactoringChanges *refactoringChanges() const;
Range range(unsigned tokenIndex) const;
Range range(CPlusPlus::AST *ast) const;
const CPlusPlus::Token &tokenAt(unsigned index) const;
CPlusPlus::Scope *scopeAt(unsigned index) const;
int startOf(unsigned index) const;
int startOf(const CPlusPlus::AST *ast) const;
int endOf(unsigned index) const;
int endOf(const CPlusPlus::AST *ast) const;
void startAndEndOf(unsigned index, int *start, int *end) const;
bool isCursorOn(unsigned tokenIndex) const;
bool isCursorOn(const CPlusPlus::AST *ast) const;
QString textOf(const CPlusPlus::AST *ast) const;
private:
CppRefactoringChanges *_refactoringChanges;
CPlusPlus::Document::Ptr _document;
CPlusPlus::AST *_topLevelNode;
QList<CPlusPlus::AST *> _path;
CPlusPlus::Snapshot _snapshot;
CppEditor::Internal::SemanticInfo _semanticInfo;
CPlusPlus::LookupContext _context;
};
class CppQuickFixCollector: public TextEditor::QuickFixCollector
class CPPEDITOR_EXPORT CppQuickFixOperation: public TextEditor::QuickFixOperation
{
Q_OBJECT
Q_DISABLE_COPY(CppQuickFixOperation)
public:
CppQuickFixCollector();
virtual ~CppQuickFixCollector();
CppQuickFixOperation(const CppQuickFixState &state, int priority = -1);
virtual ~CppQuickFixOperation();
virtual bool supportsEditor(TextEditor::ITextEditable *editor);
virtual TextEditor::QuickFixState *initializeCompletion(TextEditor::ITextEditable *editable);
protected:
QString fileName() const;
CppRefactoringChanges *refactoringChanges() const;
const CppQuickFixState &state() const;
protected: // Utility functions forwarding to CppQuickFixState
typedef Utils::ChangeSet::Range Range;
bool isCursorOn(unsigned tokenIndex) const { return state().isCursorOn(tokenIndex); }
bool isCursorOn(const CPlusPlus::AST *ast) const { return state().isCursorOn(ast); }
Range range(int start, int end) const { return CppQuickFixState::range(start, end); }
Range range(unsigned tokenIndex) const { return state().range(tokenIndex); }
Range range(CPlusPlus::AST *ast) const { return state().range(ast); }
int startOf(unsigned index) const { return state().startOf(index); }
int startOf(const CPlusPlus::AST *ast) const { return state().startOf(ast); }
int endOf(unsigned index) const { return state().endOf(index); }
int endOf(const CPlusPlus::AST *ast) const { return state().endOf(ast); }
private:
CppQuickFixState _state;
QScopedPointer<CppRefactoringChanges> _refactoringChanges;
};
class CppQuickFixFactory: public TextEditor::IQuickFixFactory
class CPPEDITOR_EXPORT CppQuickFixFactory: public TextEditor::QuickFixFactory
{
Q_OBJECT
public:
CppQuickFixFactory(QObject *parent = 0);
CppQuickFixFactory();
virtual ~CppQuickFixFactory();
/*
* Returns true if this IQuickFixFactory can be used with the given editor.
*/
virtual QList<TextEditor::QuickFixOperation::Ptr> quickFixOperations(TextEditor::BaseTextEditor *editor);
virtual QList<TextEditor::QuickFixOperation::Ptr> matchingOperations(TextEditor::QuickFixState *state);
virtual QList<CppQuickFixOperation::Ptr> match(const CppQuickFixState &state) = 0;
protected:
static QList<CppQuickFixOperation::Ptr> singleResult(CppQuickFixOperation *operation);
static QList<CppQuickFixOperation::Ptr> noResult();
};
namespace Internal {
class CppQuickFixCollector: public TextEditor::QuickFixCollector
{
Q_OBJECT
public:
CppQuickFixCollector();
virtual ~CppQuickFixCollector();
virtual bool supportsEditor(TextEditor::ITextEditable *editor);
virtual TextEditor::QuickFixState *initializeCompletion(TextEditor::BaseTextEditor *editor);
virtual QList<TextEditor::QuickFixFactory *> quickFixFactories() const;
static void registerQuickFixes(ExtensionSystem::IPlugin *plugIn);
};
} // end of namespace Internal
......
This diff is collapsed.
......@@ -40,11 +40,10 @@
#include <QtCore/QFileInfo>
using namespace QmlJS::AST;
using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal;
ComponentFromObjectDef::ComponentFromObjectDef(TextEditor::BaseTextEditor *editor)
: QmlJSQuickFixOperation(editor), _objDef(0)
{}
namespace {
static QString getId(UiObjectDefinition *def)
{
......@@ -74,60 +73,71 @@ static QString getId(UiObjectDefinition *def)
return QString();
}
QString ComponentFromObjectDef::description() const
class Operation: public QmlJSQuickFixOperation
{
return QCoreApplication::translate("QmlJSEditor::ComponentFromObjectDef",
"Extract Component");
}
UiObjectDefinition *_objDef;
public:
Operation(const QmlJSQuickFixState &state, UiObjectDefinition *objDef)
: QmlJSQuickFixOperation(state, 0)
, _objDef(objDef)
{
setDescription(QCoreApplication::translate("QmlJSEditor::ComponentFromObjectDef",
"Extract Component"));
}
void ComponentFromObjectDef::createChanges()
{
Q_ASSERT(_objDef != 0);
virtual void createChanges()
{
Q_ASSERT(_objDef != 0);
QString componentName = getId(_objDef);
componentName[0] = componentName.at(0).toUpper();
QString componentName = getId(_objDef);
componentName[0] = componentName.at(0).toUpper();
const QString path = editor()->file()->fileName();
const QString newFileName = QFileInfo(path).path() + QDir::separator() + componentName + QLatin1String(".qml");
const QString path = fileName();
const QString newFileName = QFileInfo(path).path() + QDir::separator() + componentName + QLatin1String(".qml");
QString imports;
UiProgram *prog = semanticInfo().document->qmlProgram();
if (prog && prog->imports) {
const int start = startPosition(prog->imports->firstSourceLocation());
const int end = startPosition(prog->members->member->firstSourceLocation());
imports = textOf(start, end);
}
QString imports;
UiProgram *prog = state().semanticInfo().document->qmlProgram();
if (prog && prog->imports) {
const int start = startPosition(prog->imports->firstSourceLocation());
const int end = startPosition(prog->members->member->firstSourceLocation());
imports = state().textOf(start, end);
}
const int start = startPosition(_objDef->firstSourceLocation());
const int end = startPosition(_objDef->lastSourceLocation());
const QString txt = imports + textOf(start, end) + QLatin1String("}\n");
const int start = startPosition(_objDef->firstSourceLocation());
const int end = startPosition(_objDef->lastSourceLocation());
const QString txt = imports + state().textOf(start, end)
+ QLatin1String("}\n");
Utils::ChangeSet changes;
changes.replace(start, end, componentName + QLatin1String(" {\n"));
qmljsRefactoringChanges()->changeFile(fileName(), changes);
qmljsRefactoringChanges()->reindent(fileName(), range(start, end + 1));
Utils::ChangeSet changes;
changes.replace(start, end, componentName + QLatin1String(" {\n"));
refactoringChanges()->changeFile(fileName(), changes);
refactoringChanges()->reindent(fileName(), range(start, end + 1));
qmljsRefactoringChanges()->createFile(newFileName, txt);
qmljsRefactoringChanges()->reindent(newFileName, range(0, txt.length() - 1));
}
refactoringChanges()->createFile(newFileName, txt);
refactoringChanges()->reindent(newFileName, range(0, txt.length() - 1));
}
};
} // end of anonymous namespace
int ComponentFromObjectDef::check()
QList<QmlJSQuickFixOperation::Ptr> ComponentFromObjectDef::match(const QmlJSQuickFixState &state)
{
_objDef = 0;
const int pos = textCursor().position();
QList<QmlJSQuickFixOperation::Ptr> result;
const int pos = state.textCursor().position();
QList<Node *> path = semanticInfo().astPath(pos);
QList<Node *> path = state.semanticInfo().astPath(pos);
for (int i = path.size() - 1; i >= 0; --i) {
Node *node = path.at(i);
if (UiObjectDefinition *objDef = cast<UiObjectDefinition *>(node)) {
if (i > 0 && !cast<UiProgram*>(path.at(i - 1))) { // node is not the root node
if (!getId(objDef).isEmpty()) {
_objDef = objDef;
return 0;
result.append(QmlJSQuickFixOperation::Ptr(new Operation(state, objDef)));
return result;
}
}
}
}
return -1;
return result;
}
......@@ -35,17 +35,10 @@
namespace QmlJSEditor {
namespace Internal {
class ComponentFromObjectDef: public QmlJSQuickFixOperation
class ComponentFromObjectDef: public QmlJSQuickFixFactory
{
public:
ComponentFromObjectDef(TextEditor::BaseTextEditor *editor);
virtual QString description() const;
virtual void createChanges();
virtual int check();
private:
QmlJS::AST::UiObjectDefinition *_objDef;
virtual QList<QmlJSQuickFixOperation::Ptr> match(const QmlJSQuickFixState &state);
};
} // namespace Internal
......
......@@ -179,6 +179,7 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
m_quickFixCollector = new QmlJSQuickFixCollector;
addAutoReleasedObject(m_quickFixCollector);
QmlJSQuickFixCollector::registerQuickFixes(this);
addAutoReleasedObject(new QmlJSOutlineWidgetFactory);
......
This diff is collapsed.
......@@ -31,49 +31,85 @@
#define QMLJSQUICKFIX_H
#include "qmljseditor.h"
#include "qmljsrefactoringchanges.h"
#include <texteditor/quickfix.h>
#include <qmljs/parser/qmljsastfwd_p.h>
#include <qmljs/qmljsdocument.h>
namespace ExtensionSystem {
class IPlugin;
}
namespace QmlJS {
class ModelManagerInterface;
}
namespace QmlJSEditor {
class QmlJSRefactoringChanges;
namespace Internal {
class QmlJSQuickFixCollector;
} // end of namespace Internal
class QmlJSQuickFixOperation: public TextEditor::QuickFixOperation
class QMLJS_EXPORT QmlJSQuickFixState: public TextEditor::QuickFixState
{
Q_DISABLE_COPY(QmlJSQuickFixOperation)
friend class Internal::QmlJSQuickFixCollector;
public:
QmlJSQuickFixOperation(TextEditor::BaseTextEditor *editor);
virtual ~QmlJSQuickFixOperation();
QmlJSQuickFixState(TextEditor::BaseTextEditor *editor);
typedef Utils::ChangeSet::Range Range;
Internal::SemanticInfo semanticInfo() const;
QmlJS::Snapshot snapshot() const;
QmlJS::Document::Ptr document() const;
const QmlJS::Snapshot &snapshot() const;
const SemanticInfo &semanticInfo() const;
virtual int check() = 0;
virtual int match(TextEditor::QuickFixState *state);
unsigned startPosition(const QmlJS::AST::SourceLocation &loc) const;
private:
Internal::SemanticInfo _semanticInfo;
};
class QMLJS_EXPORT QmlJSQuickFixOperation: public TextEditor::QuickFixOperation
{
Q_DISABLE_COPY(QmlJSQuickFixOperation)
public:
QmlJSQuickFixOperation(const QmlJSQuickFixState &state, int priority = -1);
virtual ~QmlJSQuickFixOperation();
const QmlJSQuickFixState &state() const;
protected:
QString fileName() const;
virtual void apply();
QmlJSRefactoringChanges *qmljsRefactoringChanges() const;
virtual TextEditor::RefactoringChanges *refactoringChanges() const;
QmlJSRefactoringChanges *refactoringChanges() const;
unsigned startPosition(const QmlJS::AST::SourceLocation &loc) const;
protected: // Utility functions forwarding to QmlJSQuickFixState
unsigned startPosition(const QmlJS::AST::SourceLocation &loc) const
{ return state().startPosition(loc); }
static QmlJSQuickFixState::Range range(int start, int end)
{ return QmlJSQuickFixState::range(start, end); }
private:
SemanticInfo _semanticInfo;
QmlJSRefactoringChanges *_refactoringChanges;
QmlJSQuickFixState _state;
QScopedPointer<QmlJSRefactoringChanges> _refactoringChanges;
};
class QMLJS_EXPORT QmlJSQuickFixFactory: public TextEditor::QuickFixFactory
{
Q_OBJECT
public:
QmlJSQuickFixFactory();
virtual ~QmlJSQuickFixFactory();
virtual QList<TextEditor::QuickFixOperation::Ptr> matchingOperations(TextEditor::QuickFixState *state);
virtual QList<QmlJSQuickFixOperation::Ptr> match(const QmlJSQuickFixState &state) = 0;
};
namespace Internal {
class QmlJSQuickFixCollector: public TextEditor::QuickFixCollector
{
Q_OBJECT
......@@ -83,8 +119,11 @@ public:
virtual ~QmlJSQuickFixCollector();
virtual bool supportsEditor(TextEditor::ITextEditable *editor);
virtual TextEditor::QuickFixState *initializeCompletion(TextEditor::ITextEditable *editable);
virtual QList<TextEditor::QuickFixOperation::Ptr> quickFixOperations(TextEditor::BaseTextEditor *editor) const;
virtual TextEditor::QuickFixState *initializeCompletion(TextEditor::BaseTextEditor *editor);
virtual QList<TextEditor::QuickFixFactory *> quickFixFactories() const;
static void registerQuickFixes(ExtensionSystem::IPlugin *plugIn);
};
} // end of namespace Internal
......
......@@ -40,7 +40,7 @@ class ModelManagerInterface;
namespace QmlJSEditor {
class QmlJSRefactoringChanges: public TextEditor::RefactoringChanges