From 7fa83125ab2df51a13889005560d0cdde5b4a599 Mon Sep 17 00:00:00 2001 From: ellieplayswow <164806095+ellieplayswow@users.noreply.github.com> Date: Wed, 30 Apr 2025 21:17:48 +0100 Subject: [PATCH] bugfix(replace): fix issue with regexes targeting empty lines replacing with nothing (#347) --- src/tab.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/tab.rs b/src/tab.rs index c6644e0..c45361c 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -286,13 +286,26 @@ impl EditorTab { end.index = index + len; editor.start_change(); + // if index = 0 and len = 0, we are targeting and deleting an empty line + // we'll move either cursor or end to delete the newline + if index == 0 && len == 0 { + if cursor.line > 0 { + // move the cursor up one line + cursor.line -= 1; + cursor.index = + editor.with_buffer(|buffer| buffer.lines[cursor.line].text().len()); + } else if cursor.line + 1 < editor.with_buffer(|buffer| buffer.lines.len()) { + // move the end down one line + end.line += 1; + end.index = 0; + } + } editor.delete_range(cursor, end); cursor = editor.insert_at(cursor, replace, None); editor.set_cursor(cursor); // Need to disable selection to prevent the new cursor showing selection to old location editor.set_selection(Selection::None); editor.finish_change(); - return true; }