Fix replacing last eol with a single eol

This commit is contained in:
Maxime Coste 2017-06-26 16:16:46 +01:00
parent f41d78083a
commit 475e8849a1
5 changed files with 13 additions and 4 deletions

View File

@ -584,7 +584,10 @@ BufferCoord Buffer::erase(BufferCoord begin, BufferCoord end)
BufferCoord Buffer::replace(BufferCoord begin, BufferCoord end, StringView content) BufferCoord Buffer::replace(BufferCoord begin, BufferCoord end, StringView content)
{ {
if (is_end(end) and not content.empty() and content.back() == '\n') if (is_end(end) and not content.empty() and content.back() == '\n')
{
end = back_coord();
content = content.substr(0, content.length() - 1); content = content.substr(0, content.length() - 1);
}
auto pos = erase(begin, end); auto pos = erase(begin, end);
return insert(pos, content); return insert(pos, content);

View File

@ -404,6 +404,7 @@ void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
replace(*m_buffer, sel, str) replace(*m_buffer, sel, str)
: m_buffer->insert(changes_tracker.get_new_coord(insert_pos[index]), str); : m_buffer->insert(changes_tracker.get_new_coord(insert_pos[index]), str);
size_t old_timestamp = m_timestamp;
changes_tracker.update(*m_buffer, m_timestamp); changes_tracker.update(*m_buffer, m_timestamp);
if (out_insert_pos) if (out_insert_pos)
@ -411,16 +412,16 @@ void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
if (mode == InsertMode::Replace) if (mode == InsertMode::Replace)
{ {
if (str.empty()) auto changes = m_buffer->changes_since(old_timestamp);
if (changes.size() < 2) // Nothing got inserted, either str was empty, or just \n at end of buffer
sel.anchor() = sel.cursor() = m_buffer->clamp(pos); sel.anchor() = sel.cursor() = m_buffer->clamp(pos);
else else
{ {
// we want min and max from *before* we do any change // we want min and max from *before* we do any change
auto& min = sel.min(); auto& min = sel.min();
auto& max = sel.max(); auto& max = sel.max();
auto& change = m_buffer->changes_since(0).back(); min = changes.back().begin;
min = change.begin; max = m_buffer->char_prev(changes.back().end);
max = m_buffer->char_prev(change.end);
} }
} }
else if (not str.empty()) else if (not str.empty())

View File

@ -0,0 +1 @@
yjR

View File

@ -0,0 +1,2 @@

View File

@ -0,0 +1,2 @@