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

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

namespace Kakoune
{

void FilterGroup::operator()(Buffer& buffer,
                             Selection& selection, String& content)
{
    for (auto& filter : m_filters)
       filter.second(buffer, selection, content);
}

void FilterGroup::append(FilterAndId&& filter)
{
    if (m_filters.contains(filter.first))
        throw runtime_error("duplicate filter id: " + filter.first);

    m_filters.append(std::forward<FilterAndId>(filter));
}

void FilterGroup::remove(const String& id)
{
    m_filters.remove(id);
}

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

    return *group;
}


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

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

}