Fix BufferIterator::on_erase and tweak Buffer

This commit is contained in:
Maxime Coste 2012-06-28 14:26:01 +02:00
parent 9b6b6b6b17
commit 36e4dacdf5
2 changed files with 9 additions and 8 deletions

View File

@ -152,7 +152,7 @@ void Buffer::end_undo_group()
if (m_current_undo_group.empty()) if (m_current_undo_group.empty())
return; return;
m_history.push_back(m_current_undo_group); m_history.push_back(std::move(m_current_undo_group));
m_history_cursor = m_history.end(); m_history_cursor = m_history.end();
m_current_undo_group.clear(); m_current_undo_group.clear();
@ -277,6 +277,7 @@ void Buffer::insert(const BufferIterator& pos, const String& content)
void Buffer::erase(const BufferIterator& pos, BufferSize length) void Buffer::erase(const BufferIterator& pos, BufferSize length)
{ {
BufferIterator end = pos + length; BufferIterator end = pos + length;
assert(end.is_valid());
String prefix = m_lines[pos.line()].content.substr(0, pos.column()); String prefix = m_lines[pos.line()].content.substr(0, pos.column());
String suffix = m_lines[end.line()].content.substr(end.column()); String suffix = m_lines[end.line()].content.substr(end.column());
Line new_line = { m_lines[pos.line()].start, prefix + suffix }; Line new_line = { m_lines[pos.line()].start, prefix + suffix };

View File

@ -32,6 +32,7 @@ inline BufferIterator& BufferIterator::operator=(const BufferIterator& iterator)
{ {
m_buffer = iterator.m_buffer; m_buffer = iterator.m_buffer;
m_coord = iterator.m_coord; m_coord = iterator.m_coord;
assert(is_valid());
return *this; return *this;
} }
@ -90,18 +91,17 @@ inline void BufferIterator::on_erase(const BufferCoord& begin,
if (m_coord < begin) if (m_coord < begin)
return; return;
BufferCoord measure;
measure.line = end.line - begin.line;
measure.column = end.column - (measure.line == 0 ? begin.column : 0);
if (m_coord <= end) if (m_coord <= end)
m_coord = begin; m_coord = begin;
else else
{ {
m_coord.line -= measure.line;
if (measure.line > 0 and begin.line == m_coord.line)
m_coord.column += begin.column;
if (end.line == m_coord.line) if (end.line == m_coord.line)
m_coord.column -= measure.column; {
m_coord.line = begin.line;
m_coord.column = begin.column + m_coord.column - end.column;
}
else
m_coord.line -= end.line - begin.line;
} }
if (is_end()) if (is_end())