From 69bebdb44e90d9b338e69d415ba442396ac77841 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 3 Sep 2012 23:46:04 +0200 Subject: [PATCH] fix open line above for first line and add a unit test --- src/editor.cc | 11 +++++++++++ src/unit_tests.cc | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/editor.cc b/src/editor.cc index fe00dd64..8de99e02 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -356,7 +356,18 @@ IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode) } if (mode == Mode::OpenLineBelow or mode == Mode::OpenLineAbove) + { insert("\n"); + if (mode == Mode::OpenLineAbove) + { + for (auto& sel : m_editor.m_selections.back()) + { + // special case, the --first line above did nothing, so we need to compensate now + if (sel.first() == buffer().begin() + 1) + sel = Selection(buffer().begin(), buffer().begin()); + } + } + } } IncrementalInserter::~IncrementalInserter() diff --git a/src/unit_tests.cc b/src/unit_tests.cc index 6654313e..b6b1a35e 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -45,6 +45,22 @@ void test_editor() } } +void test_incremental_inserter() +{ + Buffer buffer("test", Buffer::Type::Scratch, "test\n\nyoupi\nmatin\n"); + Editor editor(buffer); + + editor.select(buffer.begin()); + { + IncrementalInserter inserter(editor, IncrementalInserter::Mode::OpenLineAbove); + assert(editor.is_editing()); + assert(editor.selections().size() == 1); + assert(editor.selections().front().first() == buffer.begin()); + assert(editor.selections().front().last() == buffer.begin()); + assert(*buffer.begin() == L'\n'); + } + assert(not editor.is_editing()); +} void test_string() { assert(int_to_str(124) == "124"); @@ -65,4 +81,5 @@ void run_unit_tests() test_string(); test_buffer(); test_editor(); + test_incremental_inserter(); }