Buffer: Allow inserting at the end when the inserted text ends with an end of line

Adapted other code in consequence and added unit tests so that this behavior
is maintained.
This commit is contained in:
Maxime Coste 2012-09-10 19:26:17 +02:00
parent a37b14785f
commit f9e31856cf
6 changed files with 22 additions and 10 deletions

View File

@ -367,16 +367,17 @@ void Buffer::apply_modification(const Modification& modification)
} }
} }
void Buffer::insert(BufferIterator pos, const String& content) void Buffer::insert(BufferIterator pos, String content)
{ {
if (content.empty()) if (content.empty())
return; return;
if (pos.is_end()) if (pos.is_end() and content.back() != '\n')
--pos; content += '\n';
m_current_undo_group.emplace_back(Modification::Insert, pos, content); m_current_undo_group.emplace_back(Modification::Insert, pos,
do_insert(pos, content); std::move(content));
do_insert(pos, m_current_undo_group.back().content);
} }
void Buffer::erase(BufferIterator begin, BufferIterator end) void Buffer::erase(BufferIterator begin, BufferIterator end)

View File

@ -111,7 +111,7 @@ public:
Type type() const { return m_type; } Type type() const { return m_type; }
void insert(BufferIterator pos, const String& content); void insert(BufferIterator pos, String content);
void erase(BufferIterator begin, BufferIterator end); void erase(BufferIterator begin, BufferIterator end);
size_t timestamp() const { return m_timestamp; } size_t timestamp() const { return m_timestamp; }

View File

@ -251,7 +251,7 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
ssize_t count = read(fd, data, 512); ssize_t count = read(fd, data, 512);
if (count > 0) if (count > 0)
{ {
buffer->insert(buffer->end(), String(data, data + count)); buffer->insert(buffer->end()-1, String(data, data + count));
buffer->reset_undo_data(); buffer->reset_undo_data();
context.draw_ifn(); context.draw_ifn();
} }

View File

@ -353,8 +353,11 @@ IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode)
last = first; last = first;
break; break;
} }
if (first.is_end())
--first;
if (last.is_end())
--last;
sel = Selection(first, last); sel = Selection(first, last);
} }
if (mode == Mode::OpenLineBelow or mode == Mode::OpenLineAbove) if (mode == Mode::OpenLineBelow or mode == Mode::OpenLineAbove)
{ {

View File

@ -89,7 +89,6 @@ Buffer* create_buffer_from_file(const String& filename)
Buffer* buffer = new Buffer(filename, Buffer::Type::File, ""); Buffer* buffer = new Buffer(filename, Buffer::Type::File, "");
String content; String content;
char buf[256]; char buf[256];
bool crlf = false; bool crlf = false;
@ -118,7 +117,7 @@ Buffer* create_buffer_from_file(const String& filename)
if (buf[pos] == '\r') if (buf[pos] == '\r')
crlf = true; crlf = true;
buffer->insert(buffer->end(), String(buf+start, buf+pos)); buffer->insert(buffer->end()-1, String(buf+start, buf+pos));
start = pos+1; start = pos+1;
} }
++pos; ++pos;

View File

@ -27,6 +27,15 @@ void test_buffer()
BufferIterator end = buffer.iterator_at({ 4, 5 }) + 1; BufferIterator end = buffer.iterator_at({ 4, 5 }) + 1;
String str = buffer.string(begin, end); String str = buffer.string(begin, end);
assert(str == "youpi"); assert(str == "youpi");
// check insert at end behaviour: auto add end of line if necessary
begin = buffer.end() - 1;
buffer.insert(buffer.end(), "tchou");
assert(buffer.string(begin+1, buffer.end()) == "tchou\n");
begin = buffer.end() - 1;
buffer.insert(buffer.end(), "kanaky\n");
assert(buffer.string(begin+1, buffer.end()) == "kanaky\n");
} }
void test_editor() void test_editor()