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
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,
const QList<QTextLayout::FormatRange> &formats)
const QList<QTextLayout::FormatRange> &fmts)
{
// qDebug() << "setAdditionalFormats() on block" << block.blockNumber();
......@@ -679,42 +684,48 @@ void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block,
if (block.layout() == 0)
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();
while (it != all.end()) {
if (it->format.property(QTextFormat::UserProperty).toBool()) {
if (skip < formats.size()
&& it->start == formats.at(skip).start
&& it->length == formats.at(skip).length
&& it->format == formats.at(skip).format) {
++skip;
++it;
} else {
it = all.erase(it);
modified = true;
}
} else {
++it;
}
qSort(previousSemanticFormats.begin(), previousSemanticFormats.end(), byStartOfRange);
foreach (QTextLayout::FormatRange r, formats) {
r.format.setProperty(QTextFormat::UserProperty, true);
formatsToApply.append(r);
}
if (!modified && skip == formats.length())
return; // skip'em all
if (formats.size() == previousSemanticFormats.size()) {
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) {
QTextLayout::FormatRange range = formats.at(i);
range.format.setProperty(QTextFormat::UserProperty, true);
all.append(range);
if (range.start != previousRange.start ||
range.length != previousRange.length ||
range.format != previousRange.format)
break;
}
if (index == formats.size())
return;
}
bool wasInReformatBlocks = d->inReformatBlocks;
d->inReformatBlocks = true;
block.layout()->setAdditionalFormats(all);
block.layout()->setAdditionalFormats(formatsToApply);
document()->markContentsDirty(block.position(), block.length()-1);
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