diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index 7cfb9608bbd3f85329a46f1a770d4a8316a03967..1924aa45774b3046444624e37d915713811eae92 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -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(); } diff --git a/src/plugins/texteditor/semantichighlighter.cpp b/src/plugins/texteditor/semantichighlighter.cpp index 75dfcd3adc1e4a4c7720287d465ae0adf5b1f40a..14c869c88c8244f18ab5f417ce706e38e94c71df 100644 --- a/src/plugins/texteditor/semantichighlighter.cpp +++ b/src/plugins/texteditor/semantichighlighter.cpp @@ -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(); } } diff --git a/src/plugins/texteditor/syntaxhighlighter.cpp b/src/plugins/texteditor/syntaxhighlighter.cpp index 90f2e272e091d574fea37bd20a27e0fbdb0c0d95..1a2776dc76e37ae1a5aeec2dde06096633c4352b 100644 --- a/src/plugins/texteditor/syntaxhighlighter.cpp +++ b/src/plugins/texteditor/syntaxhighlighter.cpp @@ -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; } diff --git a/src/plugins/texteditor/syntaxhighlighter.h b/src/plugins/texteditor/syntaxhighlighter.h index 2f3ad075ae8fb7814c856c5310122da6bfc99a6b..8cee09a209ea11124d22e74a73432583181aaac2 100644 --- a/src/plugins/texteditor/syntaxhighlighter.h +++ b/src/plugins/texteditor/syntaxhighlighter.h @@ -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);