summaryrefslogtreecommitdiff
path: root/src/highlighter_group.cc
blob: 1dac5fe9cbd5de3a364692207052058cef46869c (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
#include "highlighter_group.hh"

#include "exception.hh"
#include "utils.hh"

namespace Kakoune
{

void HighlighterGroup::operator()(DisplayBuffer& display_buffer)
{
    for (auto& highlighter : m_highlighters)
       highlighter.second(display_buffer);
}

void HighlighterGroup::append(HighlighterAndId&& highlighter)
{
    if (m_highlighters.contains(highlighter.first))
        throw runtime_error("duplicate highlighter id: " + highlighter.first);

    m_highlighters.append(std::forward<HighlighterAndId>(highlighter));
}

void HighlighterGroup::remove(const String& id)
{
    m_highlighters.remove(id);
}

HighlighterGroup& HighlighterGroup::get_group(const String& id)
{
    auto it = m_highlighters.find(id);
    if (it == m_highlighters.end())
        throw runtime_error("no such id: " + id);
    HighlighterGroup* group = it->second.target<HighlighterGroup>();
    if (not group)
        throw runtime_error("not a group: " + id);

    return *group;
}


CandidateList HighlighterGroup::complete_id(const String& prefix,
                                            ByteCount cursor_pos)
{
    return m_highlighters.complete_id(prefix, cursor_pos);
}

CandidateList HighlighterGroup::complete_group_id(const String& prefix,
                                                  ByteCount cursor_pos)
{
    return m_highlighters.complete_id_if(
        prefix, cursor_pos,
        [](std::pair<String, HighlighterFunc>& func)
        { return func.second.target<HighlighterGroup>() != nullptr; });
}

}