From 1f6c25a72e704a849510083361ed4eb1979354a7 Mon Sep 17 00:00:00 2001
From: mae <qt-info@nokia.com>
Date: Tue, 19 Jan 2010 16:18:28 +0100
Subject: [PATCH] snippet fixes

done with roberto
---
 src/plugins/texteditor/basetexteditor.cpp | 40 +++++++++++++++++------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index e03659adf19..59f69af6646 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -1178,7 +1178,13 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
         }
     }
 
-    if (e->key() == Qt::Key_H && e->modifiers() == Qt::MetaModifier) {
+    if (e->key() == Qt::Key_H && e->modifiers() ==
+#ifdef Q_OS_DARWIN
+        Qt::MetaModifier
+#else
+        Qt::ControlModifier
+#endif
+        ) {
         universalHelper();
         e->accept();
         return;
@@ -1256,6 +1262,8 @@ void BaseTextEditor::universalHelper()
     }
 
     int pos = 0;
+    QMap<int, int> positions;
+
     while (pos < snippet.size()) {
         if (snippet.at(pos) != QLatin1Char('$')) {
             const int start = pos;
@@ -1278,22 +1286,34 @@ void BaseTextEditor::universalHelper()
             int cursorPosition = cursor.position();
             cursor.insertText(textToInsert);
 
-            QTextCursor tc(document());
-            tc.setPosition(cursorPosition - 1);
-            tc.setPosition(cursorPosition + textToInsert.length(), QTextCursor::KeepAnchor);
-
-            QTextEdit::ExtraSelection selection;
-            selection.cursor = tc;
-            selection.format.setBackground(Qt::darkCyan);
+            if (textToInsert.isEmpty()) {
+                positions.insert(cursorPosition, 0);
+            } else {
+                positions.insert(cursorPosition-1, textToInsert.length()+1);
+            }
 
-            selections.append(selection);
             ++pos;
         }
     }
 
-    cursor.endEditBlock();
+    QMapIterator<int,int> it(positions);
+    while (it.hasNext()) {
+        it.next();
+        int length = it.value();
+        int position = it.key();
+
+        QTextCursor tc(document());
+        tc.setPosition(position);
+        tc.setPosition(position + length, QTextCursor::KeepAnchor);
+        QTextEdit::ExtraSelection selection;
+        selection.cursor = tc;
+        selection.format.setBackground(length ? Qt::darkCyan : Qt::darkMagenta);
+        selections.append(selection);
+    }
+
     cursor.setPosition(startCursorPosition, QTextCursor::KeepAnchor);
     indent(cursor.document(), cursor, QChar());
+    cursor.endEditBlock();
 
     setExtraSelections(BaseTextEditor::SnippetPlaceholderSelection, selections);
 
-- 
GitLab