diff --git a/src/plugins/texteditor/basetextdocument.cpp b/src/plugins/texteditor/basetextdocument.cpp index b25bafebd825671ae994dd1ba4ac7f6ca09268ad..d28e2e4dee52d96a52a7904ea30f6c08fedc80ad 100644 --- a/src/plugins/texteditor/basetextdocument.cpp +++ b/src/plugins/texteditor/basetextdocument.cpp @@ -162,7 +162,16 @@ bool BaseTextDocument::save(const QString &fileName) { QTextCursor cursor(m_document); + // When saving the current editor, make sure to maintain the cursor position for undo + Core::IEditor *currentEditor = Core::EditorManager::instance()->currentEditor(); + if (BaseTextEditorEditable *editable = qobject_cast<BaseTextEditorEditable*>(currentEditor)) { + if (editable->file() == this) + cursor = editable->editor()->textCursor(); + } + cursor.beginEditBlock(); + cursor.movePosition(QTextCursor::Start); + if (m_storageSettings.m_cleanWhitespace) cleanWhitespace(cursor, m_storageSettings.m_cleanIndentation, m_storageSettings.m_inEntireDocument); if (m_storageSettings.m_addFinalNewLine)