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: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2017-12-15 15:46:37 +01:00
parent 4ed12d95f8
commit 479ab4ef22
10 changed files with 88 additions and 102 deletions

View File

@@ -29,8 +29,6 @@
#include <cpptools/cpprefactoringchanges.h> #include <cpptools/cpprefactoringchanges.h>
#include <utils/algorithm.h>
using namespace CppEditor; using namespace CppEditor;
using namespace CppEditor::Internal; using namespace CppEditor::Internal;
using namespace CppTools; using namespace CppTools;
@@ -43,12 +41,3 @@ CppQuickFixOperation::CppQuickFixOperation(const CppQuickFixInterface &interface
CppQuickFixOperation::~CppQuickFixOperation() CppQuickFixOperation::~CppQuickFixOperation()
{} {}
void CppQuickFixFactory::matchingOperations(const QuickFixInterface &interface, QuickFixOperations &result)
{
auto cppInterface = interface.staticCast<const CppQuickFixInterface>();
if (cppInterface->path().isEmpty())
return;
match(*cppInterface, result);
}

View File

@@ -42,13 +42,25 @@ public:
~CppQuickFixOperation(); ~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 Q_OBJECT
public: public:
void matchingOperations(const TextEditor::QuickFixInterface &interface, CppQuickFixFactory();
TextEditor::QuickFixOperations &result); ~CppQuickFixFactory();
/*! /*!
Implement this function to match and create the appropriate Implement this function to match and create the appropriate
@@ -56,6 +68,8 @@ public:
*/ */
virtual void match(const Internal::CppQuickFixInterface &interface, virtual void match(const Internal::CppQuickFixInterface &interface,
TextEditor::QuickFixOperations &result) = 0; TextEditor::QuickFixOperations &result) = 0;
static const QList<CppQuickFixFactory *> &cppQuickFixFactories();
}; };
} // namespace CppEditor } // namespace CppEditor

View File

@@ -55,12 +55,13 @@ class CppQuickFixAssistProcessor : public IAssistProcessor
IAssistProposal *perform(const AssistInterface *interface) override IAssistProposal *perform(const AssistInterface *interface) override
{ {
QSharedPointer<const AssistInterface> assistInterface(interface); QSharedPointer<const AssistInterface> assistInterface(interface);
auto cppInterface = assistInterface.staticCast<const CppQuickFixInterface>();
if (cppInterface->path().isEmpty())
return nullptr;
QuickFixOperations quickFixes; QuickFixOperations quickFixes;
for (CppQuickFixFactory *factory : CppQuickFixFactory::cppQuickFixFactories())
for (QuickFixFactory *factory : QuickFixFactory::allQuickFixFactories()) factory->match(*cppInterface, quickFixes);
if (qobject_cast<CppQuickFixFactory *>(factory) != nullptr)
factory->matchingOperations(assistInterface, quickFixes);
return GenericProposal::createProposal(interface, quickFixes); return GenericProposal::createProposal(interface, quickFixes);
} }

View File

