summaryrefslogtreecommitdiff
path: root/src/completion.hh
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2016-02-09 22:50:10 +0000
committerMaxime Coste <frrrwww@gmail.com>2016-02-09 22:50:10 +0000
commit87769c9b03b516053249f86355a4e503a529c659 (patch)
tree80521de80ecef7c95b40297265cb7d77941e2fa6 /src/completion.hh
parentc0e4eca982078c21f7cf4962b5b38119a888c777 (diff)
Migrate most completion to ranked match
Diffstat (limited to 'src/completion.hh')
-rw-r--r--src/completion.hh43
1 files changed, 13 insertions, 30 deletions
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 <functional>
+#include <algorithm>
#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<typename Container>
+CandidateList complete(StringView prefix, ByteCount cursor_pos,
+ const Container& container)
{
- template<typename Container, typename Func>
- 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<typename Container, typename Func, typename... Rest>
- void do_matches(Container&& container, StringView prefix,
- CandidateList& res, Func match_func, Rest... rest)
+ prefix = prefix.substr(0, cursor_pos);
+ Vector<RankedMatch> 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<typename Container, typename... MatchFunc>
-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<typename Container>
-CandidateList complete(StringView prefix, ByteCount cursor_pos,
- const Container& container)
-{
- return complete(prefix, cursor_pos, container, prefix_match);
-}
-
}
#endif // completion_hh_INCLUDED