From fdbb34adb8a1301d7567d4c70952ab5e0fc85c0c Mon Sep 17 00:00:00 2001
From: Leandro Melo <leandro.melo@nokia.com>
Date: Mon, 29 Nov 2010 17:25:01 +0100
Subject: [PATCH] Snippets: Track begin of first selection

Since in the snippets overlay the selections are created with
ExpandBegin the interest is mostly in detecting when the first one
moved to the left (for example, when an undo is performed right
after inserting the snippet). However, this tracking doesn't need
to necessarily be associated with that flag.
---
 src/plugins/texteditor/basetexteditor.cpp    |  8 +++-----
 src/plugins/texteditor/texteditoroverlay.cpp | 16 ++++++++++++++--
 src/plugins/texteditor/texteditoroverlay.h   |  3 +++
 3 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index bf76217af71..f9aa5824443 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -670,10 +670,7 @@ void BaseTextEditor::editorContentsChange(int position, int charsRemoved, int ch
     if (d->m_snippetOverlay->isVisible()) {
         QTextCursor cursor = textCursor();
         cursor.setPosition(position);
-        if (!d->m_snippetOverlay->hasCursorInSelection(cursor)) {
-            d->m_snippetOverlay->hide();
-            d->m_snippetOverlay->clear();
-        }
+        d->snippetCheckCursor(cursor);
     }
 
     if (doc->isRedoAvailable())
@@ -2455,7 +2452,8 @@ bool BaseTextEditorPrivate::snippetCheckCursor(const QTextCursor &cursor)
     QTextCursor end = cursor;
     end.setPosition(cursor.selectionEnd());
     if (!m_snippetOverlay->hasCursorInSelection(start)
-        || !m_snippetOverlay->hasCursorInSelection(end)) {
+        || !m_snippetOverlay->hasCursorInSelection(end)
+        || m_snippetOverlay->hasFirstSelectionBeginMoved()) {
         m_snippetOverlay->setVisible(false);
         m_snippetOverlay->clear();
         return false;
diff --git a/src/plugins/texteditor/texteditoroverlay.cpp b/src/plugins/texteditor/texteditoroverlay.cpp
index 5059b20945f..24a419f4df4 100644
--- a/src/plugins/texteditor/texteditoroverlay.cpp
+++ b/src/plugins/texteditor/texteditoroverlay.cpp
@@ -43,6 +43,7 @@ TextEditorOverlay::TextEditorOverlay(BaseTextEditor *editor) :
     m_borderWidth(1),
     m_dropShadowWidth(2),
     m_alpha(true),
+    m_firstSelectionOriginalBegin(-1),
     m_editor(editor),
     m_viewport(editor->viewport())
 {
@@ -69,6 +70,7 @@ void TextEditorOverlay::clear()
     if (m_selections.isEmpty())
         return;
     m_selections.clear();
+    m_firstSelectionOriginalBegin = -1;
     update();
 }
 
@@ -94,13 +96,16 @@ void TextEditorOverlay::addOverlaySelection(int begin, int end,
         }
     }
 
-
     if (overlaySelectionFlags & LockSize)
         selection.m_fixedLength = (end - begin);
 
-
     selection.m_dropShadow = (overlaySelectionFlags & DropShadow);
 
+    if (m_selections.isEmpty())
+        m_firstSelectionOriginalBegin = begin;
+    else if (begin < m_firstSelectionOriginalBegin)
+        qWarning() << "overlay selections not in order";
+
     m_selections.append(selection);
     update();
 }
@@ -525,3 +530,10 @@ void TextEditorOverlay::updateEquivalentSelections(const QTextCursor &cursor)
         }
     }
 }
+
+bool TextEditorOverlay::hasFirstSelectionBeginMoved() const
+{
+    if (m_firstSelectionOriginalBegin == -1 || m_selections.isEmpty())
+        return false;
+    return m_selections.at(0).m_cursor_begin.position() != m_firstSelectionOriginalBegin;
+}
diff --git a/src/plugins/texteditor/texteditoroverlay.h b/src/plugins/texteditor/texteditoroverlay.h
index 48a61ad1c81..b6e5f7e09b7 100644
--- a/src/plugins/texteditor/texteditoroverlay.h
+++ b/src/plugins/texteditor/texteditoroverlay.h
@@ -101,6 +101,8 @@ public:
     void mapEquivalentSelections();
     void updateEquivalentSelections(const QTextCursor &cursor);
 
+    bool hasFirstSelectionBeginMoved() const;
+
 private:
     QPainterPath createSelectionPath(const QTextCursor &begin, const QTextCursor &end, const QRect& clip);
     void paintSelection(QPainter *painter, const OverlaySelection &selection);
@@ -113,6 +115,7 @@ private:
     int m_borderWidth;
     int m_dropShadowWidth;
     bool m_alpha;
+    int m_firstSelectionOriginalBegin;
     BaseTextEditor *m_editor;
     QWidget *m_viewport;
     QList<OverlaySelection> m_selections;
-- 
GitLab