From 9539bb2b28c05aa3ff7ca1aa19161ae864116422 Mon Sep 17 00:00:00 2001
From: Roberto Raggi <qtc-committer@nokia.com>
Date: Tue, 10 Feb 2009 22:56:04 +0100
Subject: [PATCH] Less annoying syntax checker.

---
 src/libs/cplusplus/CppDocument.cpp            |  3 ++
 src/plugins/cpptools/cppmodelmanager.cpp      | 43 +++++++++++++++++--
 src/plugins/cpptools/cppmodelmanager.h        | 17 ++++++++
 .../cpptools/cpptoolseditorsupport.cpp        | 24 +++++++++--
 src/plugins/cpptools/cpptoolseditorsupport.h  |  3 +-
 5 files changed, 83 insertions(+), 7 deletions(-)

diff --git a/src/libs/cplusplus/CppDocument.cpp b/src/libs/cplusplus/CppDocument.cpp
index 06655b0960a..8c2d6265909 100644
--- a/src/libs/cplusplus/CppDocument.cpp
+++ b/src/libs/cplusplus/CppDocument.cpp
@@ -240,6 +240,9 @@ void Document::startSkippingBlocks(unsigned start)
 
 void Document::stopSkippingBlocks(unsigned stop)
 {
+    if (_skippedBlocks.isEmpty())
+        return;
+
     unsigned start = _skippedBlocks.back().begin();
     if (start > stop)
         _skippedBlocks.removeLast(); // Ignore this block, it's invalid.
diff --git a/src/plugins/cpptools/cppmodelmanager.cpp b/src/plugins/cpptools/cppmodelmanager.cpp
index c7e1d3fc5da..c28c216138e 100644
--- a/src/plugins/cpptools/cppmodelmanager.cpp
+++ b/src/plugins/cpptools/cppmodelmanager.cpp
@@ -69,6 +69,7 @@
 #include <QtCore/QDebug>
 #include <QtCore/QMutexLocker>
 #include <QtCore/QTime>
+#include <QtCore/QTimer>
 
 using namespace CppTools;
 using namespace CppTools::Internal;
@@ -454,6 +455,12 @@ CppModelManager::CppModelManager(QObject *parent)
     ProjectExplorer::SessionManager *session = m_projectExplorer->session();
     QTC_ASSERT(session, return);
 
+    m_updateEditorSelectionsTimer = new QTimer(this);
+    m_updateEditorSelectionsTimer->setInterval(500);
+    m_updateEditorSelectionsTimer->setSingleShot(true);
+    connect(m_updateEditorSelectionsTimer, SIGNAL(timeout()),
+            this, SLOT(updateEditorSelections()));
+
     connect(session, SIGNAL(projectAdded(ProjectExplorer::Project*)),
             this, SLOT(onProjectAdded(ProjectExplorer::Project*)));
 
@@ -717,8 +724,8 @@ void CppModelManager::onDocumentUpdated(Document::Ptr doc)
                     continue;
                 else if (lines.contains(m.line()))
                     continue;
-                else if (lines.size() == MAX_SELECTION_COUNT)
-                    break; // we're done.
+                //else if (lines.size() == MAX_SELECTION_COUNT)
+                    //break; // we're done.
 
                 lines.insert(m.line());
 
@@ -740,12 +747,42 @@ void CppModelManager::onDocumentUpdated(Document::Ptr doc)
                 sel.cursor = c;
                 selections.append(sel);
             }
-            ed->setExtraSelections(TextEditor::BaseTextEditor::CodeWarningsSelection, selections);
+
+            QList<Editor> todo;
+            foreach (Editor e, todo) {
+                if (e.widget != ed)
+                    todo.append(e);
+            }
+
+            Editor e;
+            e.widget = ed;
+            e.selections = selections;
+            todo.append(e);
+            m_todo = todo;
+            postEditorUpdate();
             break;
         }
     }
 }
 
