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

Fixed behavior of h, l and I, gg commands added #15

Merged
merged 4 commits into from
Dec 6, 2016
Merged
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
41 changes: 32 additions & 9 deletions spyder_vim/vim_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


VIM_COMMAND_PREFIX = ":!/?"
VIM_PREFIX = "cdfFmrtTyzZ@'`\"<>"
VIM_PREFIX = "cdfFgmrtTyzZ@'`\"<>"
RE_VIM_PREFIX_STR = r"^(\d*)([{prefixes}].|[^{prefixes}0123456789])(.*)$"
RE_VIM_PREFIX = re.compile(RE_VIM_PREFIX_STR.format(prefixes=VIM_PREFIX))
SYMBOLS_REPLACEMENT = {
Expand Down Expand Up @@ -46,16 +46,24 @@ def __call__(self, key, repeat):
method(repeat)

def _move_cursor(self, movement, repeat=1):
editor = self._widget.editor()
cursor = editor.textCursor()
cursor = self._editor_cursor()
cursor.movePosition(movement, n=repeat)
editor.setTextCursor(cursor)
self._widget.editor().setTextCursor(cursor)
self._widget.update_vim_cursor()

def _editor_cursor(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful helper, thanks. Could you use it in _move_cursor() above too ?

"""returns editor's cursor object"""
editor = self._widget.editor()
cursor = editor.textCursor()
return cursor

# %% Movement
def h(self, repeat=1):
# TODO: stop at start of line
self._move_cursor(QTextCursor.Left, repeat)
cursor = self._editor_cursor()
if not cursor.atBlockStart():
self._move_cursor(QTextCursor.Left)
if repeat > 1:
self.h(repeat-1)

def j(self, repeat=1):
self._move_cursor(QTextCursor.Down, repeat)
Expand All @@ -64,8 +72,11 @@ def k(self, repeat=1):
self._move_cursor(QTextCursor.Up, repeat)

def l(self, repeat=1):
# TODO: stop at end of line
self._move_cursor(QTextCursor.Right, repeat)
cursor = self._editor_cursor()
if not cursor.atBlockEnd():
self._move_cursor(QTextCursor.Right)
if repeat > 1:
self.l(repeat-1)

def w(self, repeat=1):
self._move_cursor(QTextCursor.NextWord, repeat)
Expand All @@ -79,14 +90,24 @@ def DOLLAR(self, repeat=1):
def ZERO(self, repeat=1):
self._move_cursor(QTextCursor.StartOfLine)

def G(self, repeat):
def G(self, repeat=-1):
if repeat == -1:
self._move_cursor(QTextCursor.End)
else:
self.gg(repeat)

def gg(self, repeat=1):
editor = self._widget.editor()
editor.go_to_line(repeat)
self._widget.update_vim_cursor()

# %% Insertion
def i(self, repeat):
self._widget.editor().setFocus()

def I(self, repeat):
self._move_cursor(QTextCursor.StartOfLine)
self._widget.editor().setFocus()

def a(self, repeat):
self.l()
Expand Down Expand Up @@ -294,6 +315,8 @@ def on_text_changed(self, text):
if text.startswith("0"):
# Special case to simplify regexp
repeat, key, leftover = 1, "0", text[1:]
elif text.startswith("G"):
repeat, key, leftover = -1, "G", text[1:]
else:
match = RE_VIM_PREFIX.match(text)
if not match:
Expand Down