2012-12-11 19:51:59 +01:00
|
|
|
#include "dynamic_selection_list.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
DynamicSelectionList::DynamicSelectionList(const Buffer& buffer,
|
|
|
|
SelectionList selections)
|
2013-03-31 13:49:56 +02:00
|
|
|
: SelectionList(std::move(selections)),
|
|
|
|
BufferChangeListener_AutoRegister(buffer)
|
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
|
|
|
}
|
|
|
|
|
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
|
2013-03-31 13:49:56 +02:00
|
|
|
const Buffer* buf = &buffer();
|
2012-12-12 14:21:50 +01:00
|
|
|
for (auto& sel : *this)
|
2013-01-23 14:25:48 +01:00
|
|
|
{
|
2013-03-31 13:49:56 +02:00
|
|
|
assert(buf == &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-18 19:06:04 +01:00
|
|
|
namespace
|
2013-03-15 14:22:42 +01:00
|
|
|
{
|
|
|
|
|
2013-03-18 19:06:04 +01:00
|
|
|
template<template <bool, bool> class UpdateFunc>
|
|
|
|
void on_buffer_change(SelectionList& sels, const BufferCoord& begin, const BufferCoord& end, LineCount end_line)
|
|
|
|
{
|
|
|
|
auto update_beg = std::lower_bound(sels.begin(), sels.end(), begin,
|
|
|
|
[](const Selection& s, const BufferCoord& c) { return std::max(s.first(), s.last()).coord() < c; });
|
|
|
|
auto update_only_line_beg = std::upper_bound(sels.begin(), sels.end(), end_line,
|
|
|
|
[](LineCount l, const Selection& s) { return l < std::min(s.first(), s.last()).line(); });
|
2013-03-15 14:22:42 +01:00
|
|
|
|
2013-03-18 19:06:04 +01:00
|
|
|
if (update_beg != update_only_line_beg)
|
|
|
|
{
|
|
|
|
// for the first one, we are not sure if min < begin
|
|
|
|
UpdateFunc<false, false>{}(update_beg->first(), begin, end);
|
|
|
|
UpdateFunc<false, false>{}(update_beg->last(), begin, end);
|
|
|
|
}
|
|
|
|
for (auto it = update_beg+1; it < update_only_line_beg; ++it)
|
|
|
|
{
|
|
|
|
UpdateFunc<false, true>{}(it->first(), begin, end);
|
|
|
|
UpdateFunc<false, true>{}(it->last(), begin, end);
|
|
|
|
}
|
|
|
|
if (end.line > begin.line)
|
|
|
|
{
|
|
|
|
for (auto it = update_only_line_beg; it != sels.end(); ++it)
|
|
|
|
{
|
|
|
|
UpdateFunc<true, true>{}(it->first(), begin, end);
|
|
|
|
UpdateFunc<true, true>{}(it->last(), begin, end);
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 14:22:42 +01:00
|
|
|
}
|
|
|
|
|
2013-03-18 19:06:04 +01:00
|
|
|
template<bool assume_different_line, bool assume_greater_than_begin>
|
|
|
|
struct UpdateInsert
|
2013-03-15 14:22:42 +01:00
|
|
|
{
|
2013-03-18 19:06:04 +01:00
|
|
|
void operator()(BufferIterator& it,
|
|
|
|
const BufferCoord& begin, const BufferCoord& end) const
|
|
|
|
{
|
|
|
|
BufferCoord coord = it.coord();
|
|
|
|
if (assume_different_line)
|
|
|
|
assert(begin.line < coord.line);
|
|
|
|
if (not assume_greater_than_begin and coord < begin)
|
|
|
|
return;
|
|
|
|
if (not assume_different_line and begin.line == coord.line)
|
|
|
|
coord.column = end.column + coord.column - begin.column;
|
|
|
|
|
|
|
|
coord.line += end.line - begin.line;
|
|
|
|
it = coord;
|
|
|
|
}
|
|
|
|
};
|
2013-03-15 14:22:42 +01:00
|
|
|
|
2013-03-18 19:06:04 +01:00
|
|
|
template<bool assume_different_line, bool assume_greater_than_begin>
|
|
|
|
struct UpdateErase
|
|
|
|
{
|
|
|
|
void operator()(BufferIterator& it,
|
|
|
|
const BufferCoord& begin, const BufferCoord& end) const
|
2013-03-15 14:22:42 +01:00
|
|
|
{
|
2013-03-18 19:06:04 +01:00
|
|
|
BufferCoord coord = it.coord();
|
|
|
|
if (not assume_greater_than_begin and coord < begin)
|
|
|
|
return;
|
|
|
|
if (assume_different_line)
|
|
|
|
assert(end.line < coord.line);
|
|
|
|
if (not assume_different_line and coord <= end)
|
|
|
|
coord = it.buffer().clamp(begin);
|
|
|
|
else
|
2013-03-15 14:22:42 +01:00
|
|
|
{
|
2013-03-18 19:06:04 +01:00
|
|
|
if (not assume_different_line and end.line == coord.line)
|
|
|
|
{
|
|
|
|
coord.line = begin.line;
|
|
|
|
coord.column = begin.column + coord.column - end.column;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
coord.line -= end.line - begin.line;
|
2013-03-15 14:22:42 +01:00
|
|
|
}
|
2013-03-18 19:06:04 +01:00
|
|
|
it = coord;
|
2013-03-15 14:22:42 +01:00
|
|
|
}
|
2013-03-18 19:06:04 +01:00
|
|
|
};
|
|
|
|
|
2013-03-15 14:22:42 +01:00
|
|
|
}
|
|
|
|
|
2012-12-11 19:51:59 +01:00
|
|
|
void DynamicSelectionList::on_insert(const BufferIterator& begin, const BufferIterator& end)
|
|
|
|
{
|
2013-03-18 19:06:04 +01:00
|
|
|
on_buffer_change<UpdateInsert>(*this, begin.coord(), end.coord(), begin.line());
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicSelectionList::on_erase(const BufferIterator& begin, const BufferIterator& end)
|
|
|
|
{
|
2013-03-18 19:06:04 +01:00
|
|
|
on_buffer_change<UpdateErase>(*this, begin.coord(), end.coord(), end.line());
|
2012-12-11 19:51:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|