From 3bdc30e381c94bfc1da2b044562be88c4b4cf525 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 26 May 2015 18:38:48 +0100 Subject: [PATCH] Try matching functions one by one when completing Instead of running them all then deciding which to use. --- src/completion.hh | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/completion.hh b/src/completion.hh index 50ce6607..b6e53df3 100644 --- a/src/completion.hh +++ b/src/completion.hh @@ -55,20 +55,22 @@ inline Completions offset_pos(Completions completion, ByteCount offset) namespace detail { - template - void do_matches(Str&& str, StringView prefix, - CandidateList* res, Func match_func) + template + void do_matches(Container&& container, StringView prefix, + CandidateList& res, Func match_func) { - if (match_func(str, prefix)) - res->push_back(str); + for (auto&& elem : container) + if (match_func(elem, prefix)) + res.push_back(elem); } - template - void do_matches(Str&& str, StringView prefix, - CandidateList* res, Func match_func, Rest... rest) + template + void do_matches(Container&& container, StringView prefix, + CandidateList& res, Func match_func, Rest... rest) { - do_matches(str, prefix, res, match_func); - do_matches(str, prefix, res+1, rest...); + do_matches(container, prefix, res, match_func); + if (res.empty()) + do_matches(container, prefix, res, rest...); } } @@ -76,12 +78,9 @@ template CandidateList complete(StringView prefix, ByteCount cursor_pos, const Container& container, MatchFunc... match_func) { - CandidateList res[sizeof...(match_func)]; - auto real_prefix = prefix.substr(0, cursor_pos); - for (const auto& elem : container) - detail::do_matches(elem, real_prefix, res, match_func...); - auto it = find_if(res, [](CandidateList& c) { return not c.empty(); }); - return it == end(res) ? CandidateList{} : std::move(*it); + CandidateList res; + detail::do_matches(container, prefix.substr(0, cursor_pos), res, match_func...); + return res; } template