TextEditor: add convenience function to clear additional highlights

Change-Id: Ie3cf6e7f9fb37f3e2270abaf5fca412e7a4a40da
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
David Schulz
2019-06-12 13:15:48 +02:00
parent aa94b6f0c8
commit cea75f3ac4
4 changed files with 24 additions and 6 deletions

View File

@@ -180,8 +180,7 @@ void CppEditorDocument::applyFontSettings()
// Clear all additional formats since they may have changed // Clear all additional formats since they may have changed
QTextBlock b = document()->firstBlock(); QTextBlock b = document()->firstBlock();
while (b.isValid()) { while (b.isValid()) {
QVector<QTextLayout::FormatRange> noFormats; highlighter->clearExtraFormats(b);
highlighter->setExtraFormats(b, noFormats);
b = b.next(); b = b.next();
} }
} }

View File

@@ -86,8 +86,7 @@ void SemanticHighlighter::incrementalApplyExtraAdditionalFormats(
// clear formats of blocks until blockNumber // clear formats of blocks until blockNumber
while (currentBlockNumber < blockNumber) { while (currentBlockNumber < blockNumber) {
QVector<QTextLayout::FormatRange> noFormats; highlighter->clearExtraFormats(b);
highlighter->setExtraFormats(b, noFormats);
b = b.next(); b = b.next();
++currentBlockNumber; ++currentBlockNumber;
} }
@@ -142,8 +141,7 @@ void SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd(
QTextBlock b = doc->findBlockByNumber(firstBlockToClear); QTextBlock b = doc->findBlockByNumber(firstBlockToClear);
while (b.isValid()) { while (b.isValid()) {
QVector<QTextLayout::FormatRange> noFormats; highlighter->clearExtraFormats(b);
highlighter->setExtraFormats(b, noFormats);
b = b.next(); b = b.next();
} }
} }

View File

@@ -701,6 +701,26 @@ void SyntaxHighlighter::setExtraFormats(const QTextBlock &block,
d->inReformatBlocks = wasInReformatBlocks; 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<QTextLayout::FormatRange> 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 /* Generate at least n different colors for highlighting, excluding background
* color. */ * color. */

View File

@@ -64,6 +64,7 @@ public:
QTextDocument *document() const; QTextDocument *document() const;
void setExtraFormats(const QTextBlock &block, QVector<QTextLayout::FormatRange> &formats); void setExtraFormats(const QTextBlock &block, QVector<QTextLayout::FormatRange> &formats);
void clearExtraFormats(const QTextBlock &block);
static QList<QColor> generateColors(int n, const QColor &background); static QList<QColor> generateColors(int n, const QColor &background);