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

PR: Add r command #72

Merged
merged 4 commits into from
Aug 21, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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