diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2012-12-11 19:51:59 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2012-12-13 18:50:27 +0100 |
| commit | cfd7ee049a7c668bb2269029d159b34d2014ece6 (patch) | |
| tree | dc8d3eef1af8318acde758de012db858d4c36e30 /src/dynamic_selection_list.cc | |
| parent | e36bc74f431e2f98f049724536da86af9051811d (diff) | |
move selection updating code out of selection, to DynamicSelectionList
this avoids a lot of unnecessary (add|remove)_change_listener as
creating temporary Selections do not call that anymore.
Use can choose between a SelectionList which or a DynamicSelectionList
depending on wethear the buffer will be modified or not during the
selections lifetime.
Diffstat (limited to 'src/dynamic_selection_list.cc')
| -rw-r--r-- | src/dynamic_selection_list.cc | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/dynamic_selection_list.cc b/src/dynamic_selection_list.cc new file mode 100644 index 00000000..e1a78168 --- /dev/null +++ b/src/dynamic_selection_list.cc @@ -0,0 +1,81 @@ +#include "dynamic_selection_list.hh" + +namespace Kakoune +{ + +DynamicSelectionList::DynamicSelectionList(const Buffer& buffer, + SelectionList selections) + : m_buffer(&buffer), m_selections(std::move(selections)) +{ + m_buffer->add_change_listener(*this); +} + +DynamicSelectionList::~DynamicSelectionList() +{ + m_buffer->remove_change_listener(*this); +} + +DynamicSelectionList::DynamicSelectionList(const DynamicSelectionList& other) + : m_selections(other.m_selections), m_buffer(other.m_buffer) +{ + m_buffer->add_change_listener(*this); +} + +DynamicSelectionList& DynamicSelectionList::operator=(const DynamicSelectionList& other) +{ + m_selections = other.m_selections; + if (m_buffer != other.m_buffer) + { + m_buffer->remove_change_listener(*this); + m_buffer = other.m_buffer; + m_buffer->add_change_listener(*this); + } + return *this; +} + +DynamicSelectionList::DynamicSelectionList(DynamicSelectionList&& other) + : m_selections(std::move(other.m_selections)), m_buffer(other.m_buffer) +{ + m_buffer->add_change_listener(*this); +} + +DynamicSelectionList& DynamicSelectionList::operator=(DynamicSelectionList&& other) +{ + m_selections = std::move(other.m_selections); + if (m_buffer != other.m_buffer) + { + m_buffer->remove_change_listener(*this); + m_buffer = other.m_buffer; + m_buffer->add_change_listener(*this); + } + return *this; +} + +void DynamicSelectionList::reset(SelectionList selections) +{ + for (auto& sel : selections) + assert(&sel.buffer() == m_buffer); + m_selections = std::move(selections); +} + +void DynamicSelectionList::on_insert(const BufferIterator& begin, const BufferIterator& end) +{ + for (auto& sel : m_selections) + { + 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 : m_selections) + { + sel.first().on_erase(begin.coord(), end.coord()); + sel.last().on_erase(begin.coord(), end.coord()); + sel.check_invariant(); + } +} + +} |
