Buffer takes a vector of lines as initial content

This commit is contained in:
Maxime Coste 2012-11-23 18:42:07 +01:00
parent 53be5c87d2
commit c32a7b9b74
3 changed files with 14 additions and 9 deletions

View File

@ -12,8 +12,7 @@
namespace Kakoune namespace Kakoune
{ {
Buffer::Buffer(String name, Flags flags, Buffer::Buffer(String name, Flags flags, std::vector<String> lines)
String initial_content)
: m_name(std::move(name)), m_flags(flags | Flags::NoUndo), : m_name(std::move(name)), m_flags(flags | Flags::NoUndo),
m_history(), m_history_cursor(m_history.begin()), m_history(), m_history_cursor(m_history.begin()),
m_last_save_undo_index(0), m_last_save_undo_index(0),
@ -22,9 +21,15 @@ Buffer::Buffer(String name, Flags flags,
m_options(GlobalOptions::instance()) m_options(GlobalOptions::instance())
{ {
BufferManager::instance().register_buffer(*this); BufferManager::instance().register_buffer(*this);
if (initial_content.empty() or initial_content.back() != '\n')
initial_content += '\n'; ByteCount pos = 0;
do_insert(begin(), std::move(initial_content)); 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); Editor editor_for_hooks(*this);
Context context(editor_for_hooks); Context context(editor_for_hooks);

View File

@ -101,7 +101,7 @@ public:
NoUndo = 8, NoUndo = 8,
}; };
Buffer(String name, Flags flags, String initial_content = "\n"); Buffer(String name, Flags flags, std::vector<String> lines = { "\n" });
Buffer(const Buffer&) = delete; Buffer(const Buffer&) = delete;
Buffer& operator= (const Buffer&) = delete; Buffer& operator= (const Buffer&) = delete;
~Buffer(); ~Buffer();

View File

@ -7,7 +7,7 @@ using namespace Kakoune;
void test_buffer() 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); assert(buffer.line_count() == 4);
BufferIterator i = buffer.begin(); BufferIterator i = buffer.begin();
@ -40,7 +40,7 @@ void test_buffer()
void test_editor() 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); Editor editor(buffer);
using namespace std::placeholders; using namespace std::placeholders;
@ -56,7 +56,7 @@ void test_editor()
void test_incremental_inserter() 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 editor(buffer);
editor.select(buffer.begin()); editor.select(buffer.begin());