From ebc305b0d8440c7171e5e7b548c167f302b3467d Mon Sep 17 00:00:00 2001
From: mae <qt-info@nokia.com>
Date: Tue, 7 Sep 2010 15:55:06 +0200
Subject: [PATCH] Fix: Folded code is deleted on save

Also fixes a crash when folding code at the very end
of the document

Task-number: QTCREATORBUG-2159
---
 src/plugins/texteditor/basetextdocument.cpp       | 6 ++++--
 src/plugins/texteditor/basetextdocumentlayout.cpp | 2 +-
 src/plugins/texteditor/basetexteditor.cpp         | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/plugins/texteditor/basetextdocument.cpp b/src/plugins/texteditor/basetextdocument.cpp
index 1e13af7e101..0b799c20dfb 100644
--- a/src/plugins/texteditor/basetextdocument.cpp
+++ b/src/plugins/texteditor/basetextdocument.cpp
@@ -166,8 +166,8 @@ bool BaseTextDocument::save(const QString &fileName)
     // 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();
+        if (editable->file() == this) 
+            cursor.setPosition(editable->editor()->textCursor().position());
     }
 
     cursor.beginEditBlock();
@@ -382,6 +382,7 @@ void BaseTextDocument::cleanWhitespace(const QTextCursor &cursor)
 {
     bool hasSelection = cursor.hasSelection();
     QTextCursor copyCursor = cursor;
+    copyCursor.setVisualNavigation(false);
     copyCursor.beginEditBlock();
     cleanWhitespace(copyCursor, true, true);
     if (!hasSelection)
@@ -392,6 +393,7 @@ void BaseTextDocument::cleanWhitespace(const QTextCursor &cursor)
 void BaseTextDocument::cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, bool inEntireDocument)
 {
     BaseTextDocumentLayout *documentLayout = qobject_cast<BaseTextDocumentLayout*>(m_document->documentLayout());
+    Q_ASSERT(cursor.visualNavigation() == false);
 
     QTextBlock block = m_document->findBlock(cursor.selectionStart());
     QTextBlock end;
diff --git a/src/plugins/texteditor/basetextdocumentlayout.cpp b/src/plugins/texteditor/basetextdocumentlayout.cpp
index c69808f8de8..06cacd7d811 100644
--- a/src/plugins/texteditor/basetextdocumentlayout.cpp
+++ b/src/plugins/texteditor/basetextdocumentlayout.cpp
@@ -527,7 +527,7 @@ void BaseTextDocumentLayout::doFoldOrUnfold(const QTextBlock& block, bool unfold
     QTextBlock b = block.next();
 
     int indent = foldingIndent(block);
-    while (b.isValid() && foldingIndent(b) > indent && b.next().isValid()) {
+    while (b.isValid() && foldingIndent(b) > indent && (unfold || b.next().isValid())) {
         b.setVisible(unfold);
         b.setLineCount(unfold? qMax(1, b.layout()->lineCount()) : 0);
         if (unfold) { // do not unfold folded sub-blocks
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 5388c5cce7f..621a6772574 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -3431,7 +3431,7 @@ void BaseTextEditor::updateCurrentLineHighlight()
         if (block.isValid())
             d->m_extraArea->update(blockBoundingGeometry(block).translated(offset).toAlignedRect());
         block = document()->findBlockByNumber(cursorBlockNumber);
-        if (block.isValid())
+        if (block.isValid() && block.isVisible())
             d->m_extraArea->update(blockBoundingGeometry(block).translated(offset).toAlignedRect());
         d->m_cursorBlockNumber = cursorBlockNumber;
     }
-- 
GitLab