2012-12-11 19:51:59 +01:00
|
|
|
#include "dynamic_selection_list.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
DynamicSelectionList::DynamicSelectionList(const Buffer& buffer,
|
|
|
|
SelectionList selections)
|
2012-12-12 14:21:50 +01:00
|
|
|
: m_buffer(&buffer), SelectionList(std::move(selections))
|
2012-12-11 19:51:59 +01:00
|
|
|
{
|
2013-01-31 18:58:25 +01:00
|
|
|
m_buffer->change_listeners().insert(this);
|
2012-12-12 14:21:50 +01:00
|
|
|
check_invariant();
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicSelectionList::~DynamicSelectionList()
|
|
|
|
{
|
2013-01-31 18:58:25 +01:00
|
|
|
m_buffer->change_listeners().erase(this);
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicSelectionList::DynamicSelectionList(const DynamicSelectionList& other)
|
2012-12-12 14:21:50 +01:00
|
|
|
: SelectionList(other), m_buffer(other.m_buffer)
|
2012-12-11 19:51:59 +01:00
|
|
|
{
|
2013-01-31 18:58:25 +01:00
|
|
|
m_buffer->change_listeners().insert(this);
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicSelectionList& DynamicSelectionList::operator=(const DynamicSelectionList& other)
|
|
|
|
{
|
2012-12-12 14:21:50 +01:00
|
|
|
SelectionList::operator=((const SelectionList&)other);
|
2012-12-11 19:51:59 +01:00
|
|
|
if (m_buffer != other.m_buffer)
|
|
|
|
{
|
2013-01-31 18:58:25 +01:00
|
|
|
m_buffer->change_listeners().erase(this);
|
2012-12-11 19:51:59 +01:00
|
|
|
m_buffer = other.m_buffer;
|
2013-01-31 18:58:25 +01:00
|
|
|
m_buffer->change_listeners().insert(this);
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
2012-12-12 14:21:50 +01:00
|
|
|
check_invariant();
|
2012-12-11 19:51:59 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicSelectionList::DynamicSelectionList(DynamicSelectionList&& other)
|
2012-12-12 14:21:50 +01:00
|
|
|
: SelectionList(std::move(other)), m_buffer(other.m_buffer)
|
2012-12-11 19:51:59 +01:00
|
|
|
{
|
2013-01-31 18:58:25 +01:00
|
|
|
m_buffer->change_listeners().insert(this);
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicSelectionList& DynamicSelectionList::operator=(DynamicSelectionList&& other)
|
|
|
|
{
|
2012-12-12 14:21:50 +01:00
|
|
|
SelectionList::operator=(std::move(other));
|
2012-12-11 19:51:59 +01:00
|
|
|
if (m_buffer != other.m_buffer)
|
|
|
|
{
|
2013-01-31 18:58:25 +01:00
|
|
|
m_buffer->change_listeners().erase(this);
|
2012-12-11 19:51:59 +01:00
|
|
|
m_buffer = other.m_buffer;
|
2013-01-31 18:58:25 +01:00
|
|
|
m_buffer->change_listeners().insert(this);
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
2012-12-12 14:21:50 +01:00
|
|
|
check_invariant();
|
2012-12-11 19:51:59 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-12-12 14:21:50 +01:00
|
|
|
DynamicSelectionList& DynamicSelectionList::operator=(SelectionList selections)
|
2012-12-11 19:51:59 +01:00
|
|
|
{
|
2012-12-12 14:21:50 +01:00
|
|
|
SelectionList::operator=(std::move(selections));
|
|
|
|
check_invariant();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicSelectionList::check_invariant() const
|
|
|
|
{
|
2013-02-27 19:03:33 +01:00
|
|
|
#ifdef KAK_DEBUG
|
2012-12-12 14:21:50 +01:00
|
|
|
for (auto& sel : *this)
|
2013-01-23 14:25:48 +01:00
|
|
|
{
|
2012-12-12 14:21:50 +01:00
|
|
|
assert(m_buffer == &sel.buffer());
|
2013-01-23 14:25:48 +01:00
|
|
|
sel.check_invariant();
|
|
|
|
}
|
2013-02-27 19:03:33 +01:00
|
|
|
#endif
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
|
2013-03-15 14:22:42 +01:00
|
|
|
static void update_insert(BufferIterator& it,
|
|
|
|
const BufferCoord& begin, const BufferCoord& end)
|
|
|
|
{
|
|
|
|
BufferCoord coord = it.coord();
|
|
|
|
if (coord < begin)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (begin.line == coord.line)
|
|
|
|
coord.column = end.column + coord.column - begin.column;
|
|
|
|
coord.line += end.line - begin.line;
|
|
|
|
|
|
|
|
it = coord;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_erase(BufferIterator& it,
|
|
|
|
const BufferCoord& begin, const BufferCoord& end)
|
|
|
|
{
|
|
|
|
BufferCoord coord = it.coord();
|
|
|
|
if (coord < begin)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (coord <= end)
|
|
|
|
coord = it.buffer().clamp(begin);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (end.line == coord.line)
|
|
|
|
{
|
|
|
|
coord.line = begin.line;
|
|
|
|
coord.column = begin.column + coord.column - end.column;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
coord.line -= end.line - begin.line;
|
|
|
|
}
|
|
|
|
it = coord;
|
|
|
|
}
|
|
|
|
|
2012-12-11 19:51:59 +01:00
|
|
|
void DynamicSelectionList::on_insert(const BufferIterator& begin, const BufferIterator& end)
|
|
|
|
{
|
2013-02-27 19:03:33 +01:00
|
|
|
const BufferCoord begin_coord{begin.coord()};
|
|
|
|
const BufferCoord end_coord{end.coord()};
|
2012-12-12 14:21:50 +01:00
|
|
|
for (auto& sel : *this)
|
2012-12-11 19:51:59 +01:00
|
|
|
{
|
2013-03-15 14:22:42 +01:00
|
|
|
update_insert(sel.first(), begin_coord, end_coord);
|
|
|
|
update_insert(sel.last(), begin_coord, end_coord);
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicSelectionList::on_erase(const BufferIterator& begin, const BufferIterator& end)
|
|
|
|
{
|
2013-02-27 19:03:33 +01:00
|
|
|
const BufferCoord begin_coord{begin.coord()};
|
|
|
|
const BufferCoord end_coord{end.coord()};
|
2012-12-12 14:21:50 +01:00
|
|
|
for (auto& sel : *this)
|
2012-12-11 19:51:59 +01:00
|
|
|
{
|
2013-03-15 14:22:42 +01:00
|
|
|
update_erase(sel.first(), begin_coord, end_coord);
|
|
|
|
update_erase(sel.last(), begin_coord, end_coord);
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|