summaryrefslogtreecommitdiff
path: root/src/completion.hh
blob: 50ce66075fd1df4a8e77f5325110992ac0cabfca (plain)
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
#ifndef completion_hh_INCLUDED
#define completion_hh_INCLUDED

#include <functional>

#include "units.hh"
#include "string.hh"
#include "vector.hh"

namespace Kakoune
{

class Context;

using CandidateList = Vector<String>;

struct Completions
{
    CandidateList candidates;
    ByteCount start;
    ByteCount end;

    Completions()
        : start(0), end(0) {}

    Completions(ByteCount start, ByteCount end)
        : start(start), end(end) {}

    Completions(ByteCount start, ByteCount end, CandidateList candidates)
        : start(start), end(end), candidates(std::move(candidates)) {}
};

enum class CompletionFlags
{
    None,
    Fast
};
using Completer = std::function<Completions (const Context&, CompletionFlags,
                                             StringView, ByteCount)>;

inline Completions complete_nothing(const Context& context, CompletionFlags,
                                    StringView, ByteCount cursor_pos)
{
    return Completions(cursor_pos, cursor_pos);
}

Completions shell_complete(const Context& context, CompletionFlags,
                           StringView, ByteCount cursor_pos);

inline Completions offset_pos(Completions completion, ByteCount offset)
{
    return { completion.start + offset, completion.end + offset,
             std::move(completion.candidates) };
}

namespace detail
{
    template<typename Str, typename Func>
    void do_matches(Str&& str, StringView prefix,
                    CandidateList* res, Func match_func)
    {
        if (match_func(str, prefix))
            res->push_back(str);
    }

    template<typename Str, typename Func, typename... Rest>
    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<typename Container, typename... MatchFunc>
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<typename Container>
CandidateList complete(StringView prefix, ByteCount cursor_pos,
                       const Container& container)
{
    return complete(prefix, cursor_pos, container, prefix_match);
}

}
#endif // completion_hh_INCLUDED