From 475e8849a18cb854ad32873257b8fd74a0bedce8 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 26 Jun 2017 16:16:46 +0100 Subject: [PATCH] Fix replacing last eol with a single eol --- src/buffer.cc | 3 +++ src/selection.cc | 9 +++++---- test/regression/0-replace-last-eol-with-eol/cmd | 1 + test/regression/0-replace-last-eol-with-eol/in | 2 ++ test/regression/0-replace-last-eol-with-eol/out | 2 ++ 5 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 test/regression/0-replace-last-eol-with-eol/cmd create mode 100644 test/regression/0-replace-last-eol-with-eol/in create mode 100644 test/regression/0-replace-last-eol-with-eol/out diff --git a/src/buffer.cc b/src/buffer.cc index 0598319d..0bdb8866 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -584,7 +584,10 @@ BufferCoord Buffer::erase(BufferCoord begin, BufferCoord end) BufferCoord Buffer::replace(BufferCoord begin, BufferCoord end, StringView content) { if (is_end(end) and not content.empty() and content.back() == '\n') + { + end = back_coord(); content = content.substr(0, content.length() - 1); + } auto pos = erase(begin, end); return insert(pos, content); diff --git a/src/selection.cc b/src/selection.cc index 14879386..f24760c6 100644 --- a/src/selection.cc +++ b/src/selection.cc @@ -404,6 +404,7 @@ void SelectionList::insert(ConstArrayView strings, InsertMode mode, replace(*m_buffer, sel, 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); if (out_insert_pos) @@ -411,16 +412,16 @@ void SelectionList::insert(ConstArrayView strings, InsertMode mode, 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); else { // we want min and max from *before* we do any change auto& min = sel.min(); auto& max = sel.max(); - auto& change = m_buffer->changes_since(0).back(); - min = change.begin; - max = m_buffer->char_prev(change.end); + min = changes.back().begin; + max = m_buffer->char_prev(changes.back().end); } } else if (not str.empty()) diff --git a/test/regression/0-replace-last-eol-with-eol/cmd b/test/regression/0-replace-last-eol-with-eol/cmd new file mode 100644 index 00000000..6a3c76d6 --- /dev/null +++ b/test/regression/0-replace-last-eol-with-eol/cmd @@ -0,0 +1 @@ +yjR diff --git a/test/regression/0-replace-last-eol-with-eol/in b/test/regression/0-replace-last-eol-with-eol/in new file mode 100644 index 00000000..139597f9 --- /dev/null +++ b/test/regression/0-replace-last-eol-with-eol/in @@ -0,0 +1,2 @@ + + diff --git a/test/regression/0-replace-last-eol-with-eol/out b/test/regression/0-replace-last-eol-with-eol/out new file mode 100644 index 00000000..139597f9 --- /dev/null +++ b/test/regression/0-replace-last-eol-with-eol/out @@ -0,0 +1,2 @@ + +