Fixes in line_modification change, along with style tweak

This commit is contained in:
Maxime Coste 2015-02-01 22:33:59 +00:00
parent 0b4ecef2d2
commit d3bccfeb94

View File

@ -7,31 +7,29 @@ namespace Kakoune
static LineModification make_line_modif(const Buffer::Change& change) static LineModification make_line_modif(const Buffer::Change& change)
{ {
LineModification res = {}; LineCount num_added = 0, num_removed = 0;
res.old_line = change.begin.line;
res.new_line = change.begin.line;
if (change.type == Buffer::Change::Insert) if (change.type == Buffer::Change::Insert)
{ {
res.num_added = change.end.line - change.begin.line; num_added = change.end.line - change.begin.line;
// inserted a new line at buffer end but end coord is on same line // inserted a new line at buffer end but end coord is on same line
if (change.at_end and change.end.column != 0) if (change.at_end and change.end.column != 0)
++res.num_added; ++num_added;
} }
else else
{ {
res.num_removed = change.end.line - change.begin.line; num_removed = change.end.line - change.begin.line;
// removed last line, but end coord is on same line // removed last line, but end coord is on same line
if (change.at_end and change.end.column != 0) if (change.at_end and change.end.column != 0)
++res.num_removed; ++num_removed;
} }
// modified a line // modified a line
if (not change.at_end and if (not change.at_end and
(change.begin.column != 0 or change.end.column != 0)) (change.begin.column != 0 or change.end.column != 0))
{ {
++res.num_removed; ++num_removed;
++res.num_added; ++num_added;
} }
return res; return { change.begin.line, change.begin.line, num_removed, num_added };
} }
Vector<LineModification> compute_line_modifications(const Buffer& buffer, size_t timestamp) Vector<LineModification> compute_line_modifications(const Buffer& buffer, size_t timestamp)
@ -51,7 +49,10 @@ Vector<LineModification> compute_line_modifications(const Buffer& buffer, size_t
if (change.new_line <= prev.new_line + prev.num_added) if (change.new_line <= prev.new_line + prev.num_added)
{ {
--pos; --pos;
LineCount removed_from_previously_added_by_pos = clamp(pos->new_line + pos->num_added - change.new_line, 0_line, std::min(pos->num_added, change.num_removed)); const LineCount removed_from_previously_added_by_pos =
clamp(pos->new_line + pos->num_added - change.new_line,
0_line, std::min(pos->num_added, change.num_removed));
pos->num_removed += change.num_removed - removed_from_previously_added_by_pos; pos->num_removed += change.num_removed - removed_from_previously_added_by_pos;
pos->num_added += change.num_added - removed_from_previously_added_by_pos; pos->num_added += change.num_added - removed_from_previously_added_by_pos;
} }
@ -64,8 +65,8 @@ Vector<LineModification> compute_line_modifications(const Buffer& buffer, size_t
else else
pos = res.insert(pos, change); pos = res.insert(pos, change);
auto& modif = *pos;
auto next = pos + 1; auto next = pos + 1;
auto diff = buf_change.end.line - buf_change.begin.line;
if (buf_change.type == Buffer::Change::Erase) if (buf_change.type == Buffer::Change::Erase)
{ {
auto delend = std::upper_bound(next, res.end(), change.new_line + change.num_removed, auto delend = std::upper_bound(next, res.end(), change.new_line + change.num_removed,
@ -74,16 +75,22 @@ Vector<LineModification> compute_line_modifications(const Buffer& buffer, size_t
for (auto it = next; it != delend; ++it) for (auto it = next; it != delend; ++it)
{ {
LineCount removed_from_previously_added_by_it = std::min(it->num_added, change.new_line + change.num_removed - it->new_line); const LineCount removed_from_previously_added_by_it =
modif.num_removed += it->num_removed - removed_from_previously_added_by_it; std::min(it->num_added, change.new_line + change.num_removed - it->new_line);
modif.num_added += it->num_added - removed_from_previously_added_by_it;
pos->num_removed += it->num_removed - removed_from_previously_added_by_it;
pos->num_added += it->num_added - removed_from_previously_added_by_it;
} }
next = res.erase(next, delend); next = res.erase(next, delend);
}
auto diff = buf_change.end.line - buf_change.begin.line; for (auto it = next; it != res.end(); ++it)
for (auto it = next; it != res.end(); ++it) it->new_line -= diff;
it->new_line += diff; }
else
{
for (auto it = next; it != res.end(); ++it)
it->new_line += diff;
}
} }
return res; return res;
} }