Skip to content

Commit

Permalink
Merge pull request #45 from bgallois/add_features
Browse files Browse the repository at this point in the history
Add features
  • Loading branch information
goanpeca committed May 8, 2020
2 parents 1d47a13 + b5ff632 commit 9dd3c50
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
66 changes: 66 additions & 0 deletions spyder_vim/tests/test_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ def test_k_command(vim_bot):
assert new_line == line - 1


def test_arrowup_command(vim_bot):
"""Test k command (Cursor moves up)."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.stdkey_backspace()
cmd_line = vim.get_focus_widget()
line, _ = editor.get_cursor_line_column()
qtbot.keyPress(editor, Qt.Key_Up)
new_line, _ = editor.get_cursor_line_column()
assert new_line == line - 1


def test_h_command(vim_bot):
"""Test h command (Cursor moves to the left)."""
main, editor_stack, editor, vim, qtbot = vim_bot
Expand All @@ -187,6 +198,18 @@ def test_j_command(vim_bot):
assert new_line == line + 1


def test_arrowdown_command(vim_bot):
"""Test k command (Cursor moves up)."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.stdkey_backspace()
editor.stdkey_up(True)
cmd_line = vim.get_focus_widget()
line, _ = editor.get_cursor_line_column()
qtbot.keyPress(editor, Qt.Key_Down)
new_line, _ = editor.get_cursor_line_column()
assert new_line == line + 1


def test_l_shortchut(vim_bot):
"""Test j command (Cursor moves right)."""
main, editor_stack, editor, vim, qtbot = vim_bot
Expand All @@ -199,6 +222,30 @@ def test_l_shortchut(vim_bot):
assert new_col == col + 1


def test_arrowright_shortchut(vim_bot):
"""Test j command (Cursor moves right)."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.stdkey_backspace()
qtbot.keyPress(editor, Qt.Key_Left)
cmd_line = vim.get_focus_widget()
_, col = editor.get_cursor_line_column()
qtbot.keyPress(editor, Qt.Key_Right)
_, new_col = editor.get_cursor_line_column()
assert new_col == col + 1


def test_arrowleft_shortchut(vim_bot):
"""Test j command (Cursor moves right)."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.stdkey_backspace()
qtbot.keyPress(editor, Qt.Key_Right)
cmd_line = vim.get_focus_widget()
_, col = editor.get_cursor_line_column()
qtbot.keyPress(editor, Qt.Key_Left)
_, new_col = editor.get_cursor_line_column()
assert new_col == col - 1


def test_w_shortchut(vim_bot):
"""Test w command (Cursor moves to the next word)."""
main, editor_stack, editor, vim, qtbot = vim_bot
Expand Down Expand Up @@ -476,6 +523,25 @@ def test_u_command(vim_bot):
assert new_col == col - len('spam')


def test_d_command(vim_bot):
"""Delete selection."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.stdkey_backspace()
editor.go_to_line(3)
# editor.stdkey_up(True)
editor.moveCursor(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
lines, cols = editor.get_cursor_line_column()
editor.moveCursor(QTextCursor.StartOfLine, QTextCursor.KeepAnchor)
cmd_line = vim.get_focus_widget()
qtbot.keyPress(cmd_line, 'v')
qtbot.keyPress(cmd_line, 'l')
qtbot.keyPress(cmd_line, 'l')
qtbot.keyClicks(cmd_line, 'd')
editor.moveCursor(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
new_lines, new_cols = editor.get_cursor_line_column()
assert new_cols == cols - 2


def test_dd_command(vim_bot):
"""Delete line."""
main, editor_stack, editor, vim, qtbot = vim_bot
Expand Down
23 changes: 21 additions & 2 deletions spyder_vim/vim_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
RE_VIM_PREFIX_STR = r"^(\d*)([{prefixes}].|[^{prefixes}0123456789])(.*)$"
RE_VIM_PREFIX = re.compile(RE_VIM_PREFIX_STR.format(prefixes=VIM_PREFIX))

VIM_VISUAL_OPS = "hjklGyw"
VIM_VISUAL_OPS = "dhjklGyw"
VIM_VISUAL_PREFIX = "agi"

RE_VIM_VISUAL_PREFIX = re.compile(
Expand Down Expand Up @@ -388,12 +388,23 @@ def U(self, repeat):
pass

# %% Deletions
def d(self, repeat):
editor = self._widget.editor()
selection = editor.get_extra_selections('vim_visual')[0]
cursor = selection.cursor
editor.setTextCursor(cursor)
editor.cut()
self._widget.update_vim_cursor()

def dd(self, repeat):
"""Delete line."""
editor = self._widget.editor()
cursor = editor.textCursor()
cursor.movePosition(QTextCursor.StartOfLine)
cursor.movePosition(QTextCursor.Down, QTextCursor.KeepAnchor, repeat)
if not cursor.movePosition(QTextCursor.Down, QTextCursor.KeepAnchor, repeat):
cursor.movePosition(QTextCursor.Up)
cursor.movePosition(QTextCursor.EndOfLine)
cursor.movePosition(QTextCursor.End, QTextCursor.KeepAnchor, repeat)
editor.setTextCursor(cursor)
editor.cut()
self._update_selection_type("line")
Expand Down Expand Up @@ -664,6 +675,14 @@ def keyPressEvent(self, event):
elif event.key() == Qt.Key_Return:
self.setText(self.text() + "\r")
self.parent().on_return()
elif event.key() == Qt.Key_Left and not self.text():
self.setText("h")
elif event.key() == Qt.Key_Right and not self.text():
self.setText("l")
elif event.key() == Qt.Key_Up and not self.text():
self.setText("k")
elif event.key() == Qt.Key_Down and not self.text():
self.setText("j")
else:
QLineEdit.keyPressEvent(self, event)

Expand Down

0 comments on commit 9dd3c50

Please sign in to comment.