Skip to content

Commit 87769c9

Browse files
committed
Migrate most completion to ranked match
1 parent c0e4eca commit 87769c9

File tree

3 files changed

+16
-43
lines changed

3 files changed

+16
-43
lines changed

src/client_manager.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ CandidateList ClientManager::complete_client_name(StringView prefix,
176176
ByteCount cursor_pos) const
177177
{
178178
auto c = transformed(m_clients, [](const std::unique_ptr<Client>& c){ return c->context().name(); });
179-
return complete(prefix, cursor_pos, c, prefix_match, subsequence_match);
179+
return complete(prefix, cursor_pos, c);
180180
}
181181

182182
}

src/commands.cc

+2-12
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,8 @@ const PerArgumentCommandCompleter filename_completer({
6060

6161
static CandidateList complete_buffer_name(StringView prefix, ByteCount cursor_pos)
6262
{
63-
prefix = prefix.substr(0, cursor_pos);
64-
Vector<RankedMatch> matches;
65-
for (auto& buffer : BufferManager::instance())
66-
{
67-
if (RankedMatch match{buffer->display_name(), prefix})
68-
matches.push_back(match);
69-
}
70-
std::sort(matches.begin(), matches.end());
71-
CandidateList res;
72-
for (auto& m : matches)
73-
res.push_back(m.candidate().str());
74-
return res;
63+
auto c = transformed(BufferManager::instance(), [](const SafePtr<Buffer>& b){ return b->display_name(); });
64+
return complete(prefix, cursor_pos, c);
7565
}
7666

7767
const PerArgumentCommandCompleter buffer_completer({

src/completion.hh

+13-30
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#define completion_hh_INCLUDED
33

44
#include <functional>
5+
#include <algorithm>
56

67
#include "units.hh"
78
#include "string.hh"
89
#include "vector.hh"
10+
#include "ranked_match.hh"
911

1012
namespace Kakoune
1113
{
@@ -53,42 +55,23 @@ inline Completions offset_pos(Completions completion, ByteCount offset)
5355
std::move(completion.candidates) };
5456
}
5557

56-
namespace detail
58+
template<typename Container>
59+
CandidateList complete(StringView prefix, ByteCount cursor_pos,
60+
const Container& container)
5761
{
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)
7065
{
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);
7468
}
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());
8170
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());
8373
return res;
8474
}
8575

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-
9376
}
9477
#endif // completion_hh_INCLUDED

0 commit comments

Comments
 (0)