aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@qt.io>2023-08-02 13:56:28 +0200
committerDavid Schulz <david.schulz@qt.io>2023-08-04 11:20:18 +0000
commit66d49e6a53a9c535a9eb277f2bcd50e0fc3f4cfc (patch)
treeeae4f5457596fd5a9d24d25ffc83a8ea567cf9b0
parent8d554aba1a00cbd31ea82b5dbf8450c4ae67ddf7 (diff)
TextEditor: Fix crash on reloading the document
as part of the reload operation we set the document to nullptr inside TextBlockUserData::documentClosing, but we do not remove the mark from the marks cache. So when a text mark gets deleted while a document is reloaded the mark does not get readded to the document, but it wont get removed from the marks cache inside the document either, so we have to manually make sure the mark is removed from the cache in this situation. Fixes: QTCREATORBUG-29432 Change-Id: I3ae4182c4d2bbd3426c1d7a60275d21ac20ea99a Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
-rw-r--r--src/plugins/texteditor/textdocument.cpp2
-rw-r--r--src/plugins/texteditor/textdocumentlayout.cpp34
-rw-r--r--src/plugins/texteditor/textdocumentlayout.h2
-rw-r--r--src/plugins/texteditor/texteditorplugin.h2
4 files changed, 35 insertions, 5 deletions
diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp
index f8512e9a11..98a3eda4a1 100644
--- a/src/plugins/texteditor/textdocument.cpp
+++ b/src/plugins/texteditor/textdocument.cpp
@@ -837,7 +837,7 @@ bool TextDocument::reload(QString *errorString, const FilePath &realFilePath)
auto documentLayout =
qobject_cast<TextDocumentLayout*>(d->m_document.documentLayout());
if (documentLayout)
- documentLayout->documentAboutToReload(); // removes text marks non-permanently
+ documentLayout->documentAboutToReload(this); // removes text marks non-permanently
bool success = openImpl(errorString, filePath(), realFilePath, /*reload =*/true)
== OpenResult::Success;
diff --git a/src/plugins/texteditor/textdocumentlayout.cpp b/src/plugins/texteditor/textdocumentlayout.cpp
index 08c1e21117..a1a04e4447 100644
--- a/src/plugins/texteditor/textdocumentlayout.cpp
+++ b/src/plugins/texteditor/textdocumentlayout.cpp
@@ -4,9 +4,14 @@
#include "textdocumentlayout.h"
#include "fontsettings.h"
#include "textdocument.h"
+#include "texteditorplugin.h"
#include "texteditorsettings.h"
+
#include <utils/qtcassert.h>
+#include <utils/temporarydirectory.h>
+
#include <QDebug>
+#include <QTest>
namespace TextEditor {
@@ -667,11 +672,15 @@ TextMarks TextDocumentLayout::documentClosing()
return marks;
}
-void TextDocumentLayout::documentAboutToReload()
+void TextDocumentLayout::documentAboutToReload(TextDocument *baseTextDocument)
{
m_reloadMarks = documentClosing();
- for (TextMark *mark : std::as_const(m_reloadMarks))
- mark->setDeleteCallback([this, mark] { m_reloadMarks.removeOne(mark); });
+ for (TextMark *mark : std::as_const(m_reloadMarks)) {
+ mark->setDeleteCallback([this, mark, baseTextDocument] {
+ baseTextDocument->removeMarkFromMarksCache(mark);
+ m_reloadMarks.removeOne(mark);
+ });
+ }
}
void TextDocumentLayout::documentReloaded(TextDocument *baseTextDocument)
@@ -860,4 +869,23 @@ TextSuggestion::TextSuggestion()
TextSuggestion::~TextSuggestion() = default;
+#ifdef WITH_TESTS
+
+void Internal::TextEditorPlugin::testDeletingMarkOnReload()
+{
+ auto doc = new TextDocument();
+ doc->setFilePath(Utils::TemporaryDirectory::masterDirectoryFilePath() / "TestMarkDoc.txt");
+ doc->setPlainText("asd");
+ auto documentLayout = qobject_cast<TextDocumentLayout *>(doc->document()->documentLayout());
+ QVERIFY(documentLayout);
+ auto mark = new TextMark(doc, 1, TextMarkCategory{"testMark","testMark"});
+ QVERIFY(doc->marks().contains(mark));
+ documentLayout->documentAboutToReload(doc); // removes text marks non-permanently
+ delete mark;
+ documentLayout->documentReloaded(doc); // re-adds text marks
+ QVERIFY(!doc->marks().contains(mark));
+}
+
+#endif
+
} // namespace TextEditor
diff --git a/src/plugins/texteditor/textdocumentlayout.h b/src/plugins/texteditor/textdocumentlayout.h
index 33387093da..427500a832 100644
--- a/src/plugins/texteditor/textdocumentlayout.h
+++ b/src/plugins/texteditor/textdocumentlayout.h
@@ -246,7 +246,7 @@ public:
QRectF blockBoundingRect(const QTextBlock &block) const override;
TextMarks documentClosing();
- void documentAboutToReload();
+ void documentAboutToReload(TextDocument *baseTextDocument);
void documentReloaded(TextDocument *baseextDocument);
void updateMarksLineNumber();
void updateMarksBlock(const QTextBlock &block);
diff --git a/src/plugins/texteditor/texteditorplugin.h b/src/plugins/texteditor/texteditorplugin.h
index 3df46c0b57..f572506195 100644
--- a/src/plugins/texteditor/texteditorplugin.h
+++ b/src/plugins/texteditor/texteditorplugin.h
@@ -40,6 +40,8 @@ private slots:
void testFormatting_data();
void testFormatting();
+
+ void testDeletingMarkOnReload();
#endif
};