Skip to content
Snippets Groups Projects
Commit 972c75f9 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Alternative implementation of SyntaxHighlighter::setExtraAdditionalFormats().

Sort the semantic ranges before to compare them.
parent df1770ff
No related branches found
No related tags found
No related merge requests found
...@@ -665,8 +665,13 @@ QTextBlock SyntaxHighlighter::currentBlock() const ...@@ -665,8 +665,13 @@ QTextBlock SyntaxHighlighter::currentBlock() const
return d->currentBlock; return d->currentBlock;
} }
static bool byStartOfRange(const QTextLayout::FormatRange &range, const QTextLayout::FormatRange &other)
{
return range.start < other.start;
}
void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block, void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block,
const QList<QTextLayout::FormatRange> &formats) const QList<QTextLayout::FormatRange> &fmts)
{ {
// qDebug() << "setAdditionalFormats() on block" << block.blockNumber(); // qDebug() << "setAdditionalFormats() on block" << block.blockNumber();
...@@ -679,42 +684,48 @@ void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block, ...@@ -679,42 +684,48 @@ void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block,
if (block.layout() == 0) if (block.layout() == 0)
return; return;
QList<QTextLayout::FormatRange> all = block.layout()->additionalFormats(); QList<QTextLayout::FormatRange> formats = fmts;
qSort(formats.begin(), formats.end(), byStartOfRange);
bool modified = false; QList<QTextLayout::FormatRange> previousSemanticFormats;
QList<QTextLayout::FormatRange> formatsToApply;
int skip = 0; const QList<QTextLayout::FormatRange> all = block.layout()->additionalFormats();
foreach (const QTextLayout::FormatRange &r, all) {
if (r.format.hasProperty(QTextFormat::UserProperty))
previousSemanticFormats.append(r);
else
formatsToApply.append(r);
}
QList<QTextLayout::FormatRange>::Iterator it = all.begin(); qSort(previousSemanticFormats.begin(), previousSemanticFormats.end(), byStartOfRange);
while (it != all.end()) {
if (it->format.property(QTextFormat::UserProperty).toBool()) { foreach (QTextLayout::FormatRange r, formats) {
if (skip < formats.size() r.format.setProperty(QTextFormat::UserProperty, true);
&& it->start == formats.at(skip).start formatsToApply.append(r);
&& it->length == formats.at(skip).length
&& it->format == formats.at(skip).format) {
++skip;
++it;
} else {
it = all.erase(it);
modified = true;
}
} else {
++it;
}
} }
if (!modified && skip == formats.length()) if (formats.size() == previousSemanticFormats.size()) {
return; // skip'em all int index = 0;
for (; index != formats.size(); ++index) {
QTextLayout::FormatRange range = formats.at(index);
range.format.setProperty(QTextFormat::UserProperty, true);
const QTextLayout::FormatRange &previousRange = previousSemanticFormats.at(index);
for (int i = skip; i < formats.length(); ++i) { if (range.start != previousRange.start ||
QTextLayout::FormatRange range = formats.at(i); range.length != previousRange.length ||
range.format.setProperty(QTextFormat::UserProperty, true); range.format != previousRange.format)
all.append(range); break;
}
if (index == formats.size())
return;
} }
bool wasInReformatBlocks = d->inReformatBlocks; bool wasInReformatBlocks = d->inReformatBlocks;
d->inReformatBlocks = true; d->inReformatBlocks = true;
block.layout()->setAdditionalFormats(all); block.layout()->setAdditionalFormats(formatsToApply);
document()->markContentsDirty(block.position(), block.length()-1); document()->markContentsDirty(block.position(), block.length()-1);
d->inReformatBlocks = wasInReformatBlocks; d->inReformatBlocks = wasInReformatBlocks;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment