|
2 | 2 | #define completion_hh_INCLUDED
|
3 | 3 |
|
4 | 4 | #include <functional>
|
| 5 | +#include <algorithm> |
5 | 6 |
|
6 | 7 | #include "units.hh"
|
7 | 8 | #include "string.hh"
|
8 | 9 | #include "vector.hh"
|
| 10 | +#include "ranked_match.hh" |
9 | 11 |
|
10 | 12 | namespace Kakoune
|
11 | 13 | {
|
@@ -53,42 +55,23 @@ inline Completions offset_pos(Completions completion, ByteCount offset)
|
53 | 55 | std::move(completion.candidates) };
|
54 | 56 | }
|
55 | 57 |
|
56 |
| -namespace detail |
| 58 | +template<typename Container> |
| 59 | +CandidateList complete(StringView prefix, ByteCount cursor_pos, |
| 60 | + const Container& container) |
57 | 61 | {
|
58 |
| - template<typename Container, typename Func> |
59 |
| - void do_matches(Container&& container, StringView prefix, |
60 |
| - CandidateList& res, Func match_func) |
61 |
| - { |
62 |
| - for (auto&& elem : container) |
63 |
| - if (match_func(elem, prefix)) |
64 |
| - res.push_back(elem); |
65 |
| - } |
66 |
| - |
67 |
| - template<typename Container, typename Func, typename... Rest> |
68 |
| - void do_matches(Container&& container, StringView prefix, |
69 |
| - CandidateList& res, Func match_func, Rest... rest) |
| 62 | + prefix = prefix.substr(0, cursor_pos); |
| 63 | + Vector<RankedMatch> matches; |
| 64 | + for (const auto& str : container) |
70 | 65 | {
|
71 |
| - do_matches(container, prefix, res, match_func); |
72 |
| - if (res.empty()) |
73 |
| - do_matches(container, prefix, res, rest...); |
| 66 | + if (RankedMatch match{str, prefix}) |
| 67 | + matches.push_back(match); |
74 | 68 | }
|
75 |
| -} |
76 |
| - |
77 |
| -template<typename Container, typename... MatchFunc> |
78 |
| -CandidateList complete(StringView prefix, ByteCount cursor_pos, |
79 |
| - const Container& container, MatchFunc... match_func) |
80 |
| -{ |
| 69 | + std::sort(matches.begin(), matches.end()); |
81 | 70 | CandidateList res;
|
82 |
| - detail::do_matches(container, prefix.substr(0, cursor_pos), res, match_func...); |
| 71 | + for (auto& m : matches) |
| 72 | + res.push_back(m.candidate().str()); |
83 | 73 | return res;
|
84 | 74 | }
|
85 | 75 |
|
86 |
| -template<typename Container> |
87 |
| -CandidateList complete(StringView prefix, ByteCount cursor_pos, |
88 |
| - const Container& container) |
89 |
| -{ |
90 |
| - return complete(prefix, cursor_pos, container, prefix_match); |
91 |
| -} |
92 |
| - |
93 | 76 | }
|
94 | 77 | #endif // completion_hh_INCLUDED
|
0 commit comments