Skip to content

Commit

Permalink
Set cursor position after replacement to last selected range
Browse files Browse the repository at this point in the history
  • Loading branch information
lieryan authored and jonathanslenders committed Mar 6, 2020
1 parent 862be13 commit 5e45d33
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pyvim/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ def substitute(editor, range_start, range_end, search, replace, flags):
""" Substitute /search/ with /replace/ over a range of text """
def get_line_index_iterator(cursor_position_row, range_start, range_end):
if not range_start:
assert not range_end
range_start = range_end = cursor_position_row
else:
range_start = int(range_start) - 1
Expand All @@ -739,7 +740,8 @@ def get_transform_callback(search, replace, flags):
transform_callback = get_transform_callback(search, replace, flags)
new_text = buffer.transform_lines(line_index_iterator, transform_callback)

new_cursor_position_row = int(range_start) - 1 if range_start and not range_end else cursor_position_row
assert len(line_index_iterator) >= 1
new_cursor_position_row = line_index_iterator[-1]

# update text buffer
buffer.document = Document(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_substitute.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ def test_substitute_range(editor, editor_buffer):
assert 'Violet is blue,' in editor_buffer.buffer.text
assert 'And so are you.' in editor_buffer.buffer.text
# FIXME: vim would have set the cursor position on last substituted line
# but we set the cursor position on the end_range even when there
# is not substitution there
# assert editor_buffer.buffer.cursor_position \
# == editor_buffer.buffer.text.index('Violet')
assert editor_buffer.buffer.cursor_position \
== editor_buffer.buffer.text.index('Sugar')


def test_substitute_range_boundaries(editor, editor_buffer):
Expand Down

0 comments on commit 5e45d33

Please sign in to comment.