summaryrefslogtreecommitdiff
path: root/src/highlighter.hh
blob: 8f41cc16ca6bdcc3d9ab39d49e5f4a70f436d7c5 (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
#ifndef highlighter_hh_INCLUDED
#define highlighter_hh_INCLUDED

#include "function_registry.hh"
#include "memoryview.hh"
#include "string.hh"
#include "utils.hh"

#include <functional>

namespace Kakoune
{

class DisplayBuffer;
class Context;

enum class HighlightFlags
{
    Highlight,
    MoveOnly
};

// An Highlighter is a function which mutates a DisplayBuffer in order to
// change the visual representation of a file. It could be changing text
// color, adding information text (line numbering for example) or replacing
// buffer content (folding for example)

struct Highlighter;

using HighlighterAndId = std::pair<String, std::unique_ptr<Highlighter>>;

struct Highlighter
{
    virtual ~Highlighter() {}
    virtual void highlight(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer) = 0;

    virtual bool has_children() const { return false; }
    virtual Highlighter& get_child(StringView path) { throw runtime_error("this highlighter do not hold children"); }
    virtual void add_child(HighlighterAndId&& hl) { throw runtime_error("this highlighter do not hold children"); }
    virtual void remove_child(StringView id) { throw runtime_error("this highlighter do not hold children"); }
    virtual Completions complete_child(StringView path, ByteCount cursor_pos, bool group) const { throw runtime_error("this highlighter do not hold children"); }
};

template<typename Func>
struct SimpleHighlighter : public Highlighter
{
    SimpleHighlighter(Func func) : m_func(std::move(func)) {}
    virtual void highlight(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer) override
    {
        m_func(context, flags, display_buffer);
    }
private:
    Func m_func;
};

template<typename T>
std::unique_ptr<SimpleHighlighter<T>> make_simple_highlighter(T func)
{
    return make_unique<SimpleHighlighter<T>>(std::move(func));
}

using HighlighterParameters = memoryview<String>;
using HighlighterFactory = std::function<HighlighterAndId (HighlighterParameters params)>;

struct HighlighterRegistry : FunctionRegistry<HighlighterFactory>,
                             Singleton<HighlighterRegistry>
{};

}

#endif // highlighter_hh_INCLUDED