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)