@@ -79,6 +79,24 @@ using namespace TextEditor;
using Utils::ChangeSet; using Utils::ChangeSet;
namespace CppEditor { 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 { namespace Internal {
void registerQuickFixes(ExtensionSystem::IPlugin *plugIn) void registerQuickFixes(ExtensionSystem::IPlugin *plugIn)
@@ -2993,15 +3011,15 @@ public:
switch (m_type) { switch (m_type) {
case GetterSetterType: case GetterSetterType:
setPriority(5); setPriority(5);
setDescription(QuickFixFactory::tr("Create Getter and Setter Member Functions")); setDescription(CppQuickFixFactory::tr("Create Getter and Setter Member Functions"));
break; break;
case GetterType: case GetterType:
setPriority(4); setPriority(4);
setDescription(QuickFixFactory::tr("Create Getter Member Function")); setDescription(CppQuickFixFactory::tr("Create Getter Member Function"));
break; break;
case SetterType: case SetterType:
setPriority(3); setPriority(3);
setDescription(QuickFixFactory::tr("Create Setter Member Function")); setDescription(CppQuickFixFactory::tr("Create Setter Member Function"));
break; break;
default: default:
break; break;
@@ -4125,8 +4143,8 @@ public:
{ {
setDescription( setDescription(
mode == FromPointer mode == FromPointer
? QuickFixFactory::tr("Convert to Stack Variable") ? CppQuickFixFactory::tr("Convert to Stack Variable")
: QuickFixFactory::tr("Convert to Pointer")); : CppQuickFixFactory::tr("Convert to Pointer"));
} }
void perform() override void perform() override
@@ -4472,7 +4490,7 @@ public:
, m_signalName(signalName) , m_signalName(signalName)
, m_storageName(storageName) , m_storageName(storageName)
{ {
setDescription(QuickFixFactory::tr("Generate Missing Q_PROPERTY Members")); setDescription(CppQuickFixFactory::tr("Generate Missing Q_PROPERTY Members"));
} }
void perform() void perform()

View File

@@ -452,8 +452,7 @@ void RunAllQuickFixesTokenAction::run(CppEditorWidget *editorWidget)
if (qfi.path().isEmpty()) if (qfi.path().isEmpty())
return; return;
for (QuickFixFactory *quickFixFactory : QuickFixFactory::allQuickFixFactories()) { for (CppQuickFixFactory *cppQuickFixFactory : CppQuickFixFactory::cppQuickFixFactories()) {
if (auto cppQuickFixFactory = qobject_cast<CppQuickFixFactory *>(quickFixFactory)) {
QuickFixOperations operations; QuickFixOperations operations;
// Some Quick Fixes pop up a dialog and are therefore inappropriate for this test. // Some Quick Fixes pop up a dialog and are therefore inappropriate for this test.
// Where possible, use a guiless version of the factory. // Where possible, use a guiless version of the factory.
@@ -473,7 +472,6 @@ void RunAllQuickFixesTokenAction::run(CppEditorWidget *editorWidget)
QApplication::processEvents(); QApplication::processEvents();
} }
} }
}
} }
class SwitchHeaderSourceFileAction : public TestActionsTestCase::AbstractAction class SwitchHeaderSourceFileAction : public TestActionsTestCase::AbstractAction

View File

@@ -71,11 +71,4 @@ QString QmlJSQuickFixOperation::fileName() const
return m_interface->semanticInfo().document->fileName(); return m_interface->semanticInfo().document->fileName();
} }
void QmlJSQuickFixFactory::matchingOperations(const QuickFixInterface &interface,
QuickFixOperations &result)
{
match(interface.staticCast<const QmlJSQuickFixAssistInterface>(), result);
}
} // namespace QmlJSEditor } // namespace QmlJSEditor

View File

@@ -76,14 +76,13 @@ private:
QmlJSQuickFixInterface m_interface; QmlJSQuickFixInterface m_interface;
}; };
class QmlJSQuickFixFactory: public TextEditor::QuickFixFactory class QmlJSQuickFixFactory: public QObject
{ {
Q_OBJECT Q_OBJECT
protected: public:
QmlJSQuickFixFactory() {} QmlJSQuickFixFactory();
~QmlJSQuickFixFactory();
void matchingOperations(const QuickFixInterface &interface, QuickFixOperations &result);
/*! /*!
Implement this function to match and create the appropriate Implement this function to match and create the appropriate

View File

@@ -43,7 +43,23 @@ namespace QmlJSEditor {
using namespace Internal; 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, QmlJSQuickFixAssistInterface::QmlJSQuickFixAssistInterface(QmlJSEditorWidget *editor,
AssistReason reason) AssistReason reason)
@@ -74,12 +90,12 @@ class QmlJSQuickFixAssistProcessor : public IAssistProcessor
IAssistProposal *perform(const AssistInterface *interface) override IAssistProposal *perform(const AssistInterface *interface) override
{ {
QSharedPointer<const AssistInterface> assistInterface(interface); QSharedPointer<const AssistInterface> assistInterface(interface);
auto qmlJSInterface = assistInterface.staticCast<const QmlJSQuickFixAssistInterface>();
QuickFixOperations quickFixes; QuickFixOperations quickFixes;
for (QuickFixFactory *factory : QuickFixFactory::allQuickFixFactories()) for (QmlJSQuickFixFactory *factory : g_qmlJSQuickFixFactories)
if (qobject_cast<QmlJSQuickFixFactory *>(factory) != nullptr) factory->match(qmlJSInterface, quickFixes);
factory->matchingOperations(assistInterface, quickFixes);
return GenericProposal::createProposal(interface, quickFixes); return GenericProposal::createProposal(interface, quickFixes);
} }

View File

@@ -55,21 +55,3 @@ void QuickFixOperation::setDescription(const QString &description)
{ {
_description = 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;
}

View File

@@ -95,30 +95,6 @@ inline QuickFixOperations &operator<<(QuickFixOperations &list, QuickFixOperatio
typedef QSharedPointer<const AssistInterface> QuickFixInterface; 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 } // namespace TextEditor
Q_DECLARE_METATYPE(TextEditor::QuickFixOperation::Ptr) Q_DECLARE_METATYPE(TextEditor::QuickFixOperation::Ptr)