QmlJSEditor: Create QuickFixes directly

So far there have been factories instantiated and maintained to get
a list of 'matching' operation by iterating over the factories'
match() functions. The same effect can be achieved more directly
by calling stand-alone functions.

Change-Id: I868489d36f9d8339e0d8855d832df8400501026c
Reviewed-by: Marco Benelli <marco.benelli@qt.io>
This commit is contained in:
hjk
2017-12-18 12:23:28 +01:00
parent e4eb4c3d0f
commit b4faec80ba
9 changed files with 107 additions and 156 deletions

View File

@@ -132,7 +132,7 @@ bool BaseTextEditModifier::moveToComponent(int nodeOffset)
if (!object) if (!object)
return false; return false;
QmlJSEditor::ComponentFromObjectDef::perform(document->filePath().toString(), object); QmlJSEditor::performComponentFromObjectDef(document->filePath().toString(), object);
return true; return true;
} }
} }

View File

@@ -233,7 +233,7 @@ public:
} // end of anonymous namespace } // end of anonymous namespace
void ComponentFromObjectDef::match(const QmlJSQuickFixInterface &interface, QuickFixOperations &result) void matchComponentFromObjectDefQuickFix(const QmlJSQuickFixInterface &interface, QuickFixOperations &result)
{ {
const int pos = interface->currentFile()->cursor().position(); const int pos = interface->currentFile()->cursor().position();
@@ -258,7 +258,7 @@ void ComponentFromObjectDef::match(const QmlJSQuickFixInterface &interface, Quic
} }
} }
void ComponentFromObjectDef::perform(const QString &fileName, QmlJS::AST::UiObjectDefinition *objDef) void performComponentFromObjectDef(const QString &fileName, QmlJS::AST::UiObjectDefinition *objDef)
{ {
QmlJSRefactoringChanges refactoring(QmlJS::ModelManagerInterface::instance(), QmlJSRefactoringChanges refactoring(QmlJS::ModelManagerInterface::instance(),
QmlJS::ModelManagerInterface::instance()->snapshot()); QmlJS::ModelManagerInterface::instance()->snapshot());

View File

@@ -29,12 +29,10 @@
namespace QmlJSEditor { namespace QmlJSEditor {
class QMLJSEDITOR_EXPORT ComponentFromObjectDef : public QmlJSQuickFixFactory QMLJSEDITOR_EXPORT void matchComponentFromObjectDefQuickFix
{ (const QmlJSQuickFixInterface &interface, QuickFixOperations &result);
public:
void match(const QmlJSQuickFixInterface &interface, QuickFixOperations &result) override;
static void perform(const QString &fileName, QmlJS::AST::UiObjectDefinition *objDef); QMLJSEDITOR_EXPORT void performComponentFromObjectDef
}; (const QString &fileName, QmlJS::AST::UiObjectDefinition *objDef);
} // namespace QmlJSEditor } // namespace QmlJSEditor

View File

@@ -78,8 +78,6 @@ enum {
namespace QmlJSEditor { namespace QmlJSEditor {
using namespace Internal; using namespace Internal;
void registerQuickFixes(ExtensionSystem::IPlugin *plugIn);
QmlJSEditorPlugin *QmlJSEditorPlugin::m_instance = 0; QmlJSEditorPlugin *QmlJSEditorPlugin::m_instance = 0;
QmlJSEditorPlugin::QmlJSEditorPlugin() : QmlJSEditorPlugin::QmlJSEditorPlugin() :
@@ -193,8 +191,6 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
FileIconProvider::registerIconOverlayForSuffix(ProjectExplorer::Constants::FILEOVERLAY_QML, "qml"); FileIconProvider::registerIconOverlayForSuffix(ProjectExplorer::Constants::FILEOVERLAY_QML, "qml");
registerQuickFixes(this);
addAutoReleasedObject(new QmlJSOutlineWidgetFactory); addAutoReleasedObject(new QmlJSOutlineWidgetFactory);
addAutoReleasedObject(new QuickToolBar); addAutoReleasedObject(new QuickToolBar);

View File

@@ -34,8 +34,6 @@
#include <QSharedPointer> #include <QSharedPointer>
namespace QmlJS { class ModelManagerInterface; }
namespace QmlJSEditor { namespace QmlJSEditor {
namespace Internal { class QmlJSQuickFixAssistInterface; } namespace Internal { class QmlJSQuickFixAssistInterface; }
@@ -76,19 +74,6 @@ private:
QmlJSQuickFixInterface m_interface; QmlJSQuickFixInterface m_interface;
}; };
class QmlJSQuickFixFactory: public QObject TextEditor::QuickFixOperations findQmlJSQuickFixes(const TextEditor::AssistInterface *interface);
{
Q_OBJECT
public:
QmlJSQuickFixFactory();
~QmlJSQuickFixFactory();
/*!
Implement this function to match and create the appropriate
QmlJSQuickFixOperation objects.
*/
virtual void match(const QmlJSQuickFixInterface &interface, TextEditor::QuickFixOperations &result) = 0;
};
} // namespace QmlJSEditor } // namespace QmlJSEditor

View File

@@ -42,22 +42,6 @@ namespace QmlJSEditor {
using namespace Internal; using namespace Internal;
// -----------------------
// QmlJSQuickFixFactory
// -----------------------
static QList<QmlJSQuickFixFactory *> g_qmlJSQuickFixFactories;
QmlJSQuickFixFactory::QmlJSQuickFixFactory()
{
g_qmlJSQuickFixFactories.append(this);
}
QmlJSQuickFixFactory::~QmlJSQuickFixFactory()
{
g_qmlJSQuickFixFactories.removeOne(this);
}
// ----------------------- // -----------------------
// QmlJSQuickFixAssistInterface // QmlJSQuickFixAssistInterface
// ----------------------- // -----------------------
@@ -89,15 +73,7 @@ class QmlJSQuickFixAssistProcessor : public IAssistProcessor
{ {
IAssistProposal *perform(const AssistInterface *interface) override IAssistProposal *perform(const AssistInterface *interface) override
{ {
QSharedPointer<const AssistInterface> assistInterface(interface); return GenericProposal::createProposal(interface, findQmlJSQuickFixes(interface));
auto qmlJSInterface = assistInterface.staticCast<const QmlJSQuickFixAssistInterface>();
QuickFixOperations quickFixes;
for (QmlJSQuickFixFactory *factory : g_qmlJSQuickFixFactories)
factory->match(qmlJSInterface, quickFixes);
return GenericProposal::createProposal(interface, quickFixes);
} }
}; };

View File

@@ -33,13 +33,14 @@
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <qmljs/parser/qmljsast_p.h> #include <qmljs/parser/qmljsast_p.h>
#include <qmljstools/qmljsrefactoringchanges.h> #include <qmljstools/qmljsrefactoringchanges.h>
#include <texteditor/codeassist/assistinterface.h>
#include <QApplication> #include <QApplication>
using namespace QmlJS; using namespace QmlJS;
using namespace QmlJS::AST; using namespace QmlJS::AST;
using namespace QmlJSTools; using namespace QmlJSTools;
using TextEditor::RefactoringChanges; using namespace TextEditor;
namespace QmlJSEditor { namespace QmlJSEditor {
@@ -57,35 +58,12 @@ namespace {
width: 10 width: 10
} }
*/ */
class SplitInitializerOp: public QmlJSQuickFixFactory class SplitInitializerOperation: public QmlJSQuickFixOperation
{ {
void match(const QmlJSQuickFixInterface &interface, QuickFixOperations &result) override
{
UiObjectInitializer *objectInitializer = 0;
const int pos = interface->currentFile()->cursor().position();
if (Node *member = interface->semanticInfo().rangeAt(pos)) {
if (UiObjectBinding *b = AST::cast<UiObjectBinding *>(member)) {
if (b->initializer->lbraceToken.startLine == b->initializer->rbraceToken.startLine)
objectInitializer = b->initializer;
} else if (UiObjectDefinition *b = AST::cast<UiObjectDefinition *>(member)) {
if (b->initializer->lbraceToken.startLine == b->initializer->rbraceToken.startLine)
objectInitializer = b->initializer;
}
}
if (objectInitializer)
result << new Operation(interface, objectInitializer);
}
class Operation: public QmlJSQuickFixOperation
{
UiObjectInitializer *_objectInitializer; UiObjectInitializer *_objectInitializer;
public: public:
Operation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface, SplitInitializerOperation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
UiObjectInitializer *objectInitializer) UiObjectInitializer *objectInitializer)
: QmlJSQuickFixOperation(interface, 0) : QmlJSQuickFixOperation(interface, 0)
, _objectInitializer(objectInitializer) , _objectInitializer(objectInitializer)
@@ -119,40 +97,45 @@ class SplitInitializerOp: public QmlJSQuickFixFactory
currentFile->startOf(_objectInitializer->rbraceToken))); currentFile->startOf(_objectInitializer->rbraceToken)));
currentFile->apply(); currentFile->apply();
} }
};
}; };
void matchSplitInitializerQuickFix(const QmlJSQuickFixInterface &interface, QuickFixOperations &result)
{
UiObjectInitializer *objectInitializer = 0;
const int pos = interface->currentFile()->cursor().position();
if (Node *member = interface->semanticInfo().rangeAt(pos)) {
if (UiObjectBinding *b = AST::cast<UiObjectBinding *>(member)) {
if (b->initializer->lbraceToken.startLine == b->initializer->rbraceToken.startLine)
objectInitializer = b->initializer;
} else if (UiObjectDefinition *b = AST::cast<UiObjectDefinition *>(member)) {
if (b->initializer->lbraceToken.startLine == b->initializer->rbraceToken.startLine)
objectInitializer = b->initializer;
}
}
if (objectInitializer)
result << new SplitInitializerOperation(interface, objectInitializer);
}
/* /*
Adds a comment to suppress a static analysis message Adds a comment to suppress a static analysis message
*/ */
class AddAnalysisMessageSuppressionComment: public QmlJSQuickFixFactory class AnalysizeMessageSuppressionOperation: public QmlJSQuickFixOperation
{ {
Q_DECLARE_TR_FUNCTIONS(QmlJSEditor::AddAnalysisMessageSuppressionComment)
public:
void match(const QmlJSQuickFixInterface &interface, QuickFixOperations &result) override
{
const QList<StaticAnalysis::Message> &messages = interface->semanticInfo().staticAnalysisMessages;
foreach (const StaticAnalysis::Message &message, messages) {
if (interface->currentFile()->isCursorOn(message.location)) {
result << new Operation(interface, message);
return;
}
}
}
private:
class Operation: public QmlJSQuickFixOperation
{
StaticAnalysis::Message _message; StaticAnalysis::Message _message;
public: Q_DECLARE_TR_FUNCTIONS(AddAnalysisMessageSuppressionComment)
Operation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
public:
AnalysizeMessageSuppressionOperation(const QSharedPointer<const QmlJSQuickFixAssistInterface> &interface,
const StaticAnalysis::Message &message) const StaticAnalysis::Message &message)
: QmlJSQuickFixOperation(interface, 0) : QmlJSQuickFixOperation(interface, 0)
, _message(message) , _message(message)
{ {
setDescription(AddAnalysisMessageSuppressionComment::tr("Add a Comment to Suppress This Message")); setDescription(tr("Add a Comment to Suppress This Message"));
} }
virtual void performChanges(QmlJSRefactoringFilePtr currentFile, virtual void performChanges(QmlJSRefactoringFilePtr currentFile,
@@ -165,17 +148,35 @@ private:
currentFile->appendIndentRange(Range(insertLoc, insertLoc + 1)); currentFile->appendIndentRange(Range(insertLoc, insertLoc + 1));
currentFile->apply(); currentFile->apply();
} }
};
}; };
void matchAddAnalysisMessageSuppressionCommentQuickFix(const QmlJSQuickFixInterface &interface, QuickFixOperations &result)
{
const QList<StaticAnalysis::Message> &messages = interface->semanticInfo().staticAnalysisMessages;
foreach (const StaticAnalysis::Message &message, messages) {
if (interface->currentFile()->isCursorOn(message.location)) {
result << new AnalysizeMessageSuppressionOperation(interface, message);
return;
}
}
}
} // end of anonymous namespace } // end of anonymous namespace
void registerQuickFixes(ExtensionSystem::IPlugin *plugIn) QuickFixOperations findQmlJSQuickFixes(const AssistInterface *interface)
{ {
plugIn->addAutoReleasedObject(new SplitInitializerOp); QSharedPointer<const AssistInterface> assistInterface(interface);
plugIn->addAutoReleasedObject(new ComponentFromObjectDef); auto qmlJSInterface = assistInterface.staticCast<const QmlJSQuickFixAssistInterface>();
plugIn->addAutoReleasedObject(new WrapInLoader);
plugIn->addAutoReleasedObject(new AddAnalysisMessageSuppressionComment); QuickFixOperations quickFixes;
matchSplitInitializerQuickFix(qmlJSInterface, quickFixes);
matchComponentFromObjectDefQuickFix(qmlJSInterface, quickFixes);
matchWrapInLoaderQuickFix(qmlJSInterface, quickFixes);
matchAddAnalysisMessageSuppressionCommentQuickFix(qmlJSInterface, quickFixes);
return quickFixes;
} }
} // namespace QmlJSEditor } // namespace QmlJSEditor

View File

@@ -174,7 +174,7 @@ public:
} // end of anonymous namespace } // end of anonymous namespace
void WrapInLoader::match(const QmlJSQuickFixInterface &interface, QuickFixOperations &result) void matchWrapInLoaderQuickFix(const QmlJSQuickFixInterface &interface, QuickFixOperations &result)
{ {
const int pos = interface->currentFile()->cursor().position(); const int pos = interface->currentFile()->cursor().position();

View File

@@ -28,12 +28,7 @@
#include "qmljsquickfix.h" #include "qmljsquickfix.h"
namespace QmlJSEditor { namespace QmlJSEditor {
namespace Internal {
class WrapInLoader: public QmlJSQuickFixFactory void matchWrapInLoaderQuickFix(const QmlJSQuickFixInterface &interface, QuickFixOperations &result);
{
void match(const QmlJSQuickFixInterface &interface, QuickFixOperations &result) override;
};
} // namespace Internal
} // namespace QmlJSEditor } // namespace QmlJSEditor