diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2018-11-06 12:08:05 +0100 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2018-11-08 16:38:27 +0000 |
commit | dec7961709c90f6977d2447f7fa6c6625af41cb2 (patch) | |
tree | 2b5a6c33fd369b3b0ea5192e8b1c3df0bfda44a1 | |
parent | bdebc90c2826866e4434a6429aa6f822ee3cb8f6 (diff) |
QSyntaxHighlighter: Delay all highlights until first rehighlight
When calling setDocument (directly or through the constructor) a delayed
rehighlight is initiated. Previously, if any text was changed before
this rehighlight could run it would cancel the rehighlight, even if the
changed text only caused a new block of text to be highlighted.
Fixes: QTBUG-71307
Change-Id: Ib09b664d90906f5b4427105f0e45469806f3a779
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@qt.io>
-rw-r--r-- | src/gui/text/qsyntaxhighlighter.cpp | 10 | ||||
-rw-r--r-- | tests/auto/gui/text/qsyntaxhighlighter/tst_qsyntaxhighlighter.cpp | 15 |
2 files changed, 20 insertions, 5 deletions
diff --git a/src/gui/text/qsyntaxhighlighter.cpp b/src/gui/text/qsyntaxhighlighter.cpp index fcda17605f..b09f8b565a 100644 --- a/src/gui/text/qsyntaxhighlighter.cpp +++ b/src/gui/text/qsyntaxhighlighter.cpp @@ -157,14 +157,12 @@ void QSyntaxHighlighterPrivate::applyFormatChanges() void QSyntaxHighlighterPrivate::_q_reformatBlocks(int from, int charsRemoved, int charsAdded) { - if (!inReformatBlocks) + if (!inReformatBlocks && !rehighlightPending) reformatBlocks(from, charsRemoved, charsAdded); } void QSyntaxHighlighterPrivate::reformatBlocks(int from, int charsRemoved, int charsAdded) { - rehighlightPending = false; - QTextBlock block = doc->findBlock(from); if (!block.isValid()) return; @@ -346,8 +344,10 @@ void QSyntaxHighlighter::setDocument(QTextDocument *doc) if (d->doc) { connect(d->doc, SIGNAL(contentsChange(int,int,int)), this, SLOT(_q_reformatBlocks(int,int,int))); - d->rehighlightPending = true; - QTimer::singleShot(0, this, SLOT(_q_delayedRehighlight())); + if (!d->doc->isEmpty()) { + d->rehighlightPending = true; + QTimer::singleShot(0, this, SLOT(_q_delayedRehighlight())); + } } } diff --git a/tests/auto/gui/text/qsyntaxhighlighter/tst_qsyntaxhighlighter.cpp b/tests/auto/gui/text/qsyntaxhighlighter/tst_qsyntaxhighlighter.cpp index 0a2e024701..9d6ce78b24 100644 --- a/tests/auto/gui/text/qsyntaxhighlighter/tst_qsyntaxhighlighter.cpp +++ b/tests/auto/gui/text/qsyntaxhighlighter/tst_qsyntaxhighlighter.cpp @@ -74,6 +74,7 @@ private slots: void emptyBlocks(); void setCharFormat(); void highlightOnInit(); + void highlightOnInitAndAppend(); void stopHighlightingWhenStateDoesNotChange(); void unindent(); void highlightToEndOfDocument(); @@ -265,6 +266,19 @@ void tst_QSyntaxHighlighter::highlightOnInit() QTRY_VERIFY(hl->highlighted); } +void tst_QSyntaxHighlighter::highlightOnInitAndAppend() +{ + cursor.insertText("Hello"); + cursor.insertBlock(); + cursor.insertText("World"); + + TestHighlighter *hl = new TestHighlighter(doc); + cursor.insertBlock(); + cursor.insertText("More text"); + QTRY_VERIFY(hl->highlighted); + QVERIFY(hl->highlightedText.endsWith(doc->toPlainText().remove(QLatin1Char('\n')))); +} + class StateTestHighlighter : public QSyntaxHighlighter { public: @@ -330,6 +344,7 @@ void tst_QSyntaxHighlighter::unindent() QCOMPARE(doc->toPlainText(), plainText); TestHighlighter *hl = new TestHighlighter(doc); + QTRY_VERIFY(hl->highlighted); hl->callCount = 0; cursor.movePosition(QTextCursor::Start); |