Commit 479ab4ef authored by hjk's avatar hjk

TextEditor: Split the global QuickFixFactory list

It's only ever used in the filtered Cpp/QmlJs variants. Splitting
the class simplifies the code and avoids re-doing filtering over
and over again.

Also inline QuickFixFactory::matchingOperations() into callers

Change-Id: I730756315f2e0321649259ef229631233b12fbdd
Reviewed-by: 's avatarDavid Schulz <david.schulz@qt.io>
parent 4ed12d95
......@@ -29,8 +29,6 @@
#include <cpptools/cpprefactoringchanges.h>
#include <utils/algorithm.h>
using namespace CppEditor;
using namespace CppEditor::Internal;
using namespace CppTools;
......@@ -43,12 +41,3 @@ CppQuickFixOperation::CppQuickFixOperation(const CppQuickFixInterface &interface
CppQuickFixOperation::~CppQuickFixOperation()
{}
void CppQuickFixFactory::matchingOperations(const QuickFixInterface &interface, QuickFixOperations &result)
{
auto cppInterface = interface.staticCast<const CppQuickFixInterface>();
if (cppInterface->path().isEmpty())
return;
match(*cppInterface, result);
}
......@@ -42,20 +42,34 @@ public:
~CppQuickFixOperation();
};
class CPPEDITOR_EXPORT CppQuickFixFactory: public TextEditor::QuickFixFactory
/*!
The QuickFixFactory is responsible for generating QuickFixOperation s which are
applicable to the given QuickFixState.
A QuickFixFactory should not have any state -- it can be invoked multiple times
for different QuickFixState objects to create the matching operations, before any
of those operations are applied (or released).
This way, a single factory can be used by multiple editors, and a single editor
can have multiple QuickFixCollector objects for different parts of the code.
*/
class CPPEDITOR_EXPORT CppQuickFixFactory : public QObject
{
Q_OBJECT
public:
void matchingOperations(const TextEditor::QuickFixInterface &interface,
TextEditor::QuickFixOperations &result);
CppQuickFixFactory();
~CppQuickFixFactory();
/*!
Implement this function to match and create the appropriate
CppQuickFixOperation objects.
*/
virtual void match(const Internal::CppQuickFixInterface &interface,
TextEditor::QuickFixOperations &result) = 0;
TextEditor::QuickFixOperations &result) = 0;
static const QList<CppQuickFixFactory *> &cppQuickFixFactories();
};
} // namespace CppEditor
......@@ -55,12 +55,13 @@ class CppQuickFixAssistProcessor : public IAssistProcessor
IAssistProposal *perform(const AssistInterface *interface) override
{
QSharedPointer<const AssistInterface> assistInterface(interface);
auto cppInterface = assistInterface.staticCast<const CppQuickFixInterface>();
if (cppInterface->path().isEmpty())
return nullptr;
QuickFixOperations quickFixes;
for (QuickFixFactory *factory : QuickFixFactory::allQuickFixFactories())
if (qobject_cast<CppQuickFixFactory *>(factory) != nullptr)
factory->matchingOperations(assistInterface, quickFixes);
for (CppQuickFixFactory *factory : CppQuickFixFactory::cppQuickFixFactories())
factory->match(*cppInterface, quickFixes);
return GenericProposal::createProposal(interface, quickFixes);
}
......
......@@ -79,6 +79,24 @@ using namespace TextEditor;
using Utils::ChangeSet;
namespace CppEditor {
static QList<CppQuickFixFactory *> g_cppQuickFixFactories;
CppQuickFixFactory::CppQuickFixFactory()
{
g_cppQuickFixFactories.append(this);
}
CppQuickFixFactory::~CppQuickFixFactory()
{
g_cppQuickFixFactories.removeOne(this);
}
const QList<CppQuickFixFactory *> &CppQuickFixFactory::cppQuickFixFactories()
{
return g_cppQuickFixFactories;
}
namespace Internal {
void registerQuickFixes(ExtensionSystem::IPlugin *plugIn)
......@@ -2993,15 +3011,15 @@ public:
switch (m_type) {
case GetterSetterType:
setPriority(5);
setDescription(QuickFixFactory::tr("Create Getter and Setter Member Functions"));
setDescription(CppQuickFixFactory::tr("Create Getter and Setter Member Functions"));
break;
case GetterType:
setPriority(4);
setDescription(QuickFixFactory::tr("Create Getter Member Function"));
setDescription(CppQuickFixFactory::tr("Create Getter Member Function"));
break;
case SetterType:
setPriority(3);
setDescription(QuickFixFactory::tr("Create Setter Member Function"));
setDescription(CppQuickFixFactory::tr("Create Setter Member Function"));
break;
default:
break;
......@@ -4125,8 +4143,8 @@ public:
{
setDescription(
mode == FromPointer
? QuickFixFactory::tr("Convert to Stack Variable")
: QuickFixFactory::tr("Convert to Pointer"));
? CppQuickFixFactory::tr("Convert to Stack Variable")
: CppQuickFixFactory::tr("Convert to Pointer"));
}
void perform() override
......@@ -4472,7 +4490,7 @@ public:
, m_signalName(signalName)
, m_storageName(storageName)
{
setDescription(QuickFixFactory::tr("Generate Missing Q_PROPERTY Members"));
setDescription(CppQuickFixFactory::tr("Generate Missing Q_PROPERTY Members"));
}
void perform()
......
......@@ -452,26 +452,24 @@ void RunAllQuickFixesTokenAction::run(CppEditorWidget *editorWidget)
if (qfi.path().isEmpty())
return;
for (QuickFixFactory *quickFixFactory : QuickFixFactory::allQuickFixFactories()) {
if (auto cppQuickFixFactory = qobject_cast<CppQuickFixFactory *>(quickFixFactory)) {
QuickFixOperations operations;
// Some Quick Fixes pop up a dialog and are therefore inappropriate for this test.
// Where possible, use a guiless version of the factory.
if (qobject_cast<InsertVirtualMethods *>(cppQuickFixFactory)) {
QScopedPointer<CppQuickFixFactory> factoryProducingGuiLessOperations;
factoryProducingGuiLessOperations.reset(InsertVirtualMethods::createTestFactory());
factoryProducingGuiLessOperations->match(qfi, operations);
} else {
cppQuickFixFactory->match(qfi, operations);
}
for (CppQuickFixFactory *cppQuickFixFactory : CppQuickFixFactory::cppQuickFixFactories()) {
QuickFixOperations operations;
// Some Quick Fixes pop up a dialog and are therefore inappropriate for this test.
// Where possible, use a guiless version of the factory.
if (qobject_cast<InsertVirtualMethods *>(cppQuickFixFactory)) {
QScopedPointer<CppQuickFixFactory> factoryProducingGuiLessOperations;
factoryProducingGuiLessOperations.reset(InsertVirtualMethods::createTestFactory());
factoryProducingGuiLessOperations->match(qfi, operations);
} else {
cppQuickFixFactory->match(qfi, operations);
}
foreach (QuickFixOperation::Ptr operation, operations) {
qDebug() << " -- Performing Quick Fix" << operation->description();
operation->perform();
TestActionsTestCase::escape();
TestActionsTestCase::undoChangesInAllEditorWidgets();
QApplication::processEvents();
}
foreach (QuickFixOperation::Ptr operation, operations) {
qDebug() << " -- Performing Quick Fix" << operation->description();
operation->perform();
TestActionsTestCase::escape();
TestActionsTestCase::undoChangesInAllEditorWidgets();
QApplication::processEvents();
}
}
}
......
......@@ -71,11 +71,4 @@ QString QmlJSQuickFixOperation::fileName() const
return m_interface->semanticInfo().document->fileName();
}
void QmlJSQuickFixFactory::matchingOperations(const QuickFixInterface &interface,
QuickFixOperations &result)
{
match(interface.staticCast<const QmlJSQuickFixAssistInterface>(), result);
}
} // namespace QmlJSEditor
......@@ -76,14 +76,13 @@ private:
QmlJSQuickFixInterface m_interface;
};
class QmlJSQuickFixFactory: public TextEditor::QuickFixFactory
class QmlJSQuickFixFactory: public QObject
{
Q_OBJECT
protected:
QmlJSQuickFixFactory() {}
void matchingOperations(const QuickFixInterface &interface, QuickFixOperations &result);
public:
QmlJSQuickFixFactory();
~QmlJSQuickFixFactory();
/*!
Implement this function to match and create the appropriate
......
......@@ -43,7 +43,23 @@ namespace QmlJSEditor {
using namespace Internal;
// -----------------------
// QuickFixAssistInterface
// QmlJSQuickFixFactory
// -----------------------
static QList<QmlJSQuickFixFactory *> g_qmlJSQuickFixFactories;
QmlJSQuickFixFactory::QmlJSQuickFixFactory()
{
g_qmlJSQuickFixFactories.append(this);
}
QmlJSQuickFixFactory::~QmlJSQuickFixFactory()
{
g_qmlJSQuickFixFactories.removeOne(this);
}
// -----------------------
// QmlJSQuickFixAssistInterface
// -----------------------
QmlJSQuickFixAssistInterface::QmlJSQuickFixAssistInterface(QmlJSEditorWidget *editor,
AssistReason reason)
......@@ -74,12 +90,12 @@ class QmlJSQuickFixAssistProcessor : public IAssistProcessor
IAssistProposal *perform(const AssistInterface *interface) override
{
QSharedPointer<const AssistInterface> assistInterface(interface);
auto qmlJSInterface = assistInterface.staticCast<const QmlJSQuickFixAssistInterface>();
QuickFixOperations quickFixes;
for (QuickFixFactory *factory : QuickFixFactory::allQuickFixFactories())
if (qobject_cast<QmlJSQuickFixFactory *>(factory) != nullptr)
factory->matchingOperations(assistInterface, quickFixes);
for (QmlJSQuickFixFactory *factory : g_qmlJSQuickFixFactories)
factory->match(qmlJSInterface, quickFixes);
return GenericProposal::createProposal(interface, quickFixes);
}
......
......@@ -55,21 +55,3 @@ void QuickFixOperation::setDescription(const QString &description)
{
_description = description;
}
static QList<QuickFixFactory *> g_quickFixFactories;
QuickFixFactory::QuickFixFactory(QObject *parent)
: QObject(parent)
{
g_quickFixFactories.append(this);
}
QuickFixFactory::~QuickFixFactory()
{
g_quickFixFactories.removeOne(this);
}
const QList<QuickFixFactory *> QuickFixFactory::allQuickFixFactories()
{
return g_quickFixFactories;
}
......@@ -95,30 +95,6 @@ inline QuickFixOperations &operator<<(QuickFixOperations &list, QuickFixOperatio
typedef QSharedPointer<const AssistInterface> QuickFixInterface;
/*!
The QuickFixFactory is responsible for generating QuickFixOperation s which are
applicable to the given QuickFixState.
A QuickFixFactory should not have any state -- it can be invoked multiple times
for different QuickFixState objects to create the matching operations, before any
of those operations are applied (or released).
This way, a single factory can be used by multiple editors, and a single editor
can have multiple QuickFixCollector objects for different parts of the code.
*/
class TEXTEDITOR_EXPORT QuickFixFactory: public QObject
{
Q_OBJECT
public:
QuickFixFactory(QObject *parent = 0);
~QuickFixFactory();
static const QList<QuickFixFactory *> allQuickFixFactories();
virtual void matchingOperations(const QuickFixInterface &interface, QuickFixOperations &result) = 0;
};
} // namespace TextEditor
Q_DECLARE_METATYPE(TextEditor::QuickFixOperation::Ptr)
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