forked from qt-creator/qt-creator
QmlDesigner: Fix TextEditorView::reformatFile
* Updating the document only if it actually changed * Using the text cursor of the widget * We can rely on the widget to exist * Simplify how the cursor is restored Task-number: QDS-6410 Change-Id: Idaa384533f12b9b7ad7078bcdc37c1afaf651662 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
@@ -52,6 +52,7 @@
|
|||||||
#include <qmljs/qmljsmodelmanagerinterface.h>
|
#include <qmljs/qmljsmodelmanagerinterface.h>
|
||||||
#include <qmljs/qmljsreformatter.h>
|
#include <qmljs/qmljsreformatter.h>
|
||||||
#include <utils/changeset.h>
|
#include <utils/changeset.h>
|
||||||
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QPair>
|
#include <QPair>
|
||||||
@@ -258,12 +259,7 @@ void TextEditorView::gotoCursorPosition(int line, int column)
|
|||||||
|
|
||||||
void TextEditorView::reformatFile()
|
void TextEditorView::reformatFile()
|
||||||
{
|
{
|
||||||
int oldLine = -1;
|
QTC_ASSERT(!m_widget.isNull(), return);
|
||||||
|
|
||||||
if (m_widget)
|
|
||||||
oldLine = m_widget->currentLine();
|
|
||||||
|
|
||||||
QByteArray editorState = m_widget->textEditor()->saveState();
|
|
||||||
|
|
||||||
auto document =
|
auto document =
|
||||||
qobject_cast<QmlJSEditor::QmlJSEditorDocument *>(Core::EditorManager::currentDocument());
|
qobject_cast<QmlJSEditor::QmlJSEditorDocument *>(Core::EditorManager::currentDocument());
|
||||||
@@ -292,16 +288,21 @@ void TextEditorView::reformatFile()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
const QString &newText = QmlJS::reformat(currentDocument);
|
const QString &newText = QmlJS::reformat(currentDocument);
|
||||||
QTextCursor tc(document->document());
|
if (currentDocument->source() == newText)
|
||||||
|
return;
|
||||||
|
|
||||||
|
QTextCursor tc = m_widget->textEditor()->textCursor();
|
||||||
|
int pos = m_widget->textEditor()->textCursor().position();
|
||||||
|
|
||||||
Utils::ChangeSet changeSet;
|
Utils::ChangeSet changeSet;
|
||||||
changeSet.replace(0, document->plainText().length(), newText);
|
changeSet.replace(0, document->plainText().length(), newText);
|
||||||
|
|
||||||
|
tc.beginEditBlock();
|
||||||
changeSet.apply(&tc);
|
changeSet.apply(&tc);
|
||||||
|
tc.setPosition(pos);
|
||||||
|
tc.endEditBlock();
|
||||||
|
|
||||||
m_widget->textEditor()->restoreState(editorState);
|
m_widget->textEditor()->setTextCursor(tc);
|
||||||
|
|
||||||
if (m_widget)
|
|
||||||
m_widget->gotoCursorPosition(oldLine, 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user