kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

changes.cc (2652B)


      1 #include "changes.hh"
      2 
      3 namespace Kakoune
      4 {
      5 void ForwardChangesTracker::update(const Buffer::Change& change)
      6 {
      7     kak_assert(change.begin >= cur_pos);
      8 
      9     if (change.type == Buffer::Change::Insert)
     10     {
     11         old_pos = get_old_coord(change.begin);
     12         cur_pos = change.end;
     13     }
     14     else if (change.type == Buffer::Change::Erase)
     15     {
     16         old_pos = get_old_coord(change.end);
     17         cur_pos = change.begin;
     18     }
     19 }
     20 
     21 void ForwardChangesTracker::update(const Buffer& buffer, size_t& timestamp)
     22 {
     23     for (auto& change : buffer.changes_since(timestamp))
     24         update(change);
     25     timestamp = buffer.timestamp();
     26 }
     27 
     28 BufferCoord ForwardChangesTracker::get_old_coord(BufferCoord coord) const
     29 {
     30     kak_assert(cur_pos <= coord);
     31     auto pos_change = cur_pos - old_pos;
     32     if (cur_pos.line == coord.line)
     33     {
     34         kak_assert(pos_change.column <= coord.column);
     35         coord.column -= pos_change.column;
     36     }
     37     coord.line -= pos_change.line;
     38     kak_assert(old_pos <= coord);
     39     return coord;
     40 }
     41 
     42 BufferCoord ForwardChangesTracker::get_new_coord(BufferCoord coord) const
     43 {
     44     kak_assert(old_pos <= coord);
     45     auto pos_change = cur_pos - old_pos;
     46     if (old_pos.line == coord.line)
     47     {
     48         kak_assert(-pos_change.column <= coord.column);
     49         coord.column += pos_change.column;
     50     }
     51     coord.line += pos_change.line;
     52     kak_assert(cur_pos <= coord);
     53     return coord;
     54 }
     55 
     56 BufferCoord ForwardChangesTracker::get_new_coord_tolerant(BufferCoord coord) const
     57 {
     58     if (coord < old_pos)
     59         return cur_pos;
     60     return get_new_coord(coord);
     61 }
     62 
     63 bool ForwardChangesTracker::relevant(const Buffer::Change& change, BufferCoord old_coord) const
     64 {
     65     auto new_coord = get_new_coord_tolerant(old_coord);
     66     return change.type == Buffer::Change::Insert ? change.begin <= new_coord
     67                                                  : change.begin < new_coord;
     68 }
     69 
     70 const Buffer::Change* forward_sorted_until(const Buffer::Change* first, const Buffer::Change* last)
     71 {
     72     if (first != last) {
     73         const Buffer::Change* next = first;
     74         while (++next != last) {
     75             const auto& ref = first->type == Buffer::Change::Insert ? first->end : first->begin;
     76             if (next->begin < ref)
     77                 return next;
     78             first = next;
     79         }
     80     }
     81     return last;
     82 }
     83 
     84 const Buffer::Change* backward_sorted_until(const Buffer::Change* first, const Buffer::Change* last)
     85 {
     86     if (first != last) {
     87         const Buffer::Change* next = first;
     88         while (++next != last) {
     89             if (first->begin < next->end)
     90                 return next;
     91             first = next;
     92         }
     93     }
     94     return last;
     95 }
     96 
     97 }