Fix UndoGroupOptimizer

This commit is contained in:
Maxime Coste 2013-06-12 00:14:05 +02:00
parent 7306f6b33b
commit bd8daac3a1

View File

@ -201,7 +201,7 @@ class UndoGroupOptimizer
++pos.column; ++pos.column;
++count; ++count;
} }
kak_assert(pos == endpos or pos == BufferCoord(endpos.line+1, 0)); kak_assert(pos == endpos);
return count; return count;
} }
@ -246,16 +246,22 @@ class UndoGroupOptimizer
coord.column += next_end.column - next_coord.column; coord.column += next_end.column - next_coord.column;
coord.line += next_end.line - next_coord.line; coord.line += next_end.line - next_coord.line;
} }
else if (it->type == Insert) else if (it->type == Insert and next_end > coord)
{
if (next_end > coord)
{ {
ByteCount start = count_byte_to(next_coord, coord, it_next->content); ByteCount start = count_byte_to(next_coord, coord, it_next->content);
ByteCount len = std::min(it->content.length(), it_next->content.length() - start); ByteCount len = std::min(it->content.length(), it_next->content.length() - start);
kak_assert(it_next->content.substr(start, len) == it->content.substr(0, len));
it->coord = it_next->coord; it->coord = it_next->coord;
it->content = it->content.substr(len); it->content = it->content.substr(len);
it_next->content = it_next->content.substr(0,start) + it_next->content.substr(start + len); it_next->content = it_next->content.substr(0,start) + it_next->content.substr(start + len);
} }
else if (it->type == Erase and next_end >= coord)
{
ByteCount start = count_byte_to(next_coord, coord, it_next->content);
it_next->content = it_next->content.substr(0, start) + it->content + it_next->content.substr(start);
it->coord = it_next->coord;
it->content.clear();
}
else else
{ {
if (next_end.line == coord.line) if (next_end.line == coord.line)
@ -266,18 +272,11 @@ class UndoGroupOptimizer
else else
coord.line -= next_end.line - next_coord.line; coord.line -= next_end.line - next_coord.line;
} }
}
else if (it->type == Erase and next_end >= coord)
{
ByteCount start = count_byte_to(next_coord, coord, it_next->content);
it_next->content = it_next->content.substr(0, start) + it->content + it_next->content.substr(start);
it->coord = it_next->coord;
it->content.clear();
}
std::swap(*it, *it_next); std::swap(*it, *it_next);
progress = true; progress = true;
} }
kak_assert(coord <= next_coord);
if (it->type == Erase and it_next->type == Erase and coord == next_coord) if (it->type == Erase and it_next->type == Erase and coord == next_coord)
{ {
it->content += it_next->content; it->content += it_next->content;
@ -294,18 +293,24 @@ class UndoGroupOptimizer
progress = true; progress = true;
} }
else if (it->type == Insert and it_next->type == Erase and else if (it->type == Insert and it_next->type == Erase and
coord <= next_coord and next_coord < advance(coord, it->content)) next_coord < advance(coord, it->content))
{ {
ByteCount insert_len = it->content.length(); ByteCount insert_len = it->content.length();
ByteCount erase_len = it_next->content.length(); ByteCount erase_len = it_next->content.length();
ByteCount prefix_len = count_byte_to(coord, next_coord, it->content); ByteCount prefix_len = count_byte_to(coord, next_coord, it->content);
it->content = it->content.substr(0, prefix_len) + ByteCount suffix_len = insert_len - prefix_len;
((prefix_len + erase_len < insert_len) ? if (suffix_len >= erase_len)
it->content.substr(prefix_len + erase_len) : ""); {
it_next->content = (insert_len - prefix_len < erase_len) ? it->content = it->content.substr(0, prefix_len) + it->content.substr(prefix_len + erase_len);
it_next->content.substr(insert_len - prefix_len) : ""; it_next = undo_group.erase(it_next);
}
else
{
it->content = it->content.substr(0, prefix_len);
it_next->content = it_next->content.substr(suffix_len);
++it, ++it_next; ++it, ++it_next;
}
progress = true; progress = true;
} }
else if (it->type == Erase and it_next->type == Insert and coord == next_coord and else if (it->type == Erase and it_next->type == Insert and coord == next_coord and