From 87769c9b03b516053249f86355a4e503a529c659 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 9 Feb 2016 22:50:10 +0000 Subject: Migrate most completion to ranked match --- src/completion.hh | 43 +++++++++++++------------------------------ 1 file changed, 13 insertions(+), 30 deletions(-) (limited to 'src/completion.hh') diff --git a/src/completion.hh b/src/completion.hh index b6e53df3..47b08c73 100644 --- a/src/completion.hh +++ b/src/completion.hh @@ -2,10 +2,12 @@ #define completion_hh_INCLUDED #include +#include #include "units.hh" #include "string.hh" #include "vector.hh" +#include "ranked_match.hh" namespace Kakoune { @@ -53,42 +55,23 @@ inline Completions offset_pos(Completions completion, ByteCount offset) std::move(completion.candidates) }; } -namespace detail +template +CandidateList complete(StringView prefix, ByteCount cursor_pos, + const Container& container) { - template - void do_matches(Container&& container, StringView prefix, - CandidateList& res, Func match_func) - { - for (auto&& elem : container) - if (match_func(elem, prefix)) - res.push_back(elem); - } - - template - void do_matches(Container&& container, StringView prefix, - CandidateList& res, Func match_func, Rest... rest) + prefix = prefix.substr(0, cursor_pos); + Vector matches; + for (const auto& str : container) { - do_matches(container, prefix, res, match_func); - if (res.empty()) - do_matches(container, prefix, res, rest...); + if (RankedMatch match{str, prefix}) + matches.push_back(match); } -} - -template -CandidateList complete(StringView prefix, ByteCount cursor_pos, - const Container& container, MatchFunc... match_func) -{ + std::sort(matches.begin(), matches.end()); CandidateList res; - detail::do_matches(container, prefix.substr(0, cursor_pos), res, match_func...); + for (auto& m : matches) + res.push_back(m.candidate().str()); return res; } -template -CandidateList complete(StringView prefix, ByteCount cursor_pos, - const Container& container) -{ - return complete(prefix, cursor_pos, container, prefix_match); -} - } #endif // completion_hh_INCLUDED -- cgit v1.2.3