From 0f957b374338ca5eba3bf4e7667c79a29fc6a73a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 13 Mar 2013 14:26:20 +0100 Subject: [PATCH] Editor: fix replace at end of buffer --- src/editor.cc | 4 ++-- src/unit_tests.cc | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/editor.cc b/src/editor.cc index 476e5244..0f8e3a2c 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -83,7 +83,7 @@ void Editor::insert(const String& str, InsertMode mode) { BufferIterator pos = prepare_insert(*m_buffer, sel, mode); m_buffer->insert(pos, str); - if (mode == InsertMode::Replace) + if (mode == InsertMode::Replace and not pos.is_end()) { sel.first() = pos; sel.last() = str.empty() ? pos : utf8::character_start(pos + str.length() - 1); @@ -105,7 +105,7 @@ void Editor::insert(const memoryview& strings, InsertMode mode) BufferIterator pos = prepare_insert(*m_buffer, sel, mode); const String& str = strings[std::min(i, strings.size()-1)]; m_buffer->insert(pos, str); - if (mode == InsertMode::Replace) + if (mode == InsertMode::Replace and not pos.is_end()) { sel.first() = pos; sel.last() = str.empty() ? pos : utf8::character_start(pos + str.length() - 1); diff --git a/src/unit_tests.cc b/src/unit_tests.cc index a092fd5c..09ad7f4e 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -55,15 +55,22 @@ void test_editor() Buffer buffer("test", Buffer::Flags::None, { "test\n", "\n", "youpi\n" }); Editor editor(buffer); - using namespace std::placeholders; - - editor.select(select_whole_buffer); - editor.multi_select(std::bind(select_all_matches, _1, "\\n\\h*")); - for (auto& sel : editor.selections()) { - assert(*sel.begin() == '\n'); - editor.buffer().erase(sel.begin(), sel.end()); + scoped_edition edition{editor}; + editor.select(select_whole_buffer); + editor.multi_select(std::bind(select_all_matches, std::placeholders::_1, "\\n\\h*")); + for (auto& sel : editor.selections()) + { + assert(*sel.begin() == '\n'); + editor.buffer().erase(sel.begin(), sel.end()); + } } + editor.undo(); + + Selection sel{ buffer.iterator_at_line_begin(2_line), buffer.end() }; + editor.select(sel, SelectMode::Replace); + editor.insert("",InsertMode::Replace); + assert(not editor.selections().back().first().is_end()); } void test_incremental_inserter()