Skip to content

Commit 228ca9f

Browse files
committed
fix issue #349 (replace all bug)
1 parent 5739949 commit 228ca9f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

babi/file.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,21 @@ def __init__(
185185
self.wrapped = False
186186
self._start_x = file.buf.x + offset
187187
self._start_y = file.buf.y
188+
self.original_line_change_diff = 0
188189

189190
def __iter__(self) -> _SearchIter:
190191
return self
191192

193+
def calc_past_original_offset(self, y: int, match: Match[str], replacement: str):
194+
if not self.wrapped or y != self._start_y:
195+
return
196+
self.original_line_change_diff += len(replacement) - (match.end()-match.start())
197+
192198
def _stop_if_past_original(self, y: int, match: Match[str]) -> Found:
193199
if (
194200
self.wrapped and (
195201
y > self._start_y or
196-
y == self._start_y and match.start() >= self._start_x
202+
y == self._start_y and match.start() >= self._start_x + self.original_line_change_diff
197203
)
198204
):
199205
raise StopIteration()
@@ -487,6 +493,7 @@ def replace(
487493
count += 1
488494
with self.edit_action_context('replace', final=True):
489495
replaced = match.expand(replace)
496+
search.calc_past_original_offset(line_y, match, replaced)
490497
line = screen.file.buf[line_y]
491498
if '\n' in replaced:
492499
replaced_lines = replaced.split('\n')

0 commit comments

Comments
 (0)