From 9f4498e0354a77db0ed89dcae30169f97543d271 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 20 Feb 2013 14:20:16 +0100 Subject: [PATCH] Buffer: replace {begin,end}_undo_group with a single commit_undo_group method --- src/buffer.cc | 27 +++++++-------------------- src/buffer.hh | 3 +-- src/editor.cc | 5 +---- src/unit_tests.cc | 6 ++---- 4 files changed, 11 insertions(+), 30 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 57531c9a..3eb6920c 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -152,21 +152,7 @@ String Buffer::string(const BufferIterator& begin, const BufferIterator& end) co return res; } -void Buffer::begin_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() +void Buffer::commit_undo_group() { if (m_flags & Flags::NoUndo) return; @@ -174,10 +160,14 @@ void Buffer::end_undo_group() if (m_current_undo_group.empty()) return; + m_history.erase(m_history_cursor, m_history.end()); + m_history.push_back(std::move(m_current_undo_group)); + m_current_undo_group.clear(); 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 @@ -414,10 +404,7 @@ bool Buffer::is_modified() const void Buffer::notify_saved() { if (not m_current_undo_group.empty()) - { - end_undo_group(); - begin_undo_group(); - } + commit_undo_group(); m_flags &= ~Flags::New; size_t history_cursor_index = m_history_cursor - m_history.begin(); diff --git a/src/buffer.hh b/src/buffer.hh index e67c5760..28f550a2 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -115,8 +115,7 @@ public: size_t timestamp() const { return m_timestamp; } - void begin_undo_group(); - void end_undo_group(); + void commit_undo_group(); bool undo(); bool redo(); diff --git a/src/editor.cc b/src/editor.cc index 1edcc894..d04c1305 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -384,16 +384,13 @@ void Editor::check_invariant() const void Editor::begin_edition() { ++m_edition_level; - - if (m_edition_level == 1) - m_buffer->begin_undo_group(); } void Editor::end_edition() { assert(m_edition_level > 0); if (m_edition_level == 1) - m_buffer->end_undo_group(); + m_buffer->commit_undo_group(); --m_edition_level; } diff --git a/src/unit_tests.cc b/src/unit_tests.cc index c98314af..a092fd5c 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -40,12 +40,10 @@ void test_buffer() buffer.insert(buffer.end(), "kanaky\n"); assert(buffer.string(begin+1, buffer.end()) == "kanaky\n"); - buffer.end_undo_group(); - - buffer.begin_undo_group(); + buffer.commit_undo_group(); buffer.erase(begin+1, buffer.end()); buffer.insert(buffer.end(), "mutch\n"); - buffer.end_undo_group(); + buffer.commit_undo_group(); buffer.undo(); assert(buffer.string(buffer.end() - 7, buffer.end()) == "kanaky\n"); buffer.redo();