Buffer: replace {begin,end}_undo_group with a single commit_undo_group method

This commit is contained in:
Maxime Coste 2013-02-20 14:20:16 +01:00
parent 682e4faff0
commit 9f4498e035
4 changed files with 11 additions and 30 deletions

View File

@ -152,21 +152,7 @@ String Buffer::string(const BufferIterator& begin, const BufferIterator& end) co
return res; return res;
} }
void Buffer::begin_undo_group() void Buffer::commit_undo_group()
{
if (m_flags & Flags::NoUndo)
return;
assert(m_current_undo_group.empty());
m_history.erase(m_history_cursor, m_history.end());
if (m_history.size() < m_last_save_undo_index)
m_last_save_undo_index = -1;
m_history_cursor = m_history.end();
}
void Buffer::end_undo_group()
{ {
if (m_flags & Flags::NoUndo) if (m_flags & Flags::NoUndo)
return; return;
@ -174,10 +160,14 @@ void Buffer::end_undo_group()
if (m_current_undo_group.empty()) if (m_current_undo_group.empty())
return; return;
m_history.erase(m_history_cursor, m_history.end());
m_history.push_back(std::move(m_current_undo_group)); m_history.push_back(std::move(m_current_undo_group));
m_current_undo_group.clear();
m_history_cursor = m_history.end(); m_history_cursor = m_history.end();
m_current_undo_group.clear(); if (m_history.size() < m_last_save_undo_index)
m_last_save_undo_index = -1;
} }
// A Modification holds a single atomic modification to Buffer // A Modification holds a single atomic modification to Buffer
@ -414,10 +404,7 @@ bool Buffer::is_modified() const
void Buffer::notify_saved() void Buffer::notify_saved()
{ {
if (not m_current_undo_group.empty()) if (not m_current_undo_group.empty())
{ commit_undo_group();
end_undo_group();
begin_undo_group();
}
m_flags &= ~Flags::New; m_flags &= ~Flags::New;
size_t history_cursor_index = m_history_cursor - m_history.begin(); size_t history_cursor_index = m_history_cursor - m_history.begin();

View File

@ -115,8 +115,7 @@ public:
size_t timestamp() const { return m_timestamp; } size_t timestamp() const { return m_timestamp; }
void begin_undo_group(); void commit_undo_group();
void end_undo_group();
bool undo(); bool undo();
bool redo(); bool redo();

View File

@ -384,16 +384,13 @@ void Editor::check_invariant() const
void Editor::begin_edition() void Editor::begin_edition()
{ {
++m_edition_level; ++m_edition_level;
if (m_edition_level == 1)
m_buffer->begin_undo_group();
} }
void Editor::end_edition() void Editor::end_edition()
{ {
assert(m_edition_level > 0); assert(m_edition_level > 0);
if (m_edition_level == 1) if (m_edition_level == 1)
m_buffer->end_undo_group(); m_buffer->commit_undo_group();
--m_edition_level; --m_edition_level;
} }

View File

@ -40,12 +40,10 @@ void test_buffer()
buffer.insert(buffer.end(), "kanaky\n"); buffer.insert(buffer.end(), "kanaky\n");
assert(buffer.string(begin+1, buffer.end()) == "kanaky\n"); assert(buffer.string(begin+1, buffer.end()) == "kanaky\n");
buffer.end_undo_group(); buffer.commit_undo_group();
buffer.begin_undo_group();
buffer.erase(begin+1, buffer.end()); buffer.erase(begin+1, buffer.end());
buffer.insert(buffer.end(), "mutch\n"); buffer.insert(buffer.end(), "mutch\n");
buffer.end_undo_group(); buffer.commit_undo_group();
buffer.undo(); buffer.undo();
assert(buffer.string(buffer.end() - 7, buffer.end()) == "kanaky\n"); assert(buffer.string(buffer.end() - 7, buffer.end()) == "kanaky\n");
buffer.redo(); buffer.redo();