From b6ff15aa757eb35fed5772d28e3c06b5e844a49e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 23 Dec 2014 13:54:09 +0000 Subject: Unify completion from container content logic --- src/completion.hh | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/completion.hh') diff --git a/src/completion.hh b/src/completion.hh index afaf6fbc..e978d0ac 100644 --- a/src/completion.hh +++ b/src/completion.hh @@ -53,5 +53,43 @@ inline Completions offset_pos(Completions completion, ByteCount offset) std::move(completion.candidates) }; } +namespace detail +{ + template + void do_matches(Str&& str, StringView prefix, + CandidateList* res, Func match_func) + { + if (match_func(str, prefix)) + res->push_back(str); + } + + template + void do_matches(Str&& str, StringView prefix, + CandidateList* res, Func match_func, Rest... rest) + { + do_matches(str, prefix, res, match_func); + do_matches(str, prefix, res+1, rest...); + } +} + +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); +} + +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