summaryrefslogtreecommitdiff
path: root/src/highlighter_group.cc
blob: 86ded81a44b9556c4718bede0f7778c25f04b64b (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"

namespace Kakoune
{

void HighlighterGroup::highlight(const Context& context, HighlightFlags flags,
                                 DisplayBuffer& display_buffer)
{
    for (auto& hl : m_highlighters)
       hl.second->highlight(context, flags, display_buffer);
}

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

    m_highlighters.append(std::move(hl));
}

void HighlighterGroup::remove_child(StringView id)
{
    m_highlighters.remove(id);
}

Highlighter& HighlighterGroup::get_child(StringView path)
{
    auto sep_it = find(path, '/');
    StringView id(path.begin(), sep_it);
    auto it = m_highlighters.find(id);
    if (it == m_highlighters.end())
        throw child_not_found("no such id: "_str + id);
    if (sep_it == path.end())
        return *it->second;
    else
        return it->second->get_child({sep_it+1, path.end()});
}

Completions HighlighterGroup::complete_child(StringView path, ByteCount cursor_pos, bool group) const
{
    auto sep_it = find(path, '/');
    if (sep_it != path.end())
    {
        ByteCount offset = sep_it+1 - path.begin();
        Highlighter& hl = const_cast<HighlighterGroup*>(this)->get_child({path.begin(), sep_it});
        return offset_pos(hl.complete_child(path.substr(offset), cursor_pos - offset, group), offset);
    }

    auto condition = [=](const HighlighterMap::value_type& hl)
    {
        return not group or hl.second->has_children();
    };
    return { 0, 0, m_highlighters.complete_id_if(path, cursor_pos, condition) };
}

}