TextEditor: Provide a function for inserting a parenthesis

... into a sorted list.

Change-Id: Ibc39a345425945437cc8b8d9237589746143b2d9
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2021-09-24 12:08:33 +02:00
parent 421e06393f
commit 47cd1def7a
4 changed files with 11 additions and 14 deletions

View File

@@ -94,11 +94,7 @@ void CppHighlighter::highlightBlock(const QString &text)
// Keep "semantic parentheses".
Parentheses parentheses = Utils::filtered(TextDocumentLayout::parentheses(currentBlock()),
[](const Parenthesis &p) { return p.source.isValid(); });
const auto insertParen = [&parentheses](const Parenthesis &p) {
const auto it = std::lower_bound(parentheses.begin(), parentheses.end(), p,
[](const auto &p1, const auto &p2) { return p1.pos < p2.pos; });
parentheses.insert(it, p);
};
const auto insertParen = [&parentheses](const Parenthesis &p) { insertSorted(parentheses, p); };
parentheses.reserve(5);
bool expectPreprocessorKeyword = false;

View File

@@ -210,13 +210,7 @@ void SemanticHighlighter::onHighlighterResultAvailable(int from, int to)
}
QTC_ASSERT(paren.pos != -1, continue);
paren.source = parenSource();
static const auto posCmp = [](const Parenthesis &p1, const Parenthesis &p2) {
return p1.pos < p2.pos;
};
const auto it = std::lower_bound(parentheses.second.begin(), parentheses.second.end(),
paren, posCmp);
parentheses.second.insert(it, paren);
insertSorted(parentheses.second, paren);
}
if (parentheses.first.isValid())
TextDocumentLayout::setParentheses(parentheses.first, parentheses.second);

View File

@@ -698,4 +698,11 @@ bool Parenthesis::operator==(const Parenthesis &other) const
return pos == other.pos && chr == other.chr && source == other.source && type == other.type;
}
void insertSorted(Parentheses &list, const Parenthesis &elem)
{
const auto it = std::lower_bound(list.begin(), list.end(), elem,
[](const auto &p1, const auto &p2) { return p1.pos < p2.pos; });
list.insert(it, elem);
}
} // namespace TextEditor

View File

@@ -38,8 +38,6 @@
#include <QPlainTextDocumentLayout>
namespace TextEditor {
struct Parenthesis;
using Parentheses = QVector<Parenthesis>;
struct TEXTEDITOR_EXPORT Parenthesis
{
@@ -55,6 +53,8 @@ struct TEXTEDITOR_EXPORT Parenthesis
bool operator==(const Parenthesis &other) const;
};
using Parentheses = QVector<Parenthesis>;
TEXTEDITOR_EXPORT void insertSorted(Parentheses &list, const Parenthesis &elem);
TEXTEDITOR_EXPORT QDebug operator<<(QDebug debug, const Parenthesis &parenthesis);