diff options
Diffstat (limited to 'src/gui/text/qsyntaxhighlighter.cpp')
-rw-r--r-- | src/gui/text/qsyntaxhighlighter.cpp | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/gui/text/qsyntaxhighlighter.cpp b/src/gui/text/qsyntaxhighlighter.cpp index fcda17605f..cf584f6980 100644 --- a/src/gui/text/qsyntaxhighlighter.cpp +++ b/src/gui/text/qsyntaxhighlighter.cpp @@ -45,6 +45,7 @@ #include <private/qtextdocument_p.h> #include <qtextlayout.h> #include <qpointer.h> +#include <qscopedvaluerollback.h> #include <qtextobject.h> #include <qtextcursor.h> #include <qdebug.h> @@ -68,14 +69,14 @@ public: void reformatBlocks(int from, int charsRemoved, int charsAdded); void reformatBlock(const QTextBlock &block); - inline void rehighlight(QTextCursor &cursor, QTextCursor::MoveOperation operation) { - inReformatBlocks = true; + inline void rehighlight(QTextCursor &cursor, QTextCursor::MoveOperation operation) + { + QScopedValueRollback<bool> bg(inReformatBlocks, true); cursor.beginEditBlock(); int from = cursor.position(); cursor.movePosition(operation); reformatBlocks(from, 0, cursor.position() - from); cursor.endEditBlock(); - inReformatBlocks = false; } inline void _q_delayedRehighlight() { @@ -157,14 +158,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; @@ -299,7 +298,7 @@ void QSyntaxHighlighterPrivate::reformatBlock(const QTextBlock &block) QSyntaxHighlighter::QSyntaxHighlighter(QObject *parent) : QObject(*new QSyntaxHighlighterPrivate, parent) { - if (parent->inherits("QTextEdit")) { + if (parent && parent->inherits("QTextEdit")) { QTextDocument *doc = parent->property("document").value<QTextDocument *>(); if (doc) setDocument(doc); @@ -346,8 +345,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())); + } } } @@ -376,6 +377,7 @@ void QSyntaxHighlighter::rehighlight() QTextCursor cursor(d->doc); d->rehighlight(cursor, QTextCursor::End); + d->rehighlightPending = false; // user manually did a full rehighlight } /*! |