Files
qt-creator/src/plugins/qmljseditor/qmljseditorplugin.cpp

299 lines
11 KiB
C++
Raw Normal View History

/****************************************************************************
2009-04-22 15:21:04 +02:00
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
2009-04-22 15:21:04 +02:00
**
** This file is part of Qt Creator.
2009-04-22 15:21:04 +02:00
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
2009-04-22 15:21:04 +02:00
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
2010-12-17 16:01:08 +01:00
**
****************************************************************************/
2009-04-22 15:21:04 +02:00
#include "qmljseditorplugin.h"
#include "qmljshighlighter.h"
#include "qmljseditor.h"
#include "qmljseditorconstants.h"
#include "qmljseditordocument.h"
2010-07-08 11:32:45 +02:00
#include "qmljsoutline.h"
#include "qmljspreviewrunner.h"
#include "qmljssnippetprovider.h"
#include "qmltaskmanager.h"
#include "quicktoolbar.h"
#include "quicktoolbarsettingspage.h"
#include "qmljsquickfixassist.h"
#include <qmljs/qmljsicons.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljs/qmljsreformatter.h>
#include <qmljstools/qmljstoolsconstants.h>
2009-04-22 15:21:04 +02:00
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/id.h>
#include <coreplugin/fileiconprovider.h>
2009-04-22 15:21:04 +02:00
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/editormanager/editormanager.h>
#include <projectexplorer/taskhub.h>
#include <projectexplorer/projectexplorerconstants.h>
2009-04-22 15:21:04 +02:00
#include <texteditor/texteditorconstants.h>
#include <utils/qtcassert.h>
#include <utils/json.h>
2009-04-22 15:21:04 +02:00
#include <QtPlugin>
#include <QSettings>
#include <QDir>
#include <QCoreApplication>
#include <QTextDocument>
#include <QTimer>
#include <QMenu>
#include <QAction>
2009-04-22 15:21:04 +02:00
using namespace QmlJSEditor::Constants;
using namespace ProjectExplorer;
using namespace Core;
2009-04-22 15:21:04 +02:00
enum {
QUICKFIX_INTERVAL = 20
};
namespace QmlJSEditor {
using namespace Internal;
void registerQuickFixes(ExtensionSystem::IPlugin *plugIn);
QmlJSEditorPlugin *QmlJSEditorPlugin::m_instance = 0;
2009-04-22 15:21:04 +02:00
QmlJSEditorPlugin::QmlJSEditorPlugin() :
2009-09-04 16:51:11 +02:00
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/")))
2009-04-22 15:21:04 +02:00
{
m_instance = this;
}
QmlJSEditorPlugin::~QmlJSEditorPlugin()
2009-04-22 15:21:04 +02:00
{
m_instance = 0;
}
bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *errorMessage)
2009-04-22 15:21:04 +02:00
{
m_modelManager = QmlJS::ModelManagerInterface::instance();
addAutoReleasedObject(new QmlJSSnippetProvider);
// QML task updating manager
m_qmlTaskManager = new QmlTaskManager;
addAutoReleasedObject(m_qmlTaskManager);
connect(m_modelManager, SIGNAL(documentChangedOnDisk(QmlJS::Document::Ptr)),
m_qmlTaskManager, SLOT(updateMessages()));
// recompute messages when information about libraries changes
connect(m_modelManager, SIGNAL(libraryInfoUpdated(QString,QmlJS::LibraryInfo)),
m_qmlTaskManager, SLOT(updateMessages()));
// recompute messages when project data changes (files added or removed)
connect(m_modelManager, SIGNAL(projectInfoUpdated(ProjectInfo)),
m_qmlTaskManager, SLOT(updateMessages()));
connect(m_modelManager, SIGNAL(aboutToRemoveFiles(QStringList)),
m_qmlTaskManager, SLOT(documentsRemoved(QStringList)));
Context context(Constants::C_QMLJSEDITOR_ID);
2009-04-22 15:21:04 +02:00
addAutoReleasedObject(new QmlJSEditorFactory);
2009-04-22 15:21:04 +02:00
ActionContainer *contextMenu = ActionManager::createMenu(Constants::M_CONTEXT);
ActionContainer *qmlToolsMenu = ActionManager::actionContainer(Id(QmlJSTools::Constants::M_TOOLS_QMLJS));
qmlToolsMenu->addSeparator();
Command *cmd;
cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
contextMenu->addAction(cmd);
qmlToolsMenu->addAction(cmd);
QAction *findUsagesAction = new QAction(tr("Find Usages"), this);
cmd = ActionManager::registerAction(findUsagesAction, Constants::FIND_USAGES, context);
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
connect(findUsagesAction, SIGNAL(triggered()), this, SLOT(findUsages()));
contextMenu->addAction(cmd);
qmlToolsMenu->addAction(cmd);
QAction *renameUsagesAction = new QAction(tr("Rename Symbol Under Cursor"), this);
cmd = ActionManager::registerAction(renameUsagesAction, Constants::RENAME_USAGES, context);
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+R")));
connect(renameUsagesAction, SIGNAL(triggered()), this, SLOT(renameUsages()));
contextMenu->addAction(cmd);
qmlToolsMenu->addAction(cmd);
QAction *semanticScan = new QAction(tr("Run Checks"), this);
cmd = ActionManager::registerAction(semanticScan, Id(Constants::RUN_SEMANTIC_SCAN));
cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+C")));
connect(semanticScan, SIGNAL(triggered()), this, SLOT(runSemanticScan()));
qmlToolsMenu->addAction(cmd);
m_reformatFileAction = new QAction(tr("Reformat File"), this);
cmd = ActionManager::registerAction(m_reformatFileAction, Id(Constants::REFORMAT_FILE), context);
connect(m_reformatFileAction, SIGNAL(triggered()), this, SLOT(reformatFile()));
qmlToolsMenu->addAction(cmd);
QAction *inspectElementAction = new QAction(tr("Inspect API for Element Under Cursor"), this);
cmd = ActionManager::registerAction(inspectElementAction,
Id(Constants::INSPECT_ELEMENT_UNDER_CURSOR), context);
connect(inspectElementAction, &QAction::triggered, [] {
if (auto widget = qobject_cast<QmlJSEditorWidget *>(EditorManager::currentEditor()->widget()))
widget->inspectElementUnderCursor();
});
qmlToolsMenu->addAction(cmd);
QAction *showQuickToolbar = new QAction(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, SIGNAL(triggered()), this, SLOT(showContextPane()));
contextMenu->addAction(cmd);
qmlToolsMenu->addAction(cmd);
// Insert marker for "Refactoring" menu:
Command *sep = contextMenu->addSeparator();
sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT));
contextMenu->addSeparator();
cmd = ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION);
contextMenu->addAction(cmd);
cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
contextMenu->addAction(cmd);
m_quickFixAssistProvider = new QmlJSQuickFixAssistProvider(this);
2009-05-06 15:45:19 +02:00
errorMessage->clear();
2009-04-22 15:21:04 +02:00
FileIconProvider::registerIconOverlayForSuffix(ProjectExplorer::Constants::FILEOVERLAY_QML, "qml");
registerQuickFixes(this);
2010-07-08 11:32:45 +02:00
addAutoReleasedObject(new QmlJSOutlineWidgetFactory);
addAutoReleasedObject(new QuickToolBar);
addAutoReleasedObject(new QuickToolBarSettingsPage);
connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)), SLOT(currentEditorChanged(Core::IEditor*)));
2009-04-22 15:21:04 +02:00
return true;
}
void QmlJSEditorPlugin::extensionsInitialized()
2009-04-22 15:21:04 +02:00
{
TaskHub::addCategory(Constants::TASK_CATEGORY_QML, tr("QML"));
TaskHub::addCategory(Constants::TASK_CATEGORY_QML_ANALYSIS, tr("QML Analysis"), false);
2009-04-22 15:21:04 +02:00
}
2010-07-13 17:05:47 +02:00
ExtensionSystem::IPlugin::ShutdownFlag QmlJSEditorPlugin::aboutToShutdown()
{
delete QmlJS::Icons::instance(); // delete object held by singleton
return IPlugin::aboutToShutdown();
}
Utils::JsonSchemaManager *QmlJSEditorPlugin::jsonManager() const
{
return m_jsonManager.data();
}
void QmlJSEditorPlugin::findUsages()
{
if (QmlJSEditorWidget *editor = qobject_cast<QmlJSEditorWidget*>(EditorManager::currentEditor()->widget()))
editor->findUsages();
}
void QmlJSEditorPlugin::renameUsages()
{
if (QmlJSEditorWidget *editor = qobject_cast<QmlJSEditorWidget*>(EditorManager::currentEditor()->widget()))
editor->renameUsages();
}
void QmlJSEditorPlugin::reformatFile()
{
if (m_currentDocument) {
QTC_ASSERT(!m_currentDocument->isSemanticInfoOutdated(), return);
const QString &newText = QmlJS::reformat(m_currentDocument->semanticInfo().document);
QTextCursor tc(m_currentDocument->document());
tc.movePosition(QTextCursor::Start);
tc.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
tc.insertText(newText);
}
}
void QmlJSEditorPlugin::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 *command = ActionManager::registerAction(a, id, context);
if (!keySequence.isEmpty())
command->setDefaultKeySequence(QKeySequence(keySequence));
c1->addAction(command);
return command;
}
QmlJSQuickFixAssistProvider *QmlJSEditorPlugin::quickFixAssistProvider() const
{
return m_quickFixAssistProvider;
}
void QmlJSEditorPlugin::currentEditorChanged(IEditor *editor)
{
QmlJSEditorDocument *document = 0;
if (editor)
document = qobject_cast<QmlJSEditorDocument *>(editor->document());
if (m_currentDocument)
m_currentDocument->disconnect(this);
m_currentDocument = document;
if (document) {
connect(document->document(), SIGNAL(contentsChanged()),
this, SLOT(checkCurrentEditorSemanticInfoUpToDate()));
connect(document, SIGNAL(semanticInfoUpdated(QmlJSTools::SemanticInfo)),
this, SLOT(checkCurrentEditorSemanticInfoUpToDate()));
}
}
void QmlJSEditorPlugin::runSemanticScan()
{
m_qmlTaskManager->updateSemanticMessagesNow();
TaskHub::setCategoryVisibility(Constants::TASK_CATEGORY_QML_ANALYSIS, true);
TaskHub::requestPopup();
}
void QmlJSEditorPlugin::checkCurrentEditorSemanticInfoUpToDate()
{
const bool semanticInfoUpToDate = m_currentDocument && !m_currentDocument->isSemanticInfoOutdated();
m_reformatFileAction->setEnabled(semanticInfoUpToDate);
}
} // namespace QmlJSEditor