From 3ca067ad12c671c7732366f55a838055895263f1 Mon Sep 17 00:00:00 2001
From: mae <qt-info@nokia.com>
Date: Mon, 28 Sep 2009 16:22:39 +0200
Subject: [PATCH] tune auto-indentation of pastes

when pasting text which ends with a newline, we should not
reindent the final line, instead we will preserve the indentation
that was there.
---
 src/plugins/texteditor/basetexteditor.cpp | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index c38f4df4334..a12e56e3a48 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -4530,14 +4530,27 @@ void BaseTextEditor::insertFromMimeData(const QMimeData *source)
     int pos = cursor.position() - bpos;
     int fns = ts.firstNonSpace(cursor.block().text());
 
-    cursor.insertText(text);
+    if (pos <= fns) { // cursor is at beginning of line, we will reindent
 
-    if (pos <= fns) { // cursor was at beginning of line, do reindent
+        bool hasEndingNewline = (text.endsWith(QLatin1Char('\n'))
+                                 || text.endsWith(QChar::ParagraphSeparator)
+                                 || text.endsWith(QLatin1Char('\r')));
+
+        if (hasEndingNewline)
+            cursor.setPosition(bpos); // we'll add a final newline, preserve current line's indentation
+
+        cursor.insertText(text);
         pos = cursor.position();
         cursor.setPosition(bpos);
         cursor.setPosition(pos, QTextCursor::KeepAnchor);
+        if (hasEndingNewline)
+            cursor.setPosition(cursor.position()-1, QTextCursor::KeepAnchor);
         indent(document(), cursor, QChar::Null);
         cursor.clearSelection();
+        if (hasEndingNewline)
+            cursor.setPosition(cursor.position()+1);
+    } else {
+        cursor.insertText(text);
     }
 
     cursor.endEditBlock();
-- 
GitLab