forked from qt-creator/qt-creator
QmlJS: Add 'reformat' action which regenerates the whole file.
Change-Id: I0aed6c6e197e122200d720eb9291a083095a6299 Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
This commit is contained in:
@@ -1588,6 +1588,8 @@ void QmlJSTextEditorWidget::updateSemanticInfo(const SemanticInfo &semanticInfo)
|
||||
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
||||
if (editorManager->currentEditor() == editor())
|
||||
m_semanticHighlighter->rerun(m_semanticInfo.scopeChain());
|
||||
|
||||
emit semanticInfoUpdated();
|
||||
}
|
||||
|
||||
void QmlJSTextEditorWidget::onRefactorMarkerClicked(const TextEditor::RefactorMarker &marker)
|
||||
|
||||
@@ -182,6 +182,7 @@ public slots:
|
||||
signals:
|
||||
void outlineModelIndexChanged(const QModelIndex &index);
|
||||
void selectedElementsChanged(QList<int> offsets, const QString &wordAtCursor);
|
||||
void semanticInfoUpdated();
|
||||
|
||||
private slots:
|
||||
void onDocumentUpdated(QmlJS::Document::Ptr doc);
|
||||
|
||||
@@ -55,6 +55,7 @@ const char FOLLOW_SYMBOL_UNDER_CURSOR[] = "QmlJSEditor.FollowSymbolUnderCursor";
|
||||
const char FIND_USAGES[] = "QmlJSEditor.FindUsages";
|
||||
const char RENAME_USAGES[] = "QmlJSEditor.RenameUsages";
|
||||
const char RUN_SEMANTIC_SCAN[] = "QmlJSEditor.RunSemanticScan";
|
||||
const char REFORMAT_FILE[] = "QmlJSEditor.ReformatFile";
|
||||
const char SHOW_QT_QUICK_HELPER[] = "QmlJSEditor.ShowQtQuickHelper";
|
||||
|
||||
const char TASK_CATEGORY_QML[] = "Task.Category.Qml";
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
|
||||
#include <qmljs/qmljsicons.h>
|
||||
#include <qmljs/qmljsmodelmanagerinterface.h>
|
||||
#include <qmljs/qmljsreformatter.h>
|
||||
#include <qmljstools/qmljstoolsconstants.h>
|
||||
|
||||
#include <qmldesigner/qmldesignerconstants.h>
|
||||
@@ -93,10 +94,11 @@ QmlJSEditorPlugin *QmlJSEditorPlugin::m_instance = 0;
|
||||
|
||||
QmlJSEditorPlugin::QmlJSEditorPlugin() :
|
||||
m_modelManager(0),
|
||||
m_wizard(0),
|
||||
m_editor(0),
|
||||
m_actionHandler(0),
|
||||
m_quickFixAssistProvider(0)
|
||||
m_quickFixAssistProvider(0),
|
||||
m_reformatFileAction(0),
|
||||
m_currentEditor(0)
|
||||
{
|
||||
m_instance = this;
|
||||
}
|
||||
@@ -205,6 +207,11 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
|
||||
connect(semanticScan, SIGNAL(triggered()), this, SLOT(runSemanticScan()));
|
||||
qmlToolsMenu->addAction(cmd);
|
||||
|
||||
m_reformatFileAction = new QAction(tr("Reformat File"), this);
|
||||
cmd = am->registerAction(m_reformatFileAction, Core::Id(Constants::REFORMAT_FILE), globalContext);
|
||||
connect(m_reformatFileAction, SIGNAL(triggered()), this, SLOT(reformatFile()));
|
||||
qmlToolsMenu->addAction(cmd);
|
||||
|
||||
QAction *showQuickToolbar = new QAction(tr("Show Qt Quick Toolbar"), this);
|
||||
cmd = am->registerAction(showQuickToolbar, Constants::SHOW_QT_QUICK_HELPER, context);
|
||||
#ifdef Q_WS_MACX
|
||||
@@ -300,6 +307,20 @@ void QmlJSEditorPlugin::renameUsages()
|
||||
editor->renameUsages();
|
||||
}
|
||||
|
||||
void QmlJSEditorPlugin::reformatFile()
|
||||
{
|
||||
Core::EditorManager *em = Core::EditorManager::instance();
|
||||
if (QmlJSTextEditorWidget *editor = qobject_cast<QmlJSTextEditorWidget*>(em->currentEditor()->widget())) {
|
||||
QTC_ASSERT(!editor->isOutdated(), return);
|
||||
|
||||
const QString &newText = QmlJS::reformat(editor->semanticInfo().document);
|
||||
QTextCursor tc(editor->textCursor());
|
||||
tc.movePosition(QTextCursor::Start);
|
||||
tc.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
|
||||
tc.insertText(newText);
|
||||
}
|
||||
}
|
||||
|
||||
void QmlJSEditorPlugin::showContextPane()
|
||||
{
|
||||
Core::EditorManager *em = Core::EditorManager::instance();
|
||||
@@ -327,11 +348,23 @@ QmlJSQuickFixAssistProvider *QmlJSEditorPlugin::quickFixAssistProvider() const
|
||||
|
||||
void QmlJSEditorPlugin::currentEditorChanged(Core::IEditor *editor)
|
||||
{
|
||||
if (! editor)
|
||||
return;
|
||||
QmlJSTextEditorWidget *newTextEditor = 0;
|
||||
if (editor)
|
||||
newTextEditor = qobject_cast<QmlJSTextEditorWidget *>(editor->widget());
|
||||
|
||||
else if (QmlJSTextEditorWidget *textEditor = qobject_cast<QmlJSTextEditorWidget *>(editor->widget())) {
|
||||
textEditor->forceReparse();
|
||||
if (m_currentEditor) {
|
||||
disconnect(m_currentEditor.data(), SIGNAL(contentsChanged()),
|
||||
this, SLOT(checkCurrentEditorSemanticInfoUpToDate()));
|
||||
disconnect(m_currentEditor.data(), SIGNAL(semanticInfoUpdated()),
|
||||
this, SLOT(checkCurrentEditorSemanticInfoUpToDate()));
|
||||
}
|
||||
m_currentEditor = newTextEditor;
|
||||
if (newTextEditor) {
|
||||
connect(newTextEditor, SIGNAL(contentsChanged()),
|
||||
this, SLOT(checkCurrentEditorSemanticInfoUpToDate()));
|
||||
connect(newTextEditor, SIGNAL(semanticInfoUpdated()),
|
||||
this, SLOT(checkCurrentEditorSemanticInfoUpToDate()));
|
||||
newTextEditor->forceReparse();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,4 +376,10 @@ void QmlJSEditorPlugin::runSemanticScan()
|
||||
hub->popup(false);
|
||||
}
|
||||
|
||||
void QmlJSEditorPlugin::checkCurrentEditorSemanticInfoUpToDate()
|
||||
{
|
||||
const bool semanticInfoUpToDate = m_currentEditor && !m_currentEditor->isOutdated();
|
||||
m_reformatFileAction->setEnabled(semanticInfoUpToDate);
|
||||
}
|
||||
|
||||
Q_EXPORT_PLUGIN(QmlJSEditorPlugin)
|
||||
|
||||
@@ -95,11 +95,13 @@ public Q_SLOTS:
|
||||
void followSymbolUnderCursor();
|
||||
void findUsages();
|
||||
void renameUsages();
|
||||
void reformatFile();
|
||||
void showContextPane();
|
||||
|
||||
private Q_SLOTS:
|
||||
void currentEditorChanged(Core::IEditor *editor);
|
||||
void runSemanticScan();
|
||||
void checkCurrentEditorSemanticInfoUpToDate();
|
||||
|
||||
private:
|
||||
Core::Command *addToolAction(QAction *a, Core::ActionManager *am, Core::Context &context, const Core::Id &id,
|
||||
@@ -107,18 +109,15 @@ private:
|
||||
|
||||
static QmlJSEditorPlugin *m_instance;
|
||||
|
||||
QAction *m_actionPreview;
|
||||
QmlJSPreviewRunner *m_previewRunner;
|
||||
|
||||
QmlJS::ModelManagerInterface *m_modelManager;
|
||||
QmlFileWizard *m_wizard;
|
||||
QmlJSEditorFactory *m_editor;
|
||||
TextEditor::TextEditorActionHandler *m_actionHandler;
|
||||
|
||||
QmlJSQuickFixAssistProvider *m_quickFixAssistProvider;
|
||||
|
||||
QPointer<TextEditor::ITextEditor> m_currentTextEditable;
|
||||
QmlTaskManager *m_qmlTaskManager;
|
||||
|
||||
QAction *m_reformatFileAction;
|
||||
|
||||
QPointer<QmlJSTextEditorWidget> m_currentEditor;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
Reference in New Issue
Block a user