Skip to content

Commit

Permalink
Get rid of old data when HighlightedText::setText is called
Browse files Browse the repository at this point in the history
Don't always append data, resize-to-fit and then transform.
Sadly, QStringList has no resize, so we need to copy+for_each
there instead.

Fixes: #577
  • Loading branch information
milianw committed Jan 24, 2024
1 parent e16fb89 commit e398f8c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/models/highlightedtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class AnsiHighlightingImplementation : public HighlightingImplementation
class HighlightedLine
{
public:
HighlightedLine() = default;
HighlightedLine(HighlightingImplementation* highlighter, QString text)
: m_highlighter(highlighter)
, m_text(std::move(text))
Expand Down Expand Up @@ -227,7 +228,7 @@ class HighlightedLine
return layout;
}

HighlightingImplementation* m_highlighter;
HighlightingImplementation* m_highlighter = nullptr;
QString m_text;
mutable std::unique_ptr<QTextLayout> m_layout;
};
Expand Down Expand Up @@ -258,13 +259,13 @@ void HighlightedText::setText(const QStringList& text)
emit usesAnsiChanged(usesAnsi);
}

m_highlightedLines.reserve(text.size());
std::transform(text.cbegin(), text.cend(), std::back_inserter(m_highlightedLines), [this](const QString& text) {
m_highlightedLines.resize(text.size());
std::transform(text.cbegin(), text.cend(), m_highlightedLines.begin(), [this](const QString& text) {
return HighlightedLine {m_highlighter.get(), text};
});

m_cleanedLines.reserve(text.size());
std::transform(text.cbegin(), text.cend(), std::back_inserter(m_cleanedLines), Util::removeAnsi);
m_cleanedLines = text;
std::for_each(m_cleanedLines.begin(), m_cleanedLines.end(), Util::removeAnsi);
}

void HighlightedText::setDefinition(const KSyntaxHighlighting::Definition& definition)
Expand Down

0 comments on commit e398f8c

Please sign in to comment.