From 72b78c22dec7885f73e3cfc85f9dea68a6129f56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= <thorbjorn.lindeijer@nokia.com>
Date: Wed, 7 Jul 2010 13:17:58 +0200
Subject: [PATCH] Maintain the right cursor when undoing "clean whitespace"
 after saving

Previously it would always jump to the start of the document, since
that's where the newly created text cursor is at the beginEditBlock
call. Avoid this when saving the current editor by starting with the
cursor of the editor.

Reviewed-by: mae
Task-number: QTCREATORBUG-1807
---
 src/plugins/texteditor/basetextdocument.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/plugins/texteditor/basetextdocument.cpp b/src/plugins/texteditor/basetextdocument.cpp
index b25bafebd82..d28e2e4dee5 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)
-- 
GitLab