From c32a7b9b74dfef609b27f3482a76b15329185e1a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 23 Nov 2012 18:42:07 +0100 Subject: [PATCH] Buffer takes a vector of lines as initial content --- src/buffer.cc | 15 ++++++++++----- src/buffer.hh | 2 +- src/unit_tests.cc | 6 +++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index ac0b8fce..78d89ce4 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -12,8 +12,7 @@ namespace Kakoune { -Buffer::Buffer(String name, Flags flags, - String initial_content) +Buffer::Buffer(String name, Flags flags, std::vector lines) : m_name(std::move(name)), m_flags(flags | Flags::NoUndo), m_history(), m_history_cursor(m_history.begin()), m_last_save_undo_index(0), @@ -22,9 +21,15 @@ Buffer::Buffer(String name, Flags flags, m_options(GlobalOptions::instance()) { BufferManager::instance().register_buffer(*this); - if (initial_content.empty() or initial_content.back() != '\n') - initial_content += '\n'; - do_insert(begin(), std::move(initial_content)); + + ByteCount pos = 0; + m_lines.reserve(lines.size()); + for (auto& line : lines) + { + assert(not line.empty() and line.back() == '\n'); + m_lines.emplace_back(Line{ pos, std::move(line) }); + pos += m_lines.back().length(); + } Editor editor_for_hooks(*this); Context context(editor_for_hooks); diff --git a/src/buffer.hh b/src/buffer.hh index e3c72665..2c4b0042 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -101,7 +101,7 @@ public: NoUndo = 8, }; - Buffer(String name, Flags flags, String initial_content = "\n"); + Buffer(String name, Flags flags, std::vector lines = { "\n" }); Buffer(const Buffer&) = delete; Buffer& operator= (const Buffer&) = delete; ~Buffer(); diff --git a/src/unit_tests.cc b/src/unit_tests.cc index 2936d9de..809be574 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -7,7 +7,7 @@ using namespace Kakoune; void test_buffer() { - Buffer buffer("test", Buffer::Flags::None, "allo ?\nmais que fais la police\n hein ?\n youpi\n"); + Buffer buffer("test", Buffer::Flags::None, { "allo ?\n", "mais que fais la police\n", " hein ?\n", " youpi\n" }); assert(buffer.line_count() == 4); BufferIterator i = buffer.begin(); @@ -40,7 +40,7 @@ void test_buffer() void test_editor() { - Buffer buffer("test", Buffer::Flags::None, "test\n\nyoupi\n"); + Buffer buffer("test", Buffer::Flags::None, { "test\n", "\n", "youpi\n" }); Editor editor(buffer); using namespace std::placeholders; @@ -56,7 +56,7 @@ void test_editor() void test_incremental_inserter() { - Buffer buffer("test", Buffer::Flags::None, "test\n\nyoupi\nmatin\n"); + Buffer buffer("test", Buffer::Flags::None, { "test\n", "\n", "youpi\n", "matin\n" }); Editor editor(buffer); editor.select(buffer.begin());