diff options
author | Roberto Raggi <roberto.raggi@nokia.com> | 2010-08-09 11:59:07 +0200 |
---|---|---|
committer | Roberto Raggi <roberto.raggi@nokia.com> | 2010-08-09 12:00:32 +0200 |
commit | 972c75f968027abe1b7e2dc1de2975bd3228398a (patch) | |
tree | 568c105cb70453ed954e5536c9d28b4afe029712 /src/plugins/texteditor/syntaxhighlighter.cpp | |
parent | df1770ff554dfa146367dc58a298af107cade180 (diff) |
Alternative implementation of SyntaxHighlighter::setExtraAdditionalFormats().
Sort the semantic ranges before to compare them.
Diffstat (limited to 'src/plugins/texteditor/syntaxhighlighter.cpp')
-rw-r--r-- | src/plugins/texteditor/syntaxhighlighter.cpp | 65 |
1 files changed, 38 insertions, 27 deletions
diff --git a/src/plugins/texteditor/syntaxhighlighter.cpp b/src/plugins/texteditor/syntaxhighlighter.cpp index c5eaf2c640..fbc67d42fc 100644 --- a/src/plugins/texteditor/syntaxhighlighter.cpp +++ b/src/plugins/texteditor/syntaxhighlighter.cpp @@ -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; } |