summaryrefslogtreecommitdiff
path: root/src/insert_completer.cc
diff options
context:
space:
mode:
authorJohannes Altmanninger <aclopte@gmail.com>2023-12-02 09:31:48 +0100
committerJohannes Altmanninger <aclopte@gmail.com>2023-12-02 09:57:15 +0100
commitd6215dc25d02d1f8d958d3f5ee5c2d8b39d48e77 (patch)
tree8100a4752804f3ecf6e7e7d17b5dce13e13d5aa7 /src/insert_completer.cc
parent84ecd41da1368b4f9f20b08dfb5ced57241ec3f3 (diff)
Reuse for_n_best when sorting values from complete options
While at it, remove a needless reserve() call and reserve an extra slot because "InsertCompleter::try_complete" might add one more element.
Diffstat (limited to 'src/insert_completer.cc')
-rw-r--r--src/insert_completer.cc27
1 files changed, 13 insertions, 14 deletions
diff --git a/src/insert_completer.cc b/src/insert_completer.cc
index 967131d9..c43d9761 100644
--- a/src/insert_completer.cc
+++ b/src/insert_completer.cc
@@ -164,7 +164,7 @@ InsertCompletion complete_word(const SelectionList& sels,
constexpr size_t max_count = 100;
// Gather best max_count matches
InsertCompletion::CandidateList candidates;
- candidates.reserve(std::min(matches.size(), max_count));
+ candidates.reserve(std::min(matches.size(), max_count) + 1);
for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; },
[&](RankedMatchAndBuffer& m) {
@@ -314,20 +314,19 @@ InsertCompletion complete_option(const SelectionList& sels,
constexpr size_t max_count = 100;
// Gather best max_count matches
- auto greater = [](auto& lhs, auto& rhs) { return rhs < lhs; };
- auto first = matches.begin(), last = matches.end();
- std::make_heap(first, last, greater);
InsertCompletion::CandidateList candidates;
- candidates.reserve(std::min(matches.size(), max_count));
- candidates.reserve(matches.size());
- while (candidates.size() < max_count and first != last)
- {
- if (candidates.empty() or candidates.back().completion != first->candidate()
- or candidates.back().on_select != first->on_select)
- candidates.push_back({ first->candidate().str(), first->on_select.str(),
- std::move(first->menu_entry) });
- std::pop_heap(first, last--, greater);
- }
+ candidates.reserve(std::min(matches.size(), max_count) + 1);
+
+ for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; },
+ [&](RankedMatchAndInfo& m) {
+ if (not candidates.empty()
+ and candidates.back().completion == m.candidate()
+ and candidates.back().on_select == m.on_select)
+ return false;
+ candidates.push_back({ m.candidate().str(), m.on_select.str(),
+ std::move(m.menu_entry) });
+ return true;
+ });
auto end = cursor_pos;
if (match[3].matched)