summaryrefslogtreecommitdiff
path: root/src/insert_completer.cc
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2018-04-15 13:20:13 +1000
committerMaxime Coste <mawww@kakoune.org>2018-04-19 07:58:42 +1000
commite207bd30d4007c9031bd639f8232adc8dc4dce25 (patch)
tree263da55b52b6db7c31d365c67a41757c8c2b5f49 /src/insert_completer.cc
parentc2759ac526e0b99f572d91c5ad473de43e7eb04e (diff)
Extract a for_n_best algorithm from completion function
Provide the heap based n-best algorithm through a nice interface.
Diffstat (limited to 'src/insert_completer.cc')
-rw-r--r--src/insert_completer.cc37
1 files changed, 17 insertions, 20 deletions
diff --git a/src/insert_completer.cc b/src/insert_completer.cc
index 1785648c..225ac5c9 100644
--- a/src/insert_completer.cc
+++ b/src/insert_completer.cc
@@ -177,30 +177,27 @@ InsertCompletion complete_word(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));
- while (candidates.size() < max_count and first != last)
- {
- if (candidates.empty() or candidates.back().completion != first->candidate())
- {
- DisplayLine menu_entry;
- if (other_buffers && first->buffer)
- {
- const auto pad_len = longest + 1 - first->candidate().char_length();
- menu_entry.push_back(first->candidate().str());
- menu_entry.push_back(String{' ', pad_len});
- menu_entry.push_back({ first->buffer->display_name(), faces["MenuInfo"] });
- }
- else
- menu_entry.push_back(first->candidate().str());
- candidates.push_back({first->candidate().str(), "", std::move(menu_entry)});
+ for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; },
+ [&](RankedMatchAndBuffer& m) {
+ if (not candidates.empty() and candidates.back().completion == m.candidate())
+ return false;
+ DisplayLine menu_entry;
+ if (other_buffers && m.buffer)
+ {
+ const auto pad_len = longest + 1 - m.candidate().char_length();
+ menu_entry.push_back(m.candidate().str());
+ menu_entry.push_back(String{' ', pad_len});
+ menu_entry.push_back({ m.buffer->display_name(), faces["MenuInfo"] });
}
- std::pop_heap(first, last--, greater);
- }
+ else
+ menu_entry.push_back(m.candidate().str());
+
+ candidates.push_back({m.candidate().str(), "", std::move(menu_entry)});
+ return true;
+ });
return { std::move(candidates), word_begin, cursor_pos, buffer.timestamp() };
}