Skip to content

Commit

Permalink
Merge pull request #36 from mariacamilarg/master
Browse files Browse the repository at this point in the history
PR: Command e to move to end of current word
  • Loading branch information
goanpeca committed May 19, 2020
2 parents c764ad4 + 24b65ae commit 62ffd57
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
49 changes: 49 additions & 0 deletions spyder_vim/tests/test_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,18 @@ def test_w_shortchut(vim_bot):
assert new_col == col + 1


def test_w_shortchut_char_mode(vim_bot):
"""Test w command (Cursor moves to the next word)."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.go_to_line(2)
editor.moveCursor(QTextCursor.StartOfLine, QTextCursor.KeepAnchor)
editor.stdkey_backspace()
cmd_line = vim.get_focus_widget()
qtbot.keyClicks(cmd_line, 'v2wy')
clipboard = QApplication.clipboard().text().replace('\u2029', '\n')
assert clipboard == 'line 1\nl'


def test_b_shortchut(vim_bot):
"""Test b command (Cursor moves to the previous word)."""
main, editor_stack, editor, vim, qtbot = vim_bot
Expand All @@ -619,6 +631,43 @@ def test_b_shortchut(vim_bot):
assert new_col == col - 1


def test_b_shortchut_char_mode(vim_bot):
"""Test b command (Cursor moves to the next word)."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.stdkey_backspace()
editor.go_to_line(3)
editor.moveCursor(QTextCursor.StartOfLine, QTextCursor.KeepAnchor)
cmd_line = vim.get_focus_widget()
qtbot.keyClicks(cmd_line, 'v2byp')
clipboard = QApplication.clipboard().text().replace('\u2029', '\n')
assert clipboard == 'line 1\nl'


def test_e_shortchut(vim_bot):
"""Test e command (Cursor moves to the previous word)."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.stdkey_backspace()
qtbot.keyPress(editor, Qt.Key_Left)
editor.moveCursor(QTextCursor.EndOfWord, QTextCursor.KeepAnchor)
cmd_line = vim.get_focus_widget()
_, col = editor.get_cursor_line_column()
qtbot.keyClicks(cmd_line, 'e')
_, new_col = editor.get_cursor_line_column()
assert new_col == col - 1


def test_e_shortchut_char_mode(vim_bot):
"""Test b command (Cursor moves to the next word)."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.stdkey_backspace()
editor.go_to_line(3)
editor.moveCursor(QTextCursor.StartOfLine, QTextCursor.KeepAnchor)
cmd_line = vim.get_focus_widget()
qtbot.keyClicks(cmd_line, 'v3ey')
clipboard = QApplication.clipboard().text().replace('\u2029', '\n')
assert clipboard == 'line 2\nline'


def test_f_shortchut(vim_bot):
"""Cursor moves to the next ocurrence of a character."""
main, editor_stack, editor, vim, qtbot = vim_bot
Expand Down
37 changes: 34 additions & 3 deletions spyder_vim/vim_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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 = "dhjklnNGyw$^0 \r\b"
VIM_VISUAL_OPS = "bdehjklnNGyw$^0 \r\b"
VIM_VISUAL_PREFIX = "agi"
VIM_ARG_PREFIX = "fF"

Expand Down Expand Up @@ -295,14 +295,45 @@ def l(self, repeat=1): # analysis:ignore

def w(self, repeat=1):
"""Move to the next word."""
self._move_cursor(QTextCursor.NextWord, repeat)
self._move_cursor(QTextCursor.NextWord)
cursor = self._editor_cursor()
if cursor.atBlockEnd():
self._move_cursor(QTextCursor.NextWord)
if self.visual_mode == 'char':
cursor = self._editor_cursor()
self._move_selection(cursor.position())
if repeat > 1:
self.w(repeat - 1)

def b(self, repeat=1):
"""Move to the previous word."""
self._move_cursor(QTextCursor.PreviousWord, repeat)
self._move_cursor(QTextCursor.PreviousWord)
cursor = self._editor_cursor()
if cursor.atBlockEnd():
self._move_cursor(QTextCursor.PreviousWord)
if self.visual_mode == 'char':
cursor = self._editor_cursor()
self._move_selection(cursor.position(), move_start=True)
if repeat > 1:
self.b(repeat - 1)

def e(self, repeat=1):
"""Go to end of current word.
Or go to end of next word if cursor is currently on whitespace.
"""
cursor = self._editor_cursor()
cur_pos_in_block = cursor.positionInBlock()
char = self._get_line(cursor)[cur_pos_in_block:cur_pos_in_block + 2]
if not char.isalnum():
self.w(repeat=1)
self._move_cursor(QTextCursor.EndOfWord)
self.h(repeat=1)
if self.visual_mode == 'char':
cursor = self._editor_cursor()
self._move_selection(cursor.position())
if repeat > 1:
self.e(repeat - 1)

def f(self, leftover, repeat=1):
"""Go to the next ocurrence of a character."""
Expand Down

0 comments on commit 62ffd57

Please sign in to comment.