2017-01-13 14:45:46 +01:00
|
|
|
#ifndef changes_hh_INCLUDED
|
|
|
|
#define changes_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "buffer.hh"
|
|
|
|
#include "coord.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
// This tracks position changes for changes that are done
|
|
|
|
// in a forward way (each change takes place at a position)
|
|
|
|
// *after* the previous one.
|
|
|
|
struct ForwardChangesTracker
|
|
|
|
{
|
|
|
|
BufferCoord cur_pos; // last change position at current modification
|
|
|
|
BufferCoord old_pos; // last change position at start
|
|
|
|
|
|
|
|
void update(const Buffer::Change& change);
|
|
|
|
void update(const Buffer& buffer, size_t& timestamp);
|
|
|
|
|
|
|
|
BufferCoord get_old_coord(BufferCoord coord) const;
|
|
|
|
BufferCoord get_new_coord(BufferCoord coord) const;
|
|
|
|
BufferCoord get_new_coord_tolerant(BufferCoord coord) const;
|
|
|
|
|
|
|
|
bool relevant(const Buffer::Change& change, BufferCoord old_coord) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Buffer::Change* forward_sorted_until(const Buffer::Change* first, const Buffer::Change* last);
|
|
|
|
const Buffer::Change* backward_sorted_until(const Buffer::Change* first, const Buffer::Change* last);
|
|
|
|
|
2020-04-06 03:13:24 +02:00
|
|
|
template<typename Range, typename AdvanceFunc>
|
|
|
|
auto update_range(ForwardChangesTracker& changes_tracker, Range& range, AdvanceFunc&& advance_while_relevant)
|
|
|
|
{
|
|
|
|
auto& first = get_first(range);
|
|
|
|
auto& last = get_last(range);
|
|
|
|
advance_while_relevant(first);
|
|
|
|
first = changes_tracker.get_new_coord_tolerant(first);
|
|
|
|
|
|
|
|
if (last < BufferCoord{0,0})
|
|
|
|
return;
|
|
|
|
|
|
|
|
advance_while_relevant(last);
|
|
|
|
last = changes_tracker.get_new_coord_tolerant(last);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:45:46 +01:00
|
|
|
template<typename RangeContainer>
|
|
|
|
void update_forward(ConstArrayView<Buffer::Change> changes, RangeContainer& ranges)
|
|
|
|
{
|
|
|
|
ForwardChangesTracker changes_tracker;
|
2017-11-07 13:00:45 +01:00
|
|
|
auto advance_while_relevant = [&, it = changes.begin()]
|
|
|
|
(const BufferCoord& pos) mutable {
|
|
|
|
while (it != changes.end() and changes_tracker.relevant(*it, pos))
|
|
|
|
changes_tracker.update(*it++);
|
2017-01-13 14:45:46 +01:00
|
|
|
};
|
|
|
|
|
2021-03-08 21:26:38 +01:00
|
|
|
auto range_it = std::lower_bound(ranges.begin(), ranges.end(), changes.front(),
|
|
|
|
[](auto& range, const Buffer::Change& change) { return get_last(range) < change.begin; });
|
|
|
|
for (auto end = ranges.end(); range_it != end; ++range_it)
|
|
|
|
update_range(changes_tracker, *range_it, advance_while_relevant);
|
2017-01-13 14:45:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RangeContainer>
|
|
|
|
void update_backward(ConstArrayView<Buffer::Change> changes, RangeContainer& ranges)
|
|
|
|
{
|
|
|
|
ForwardChangesTracker changes_tracker;
|
2017-11-07 13:00:45 +01:00
|
|
|
auto advance_while_relevant = [&, it = changes.rbegin()]
|
2017-11-03 14:51:10 +01:00
|
|
|
(const BufferCoord& pos) mutable {
|
2017-11-07 13:00:45 +01:00
|
|
|
while (it != changes.rend())
|
2017-01-13 14:45:46 +01:00
|
|
|
{
|
2017-11-07 13:00:45 +01:00
|
|
|
const Buffer::Change change{it->type,
|
|
|
|
changes_tracker.get_new_coord(it->begin),
|
|
|
|
changes_tracker.get_new_coord(it->end)};
|
2017-01-13 14:45:46 +01:00
|
|
|
if (not changes_tracker.relevant(change, pos))
|
|
|
|
break;
|
|
|
|
changes_tracker.update(change);
|
2017-11-03 14:51:10 +01:00
|
|
|
++it;
|
2017-01-13 14:45:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& range : ranges)
|
2020-04-06 03:13:24 +02:00
|
|
|
update_range(changes_tracker, range, advance_while_relevant);
|
2017-01-13 14:45:46 +01:00
|
|
|
}
|
|
|
|
|
2018-06-11 05:33:26 +02:00
|
|
|
template<typename RangeContainer>
|
2020-01-08 10:25:27 +01:00
|
|
|
void update_ranges(Buffer& buffer, size_t& timestamp, RangeContainer&& ranges)
|
2018-06-11 05:33:26 +02:00
|
|
|
{
|
|
|
|
if (timestamp == buffer.timestamp())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto changes = buffer.changes_since(timestamp);
|
|
|
|
for (auto change_it = changes.begin(); change_it != changes.end(); )
|
|
|
|
{
|
|
|
|
auto forward_end = forward_sorted_until(change_it, changes.end());
|
|
|
|
auto backward_end = backward_sorted_until(change_it, changes.end());
|
|
|
|
|
|
|
|
if (forward_end >= backward_end)
|
|
|
|
{
|
|
|
|
update_forward({ change_it, forward_end }, ranges);
|
|
|
|
change_it = forward_end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
update_backward({ change_it, backward_end }, ranges);
|
|
|
|
change_it = backward_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
timestamp = buffer.timestamp();
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:45:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // changes_hh_INCLUDED
|