Commit f18bc981 authored by Nikolai Kosjar's avatar Nikolai Kosjar

Clang: Fix reloading documents

Fix the following use case:
 * Open foo.h
 * Open foo.cpp
 * Change both files externally (e.g. git checkout) e.g. by adding a
   function (declaration + definition)
 * Confirm the reload dialog in Qt Creator
 * The added function in foo.cpp gets a diagnostic that it's out of line
   although foo.h is also reloaded.

TextDocument::contentsChangedWithPosition() is also emitted during a
reload. However, the revision of the document at that point is not yet
updated. This led to an out-dated copy of the unsaved files in the clang
backend.

Change-Id: I70580d32bb7a34dc43356dc05019cc18addf950e
Reviewed-by: default avatarMarco Bubke <marco.bubke@theqtcompany.com>
parent 48e4b82e
......@@ -116,25 +116,43 @@ void ModelManagerSupportClang::onCurrentEditorChanged(Core::IEditor *newCurrent)
void ModelManagerSupportClang::connectTextDocumentToTranslationUnit(TextEditor::TextDocument *textDocument)
{
// Handle externally changed documents
connect(textDocument, &Core::IDocument::aboutToReload,
this, &ModelManagerSupportClang::onCppDocumentAboutToReloadOnTranslationUnit,
Qt::UniqueConnection);
connect(textDocument, &Core::IDocument::reloadFinished,
this, &ModelManagerSupportClang::onCppDocumentReloadFinishedOnTranslationUnit,
Qt::UniqueConnection);
// Handle changes from e.g. refactoring actions
connect(textDocument, &TextEditor::TextDocument::contentsChangedWithPosition,
this, &ModelManagerSupportClang::onCppDocumentContentsChangedOnTranslationUnit,
Qt::UniqueConnection);
connectToTextDocumentContentsChangedForTranslationUnit(textDocument);
}
void ModelManagerSupportClang::connectTextDocumentToUnsavedFiles(TextEditor::TextDocument *textDocument)
{
// Handle externally changed documents
connect(textDocument, &Core::IDocument::aboutToReload,
this, &ModelManagerSupportClang::onCppDocumentAboutToReloadOnUnsavedFile,
Qt::UniqueConnection);
connect(textDocument, &Core::IDocument::reloadFinished,
this, &ModelManagerSupportClang::onCppDocumentReloadFinishedOnUnsavedFile,
Qt::UniqueConnection);
// Handle changes from e.g. refactoring actions
connect(textDocument, &TextEditor::TextDocument::contentsChanged,
connectToTextDocumentContentsChangedForUnsavedFile(textDocument);
}
void ModelManagerSupportClang::connectToTextDocumentContentsChangedForTranslationUnit(
TextEditor::TextDocument *textDocument)
{
connect(textDocument, &TextEditor::TextDocument::contentsChangedWithPosition,
this, &ModelManagerSupportClang::onCppDocumentContentsChangedOnTranslationUnit,
Qt::UniqueConnection);
}
void ModelManagerSupportClang::connectToTextDocumentContentsChangedForUnsavedFile(
TextEditor::TextDocument *textDocument)
{
connect(textDocument, &TextEditor::TextDocument::contentsChangedWithPosition,
this, &ModelManagerSupportClang::onCppDocumentContentsChangedOnUnsavedFile,
Qt::UniqueConnection);
}
......@@ -168,11 +186,19 @@ void ModelManagerSupportClang::onEditorOpened(Core::IEditor *editor)
}
}
void ModelManagerSupportClang::onCppDocumentAboutToReloadOnTranslationUnit()
{
TextEditor::TextDocument *textDocument = qobject_cast<TextEditor::TextDocument *>(sender());
disconnect(textDocument, &TextEditor::TextDocument::contentsChangedWithPosition,
this, &ModelManagerSupportClang::onCppDocumentContentsChangedOnTranslationUnit);
}
void ModelManagerSupportClang::onCppDocumentReloadFinishedOnTranslationUnit(bool success)
{
if (success) {
Core::IDocument *document = qobject_cast<Core::IDocument *>(sender());
m_ipcCommunicator.requestDiagnostics(document);
TextEditor::TextDocument *textDocument = qobject_cast<TextEditor::TextDocument *>(sender());
connectToTextDocumentContentsChangedForTranslationUnit(textDocument);
m_ipcCommunicator.requestDiagnostics(textDocument);
}
}
......@@ -187,11 +213,19 @@ void ModelManagerSupportClang::onCppDocumentContentsChangedOnTranslationUnit(int
m_ipcCommunicator.updateTranslationUnitIfNotCurrentDocument(document);
}
void ModelManagerSupportClang::onCppDocumentAboutToReloadOnUnsavedFile()
{
TextEditor::TextDocument *textDocument = qobject_cast<TextEditor::TextDocument *>(sender());
disconnect(textDocument, &TextEditor::TextDocument::contentsChangedWithPosition,
this, &ModelManagerSupportClang::onCppDocumentContentsChangedOnUnsavedFile);
}
void ModelManagerSupportClang::onCppDocumentReloadFinishedOnUnsavedFile(bool success)
{
if (success) {
Core::IDocument *document = qobject_cast<Core::IDocument *>(sender());
m_ipcCommunicator.updateUnsavedFile(document);
TextEditor::TextDocument *textDocument = qobject_cast<TextEditor::TextDocument *>(sender());
connectToTextDocumentContentsChangedForUnsavedFile(textDocument);
m_ipcCommunicator.updateUnsavedFile(textDocument);
}
}
......
......@@ -72,10 +72,12 @@ public:
private:
void onEditorOpened(Core::IEditor *editor);
void onCurrentEditorChanged(Core::IEditor *newCurrent);
void onCppDocumentAboutToReloadOnTranslationUnit();
void onCppDocumentReloadFinishedOnTranslationUnit(bool success);
void onCppDocumentContentsChangedOnTranslationUnit(int position,
int charsRemoved,
int charsAdded);
void onCppDocumentAboutToReloadOnUnsavedFile();
void onCppDocumentReloadFinishedOnUnsavedFile(bool success);
void onCppDocumentContentsChangedOnUnsavedFile();
......@@ -93,6 +95,9 @@ private:
void connectTextDocumentToTranslationUnit(TextEditor::TextDocument *textDocument);
void connectTextDocumentToUnsavedFiles(TextEditor::TextDocument *textDocument);
void connectToTextDocumentContentsChangedForTranslationUnit(
TextEditor::TextDocument *textDocument);
void connectToTextDocumentContentsChangedForUnsavedFile(TextEditor::TextDocument *textDocument);
void connectToWidgetsMarkContextMenuRequested(QWidget *editorWidget);
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment