From 267167a0970022f9eaede31559e4c478990ba48c Mon Sep 17 00:00:00 2001 From: Marco Benelli Date: Fri, 8 Sep 2017 11:19:21 +0200 Subject: [PATCH] qmljs: do not reset undo/redo history in reformatting Task-number: QTCREATORBUG-18645 Change-Id: I5b64fa5e59af3d871d3124d668bfd1f86883e752 Reviewed-by: Eike Ziller --- src/plugins/qmljseditor/qmljseditorplugin.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/plugins/qmljseditor/qmljseditorplugin.cpp b/src/plugins/qmljseditor/qmljseditorplugin.cpp index 34f2fa818a1..efc7d4fec7c 100644 --- a/src/plugins/qmljseditor/qmljseditorplugin.cpp +++ b/src/plugins/qmljseditor/qmljseditorplugin.cpp @@ -259,19 +259,29 @@ void QmlJSEditorPlugin::reformatFile() if (!document->isParsedCorrectly()) return; + TextEditor::TabSettings tabSettings = m_currentDocument->tabSettings(); const QString &newText = QmlJS::reformat(document, tabSettings.m_indentSize, tabSettings.m_tabSize); + + // QTextDocument::setPlainText cannot be used, as it would reset undo/redo history + const auto setNewText = [this, &newText]() { + QTextCursor tc(m_currentDocument->document()); + tc.movePosition(QTextCursor::Start); + tc.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); + tc.insertText(newText); + }; + QmlJSEditorWidget *widget = EditorManager::currentEditor() ? qobject_cast(EditorManager::currentEditor()->widget()) : nullptr; if (widget) { const int position = widget->position(); - m_currentDocument->document()->setPlainText(newText); + setNewText(); widget->setCursorPosition(position); } else { - m_currentDocument->document()->setPlainText(newText); + setNewText(); } } }