summaryrefslogtreecommitdiff
path: root/src/insert_completer.hh
blob: 713cdedb82c42de1702871d805b9ca6aac3dde04 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef insert_completer_hh_INCLUDED
#define insert_completer_hh_INCLUDED

#include "option_manager.hh"
#include "option.hh"
#include "display_buffer.hh"
#include "vector.hh"
#include "utils.hh"

#include "optional.hh"

namespace Kakoune
{

struct SelectionList;
struct Key;
class FaceRegistry;

struct InsertCompleterDesc
{
    enum Mode
    {
        Word,
        Option,
        Filename,
        Line
    };

    bool operator==(const InsertCompleterDesc& other) const = default;

    Mode mode;
    Optional<String> param;
};

using InsertCompleterDescList = Vector<InsertCompleterDesc, MemoryDomain::Options>;

String option_to_string(const InsertCompleterDesc& opt);
InsertCompleterDesc option_from_string(Meta::Type<InsertCompleterDesc>, StringView str);

inline StringView option_type_name(Meta::Type<InsertCompleterDesc>)
{
    return "completer";
}

using CompletionCandidate = std::tuple<String, String, String>;
using CompletionList = PrefixedList<String, CompletionCandidate>;

inline StringView option_type_name(Meta::Type<CompletionList>)
{
    return "completions";
}

struct InsertCompletion
{
    struct Candidate
    {
        String completion;
        String on_select;
        DisplayLine menu_entry;

        bool operator==(const Candidate& other) const { return completion == other.completion; }
        auto operator<=>(const Candidate& other) const { return completion <=> other.completion; }
    };
    using CandidateList = Vector<Candidate, MemoryDomain::Completion>;

    CandidateList candidates;
    BufferCoord begin;
    BufferCoord end;
    size_t timestamp = 0;

    bool is_valid() const { return not candidates.empty(); }
};

class InsertCompleter : public OptionManagerWatcher
{
public:
    InsertCompleter(Context& context);
    InsertCompleter(const InsertCompleter&) = delete;
    InsertCompleter& operator=(const InsertCompleter&) = delete;
    ~InsertCompleter();

    void select(int index, bool relative, FunctionRef<void (Key)> record_key);
    void update(bool allow_implicit);
    void try_accept();
    void reset();

    void explicit_file_complete();
    void explicit_word_buffer_complete();
    void explicit_word_all_complete();
    void explicit_line_buffer_complete();
    void explicit_line_all_complete();

    bool has_candidate_selected() const;

private:
    bool setup_ifn();

    template<typename Func>
    bool try_complete(Func complete_func);
    void on_option_changed(const Option& opt) override;

    void menu_show();

    Context&            m_context;
    OptionManager&      m_options;
    const FaceRegistry& m_faces;
    InsertCompletion    m_completions;
    Vector<BufferRange> m_inserted_ranges;
    int                 m_current_candidate = -1;
    bool                m_enabled = true;

    using CompleteFunc = InsertCompletion (const SelectionList& sels,
                                           const OptionManager& options,
                                           const FaceRegistry& faces);
    CompleteFunc* m_explicit_completer = nullptr;
};

}

#endif // insert_completer_hh_INCLUDED