-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathcompletion.hh
96 lines (76 loc) · 2.48 KB
/
completion.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef completion_hh_INCLUDED
#define completion_hh_INCLUDED
#include <algorithm>
#include "units.hh"
#include "string.hh"
#include "vector.hh"
#include "ranked_match.hh"
namespace Kakoune
{
class Context;
class Regex;
using CandidateList = Vector<String, MemoryDomain::Completion>;
struct Completions
{
enum class Flags
{
None = 0,
Quoted = 0b1,
Menu = 0b10,
NoEmpty = 0b100
};
constexpr friend bool with_bit_ops(Meta::Type<Flags>) { return true; }
CandidateList candidates;
ByteCount start;
ByteCount end;
Flags flags = Flags::None;
Completions()
: start(0), end(0) {}
Completions(ByteCount start, ByteCount end)
: start(start), end(end) {}
Completions(ByteCount start, ByteCount end, CandidateList candidates, Flags flags = Flags::None)
: candidates(std::move(candidates)), start(start), end(end), flags{flags} {}
};
inline Completions complete_nothing(const Context&, StringView, ByteCount cursor_pos)
{
return {cursor_pos, cursor_pos};
}
enum class FilenameFlags
{
None = 0,
OnlyDirectories = 1 << 0,
Expand = 1 << 1
};
constexpr bool with_bit_ops(Meta::Type<FilenameFlags>) { return true; }
CandidateList complete_filename(StringView prefix, const Regex& ignore_regex,
ByteCount cursor_pos = -1,
FilenameFlags flags = FilenameFlags::None);
CandidateList complete_command(StringView prefix, ByteCount cursor_pos = -1);
Completions shell_complete(const Context& context, StringView, ByteCount cursor_pos);
inline Completions offset_pos(Completions completion, ByteCount offset)
{
return {completion.start + offset, completion.end + offset,
std::move(completion.candidates), completion.flags};
}
template<typename Container>
CandidateList complete(StringView query, ByteCount cursor_pos,
const Container& container)
{
using std::begin;
static_assert(not std::is_same<decltype(*begin(container)), String>::value,
"complete require long lived strings, not temporaries");
query = query.substr(0, cursor_pos);
Vector<RankedMatch> matches;
for (const auto& str : container)
{
if (RankedMatch match{str, query})
matches.push_back(match);
}
std::sort(matches.begin(), matches.end());
CandidateList res;
for (auto& m : matches)
res.push_back(m.candidate().str());
return res;
}
}
#endif // completion_hh_INCLUDED