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:
parent
a37b14785f
commit
f9e31856cf
|
@ -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())
|
||||
return;
|
||||
|
||||
if (pos.is_end())
|
||||
--pos;
|
||||
if (pos.is_end() and content.back() != '\n')
|
||||
content += '\n';
|
||||
|
||||
m_current_undo_group.emplace_back(Modification::Insert, pos, content);
|
||||
do_insert(pos, content);
|
||||
m_current_undo_group.emplace_back(Modification::Insert, pos,
|
||||
std::move(content));
|
||||
do_insert(pos, m_current_undo_group.back().content);
|
||||
}
|
||||
|
||||
void Buffer::erase(BufferIterator begin, BufferIterator end)
|
||||
|
|
|
@ -111,7 +111,7 @@ public:
|
|||
|
||||
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);
|
||||
|
||||
size_t timestamp() const { return m_timestamp; }
|
||||
|
|
|
@ -251,7 +251,7 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
|
|||
ssize_t count = read(fd, data, 512);
|
||||
if (count > 0)
|
||||
{
|
||||
buffer->insert(buffer->end(), String(data, data + count));
|
||||
buffer->insert(buffer->end()-1, String(data, data + count));
|
||||
buffer->reset_undo_data();
|
||||
context.draw_ifn();
|
||||
}
|
||||
|
|
|
@ -353,8 +353,11 @@ IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode)
|
|||
last = first;
|
||||
break;
|
||||
}
|
||||
if (first.is_end())
|
||||
--first;
|
||||
if (last.is_end())
|
||||
--last;
|
||||
sel = Selection(first, last);
|
||||
|
||||
}
|
||||
if (mode == Mode::OpenLineBelow or mode == Mode::OpenLineAbove)
|
||||
{
|
||||
|
|
|
@ -89,7 +89,6 @@ Buffer* create_buffer_from_file(const String& filename)
|
|||
|
||||
Buffer* buffer = new Buffer(filename, Buffer::Type::File, "");
|
||||
|
||||
|
||||
String content;
|
||||
char buf[256];
|
||||
bool crlf = false;
|
||||
|
@ -118,7 +117,7 @@ Buffer* create_buffer_from_file(const String& filename)
|
|||
if (buf[pos] == '\r')
|
||||
crlf = true;
|
||||
|
||||
buffer->insert(buffer->end(), String(buf+start, buf+pos));
|
||||
buffer->insert(buffer->end()-1, String(buf+start, buf+pos));
|
||||
start = pos+1;
|
||||
}
|
||||
++pos;
|
||||
|
|
|
@ -27,6 +27,15 @@ void test_buffer()
|
|||
BufferIterator end = buffer.iterator_at({ 4, 5 }) + 1;
|
||||
String str = buffer.string(begin, end);
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user