Commit fafb99d9 authored by Thomas Hartmann's avatar Thomas Hartmann Committed by Thomas Hartmann
Browse files

QmlDesigner: Reformat .ui.qml files when saving



The feature can be turned off using a setting,
if there are serious issues with the reformating.

Change-Id: If90475af2db8aaade8bc52af0fb7cd14bfa1c121
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent bcdc41b4
...@@ -68,6 +68,7 @@ void DesignerSettings::fromSettings(QSettings *settings) ...@@ -68,6 +68,7 @@ void DesignerSettings::fromSettings(QSettings *settings)
restoreValue(settings, DesignerSettingsKey::PUPPET_KILL_TIMEOUT, 3000); // this has no ui at the moment restoreValue(settings, DesignerSettingsKey::PUPPET_KILL_TIMEOUT, 3000); // this has no ui at the moment
restoreValue(settings, DesignerSettingsKey::DEBUG_PUPPET, QString()); restoreValue(settings, DesignerSettingsKey::DEBUG_PUPPET, QString());
restoreValue(settings, DesignerSettingsKey::FORWARD_PUPPET_OUTPUT, QString()); restoreValue(settings, DesignerSettingsKey::FORWARD_PUPPET_OUTPUT, QString());
restoreValue(settings, DesignerSettingsKey::REFORMAT_UI_QML_FILES, true);
settings->endGroup(); settings->endGroup();
settings->endGroup(); settings->endGroup();
......
...@@ -57,7 +57,9 @@ const char ENABLE_MODEL_EXCEPTION_OUTPUT[] = "WarnException"; ...@@ -57,7 +57,9 @@ const char ENABLE_MODEL_EXCEPTION_OUTPUT[] = "WarnException";
const char PUPPET_KILL_TIMEOUT[] = "PuppetKillTimeout"; const char PUPPET_KILL_TIMEOUT[] = "PuppetKillTimeout";
const char DEBUG_PUPPET[] = "DebugPuppet"; const char DEBUG_PUPPET[] = "DebugPuppet";
const char FORWARD_PUPPET_OUTPUT[] = "ForwardPuppetOutput"; const char FORWARD_PUPPET_OUTPUT[] = "ForwardPuppetOutput";
const char REFORMAT_UI_QML_FILES[] = "ReformatUiQmlFiles"; /* This Setting is not exposed in ui.
The setting can be used to turn off the feature,
if there are serious issues */
} }
class DesignerSettings : public QHash<QByteArray, QVariant> class DesignerSettings : public QHash<QByteArray, QVariant>
......
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#include "shortcutmanager.h" #include "shortcutmanager.h"
#include "designersettings.h"
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h> #include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h> #include <coreplugin/actionmanager/command.h>
...@@ -33,7 +35,13 @@ ...@@ -33,7 +35,13 @@
#include <coreplugin/editormanager/documentmodel.h> #include <coreplugin/editormanager/documentmodel.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
#include <qmljseditor/qmljseditorconstants.h>
#include <qmljseditor/qmljseditordocument.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
#include <qmljs/qmljsreformatter.h>
#include "qmldesignerconstants.h" #include "qmldesignerconstants.h"
#include "qmldesignerplugin.h" #include "qmldesignerplugin.h"
...@@ -106,13 +114,29 @@ void ShortCutManager::registerActions(const Core::Context &qmlDesignerMainContex ...@@ -106,13 +114,29 @@ void ShortCutManager::registerActions(const Core::Context &qmlDesignerMainContex
SLOT(toggleRightSidebar())); SLOT(toggleRightSidebar()));
// Revert to saved // Revert to saved
QObject *em = Core::EditorManager::instance(); Core::EditorManager *em = Core::EditorManager::instance();
Core::ActionManager::registerAction(&m_revertToSavedAction,Core::Constants::REVERTTOSAVED, qmlDesignerMainContext); Core::ActionManager::registerAction(&m_revertToSavedAction,Core::Constants::REVERTTOSAVED, qmlDesignerMainContext);
connect(&m_revertToSavedAction, SIGNAL(triggered()), em, SLOT(revertToSaved())); connect(&m_revertToSavedAction, SIGNAL(triggered()), em, SLOT(revertToSaved()));
//Save //Save
Core::ActionManager::registerAction(&m_saveAction, Core::Constants::SAVE, qmlDesignerMainContext); Core::ActionManager::registerAction(&m_saveAction, Core::Constants::SAVE, qmlDesignerMainContext);
connect(&m_saveAction, SIGNAL(triggered()), em, SLOT(saveDocument())); connect(&m_saveAction, &QAction::triggered, em, [em] {
DesignerSettings settings = QmlDesignerPlugin::instance()->settings();
/* Reformat document if we have a .ui.qml file */
if (settings.value(DesignerSettingsKey::REFORMAT_UI_QML_FILES).toBool()
&& em->currentDocument()->filePath().toString().endsWith(".ui.qml"))
if (QmlJSEditor::QmlJSEditorDocument *document
= qobject_cast<QmlJSEditor::QmlJSEditorDocument *>(em->currentDocument())) {
const QString &newText = QmlJS::reformat(document->semanticInfo().document);
QTextCursor tc(document->document());
tc.movePosition(QTextCursor::Start);
tc.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
tc.insertText(newText);
}
em->saveDocument();
});
//Save As //Save As
Core::ActionManager::registerAction(&m_saveAsAction, Core::Constants::SAVEAS, qmlDesignerMainContext); Core::ActionManager::registerAction(&m_saveAsAction, Core::Constants::SAVEAS, qmlDesignerMainContext);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment