QmlJSEditorPlugin: Refactor

This follows the recently introduced pattern for plugin setup
- Pimpl QmlJSEditorPlugin
- remove unneeded uses of global object pool
- apply "static pattern"
- simplify some constructors of data members in some cases
- use in-class initialization in some case

Change-Id: I95b42d0885f4a8d6c9bfe1e4c004d3ace0a3eba5
Reviewed-by: Marco Benelli <marco.benelli@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2018-02-15 09:50:02 +01:00
parent 4786c5928a
commit 3da9c89981
11 changed files with 120 additions and 163 deletions

View File

@@ -829,7 +829,7 @@ void QmlJSEditorWidget::contextMenuEvent(QContextMenuEvent *e)
AssistInterface *interface = createAssistInterface(QuickFix, ExplicitlyInvoked);
if (interface) {
QScopedPointer<IAssistProcessor> processor(
QmlJSEditorPlugin::instance()->quickFixAssistProvider()->createProcessor());
QmlJSEditorPlugin::quickFixAssistProvider()->createProcessor());
QScopedPointer<IAssistProposal> proposal(processor->perform(interface));
if (!proposal.isNull()) {
GenericProposalModel *model = static_cast<GenericProposalModel *>(proposal->model());

View File

@@ -685,7 +685,7 @@ Internal::QmlOutlineModel *QmlJSEditorDocument::outlineModel() const
TextEditor::IAssistProvider *QmlJSEditorDocument::quickFixAssistProvider() const
{
return Internal::QmlJSEditorPlugin::instance()->quickFixAssistProvider();
return Internal::QmlJSEditorPlugin::quickFixAssistProvider();
}
void QmlJSEditorDocument::setDiagnosticRanges(const QVector<QTextLayout::FormatRange> &ranges)

View File

@@ -58,12 +58,7 @@
#include <utils/qtcassert.h>
#include <utils/json.h>
#include <QtPlugin>
#include <QSettings>
#include <QDir>
#include <QCoreApplication>
#include <QTextDocument>
#include <QTimer>
#include <QMenu>
#include <QAction>
@@ -71,57 +66,88 @@ using namespace QmlJSEditor::Constants;
using namespace ProjectExplorer;
using namespace Core;
enum {
QUICKFIX_INTERVAL = 20
namespace QmlJSEditor {
namespace Internal {
class QmlJSEditorPluginPrivate : public QObject
{
public:
QmlJSEditorPluginPrivate();
void currentEditorChanged(IEditor *editor);
void runSemanticScan();
void checkCurrentEditorSemanticInfoUpToDate();
void autoFormatOnSave(IDocument *document);
Command *addToolAction(QAction *a, Context &context, Id id,
ActionContainer *c1, const QString &keySequence);
void findUsages();
void renameUsages();
void reformatFile();
void showContextPane();
QmlJSQuickFixAssistProvider m_quickFixAssistProvider;
QmlTaskManager m_qmlTaskManager;
QAction *m_reformatFileAction = nullptr;
QPointer<QmlJSEditorDocument> m_currentDocument;
Utils::JsonSchemaManager m_jsonManager{{ICore::userResourcePath() + "/json/",
ICore::resourcePath() + "/json/"}};
QmlJSEditorFactory m_qmlJSEditorFactory;
QmlJSOutlineWidgetFactory m_qmlJSOutlineWidgetFactory;
QuickToolBar m_quickToolBar;
QmlJsEditingSettingsPage m_qmJSEditingSettingsPage;
};
namespace QmlJSEditor {
using namespace Internal;
static QmlJSEditorPlugin *m_instance = nullptr;
QmlJSEditorPlugin *QmlJSEditorPlugin::m_instance = 0;
QmlJSEditorPlugin::QmlJSEditorPlugin() :
m_modelManager(0),
m_quickFixAssistProvider(0),
m_reformatFileAction(0),
m_currentDocument(0),
m_jsonManager(new Utils::JsonSchemaManager(
QStringList() << ICore::userResourcePath() + QLatin1String("/json/")
<< ICore::resourcePath() + QLatin1String("/json/")))
QmlJSEditorPlugin::QmlJSEditorPlugin()
{
m_instance = this;
}
QmlJSEditorPlugin::~QmlJSEditorPlugin()
{
m_instance = 0;
delete d;
d = nullptr;
m_instance = nullptr;
}
bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *errorMessage)
bool QmlJSEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
{
Q_UNUSED(arguments);
Q_UNUSED(errorMessage);
d = new QmlJSEditorPluginPrivate;
return true;
}
QmlJSEditorPluginPrivate::QmlJSEditorPluginPrivate()
{
m_modelManager = QmlJS::ModelManagerInterface::instance();
TextEditor::SnippetProvider::registerGroup(Constants::QML_SNIPPETS_GROUP_ID,
tr("QML", "SnippetProvider"),
QmlJSEditorPlugin::tr("QML", "SnippetProvider"),
&QmlJSEditorFactory::decorateEditor);
QmlJS::ModelManagerInterface *modelManager = QmlJS::ModelManagerInterface::instance();
// QML task updating manager
m_qmlTaskManager = new QmlTaskManager;
addAutoReleasedObject(m_qmlTaskManager);
connect(m_modelManager, &QmlJS::ModelManagerInterface::documentChangedOnDisk,
m_qmlTaskManager, &QmlTaskManager::updateMessages);
connect(modelManager, &QmlJS::ModelManagerInterface::documentChangedOnDisk,
&m_qmlTaskManager, &QmlTaskManager::updateMessages);
// recompute messages when information about libraries changes
connect(m_modelManager, &QmlJS::ModelManagerInterface::libraryInfoUpdated,
m_qmlTaskManager, &QmlTaskManager::updateMessages);
connect(modelManager, &QmlJS::ModelManagerInterface::libraryInfoUpdated,
&m_qmlTaskManager, &QmlTaskManager::updateMessages);
// recompute messages when project data changes (files added or removed)
connect(m_modelManager, &QmlJS::ModelManagerInterface::projectInfoUpdated,
m_qmlTaskManager, &QmlTaskManager::updateMessages);
connect(m_modelManager, &QmlJS::ModelManagerInterface::aboutToRemoveFiles,
m_qmlTaskManager, &QmlTaskManager::documentsRemoved);
connect(modelManager, &QmlJS::ModelManagerInterface::projectInfoUpdated,
&m_qmlTaskManager, &QmlTaskManager::updateMessages);
connect(modelManager, &QmlJS::ModelManagerInterface::aboutToRemoveFiles,
&m_qmlTaskManager, &QmlTaskManager::documentsRemoved);
Context context(Constants::C_QMLJSEDITOR_ID);
addAutoReleasedObject(new QmlJSEditorFactory);
ActionContainer *contextMenu = ActionManager::createMenu(Constants::M_CONTEXT);
ActionContainer *qmlToolsMenu = ActionManager::actionContainer(Id(QmlJSTools::Constants::M_TOOLS_QMLJS));
@@ -132,32 +158,32 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
contextMenu->addAction(cmd);
qmlToolsMenu->addAction(cmd);
QAction *findUsagesAction = new QAction(tr("Find Usages"), this);
QAction *findUsagesAction = new QAction(QmlJSEditorPlugin::tr("Find Usages"), this);
cmd = ActionManager::registerAction(findUsagesAction, Constants::FIND_USAGES, context);
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
connect(findUsagesAction, &QAction::triggered, this, &QmlJSEditorPlugin::findUsages);
cmd->setDefaultKeySequence(QKeySequence(QmlJSEditorPlugin::tr("Ctrl+Shift+U")));
connect(findUsagesAction, &QAction::triggered, this, &QmlJSEditorPluginPrivate::findUsages);
contextMenu->addAction(cmd);
qmlToolsMenu->addAction(cmd);
QAction *renameUsagesAction = new QAction(tr("Rename Symbol Under Cursor"), this);
QAction *renameUsagesAction = new QAction(QmlJSEditorPlugin::tr("Rename Symbol Under Cursor"), this);
cmd = ActionManager::registerAction(renameUsagesAction, Constants::RENAME_USAGES, context);
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+R")));
connect(renameUsagesAction, &QAction::triggered, this, &QmlJSEditorPlugin::renameUsages);
cmd->setDefaultKeySequence(QKeySequence(QmlJSEditorPlugin::tr("Ctrl+Shift+R")));
connect(renameUsagesAction, &QAction::triggered, this, &QmlJSEditorPluginPrivate::renameUsages);
contextMenu->addAction(cmd);
qmlToolsMenu->addAction(cmd);
QAction *semanticScan = new QAction(tr("Run Checks"), this);
QAction *semanticScan = new QAction(QmlJSEditorPlugin::tr("Run Checks"), this);
cmd = ActionManager::registerAction(semanticScan, Id(Constants::RUN_SEMANTIC_SCAN));
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+C")));
connect(semanticScan, &QAction::triggered, this, &QmlJSEditorPlugin::runSemanticScan);
cmd->setDefaultKeySequence(QKeySequence(QmlJSEditorPlugin::tr("Ctrl+Shift+C")));
connect(semanticScan, &QAction::triggered, this, &QmlJSEditorPluginPrivate::runSemanticScan);
qmlToolsMenu->addAction(cmd);
m_reformatFileAction = new QAction(tr("Reformat File"), this);
m_reformatFileAction = new QAction(QmlJSEditorPlugin::tr("Reformat File"), this);
cmd = ActionManager::registerAction(m_reformatFileAction, Id(Constants::REFORMAT_FILE), context);
connect(m_reformatFileAction, &QAction::triggered, this, &QmlJSEditorPlugin::reformatFile);
connect(m_reformatFileAction, &QAction::triggered, this, &QmlJSEditorPluginPrivate::reformatFile);
qmlToolsMenu->addAction(cmd);
QAction *inspectElementAction = new QAction(tr("Inspect API for Element Under Cursor"), this);
QAction *inspectElementAction = new QAction(QmlJSEditorPlugin::tr("Inspect API for Element Under Cursor"), this);
cmd = ActionManager::registerAction(inspectElementAction,
Id(Constants::INSPECT_ELEMENT_UNDER_CURSOR), context);
connect(inspectElementAction, &QAction::triggered, [] {
@@ -166,11 +192,11 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
});
qmlToolsMenu->addAction(cmd);
QAction *showQuickToolbar = new QAction(tr("Show Qt Quick Toolbar"), this);
QAction *showQuickToolbar = new QAction(QmlJSEditorPlugin::tr("Show Qt Quick Toolbar"), this);
cmd = ActionManager::registerAction(showQuickToolbar, Constants::SHOW_QT_QUICK_HELPER, context);
cmd->setDefaultKeySequence(useMacShortcuts ? QKeySequence(Qt::META + Qt::ALT + Qt::Key_Space)
: QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Space));
connect(showQuickToolbar, &QAction::triggered, this, &QmlJSEditorPlugin::showContextPane);
connect(showQuickToolbar, &QAction::triggered, this, &QmlJSEditorPluginPrivate::showContextPane);
contextMenu->addAction(cmd);
qmlToolsMenu->addAction(cmd);
@@ -185,24 +211,13 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
contextMenu->addAction(cmd);
m_quickFixAssistProvider = new QmlJSQuickFixAssistProvider(this);
errorMessage->clear();
FileIconProvider::registerIconOverlayForSuffix(ProjectExplorer::Constants::FILEOVERLAY_QML, "qml");
addAutoReleasedObject(new QmlJSOutlineWidgetFactory);
addAutoReleasedObject(new QuickToolBar);
addAutoReleasedObject(new QmlJsEditingSettingsPage);
connect(EditorManager::instance(), &EditorManager::currentEditorChanged,
this, &QmlJSEditorPlugin::currentEditorChanged);
this, &QmlJSEditorPluginPrivate::currentEditorChanged);
connect(EditorManager::instance(), &Core::EditorManager::aboutToSave,
this, &QmlJSEditorPlugin::autoFormatOnSave);
return true;
connect(EditorManager::instance(), &EditorManager::aboutToSave,
this, &QmlJSEditorPluginPrivate::autoFormatOnSave);
}
void QmlJSEditorPlugin::extensionsInitialized()
@@ -218,24 +233,24 @@ ExtensionSystem::IPlugin::ShutdownFlag QmlJSEditorPlugin::aboutToShutdown()
return IPlugin::aboutToShutdown();
}
Utils::JsonSchemaManager *QmlJSEditorPlugin::jsonManager() const
Utils::JsonSchemaManager *QmlJSEditorPlugin::jsonManager()
{
return m_jsonManager.data();
return &m_instance->d->m_jsonManager;
}
void QmlJSEditorPlugin::findUsages()
void QmlJSEditorPluginPrivate::findUsages()
{
if (QmlJSEditorWidget *editor = qobject_cast<QmlJSEditorWidget*>(EditorManager::currentEditor()->widget()))
editor->findUsages();
}
void QmlJSEditorPlugin::renameUsages()
void QmlJSEditorPluginPrivate::renameUsages()
{
if (QmlJSEditorWidget *editor = qobject_cast<QmlJSEditorWidget*>(EditorManager::currentEditor()->widget()))
editor->renameUsages();
}
void QmlJSEditorPlugin::reformatFile()
void QmlJSEditorPluginPrivate::reformatFile()
{
if (m_currentDocument) {
QmlJS::Document::Ptr document = m_currentDocument->semanticInfo().document;
@@ -281,15 +296,15 @@ void QmlJSEditorPlugin::reformatFile()
}
}
void QmlJSEditorPlugin::showContextPane()
void QmlJSEditorPluginPrivate::showContextPane()
{
if (QmlJSEditorWidget *editor = qobject_cast<QmlJSEditorWidget*>(EditorManager::currentEditor()->widget()))
editor->showContextPane();
}
Command *QmlJSEditorPlugin::addToolAction(QAction *a,
Context &context, Id id,
ActionContainer *c1, const QString &keySequence)
Command *QmlJSEditorPluginPrivate::addToolAction(QAction *a,
Context &context, Id id,
ActionContainer *c1, const QString &keySequence)
{
Command *command = ActionManager::registerAction(a, id, context);
if (!keySequence.isEmpty())
@@ -298,14 +313,14 @@ Command *QmlJSEditorPlugin::addToolAction(QAction *a,
return command;
}
QmlJSQuickFixAssistProvider *QmlJSEditorPlugin::quickFixAssistProvider() const
QmlJSQuickFixAssistProvider *QmlJSEditorPlugin::quickFixAssistProvider()
{
return m_quickFixAssistProvider;
return &m_instance->d->m_quickFixAssistProvider;
}
void QmlJSEditorPlugin::currentEditorChanged(IEditor *editor)
void QmlJSEditorPluginPrivate::currentEditorChanged(IEditor *editor)
{
QmlJSEditorDocument *document = 0;
QmlJSEditorDocument *document = nullptr;
if (editor)
document = qobject_cast<QmlJSEditorDocument *>(editor->document());
@@ -314,26 +329,26 @@ void QmlJSEditorPlugin::currentEditorChanged(IEditor *editor)
m_currentDocument = document;
if (document) {
connect(document->document(), &QTextDocument::contentsChanged,
this, &QmlJSEditorPlugin::checkCurrentEditorSemanticInfoUpToDate);
this, &QmlJSEditorPluginPrivate::checkCurrentEditorSemanticInfoUpToDate);
connect(document, &QmlJSEditorDocument::semanticInfoUpdated,
this, &QmlJSEditorPlugin::checkCurrentEditorSemanticInfoUpToDate);
this, &QmlJSEditorPluginPrivate::checkCurrentEditorSemanticInfoUpToDate);
}
}
void QmlJSEditorPlugin::runSemanticScan()
void QmlJSEditorPluginPrivate::runSemanticScan()
{
m_qmlTaskManager->updateSemanticMessagesNow();
m_qmlTaskManager.updateSemanticMessagesNow();
TaskHub::setCategoryVisibility(Constants::TASK_CATEGORY_QML_ANALYSIS, true);
TaskHub::requestPopup();
}
void QmlJSEditorPlugin::checkCurrentEditorSemanticInfoUpToDate()
void QmlJSEditorPluginPrivate::checkCurrentEditorSemanticInfoUpToDate()
{
const bool semanticInfoUpToDate = m_currentDocument && !m_currentDocument->isSemanticInfoOutdated();
m_reformatFileAction->setEnabled(semanticInfoUpToDate);
}
void QmlJSEditorPlugin::autoFormatOnSave(Core::IDocument *document)
void QmlJSEditorPluginPrivate::autoFormatOnSave(IDocument *document)
{
if (!QmlJsEditingSettings::get().autoFormatOnSave())
return;
@@ -344,12 +359,13 @@ void QmlJSEditorPlugin::autoFormatOnSave(Core::IDocument *document)
// Check if file is contained in the current project (if wished)
if (QmlJsEditingSettings::get().autoFormatOnlyCurrentProject()) {
const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject();
if (!pro || !pro->files(ProjectExplorer::Project::SourceFiles).contains(document->filePath()))
const Project *pro = ProjectTree::currentProject();
if (!pro || !pro->files(Project::SourceFiles).contains(document->filePath()))
return;
}
reformatFile();
}
} // namespace Internal
} // namespace QmlJSEditor

