Skip to content

Commit

Permalink
Merge pull request #72 from ok97465/add_r
Browse files Browse the repository at this point in the history
PR: Add r command
  • Loading branch information
ccordoba12 committed Aug 21, 2020
2 parents e4cf55e + 6fcad38 commit 94a8ab7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
23 changes: 23 additions & 0 deletions spyder_vim/tests/test_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -1989,3 +1989,26 @@ def test_TILDE_command(vim_bot, text, command_list, result, cursor_pos):

assert editor.toPlainText() == result
assert editor.textCursor().position() == cursor_pos


@pytest.mark.parametrize(
"text, command_list, result, cursor_pos",
[
('abcde', ['r1'], '1bcde', 0),
('abcde', ['2r1'], '11cde', 1),
('abcde', ['l', '5r1'], 'abcde', 1),
('a b\n22\nc \nde\n', ['j', 'V', '2j', 'r1'], 'a b\n11\n11\n11\n', 4),
('a b\ncdef\ng\n', ['v', 'l', 'j', 'r1'], '111\n11ef\ng\n', 0)
]
)
def test_r_command(vim_bot, text, command_list, result, cursor_pos):
"""Test r command."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.set_text(text)

cmd_line = vim.get_focus_widget()
for command in command_list:
qtbot.keyClicks(cmd_line, command)

assert editor.toPlainText() == result
assert editor.textCursor().position() == cursor_pos
37 changes: 35 additions & 2 deletions spyder_vim/vim_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

VIM_VISUAL_OPS = "bdehHjJklLnNpPGyw$^0 \r\b%~"
VIM_VISUAL_PREFIX = "agi"
VIM_ARG_PREFIX = "fF\""
VIM_ARG_PREFIX = "fFr\""

RE_VIM_VISUAL_PREFIX = re.compile(
RE_VIM_PREFIX_STR.format(prefixes=VIM_VISUAL_PREFIX))
Expand Down Expand Up @@ -74,7 +74,7 @@ def __call__(self, key, repeat):
leftover = ""
if key.startswith("_"):
return
elif key[0] in "fF":
elif key[0] in "fFr":
leftover = key[1]
key = key[0]
elif key[0] in "ia" and self.visual_mode == "char":
Expand Down Expand Up @@ -439,6 +439,39 @@ def F(self, leftover, repeat=1):
except IndexError:
pass

def r(self, leftover, repeat=1):
"""Replace the character under the cursor with character of argument."""
editor = self._widget.editor()
cursor = self._editor_cursor()

if self.visual_mode:
cur_pos, end_pos = self._get_selection_positions()
if self.visual_mode == 'char':
end_pos += 1
else:
cur_pos = cursor.position()
cursor.movePosition(QTextCursor.EndOfBlock)
line_end_pos = cursor.position()
if line_end_pos - cur_pos < repeat:
self._widget.update_vim_cursor()
return
end_pos = cur_pos + repeat

cursor.setPosition(cur_pos)
cursor.setPosition(end_pos, QTextCursor.KeepAnchor)
text = cursor.selectedText().replace('\u2029', '\n')
text_sub = re.sub(r'.', leftover, text)
cursor.insertText(text_sub)

if self.visual_mode:
self.exit_visual_mode()
cursor.setPosition(cur_pos)
editor.setTextCursor(cursor)
else:
self.h(1)

self._widget.update_vim_cursor()

def SPACE(self, repeat=1):
"""Move cursor to the right."""
self._move_cursor(QTextCursor.Right, repeat)
Expand Down

0 comments on commit 94a8ab7

Please sign in to comment.