From e9b5a194784e4633b938176ffc79e345568cc8ae Mon Sep 17 00:00:00 2001 From: Silas Kraume Date: Thu, 1 Aug 2024 14:49:57 +0200 Subject: [PATCH] adjust search start index when replacing on original line --- babi/file.py | 6 ++++++ tests/features/replace_test.py | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/babi/file.py b/babi/file.py index 852c1974..01651547 100644 --- a/babi/file.py +++ b/babi/file.py @@ -189,6 +189,11 @@ def __init__( def __iter__(self) -> _SearchIter: return self + def replaced(self, y: int, match: Match[str], new: str) -> None: + if not self.wrapped or y != self._start_y: + return + self._start_x += len(new) - match.end() - match.start() + def _stop_if_past_original(self, y: int, match: Match[str]) -> Found: if ( self.wrapped and ( @@ -487,6 +492,7 @@ def replace( count += 1 with self.edit_action_context('replace', final=True): replaced = match.expand(replace) + search.replaced(line_y, match, replaced) line = screen.file.buf[line_y] if '\n' in replaced: replaced_lines = replaced.split('\n') diff --git a/tests/features/replace_test.py b/tests/features/replace_test.py index f8644502..262dd1a7 100644 --- a/tests/features/replace_test.py +++ b/tests/features/replace_test.py @@ -239,6 +239,19 @@ def test_replace_multiple_occurrences_in_line(run): h.await_text('bqbq') +def test_replace_multiple_occurences_with_line_length_change(run): + with run() as h, and_exit(h): + h.press('a_a_') + h.press('^\\') + h.await_text('search (to replace):') + h.press_and_enter('a') + h.await_text('replace with:') + h.press_and_enter('XXX') + h.await_text('replace [yes, no, all]?') + h.press('a') + h.await_text('XXX_XXX_') + + def test_replace_after_wrapping(run, ten_lines): with run(str(ten_lines)) as h, and_exit(h): h.press('Down')