+void CppModelManager::postEditorUpdate()
+{
+    m_updateEditorSelectionsTimer->start(500);
+}
+
+void CppModelManager::updateEditorSelections()
+{
+    foreach (Editor ed, m_todo) {
+        if (! ed.widget)
+            continue;
+
+        ed.widget->setExtraSelections(TextEditor::BaseTextEditor::CodeWarningsSelection,
+                                      ed.selections);
+    }
+
+    m_todo.clear();
+}
+
 void CppModelManager::onProjectAdded(ProjectExplorer::Project *)
 {
     QMutexLocker locker(&mutex);
diff --git a/src/plugins/cpptools/cppmodelmanager.h b/src/plugins/cpptools/cppmodelmanager.h
index 361c714fee7..4713c29d61b 100644
--- a/src/plugins/cpptools/cppmodelmanager.h
+++ b/src/plugins/cpptools/cppmodelmanager.h
@@ -41,6 +41,8 @@
 #include <QMap>
 #include <QFutureInterface>
 #include <QMutex>
+#include <QTimer>
+#include <QTextEdit>
 
 namespace Core {
 class ICore;
@@ -49,6 +51,7 @@ class IEditor;
 
 namespace TextEditor {
 class ITextEditor;
+class BaseTextEditor;
 }
 
 namespace ProjectExplorer {
@@ -86,6 +89,9 @@ public:
 
     void emitDocumentUpdated(CPlusPlus::Document::Ptr doc);
 
+    void stopEditorSelectionsUpdate()
+    { m_updateEditorSelectionsTimer->stop(); }
+
 Q_SIGNALS:
     void projectPathChanged(const QString &projectPath);
 
@@ -102,6 +108,8 @@ private Q_SLOTS:
     void onAboutToRemoveProject(ProjectExplorer::Project *project);
     void onSessionUnloaded();
     void onProjectAdded(ProjectExplorer::Project *project);
+    void postEditorUpdate();
+    void updateEditorSelections();
 
 private:
     QMap<QString, QByteArray> buildWorkingCopyList();
@@ -163,6 +171,15 @@ private:
     enum {
         MAX_SELECTION_COUNT = 5
     };
+
+    struct Editor {
+        QPointer<TextEditor::BaseTextEditor> widget;
+        QList<QTextEdit::ExtraSelection> selections;
+    };
+
+    QList<Editor> m_todo;
+
+    QTimer *m_updateEditorSelectionsTimer;
 };
 
 } // namespace Internal
diff --git a/src/plugins/cpptools/cpptoolseditorsupport.cpp b/src/plugins/cpptools/cpptoolseditorsupport.cpp
index 5a907a2d175..ab6fe3532cc 100644
--- a/src/plugins/cpptools/cpptoolseditorsupport.cpp
+++ b/src/plugins/cpptools/cpptoolseditorsupport.cpp
@@ -35,6 +35,7 @@
 #include "cppmodelmanager.h"
 
 #include <texteditor/itexteditor.h>
+#include <texteditor/basetexteditor.h>
 
 #include <QTimer>
 
@@ -68,12 +69,14 @@ void CppEditorSupport::setTextEditor(TextEditor::ITextEditor *textEditor)
     updateDocument();
 }
 
-QString CppEditorSupport::contents() const
+QString CppEditorSupport::contents()
 {
     if (! _textEditor)
         return QString();
+    else if (! _cachedContents.isEmpty())
+        _cachedContents = _textEditor->contents();
 
-    return _textEditor->contents();
+    return _cachedContents;
 }
 
 int CppEditorSupport::updateDocumentInterval() const
@@ -83,7 +86,20 @@ void CppEditorSupport::setUpdateDocumentInterval(int updateDocumentInterval)
 { _updateDocumentInterval = updateDocumentInterval; }
 
 void CppEditorSupport::updateDocument()
-{ _updateDocumentTimer->start(_updateDocumentInterval); }
+{
+    if (TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor*>(_textEditor->widget())) {
+        const QList<QTextEdit::ExtraSelection> selections =
+                edit->extraSelections(TextEditor::BaseTextEditor::CodeWarningsSelection);
+
+        if (! selections.isEmpty())
+            edit->setExtraSelections(TextEditor::BaseTextEditor::CodeWarningsSelection,
+                                     QList<QTextEdit::ExtraSelection>());
+
+        _modelManager->stopEditorSelectionsUpdate();
+    }
+
+    _updateDocumentTimer->start(_updateDocumentInterval);
+}
 
 void CppEditorSupport::updateDocumentNow()
 {
@@ -91,7 +107,9 @@ void CppEditorSupport::updateDocumentNow()
         _updateDocumentTimer->start(_updateDocumentInterval);
     } else {
         _updateDocumentTimer->stop();
+
         QStringList sourceFiles(_textEditor->file()->fileName());
+        _cachedContents = _textEditor->contents();
         _documentParser = _modelManager->refreshSourceFiles(sourceFiles);
     }
 }
diff --git a/src/plugins/cpptools/cpptoolseditorsupport.h b/src/plugins/cpptools/cpptoolseditorsupport.h
index 2bce101e523..6e136c2d852 100644
--- a/src/plugins/cpptools/cpptoolseditorsupport.h
+++ b/src/plugins/cpptools/cpptoolseditorsupport.h
@@ -65,7 +65,7 @@ public:
     int updateDocumentInterval() const;
     void setUpdateDocumentInterval(int updateDocumentInterval);
 
-    QString contents() const;
+    QString contents();
 
 private Q_SLOTS:
     void updateDocument();
@@ -79,6 +79,7 @@ private:
     QTimer *_updateDocumentTimer;
     int _updateDocumentInterval;
     QFuture<void> _documentParser;
+    QString _cachedContents;
 };
 
 } // namespace Internal
-- 
GitLab