Commit 97c6af94 authored by Erik Verbruggen's avatar Erik Verbruggen Committed by Erik Verbruggen

C++: optimize the semantic highlighting in the UI thread.

Done by reserving enough space in for the various QLists when created,
and by preventing unnecessary copying of lists.

Change-Id: I5a9f0f7042bbaa1cf5eb076b956c35f2191468c6
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent ff6e1008
......@@ -2008,7 +2008,8 @@ void CPPEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
// Clear all additional formats since they may have changed
QTextBlock b = document()->firstBlock();
while (b.isValid()) {
highlighter->setExtraAdditionalFormats(b, QList<QTextLayout::FormatRange>());
QList<QTextLayout::FormatRange> noFormats;
highlighter->setExtraAdditionalFormats(b, noFormats);
b = b.next();
}
......
......@@ -76,13 +76,15 @@ void TextEditor::SemanticHighlighter::incrementalApplyExtraAdditionalFormats(
// clear formats of blocks until blockNumber
while (currentBlockNumber < blockNumber) {
highlighter->setExtraAdditionalFormats(b, QList<QTextLayout::FormatRange>());
QList<QTextLayout::FormatRange> noFormats;
highlighter->setExtraAdditionalFormats(b, noFormats);
b = b.next();
++currentBlockNumber;
}
// collect all the formats for the current line
QList<QTextLayout::FormatRange> formats;
formats.reserve(to - from);
forever {
QTextLayout::FormatRange formatRange;
......@@ -131,7 +133,8 @@ void TextEditor::SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd(
QTextBlock b = doc->findBlockByNumber(firstBlockToClear);
while (b.isValid()) {
highlighter->setExtraAdditionalFormats(b, QList<QTextLayout::FormatRange>());
QList<QTextLayout::FormatRange> noFormats;
highlighter->setExtraAdditionalFormats(b, noFormats);
b = b.next();
}
}
......@@ -694,8 +694,10 @@ static bool byStartOfRange(const QTextLayout::FormatRange &range, const QTextLay
return range.start < other.start;
}
// The formats is passed in by reference in order to prevent unnecessary copying of its items.
// After this method returns, the list is modified, and should be considered invalidated!
void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block,
const QList<QTextLayout::FormatRange> &fmts)
QList<QTextLayout::FormatRange> &formats)
{
// qDebug() << "setAdditionalFormats() on block" << block.blockNumber();
// qDebug() << " is valid:" << (block.isValid() ? "Yes" : "No");
......@@ -708,21 +710,21 @@ void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block,
// << "color:" << overrides.at(i).format.foreground().color();
Q_D(SyntaxHighlighter);
if (block.layout() == 0 || block.text().isEmpty())
const int blockLength = block.length();
if (block.layout() == 0 || blockLength == 0)
return;
QList<QTextLayout::FormatRange> formats;
formats.reserve(fmts.size());
foreach (QTextLayout::FormatRange r, fmts) {
r.format.setProperty(QTextFormat::UserProperty, true);
formats.append(r);
}
qSort(formats.begin(), formats.end(), byStartOfRange);
const QList<QTextLayout::FormatRange> all = block.layout()->additionalFormats();
QList<QTextLayout::FormatRange> previousSemanticFormats;
QList<QTextLayout::FormatRange> formatsToApply;
previousSemanticFormats.reserve(all.size());
formatsToApply.reserve(all.size() + formats.size());
for (int i = 0, ei = formats.size(); i < ei; ++i)
formats[i].format.setProperty(QTextFormat::UserProperty, true);
const QList<QTextLayout::FormatRange> all = block.layout()->additionalFormats();
foreach (const QTextLayout::FormatRange &r, all) {
if (r.format.hasProperty(QTextFormat::UserProperty))
previousSemanticFormats.append(r);
......@@ -753,7 +755,7 @@ void SyntaxHighlighter::setExtraAdditionalFormats(const QTextBlock& block,
bool wasInReformatBlocks = d->inReformatBlocks;
d->inReformatBlocks = true;
block.layout()->setAdditionalFormats(formatsToApply);
document()->markContentsDirty(block.position(), block.length()-1);
document()->markContentsDirty(block.position(), blockLength - 1);
d->inReformatBlocks = wasInReformatBlocks;
}
......
......@@ -64,7 +64,7 @@ public:
void setDocument(QTextDocument *doc);
QTextDocument *document() const;
void setExtraAdditionalFormats(const QTextBlock& block, const QList<QTextLayout::FormatRange> &formats);
void setExtraAdditionalFormats(const QTextBlock& block, QList<QTextLayout::FormatRange> &formats);
static QList<QColor> generateColors(int n, const QColor &background);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment