forked from qt-creator/qt-creator
qmljs: do not reset undo/redo history in reformatting
Task-number: QTCREATORBUG-18645 Change-Id: I5b64fa5e59af3d871d3124d668bfd1f86883e752 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -259,19 +259,29 @@ void QmlJSEditorPlugin::reformatFile()
|
|||||||
|
|
||||||
if (!document->isParsedCorrectly())
|
if (!document->isParsedCorrectly())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
TextEditor::TabSettings tabSettings = m_currentDocument->tabSettings();
|
TextEditor::TabSettings tabSettings = m_currentDocument->tabSettings();
|
||||||
const QString &newText = QmlJS::reformat(document,
|
const QString &newText = QmlJS::reformat(document,
|
||||||
tabSettings.m_indentSize,
|
tabSettings.m_indentSize,
|
||||||
tabSettings.m_tabSize);
|
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()
|
QmlJSEditorWidget *widget = EditorManager::currentEditor()
|
||||||
? qobject_cast<QmlJSEditorWidget*>(EditorManager::currentEditor()->widget())
|
? qobject_cast<QmlJSEditorWidget*>(EditorManager::currentEditor()->widget())
|
||||||
: nullptr;
|
: nullptr;
|
||||||
if (widget) {
|
if (widget) {
|
||||||
const int position = widget->position();
|
const int position = widget->position();
|
||||||
m_currentDocument->document()->setPlainText(newText);
|
setNewText();
|
||||||
widget->setCursorPosition(position);
|
widget->setCursorPosition(position);
|
||||||
} else {
|
} else {
|
||||||
m_currentDocument->document()->setPlainText(newText);
|
setNewText();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user