diff --git a/src/plugins/cppeditor/cppeditordocument.cpp b/src/plugins/cppeditor/cppeditordocument.cpp index f09e3807d34..c4af4b15595 100644 --- a/src/plugins/cppeditor/cppeditordocument.cpp +++ b/src/plugins/cppeditor/cppeditordocument.cpp @@ -180,8 +180,7 @@ void CppEditorDocument::applyFontSettings() // Clear all additional formats since they may have changed QTextBlock b = document()->firstBlock(); while (b.isValid()) { - QVector noFormats; - highlighter->setExtraFormats(b, noFormats); + highlighter->clearExtraFormats(b); b = b.next(); } } diff --git a/src/plugins/texteditor/semantichighlighter.cpp b/src/plugins/texteditor/semantichighlighter.cpp index fa388ac40e0..9dded606ba5 100644 --- a/src/plugins/texteditor/semantichighlighter.cpp +++ b/src/plugins/texteditor/semantichighlighter.cpp @@ -86,8 +86,7 @@ void SemanticHighlighter::incrementalApplyExtraAdditionalFormats( // clear formats of blocks until blockNumber while (currentBlockNumber < blockNumber) { - QVector noFormats; - highlighter->setExtraFormats(b, noFormats); + highlighter->clearExtraFormats(b); b = b.next(); ++currentBlockNumber; } @@ -142,8 +141,7 @@ void SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd( QTextBlock b = doc->findBlockByNumber(firstBlockToClear); while (b.isValid()) { - QVector noFormats; - highlighter->setExtraFormats(b, noFormats); + highlighter->clearExtraFormats(b); b = b.next(); } } diff --git a/src/plugins/texteditor/syntaxhighlighter.cpp b/src/plugins/texteditor/syntaxhighlighter.cpp index bd153e2143e..06802461350 100644 --- a/src/plugins/texteditor/syntaxhighlighter.cpp +++ b/src/plugins/texteditor/syntaxhighlighter.cpp @@ -701,6 +701,26 @@ void SyntaxHighlighter::setExtraFormats(const QTextBlock &block, d->inReformatBlocks = wasInReformatBlocks; } +void SyntaxHighlighter::clearExtraFormats(const QTextBlock &block) +{ + Q_D(SyntaxHighlighter); + + const int blockLength = block.length(); + if (block.layout() == nullptr || blockLength == 0) + return; + + const QVector formatsToApply + = Utils::filtered(block.layout()->formats(), [](const QTextLayout::FormatRange &r) { + return !r.format.hasProperty(QTextFormat::UserProperty); + }); + + bool wasInReformatBlocks = d->inReformatBlocks; + d->inReformatBlocks = true; + block.layout()->setFormats(formatsToApply); + document()->markContentsDirty(block.position(), blockLength - 1); + d->inReformatBlocks = wasInReformatBlocks; +} + /* Generate at least n different colors for highlighting, excluding background * color. */ diff --git a/src/plugins/texteditor/syntaxhighlighter.h b/src/plugins/texteditor/syntaxhighlighter.h index 38d75010656..f55b9f9f004 100644 --- a/src/plugins/texteditor/syntaxhighlighter.h +++ b/src/plugins/texteditor/syntaxhighlighter.h @@ -64,6 +64,7 @@ public: QTextDocument *document() const; void setExtraFormats(const QTextBlock &block, QVector &formats); + void clearExtraFormats(const QTextBlock &block); static QList generateColors(int n, const QColor &background);