Skip to content

Commit

Permalink
Merge pull request #59 from bgallois/fix_get_line
Browse files Browse the repository at this point in the history
Fix get line
  • Loading branch information
goanpeca committed Jun 9, 2020
2 parents 50fa9bd + 5aa9ec6 commit 00b1d2f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
21 changes: 21 additions & 0 deletions spyder_vim/tests/test_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,27 @@ def test_backward_search_regex_command(vim_bot):
assert index_test == [4, 3, 2, 1, 4, 1, 2, 3, 4, 1]


def test_cursor_position(vim_bot):
"""Test cursor position"""
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, '$a')
qtbot.keyClicks(editor, 'test')
editor.stdkey_escape()
text = editor.toPlainText()
expected_text = (' 123\n'
'line 1\n'
'line 2test\n'
'line 3\n'
'line 4')
assert text == expected_text
_, col = editor.get_cursor_line_column()
assert col == 10


def test_select_command_brackets(vim_bot):
"""Test a selection"""
main, editor_stack, editor, vim, qtbot = vim_bot
Expand Down
27 changes: 23 additions & 4 deletions spyder_vim/vim_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ def _get_line(self, editor_cursor, lines=1):
print("ERROR: editor_cursor must be an instance of QTextCursor")
else:
cursor.movePosition(QTextCursor.StartOfLine)
cursor.movePosition(QTextCursor.Down, QTextCursor.KeepAnchor,
cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor,
n=lines)
line = cursor.selectedText().replace('\u2029', '\n')
line += '\n'
return line

def _update_selection_type(self, selection_type):
Expand All @@ -166,6 +167,16 @@ def exit_visual_mode(self):
self._widget.update_vim_cursor()
self.visual_mode = False

def exit_insert_mode(self):
"""Exit insert mode."""
self.mode_changed.emit("normal")
editor = self._widget.editor()
cursor = self._editor_cursor()
editor.clear_extra_selections('vim_visual')
if cursor.atBlockEnd():
self.h()
self._widget.update_vim_cursor()

def search(self, key, reverse=False):
""""Search regular expressions key inside document"""
editor = self._widget.editor()
Expand Down Expand Up @@ -258,7 +269,8 @@ def j(self, repeat=1):
else:
self._move_selection(cur_block.position(), move_start=True)
else:
if cursor.atBlockEnd():
line = self._get_line(cursor)
if line != '\n' and cursor.atBlockEnd():
self._move_cursor(QTextCursor.Left)

def k(self, repeat=1):
Expand All @@ -281,7 +293,8 @@ def k(self, repeat=1):
else:
self._move_selection(cur_block.next().position())
else:
if cursor.atBlockEnd():
line = self._get_line(cursor)
if line != '\n' and cursor.atBlockEnd():
self._move_cursor(QTextCursor.Left)

def l(self, repeat=1): # analysis:ignore
Expand Down Expand Up @@ -587,7 +600,10 @@ def I(self, repeat):
def a(self, leftover=None, repeat=1):
"""Append text after the cursor."""
if not leftover:
self._move_cursor(QTextCursor.Right)
cursor = self._editor_cursor()
line = self._get_line(cursor)
if line != '\n':
self._move_cursor(QTextCursor.Right)
self._widget.editor().setFocus()
elif leftover in list("\"\'([{<>}])"):
editor = self._widget.editor()
Expand Down Expand Up @@ -679,6 +695,7 @@ def o(self, repeat):
cursor.insertText("\n")
editor.setTextCursor(cursor)
editor.setFocus()
self._widget.update_vim_cursor()

def O(self, repeat):
"""Begin a new line above the cursor and insert text."""
Expand All @@ -689,6 +706,7 @@ def O(self, repeat):
cursor.movePosition(QTextCursor.Up)
editor.setTextCursor(cursor)
editor.setFocus()
self._widget.update_vim_cursor()

# %% Editing and cases(visual)
def u(self, repeat):
Expand Down Expand Up @@ -1047,6 +1065,7 @@ def focusInEvent(self, event):
QLineEdit.focusInEvent(self, event)
self.clear()
self.parent().on_mode_changed("normal")
self.parent().vim_keys.exit_insert_mode()

def focusOutEvent(self, event):
"""Enter editor mode."""
Expand Down

0 comments on commit 00b1d2f

Please sign in to comment.