From c2eb91e053332d010adc8b9e7918d9de28ef4c90 Mon Sep 17 00:00:00 2001
From: Nikolai Kosjar <nikolai.kosjar@digia.com>
Date: Mon, 13 Oct 2014 14:46:03 +0200
Subject: [PATCH] CppTools: Update visible documents on project update

With asynchron project managers, creating a new project from a wizard or
re-opening a session will first open the editor documents and then
provide the project information later (it's the other way around with
synchronous project managers). This sequence leave the documents with
various error diagnostics behind. Fixed by re-triggering the re-parsing.

This change does not fix QTCREATORBUG-11606 yet. Though it will trigger
a reparse with the right includes, clang still reports 'file not found'.
Will be addressed by a follow-up patch.

Task-number: QTCREATORBUG-11606
Change-Id: Ib5d251e05accf4268f9b36decc5224b2c75f610c
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@digia.com>
---
 src/plugins/cpptools/cppmodelmanager.cpp | 26 +++++++++++++++++++++++-
 src/plugins/cpptools/cppmodelmanager.h   |  3 ++-
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/plugins/cpptools/cppmodelmanager.cpp b/src/plugins/cpptools/cppmodelmanager.cpp
index 601454a65cd..d67fab8bd53 100644
--- a/src/plugins/cpptools/cppmodelmanager.cpp
+++ b/src/plugins/cpptools/cppmodelmanager.cpp
@@ -456,7 +456,7 @@ void CppModelManager::removeExtraEditorSupport(AbstractEditorSupport *editorSupp
     d->m_extraEditorSupports.remove(editorSupport);
 }
 
-EditorDocumentHandle *CppModelManager::editorDocument(const QString &filePath)
+EditorDocumentHandle *CppModelManager::editorDocument(const QString &filePath) const
 {
     QTC_ASSERT(!filePath.isEmpty(), return 0);
 
@@ -697,6 +697,24 @@ void CppModelManager::recalculateFileToProjectParts()
     }
 }
 
+void CppModelManager::updateVisibleEditorDocuments() const
+{
+    QSet<QString> visibleDocumentsInEditMode;
+    foreach (Core::IEditor *editor, Core::EditorManager::visibleEditors()) {
+        if (const Core::IDocument *document = editor->document()) {
+            const QString filePath = document->filePath();
+            QTC_ASSERT(!filePath.isEmpty(), continue);
+            visibleDocumentsInEditMode.insert(filePath);
+        }
+    }
+
+    // Re-process these documents
+    foreach (const QString &filePath, visibleDocumentsInEditMode) {
+        if (EditorDocumentHandle *editor = editorDocument(filePath))
+            editor->processor()->run();
+    }
+}
+
 QFuture<void> CppModelManager::updateProjectInfo(const ProjectInfo &newProjectInfo)
 {
     if (!newProjectInfo.isValid())
@@ -768,6 +786,12 @@ QFuture<void> CppModelManager::updateProjectInfo(const ProjectInfo &newProjectIn
 
     emit projectPartsUpdated(newProjectInfo.project().data());
 
+    // Ideally, we would update all the editor documents that depend on the 'filesToReindex'.
+    // However, on e.g. a session restore first the editor documents are created and then the
+    // project updates come in. That is, there are no reasonable dependency tables based on
+    // resolved includes that we could rely on.
+    updateVisibleEditorDocuments();
+
     // Trigger reindexing
     return updateSourceFiles(filesToReindex, ForcedProgressNotification);
 }
diff --git a/src/plugins/cpptools/cppmodelmanager.h b/src/plugins/cpptools/cppmodelmanager.h
index 1dfe3d2a7e8..e196be585e5 100644
--- a/src/plugins/cpptools/cppmodelmanager.h
+++ b/src/plugins/cpptools/cppmodelmanager.h
@@ -115,7 +115,7 @@ public:
     void addExtraEditorSupport(AbstractEditorSupport *editorSupport);
     void removeExtraEditorSupport(AbstractEditorSupport *editorSupport);
 
-    EditorDocumentHandle *editorDocument(const QString &filePath);
+    EditorDocumentHandle *editorDocument(const QString &filePath) const;
     void registerEditorDocument(EditorDocumentHandle *editorDocument);
     void unregisterEditorDocument(const QString &filePath);
 
@@ -189,6 +189,7 @@ private slots:
 private:
     void delayedGC();
     void recalculateFileToProjectParts();
+    void updateVisibleEditorDocuments() const;
 
     void replaceSnapshot(const CPlusPlus::Snapshot &newSnapshot);
     void removeFilesFromSnapshot(const QSet<QString> &removedFiles);
-- 
GitLab