Fix another bug in Buffer::replace implementation

This commit is contained in:
Maxime Coste 2016-03-17 12:36:49 +00:00
parent d277ef6d6c
commit 38f146d849
4 changed files with 12 additions and 10 deletions

View File

@ -415,21 +415,14 @@ void Buffer::apply_modification(const Modification& modification)
ByteCoord coord = modification.coord; ByteCoord coord = modification.coord;
kak_assert(is_valid(coord)); kak_assert(is_valid(coord));
// in modifications, end coords should be {line_count(), 0}
kak_assert((m_lines.empty() and coord == ByteCoord{0,0} ) or
coord != ByteCoord(line_count()-1, m_lines.back().length()));
switch (modification.type) switch (modification.type)
{ {
case Modification::Insert: case Modification::Insert:
{
do_insert(coord, content); do_insert(coord, content);
break; break;
}
case Modification::Erase: case Modification::Erase:
{ {
ByteCount count = content.length(); auto end = advance(coord, content.length());
ByteCoord end = advance(coord, count);
kak_assert(string(coord, end) == content); kak_assert(string(coord, end) == content);
do_erase(coord, end); do_erase(coord, end);
break; break;
@ -453,7 +446,7 @@ ByteCoord Buffer::insert(ByteCoord pos, StringView content)
// for undo and redo purpose it is better to use one past last line rather // for undo and redo purpose it is better to use one past last line rather
// than one past last char coord. // than one past last char coord.
auto coord = is_end(pos) ? ByteCoord{line_count()} : pos; auto coord = is_end(pos) ? line_count() : pos;
if (not (m_flags & Flags::NoUndo)) if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back(Modification::Insert, coord, real_content); m_current_undo_group.emplace_back(Modification::Insert, coord, real_content);
return do_insert(pos, real_content->strview()); return do_insert(pos, real_content->strview());
@ -489,7 +482,10 @@ ByteCoord Buffer::replace(ByteCoord begin, ByteCoord end, StringView content)
else else
real_content = intern(content); real_content = intern(content);
auto coord = is_end(pos) ? ByteCoord{line_count()} : pos; bool last_char_is_eol = not (m_lines.empty() or
m_lines.back().empty() or
m_lines.back().back() != '\n');
auto coord = is_end(pos) and last_char_is_eol ? line_count() : pos;
if (not (m_flags & Flags::NoUndo)) if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back(Modification::Insert, coord, real_content); m_current_undo_group.emplace_back(Modification::Insert, coord, real_content);
return do_insert(pos, real_content->strview()); return do_insert(pos, real_content->strview());

View File

@ -0,0 +1 @@
ey%<a-s>RuU

View File

@ -0,0 +1,4 @@
line 1
line 2
line 3
line 4

View File

@ -0,0 +1 @@
linelinelineline