From 5ad1b35a764416bbd07b0bb958a1952a26903026 Mon Sep 17 00:00:00 2001
From: Erik Verbruggen <erik.verbruggen@nokia.com>
Date: Fri, 24 Sep 2010 13:02:13 +0200
Subject: [PATCH] Editor: cleanup special cases for snippets.

---
 src/plugins/cpptools/cppcodecompletion.cpp    |  3 -
 .../qmljseditor/qmljscodecompletion.cpp       |  2 -
 src/plugins/texteditor/basetexteditor.cpp     | 73 ++++++-------------
 src/plugins/texteditor/basetexteditor.h       |  3 -
 src/plugins/texteditor/basetexteditor_p.h     |  3 -
 5 files changed, 21 insertions(+), 63 deletions(-)

diff --git a/src/plugins/cpptools/cppcodecompletion.cpp b/src/plugins/cpptools/cppcodecompletion.cpp
index a81d832bbc2..32a51d7f2cc 100644
--- a/src/plugins/cpptools/cppcodecompletion.cpp
+++ b/src/plugins/cpptools/cppcodecompletion.cpp
@@ -1822,9 +1822,6 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar t
 
     Symbol *symbol = 0;
 
-    if (TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(m_editor->widget()))
-        edit->setNextChangeIsSnippetSafe();
-
     if (item.data.isValid()) {
         if (item.data.canConvert<QString>()) {
             TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(m_editor->widget());
diff --git a/src/plugins/qmljseditor/qmljscodecompletion.cpp b/src/plugins/qmljseditor/qmljscodecompletion.cpp
index 1f1888724a9..4219b3b302c 100644
--- a/src/plugins/qmljseditor/qmljscodecompletion.cpp
+++ b/src/plugins/qmljseditor/qmljscodecompletion.cpp
@@ -904,8 +904,6 @@ void CodeCompletion::complete(const TextEditor::CompletionItem &item, QChar type
     QString toInsert = item.text;
 
     if (QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(m_editor->widget())) {
-        edit->setNextChangeIsSnippetSafe();
-
         if (item.data.isValid()) {
             QTextCursor tc = edit->textCursor();
             tc.setPosition(m_startPosition, QTextCursor::KeepAnchor);
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 900db9b9a4e..bd9e67274e5 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -89,17 +89,6 @@
 using namespace TextEditor;
 using namespace TextEditor::Internal;
 
-namespace {
-    class Locker {
-        bool *m_bool;
-    public:
-        inline Locker(bool *b):m_bool(b){ *m_bool = true; }
-        inline ~Locker() { *m_bool = false; }
-    };
-}
-
-
-
 namespace TextEditor {
 namespace Internal {
 
@@ -258,10 +247,7 @@ BaseTextEditor::BaseTextEditor(QWidget *parent)
     connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
             this, SLOT(currentEditorChanged(Core::IEditor*)));
 
-    d->m_inKeyPressEvent = false;
     d->m_moveLineUndoHack = false;
-
-    d->m_nextChangeIsSnippetSafe = false;
 }
 
 BaseTextEditor::~BaseTextEditor()
@@ -495,37 +481,12 @@ ITextMarkable *BaseTextEditor::markableInterface() const
     return baseTextDocument()->documentMarker();
 }
 
-void BaseTextEditor::maybeEmitContentsChangedBecauseOfUndo()
-{
-    if (!d->m_inKeyPressEvent && !d->m_nextChangeIsSnippetSafe) {
-        d->m_nextChangeIsSnippetSafe = false;
-
-        // i.e. the document was changed outside key press event
-        // Possible with undo, cut, paste, etc.
-        if (d->m_snippetOverlay->isVisible()) {
-
-            d->m_snippetOverlay->hide();
-            d->m_snippetOverlay->clear();
-            QTextCursor cursor = textCursor();
-            cursor.clearSelection();
-            setTextCursor(cursor);
-            return;
-        }
-
-    }
-    if (document()->isRedoAvailable()) {
-        emit editableInterface()->contentsChangedBecauseOfUndo();
-    }
-}
-
 BaseTextEditorEditable *BaseTextEditor::editableInterface() const
 {
     if (!d->m_editable) {
         d->m_editable = const_cast<BaseTextEditor*>(this)->createEditableInterface();
         connect(this, SIGNAL(textChanged()),
                 d->m_editable, SIGNAL(contentsChanged()));
-        connect(this, SIGNAL(textChanged()),
-                this, SLOT(maybeEmitContentsChangedBecauseOfUndo()));
         connect(this, SIGNAL(changed()),
                 d->m_editable, SIGNAL(changed()));
     }
@@ -683,7 +644,6 @@ void BaseTextEditor::editorContentsChange(int position, int charsRemoved, int ch
     if (d->m_animator)
         d->m_animator->finish();
 
-
     d->m_contentsChanged = true;
     QTextDocument *doc = document();
 
@@ -703,11 +663,19 @@ 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();
+        }
+    }
+
     if (doc->isRedoAvailable())
         emit editableInterface()->contentsChangedBecauseOfUndo();
 }
 
-
 void BaseTextEditor::slotSelectionChanged()
 {
     if (d->m_inBlockSelectionMode && !textCursor().hasSelection()) {
@@ -1108,8 +1076,6 @@ void BaseTextEditor::cleanWhitespace()
 
 void BaseTextEditor::keyPressEvent(QKeyEvent *e)
 {
-    Locker inKeyPressEvent(&d->m_inKeyPressEvent);
-
     viewport()->setCursor(Qt::BlankCursor);
     ToolTip::instance()->hide();
 
@@ -1958,8 +1924,7 @@ BaseTextEditorPrivate::BaseTextEditorPrivate()
     m_requestAutoCompletionRevision(0),
     m_requestAutoCompletionPosition(0),
     m_requestAutoCompletionTimer(0),
-    m_cursorBlockNumber(-1),
-    m_inKeyPressEvent(false)
+    m_cursorBlockNumber(-1)
 {
 }
 
@@ -4084,11 +4049,6 @@ QString BaseTextEditor::insertParagraphSeparator(const QTextCursor &tc) const
     return QString();
 }
 
-void BaseTextEditor::setNextChangeIsSnippetSafe()
-{
-    d->m_nextChangeIsSnippetSafe = true;
-}
-
 QString BaseTextEditor::autoComplete(QTextCursor &cursor, const QString &textToInsert) const
 {
     const bool checkBlockEnd = d->m_allowSkippingOfBlockEnd;
@@ -5441,6 +5401,12 @@ void BaseTextEditor::insertFromMimeData(const QMimeData *source)
         cursor.endEditBlock();
         setTextCursor(cursor);
         ensureCursorVisible();
+
+        if (d->m_snippetOverlay->isVisible() && lines.count() > 1) {
+            d->m_snippetOverlay->hide();
+            d->m_snippetOverlay->clear();
+        }
+
         return;
     }
 
@@ -5451,8 +5417,11 @@ void BaseTextEditor::insertFromMimeData(const QMimeData *source)
     if (CompletionSupport::instance()->isActive())
         setFocus();
 
-    if (!text.contains(QLatin1Char('\n')) && !text.contains(QLatin1Char('\t')))
-        setNextChangeIsSnippetSafe();
+    if (d->m_snippetOverlay->isVisible() && (text.contains(QLatin1Char('\n'))
+                                             || text.contains(QLatin1Char('\t')))) {
+        d->m_snippetOverlay->hide();
+        d->m_snippetOverlay->clear();
+    }
 
     const TabSettings &ts = d->m_document->tabSettings();
     QTextCursor cursor = textCursor();
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index cd215f22c63..aa19330753a 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -346,7 +346,6 @@ private slots:
     bool inFindScope(const QTextCursor &cursor);
     bool inFindScope(int selectionStart, int selectionEnd);
     void currentEditorChanged(Core::IEditor *editor);
-    void maybeEmitContentsChangedBecauseOfUndo();
 
 private:
     Internal::BaseTextEditorPrivate *d;
@@ -462,8 +461,6 @@ public:
     // Returns the text that needs to be inserted
     virtual QString insertParagraphSeparator(const QTextCursor &tc) const;
 
-    virtual void setNextChangeIsSnippetSafe();
-
 protected:
     static void countBracket(QChar open, QChar close, QChar c, int *errors, int *stillopen);
 
diff --git a/src/plugins/texteditor/basetexteditor_p.h b/src/plugins/texteditor/basetexteditor_p.h
index 61effc09c8d..b15f15c53c3 100644
--- a/src/plugins/texteditor/basetexteditor_p.h
+++ b/src/plugins/texteditor/basetexteditor_p.h
@@ -271,9 +271,6 @@ public:
 
     QPointer<BaseTextEditorAnimator> m_animator;
     int m_cursorBlockNumber;
-
-    bool m_inKeyPressEvent;
-    bool m_nextChangeIsSnippetSafe;
 };
 
 } // namespace Internal
-- 
GitLab