From 6df66e883fae36d96efaa0d3976e11e895c0c9b7 Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Tue, 18 Jun 2013 16:26:40 +0200 Subject: [PATCH] CppTools: Do not hand over partly reloaded files to the code model When a reloading operation was in progress, CppEditorSupport::contents() could return a partly reloaded file to the code model leading to parse errors and incomplete highlighting. Fixed by checking if the file is currently being reloaded. Task-number: QTCREATORBUG-9382 Change-Id: Iee97e10444763c0cbf481132afa4617c5cdbd15c Reviewed-by: Erik Verbruggen --- .../cpptools/cpptoolseditorsupport.cpp | 28 +++++++++++++++++-- src/plugins/cpptools/cpptoolseditorsupport.h | 4 +++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/plugins/cpptools/cpptoolseditorsupport.cpp b/src/plugins/cpptools/cpptoolseditorsupport.cpp index 7993ccd7dd4..86efe3bd26c 100644 --- a/src/plugins/cpptools/cpptoolseditorsupport.cpp +++ b/src/plugins/cpptools/cpptoolseditorsupport.cpp @@ -33,6 +33,7 @@ #include +#include #include #include @@ -113,6 +114,7 @@ CppEditorSupport::CppEditorSupport(CppModelManager *modelManager, BaseTextEditor , m_updateDocumentInterval(UpdateDocumentDefaultInterval) , m_revision(0) , m_cachedContentsEditorRevision(-1) + , m_fileIsBeingReloaded(false) , m_initialized(false) , m_lastHighlightRevision(0) , m_highlightingSupport(modelManager->highlightingSupport(textEditor)) @@ -142,6 +144,11 @@ CppEditorSupport::CppEditorSupport(CppModelManager *modelManager, BaseTextEditor connect(m_textEditor->document(), SIGNAL(mimeTypeChanged()), this, SLOT(onMimeTypeChanged())); + connect(m_textEditor->document(), SIGNAL(aboutToReload()), + this, SLOT(onAboutToReload())); + connect(m_textEditor->document(), SIGNAL(reloadFinished(bool)), + this, SLOT(onReloadFinished())); + updateDocument(); } @@ -162,7 +169,7 @@ QString CppEditorSupport::fileName() const QString CppEditorSupport::contents() const { const int editorRev = editorRevision(); - if (m_cachedContentsEditorRevision != editorRev) { + if (m_cachedContentsEditorRevision != editorRev && !m_fileIsBeingReloaded) { m_cachedContentsEditorRevision = editorRev; m_cachedContents = m_textEditor->textDocument()->contents(); } @@ -233,9 +240,11 @@ void CppEditorSupport::updateDocumentNow() } else { m_updateDocumentTimer->stop(); - if (m_highlightingSupport && !m_highlightingSupport->requiresSemanticInfo()) { + if (m_fileIsBeingReloaded) + return; + + if (m_highlightingSupport && !m_highlightingSupport->requiresSemanticInfo()) startHighlighting(); - } const QStringList sourceFiles(m_textEditor->document()->fileName()); m_documentParser = m_modelManager->updateSourceFiles(sourceFiles); @@ -505,3 +514,16 @@ void CppEditorSupport::onMimeTypeChanged() updateDocumentNow(); } + +void CppEditorSupport::onAboutToReload() +{ + QTC_CHECK(!m_fileIsBeingReloaded); + m_fileIsBeingReloaded = true; +} + +void CppEditorSupport::onReloadFinished() +{ + QTC_CHECK(m_fileIsBeingReloaded); + m_fileIsBeingReloaded = false; + updateDocument(); +} diff --git a/src/plugins/cpptools/cpptoolseditorsupport.h b/src/plugins/cpptools/cpptoolseditorsupport.h index a63ee7e6e5a..f3bce1b5464 100644 --- a/src/plugins/cpptools/cpptoolseditorsupport.h +++ b/src/plugins/cpptools/cpptoolseditorsupport.h @@ -121,6 +121,9 @@ signals: private slots: void onMimeTypeChanged(); + void onAboutToReload(); + void onReloadFinished(); + void updateDocument(); void updateDocumentNow(); @@ -166,6 +169,7 @@ private: // content caching mutable QString m_cachedContents; mutable int m_cachedContentsEditorRevision; + bool m_fileIsBeingReloaded; QTimer *m_updateEditorTimer; EditorUpdates m_editorUpdates;