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

namespace Kakoune
{

DynamicSelectionList::DynamicSelectionList(const Buffer& buffer,
                                           SelectionList selections)
    : m_buffer(&buffer), SelectionList(std::move(selections))
{
    m_buffer->add_change_listener(*this);
    check_invariant();
}

DynamicSelectionList::~DynamicSelectionList()
{
    m_buffer->remove_change_listener(*this);
}

DynamicSelectionList::DynamicSelectionList(const DynamicSelectionList& other)
    : SelectionList(other), m_buffer(other.m_buffer)
{
    m_buffer->add_change_listener(*this);
}

DynamicSelectionList& DynamicSelectionList::operator=(const DynamicSelectionList& other)
{
    SelectionList::operator=((const SelectionList&)other);
    if (m_buffer != other.m_buffer)
    {
        m_buffer->remove_change_listener(*this);
        m_buffer = other.m_buffer;
        m_buffer->add_change_listener(*this);
    }
    check_invariant();
    return *this;
}

DynamicSelectionList::DynamicSelectionList(DynamicSelectionList&& other)
    : SelectionList(std::move(other)), m_buffer(other.m_buffer)
{
    m_buffer->add_change_listener(*this);
}

DynamicSelectionList& DynamicSelectionList::operator=(DynamicSelectionList&& other)
{
    SelectionList::operator=(std::move(other));
    if (m_buffer != other.m_buffer)
    {
        m_buffer->remove_change_listener(*this);
        m_buffer = other.m_buffer;
        m_buffer->add_change_listener(*this);
    }
    check_invariant();
    return *this;
}

DynamicSelectionList& DynamicSelectionList::operator=(SelectionList selections)
{
    SelectionList::operator=(std::move(selections));
    check_invariant();
    return *this;
}

void DynamicSelectionList::check_invariant() const
{
    for (auto& sel : *this)
        assert(m_buffer == &sel.buffer());
}

void DynamicSelectionList::on_insert(const BufferIterator& begin, const BufferIterator& end)
{
    for (auto& sel : *this)
    {
        sel.first().on_insert(begin.coord(), end.coord());
        sel.last().on_insert(begin.coord(), end.coord());
        sel.check_invariant();
    }
}

void DynamicSelectionList::on_erase(const BufferIterator& begin, const BufferIterator& end)
{
    for (auto& sel : *this)
    {
        sel.first().on_erase(begin.coord(), end.coord());
        sel.last().on_erase(begin.coord(), end.coord());
        sel.check_invariant();
    }
}

}