TextEditor: modernize SyntaxHighlighter::setExtraFormats

Change-Id: Iaebf30d56702ef90b610d2db03c7abb40d1d2bab
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
David Schulz
2019-06-12 13:43:05 +02:00
parent 514e3776a1
commit aa94b6f0c8

View File

@@ -667,15 +667,6 @@ static bool byStartOfRange(const QTextLayout::FormatRange &range, const QTextLay
void SyntaxHighlighter::setExtraFormats(const QTextBlock &block, void SyntaxHighlighter::setExtraFormats(const QTextBlock &block,
QVector<QTextLayout::FormatRange> &formats) QVector<QTextLayout::FormatRange> &formats)
{ {
// qDebug() << "setFormats() on block" << block.blockNumber();
// qDebug() << " is valid:" << (block.isValid() ? "Yes" : "No");
// qDebug() << " has layout:" << (block.layout() ? "Yes" : "No");
// if (block.layout()) qDebug() << " has text:" << (block.text().isEmpty() ? "No" : "Yes");
// for (int i = 0; i < overrides.count(); ++i)
// qDebug() << " from " << overrides.at(i).start << "length"
// << overrides.at(i).length
// << "color:" << overrides.at(i).format.foreground().color();
Q_D(SyntaxHighlighter); Q_D(SyntaxHighlighter);
const int blockLength = block.length(); const int blockLength = block.length();
@@ -687,34 +678,17 @@ void SyntaxHighlighter::setExtraFormats(const QTextBlock &block,
const QVector<QTextLayout::FormatRange> all = block.layout()->formats(); const QVector<QTextLayout::FormatRange> all = block.layout()->formats();
QVector<QTextLayout::FormatRange> previousSemanticFormats; QVector<QTextLayout::FormatRange> previousSemanticFormats;
QVector<QTextLayout::FormatRange> formatsToApply; QVector<QTextLayout::FormatRange> formatsToApply;
previousSemanticFormats.reserve(all.size()); std::tie(previousSemanticFormats, formatsToApply)
formatsToApply.reserve(all.size() + formats.size()); = Utils::partition(all, [](const QTextLayout::FormatRange &r) {
return r.format.hasProperty(QTextFormat::UserProperty);
});
for (auto &format : formats) for (auto &format : formats)
format.format.setProperty(QTextFormat::UserProperty, true); format.format.setProperty(QTextFormat::UserProperty, true);
foreach (const QTextLayout::FormatRange &r, all) {
if (r.format.hasProperty(QTextFormat::UserProperty))
previousSemanticFormats.append(r);
else
formatsToApply.append(r);
}
if (formats.size() == previousSemanticFormats.size()) { if (formats.size() == previousSemanticFormats.size()) {
Utils::sort(previousSemanticFormats, byStartOfRange); Utils::sort(previousSemanticFormats, byStartOfRange);
if (formats == previousSemanticFormats)
int index = 0;
for (; index != formats.size(); ++index) {
const QTextLayout::FormatRange &range = formats.at(index);
const QTextLayout::FormatRange &previousRange = previousSemanticFormats.at(index);
if (range.start != previousRange.start ||
range.length != previousRange.length ||
range.format != previousRange.format)
break;
}
if (index == formats.size())
return; return;
} }