View File

@@ -26,32 +26,13 @@
#pragma once
#include <extensionsystem/iplugin.h>
#include <coreplugin/icontext.h>
#include <coreplugin/id.h>
#include <QPointer>
QT_FORWARD_DECLARE_CLASS(QAction)
namespace Utils { class JsonSchemaManager; }
namespace Core {
class Command;
class ActionContainer;
class IDocument;
class IEditor;
}
namespace QmlJS { class ModelManagerInterface; }
namespace QmlJSEditor {
class QmlJSEditorDocument;
namespace Internal {
class QmlJSQuickFixAssistProvider;
class QmlTaskManager;
class QmlJSEditorPlugin : public ExtensionSystem::IPlugin
{
@@ -60,44 +41,17 @@ class QmlJSEditorPlugin : public ExtensionSystem::IPlugin
public:
QmlJSEditorPlugin();
virtual ~QmlJSEditorPlugin();
~QmlJSEditorPlugin() final;
// IPlugin
bool initialize(const QStringList &arguments, QString *errorMessage = 0);
void extensionsInitialized();
ShutdownFlag aboutToShutdown();
static QmlJSEditorPlugin *instance()
{ return m_instance; }
QmlJSQuickFixAssistProvider *quickFixAssistProvider() const;
Utils::JsonSchemaManager *jsonManager() const;
void findUsages();
void renameUsages();
void reformatFile();
void showContextPane();
static QmlJSQuickFixAssistProvider *quickFixAssistProvider();
static Utils::JsonSchemaManager *jsonManager();
private:
void currentEditorChanged(Core::IEditor *editor);
void runSemanticScan();
void checkCurrentEditorSemanticInfoUpToDate();
void autoFormatOnSave(Core::IDocument *document);
bool initialize(const QStringList &arguments, QString *errorMessage) final;
void extensionsInitialized() final;
ShutdownFlag aboutToShutdown() final;
Core::Command *addToolAction(QAction *a, Core::Context &context, Core::Id id,
Core::ActionContainer *c1, const QString &keySequence);
static QmlJSEditorPlugin *m_instance;
QmlJS::ModelManagerInterface *m_modelManager;
QmlJSQuickFixAssistProvider *m_quickFixAssistProvider;
QmlTaskManager *m_qmlTaskManager;
QAction *m_reformatFileAction;
QPointer<QmlJSEditorDocument> m_currentDocument;
QScopedPointer<Utils::JsonSchemaManager> m_jsonManager;
class QmlJSEditorPluginPrivate *d = nullptr;
};
} // namespace Internal

View File

@@ -80,12 +80,6 @@ class QmlJSQuickFixAssistProcessor : public IAssistProcessor
// ---------------------------
// QmlJSQuickFixAssistProvider
// ---------------------------
QmlJSQuickFixAssistProvider::QmlJSQuickFixAssistProvider(QObject *parent)
: IAssistProvider(parent)
{}
QmlJSQuickFixAssistProvider::~QmlJSQuickFixAssistProvider()
{}
IAssistProvider::RunType QmlJSQuickFixAssistProvider::runType() const
{

View File

@@ -53,8 +53,8 @@ private:
class QmlJSQuickFixAssistProvider : public TextEditor::IAssistProvider
{
public:
QmlJSQuickFixAssistProvider(QObject *parent = nullptr);
~QmlJSQuickFixAssistProvider();
QmlJSQuickFixAssistProvider() = default;
~QmlJSQuickFixAssistProvider() = default;
IAssistProvider::RunType runType() const override;
TextEditor::IAssistProcessor *createProcessor() const override;

View File

@@ -125,8 +125,7 @@ QmlJSTools::SemanticInfo SemanticInfoUpdater::makeNewSemanticInfo(const QmlJS::D
semanticInfo.setRootScopeChain(QSharedPointer<const ScopeChain>(scopeChain));
if (doc->language() == Dialect::Json) {
Utils::JsonSchema *schema =
QmlJSEditorPlugin::instance()->jsonManager()->schemaForFile(doc->fileName());
Utils::JsonSchema *schema = QmlJSEditorPlugin::jsonManager()->schemaForFile(doc->fileName());
if (schema) {
JsonCheck jsonChecker(doc);
semanticInfo.staticAnalysisMessages = jsonChecker(schema);

View File

@@ -47,9 +47,7 @@ using namespace Utils;
namespace QmlJSEditor {
namespace Internal {
QmlTaskManager::QmlTaskManager(QObject *parent) :
QObject(parent),
m_updatingSemantic(false)
QmlTaskManager::QmlTaskManager()
{
// displaying results incrementally leads to flickering
// connect(&m_messageCollector, &QFutureWatcherBase::resultsReadyAt,

View File

@@ -43,7 +43,7 @@ class QmlTaskManager : public QObject
{
Q_OBJECT
public:
QmlTaskManager(QObject *parent = 0);
QmlTaskManager();
void extensionsInitialized();
@@ -77,7 +77,7 @@ private:
QHash<QString, QList<ProjectExplorer::Task> > m_docsWithTasks;
QFutureWatcher<FileErrorMessages> m_messageCollector;
QTimer m_updateDelay;
bool m_updatingSemantic;
bool m_updatingSemantic = false;
};
} // Internal

View File

@@ -74,12 +74,8 @@ static inline const ObjectValue * getPropertyChangesTarget(Node *node, const Sco
return 0;
}
QuickToolBar::QuickToolBar(QObject *parent)
: ::IContextPane(parent)
, m_editorWidget(0)
, m_blockWriting(false)
QuickToolBar::QuickToolBar()
{
m_node = 0;
contextWidget();
m_propertyOrder

View File

@@ -38,7 +38,7 @@ class QuickToolBar : public QmlJS::IContextPane
Q_OBJECT
public:
QuickToolBar(QObject *parent = 0);
QuickToolBar();
~QuickToolBar();
void apply(TextEditor::TextEditorWidget *widget, QmlJS::Document::Ptr document, const QmlJS::ScopeChain *scopeChain, QmlJS::AST::Node *node, bool update, bool force = false);
bool isAvailable(TextEditor::TextEditorWidget *widget, QmlJS::Document::Ptr document, QmlJS::AST::Node *node);
@@ -59,9 +59,9 @@ private:
QmlEditorWidgets::ContextPaneWidget* contextWidget();
QPointer<QmlEditorWidgets::ContextPaneWidget> m_widget;
QmlJS::Document::Ptr m_doc;
QmlJS::AST::Node *m_node;
TextEditor::TextEditorWidget *m_editorWidget;
bool m_blockWriting;
QmlJS::AST::Node *m_node = nullptr;
TextEditor::TextEditorWidget *m_editorWidget = nullptr;
bool m_blockWriting = false;
QStringList m_propertyOrder;
QStringList m_prototypes;
QString m_oldType;