forked from qt-creator/qt-creator
QmlDesigner: Do not move cursor when reformating
When saving a .ui.qml file the complete file is reformated. Without this patch the cursor is moved to the bottom. The cursor is still moved, but they line is kept. Since we reformat the complete file this should be fine. Change-Id: Ia1ef003e2e6ca7497dcad7e0e8044ffb99c80ea3 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -29,15 +29,21 @@
|
|||||||
|
|
||||||
#include <designmodecontext.h>
|
#include <designmodecontext.h>
|
||||||
#include <designdocument.h>
|
#include <designdocument.h>
|
||||||
|
#include <designersettings.h>
|
||||||
#include <modelnode.h>
|
#include <modelnode.h>
|
||||||
#include <model.h>
|
#include <model.h>
|
||||||
#include <zoomaction.h>
|
#include <zoomaction.h>
|
||||||
#include <nodeabstractproperty.h>
|
#include <nodeabstractproperty.h>
|
||||||
#include <nodelistproperty.h>
|
#include <nodelistproperty.h>
|
||||||
|
|
||||||
#include <qmldesignerplugin.h>
|
#include <qmldesignerplugin.h>
|
||||||
|
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
#include <texteditor/texteditor.h>
|
#include <texteditor/texteditor.h>
|
||||||
|
#include <qmljseditor/qmljseditordocument.h>
|
||||||
|
#include <qmljs/qmljsreformatter.h>
|
||||||
|
|
||||||
|
#include <utils/changeset.h>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QPair>
|
#include <QPair>
|
||||||
@@ -196,6 +202,39 @@ void TextEditorView::gotoCursorPosition(int line, int column)
|
|||||||
m_widget->gotoCursorPosition(line, column);
|
m_widget->gotoCursorPosition(line, column);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextEditorView::reformatFile()
|
||||||
|
{
|
||||||
|
int oldLine = -1;
|
||||||
|
|
||||||
|
if (m_widget)
|
||||||
|
oldLine = m_widget->currentLine();
|
||||||
|
|
||||||
|
QByteArray editorState = m_widget->textEditor()->saveState();
|
||||||
|
|
||||||
|
DesignerSettings settings = QmlDesignerPlugin::instance()->settings();
|
||||||
|
|
||||||
|
auto document =
|
||||||
|
qobject_cast<QmlJSEditor::QmlJSEditorDocument *>(Core::EditorManager::instance()->currentDocument());
|
||||||
|
|
||||||
|
/* Reformat document if we have a .ui.qml file */
|
||||||
|
if (document
|
||||||
|
&& document->filePath().toString().endsWith(".ui.qml")
|
||||||
|
&& settings.value(DesignerSettingsKey::REFORMAT_UI_QML_FILES).toBool()) {
|
||||||
|
|
||||||
|
const QString &newText = QmlJS::reformat(document->semanticInfo().document);
|
||||||
|
QTextCursor tc(document->document());
|
||||||
|
|
||||||
|
Utils::ChangeSet changeSet;
|
||||||
|
changeSet.replace(0, document->plainText().length(), newText);
|
||||||
|
changeSet.apply(&tc);
|
||||||
|
|
||||||
|
m_widget->textEditor()->restoreState(editorState);
|
||||||
|
|
||||||
|
if (m_widget)
|
||||||
|
m_widget->gotoCursorPosition(oldLine, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void TextEditorView::instancePropertyChanged(const QList<QPair<ModelNode, PropertyName> > &/*propertyList*/)
|
void TextEditorView::instancePropertyChanged(const QList<QPair<ModelNode, PropertyName> > &/*propertyList*/)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,6 +90,8 @@ public:
|
|||||||
|
|
||||||
void gotoCursorPosition(int line, int column);
|
void gotoCursorPosition(int line, int column);
|
||||||
|
|
||||||
|
void reformatFile();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<TextEditorWidget> m_widget;
|
std::unique_ptr<TextEditorWidget> m_widget;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -129,4 +129,11 @@ void TextEditorWidget::clearStatusBar()
|
|||||||
m_statusBar->clearText();
|
m_statusBar->clearText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int TextEditorWidget::currentLine() const
|
||||||
|
{
|
||||||
|
if (m_textEditor)
|
||||||
|
return m_textEditor->currentLine();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ public:
|
|||||||
void setStatusText(const QString &text);
|
void setStatusText(const QString &text);
|
||||||
void clearStatusBar();
|
void clearStatusBar();
|
||||||
|
|
||||||
|
int currentLine() const;
|
||||||
private:
|
private:
|
||||||
void updateSelectionByCursorPosition();
|
void updateSelectionByCursorPosition();
|
||||||
|
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ public:
|
|||||||
NodeInstanceView *nodeInstanceView() const;
|
NodeInstanceView *nodeInstanceView() const;
|
||||||
|
|
||||||
void exportAsImage();
|
void exportAsImage();
|
||||||
|
void reformatFileUsingTextEditorView();
|
||||||
|
|
||||||
QWidgetAction *componentViewAction() const;
|
QWidgetAction *componentViewAction() const;
|
||||||
|
|
||||||
|
|||||||
@@ -344,6 +344,11 @@ void ViewManager::exportAsImage()
|
|||||||
d->formEditorView.exportAsImage();
|
d->formEditorView.exportAsImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ViewManager::reformatFileUsingTextEditorView()
|
||||||
|
{
|
||||||
|
d->textEditorView.reformatFile();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
|
||||||
#endif //QMLDESIGNER_TEST
|
#endif //QMLDESIGNER_TEST
|
||||||
|
|||||||
@@ -143,19 +143,8 @@ void ShortCutManager::registerActions(const Core::Context &qmlDesignerMainContex
|
|||||||
//Save
|
//Save
|
||||||
Core::ActionManager::registerAction(&m_saveAction, Core::Constants::SAVE, qmlDesignerMainContext);
|
Core::ActionManager::registerAction(&m_saveAction, Core::Constants::SAVE, qmlDesignerMainContext);
|
||||||
connect(&m_saveAction, &QAction::triggered, em, [em] {
|
connect(&m_saveAction, &QAction::triggered, em, [em] {
|
||||||
DesignerSettings settings = QmlDesignerPlugin::instance()->settings();
|
QmlDesignerPlugin::instance()->viewManager().reformatFileUsingTextEditorView();
|
||||||
/* Reformat document if we have a .ui.qml file */
|
em->saveDocument();
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user