Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust search start index when replacing on original line #350

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions babi/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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')
Expand Down
13 changes: 13 additions & 0 deletions tests/features/replace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading