From 47cd1def7a7a9909a3cfc2f21f9b8a8631600a5a Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Fri, 24 Sep 2021 12:08:33 +0200 Subject: [PATCH] TextEditor: Provide a function for inserting a parenthesis ... into a sorted list. Change-Id: Ibc39a345425945437cc8b8d9237589746143b2d9 Reviewed-by: David Schulz --- src/plugins/cppeditor/cpphighlighter.cpp | 6 +----- src/plugins/cppeditor/semantichighlighter.cpp | 8 +------- src/plugins/texteditor/textdocumentlayout.cpp | 7 +++++++ src/plugins/texteditor/textdocumentlayout.h | 4 ++-- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp index c9c2dfb9562..0f1a1b2dc36 100644 --- a/src/plugins/cppeditor/cpphighlighter.cpp +++ b/src/plugins/cppeditor/cpphighlighter.cpp @@ -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; diff --git a/src/plugins/cppeditor/semantichighlighter.cpp b/src/plugins/cppeditor/semantichighlighter.cpp index 9a7764e1344..830efb08675 100644 --- a/src/plugins/cppeditor/semantichighlighter.cpp +++ b/src/plugins/cppeditor/semantichighlighter.cpp @@ -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); diff --git a/src/plugins/texteditor/textdocumentlayout.cpp b/src/plugins/texteditor/textdocumentlayout.cpp index 111a47ca067..38a367cb89f 100644 --- a/src/plugins/texteditor/textdocumentlayout.cpp +++ b/src/plugins/texteditor/textdocumentlayout.cpp @@ -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 diff --git a/src/plugins/texteditor/textdocumentlayout.h b/src/plugins/texteditor/textdocumentlayout.h index 69b0159860e..ce86ad42961 100644 --- a/src/plugins/texteditor/textdocumentlayout.h +++ b/src/plugins/texteditor/textdocumentlayout.h @@ -38,8 +38,6 @@ #include namespace TextEditor { -struct Parenthesis; -using Parentheses = QVector; struct TEXTEDITOR_EXPORT Parenthesis { @@ -55,6 +53,8 @@ struct TEXTEDITOR_EXPORT Parenthesis bool operator==(const Parenthesis &other) const; }; +using Parentheses = QVector; +TEXTEDITOR_EXPORT void insertSorted(Parentheses &list, const Parenthesis &elem); TEXTEDITOR_EXPORT QDebug operator<<(QDebug debug, const Parenthesis &parenthesis);