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: Fix cursor at end of line and brackets selection #56

Merged
merged 2 commits into from
Jun 6, 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
28 changes: 28 additions & 0 deletions spyder_vim/tests/test_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,34 @@ def test_backward_search_regex_command(vim_bot):
assert index_test == [4, 3, 2, 1, 4, 1, 2, 3, 4, 1]


def test_select_command_brackets(vim_bot):
"""Test a selection"""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.stdkey_backspace()
cmd_line = vim.get_focus_widget()
line, _ = editor.get_cursor_line_column()
qtbot.keyClicks(cmd_line, 'o')
qtbot.keyClicks(editor, '(aa(bbb[test]bbb)aa)')
qtbot.keyClicks(cmd_line, '$h')
qtbot.keyClicks(cmd_line, 'va(y')
clipboard = QApplication.clipboard().text()
assert clipboard == '(aa(bbb[test]bbb)aa)'


def test_select_command_brackets(vim_bot):
"""Test a selection"""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.stdkey_backspace()
cmd_line = vim.get_focus_widget()
line, _ = editor.get_cursor_line_column()
qtbot.keyClicks(cmd_line, 'o')
qtbot.keyClicks(editor, '(aa(bbb[test]bbb)aa)')
qtbot.keyClicks(cmd_line, '$h')
qtbot.keyClicks(cmd_line, 'vi(y')
clipboard = QApplication.clipboard().text()
assert clipboard == 'aa(bbb[test]bbb)aa'


def test_a_command_open_bracket(vim_bot):
"""Test a selection"""
main, editor_stack, editor, vim, qtbot = vim_bot
Expand Down
92 changes: 74 additions & 18 deletions spyder_vim/vim_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ def j(self, repeat=1):
self._move_selection(cur_block.next().position())
else:
self._move_selection(cur_block.position(), move_start=True)
else:
if cursor.atBlockEnd():
self._move_cursor(QTextCursor.Left)

def k(self, repeat=1):
"""Move the cursor up."""
Expand All @@ -272,6 +275,9 @@ def k(self, repeat=1):
self._move_selection(cur_block.position(), move_start=True)
else:
self._move_selection(cur_block.next().position())
else:
if cursor.atBlockEnd():
self._move_cursor(QTextCursor.Left)

def l(self, repeat=1): # analysis:ignore
"""Move cursor to the right."""
Expand Down Expand Up @@ -494,6 +500,39 @@ def i(self, leftover=None, repeat=1):
text = editor.toPlainText()
position = cursor.position()
# Find the starting position
start_position = -1
if leftover == "(":
leftover = ")"
elif leftover == "[":
leftover = "]"
elif leftover == "{":
leftover = "}"
elif leftover == "<":
leftover = ">"
stack = []
stack.append(leftover)
for i, j in enumerate(reversed(text[0:position])):
if j == "(" and stack[-1] == ")":
stack.pop()
elif j == "[" and stack[-1] == "]":
stack.pop()
elif j == "{" and stack[-1] == "}":
stack.pop()
elif j == "\"" and stack[-1] == "\"":
stack.pop()
elif j == "\'" and stack[-1] == "\'":
stack.pop()
elif j == "<" and stack[-1] == ">":
stack.pop()
elif j in list(")]}>\"\'"):
stack.append(j)
if not stack:
start_position = len(text[0:position]) - i
break
if start_position == -1:
return

# Find the matching character
if leftover == ")":
leftover = "("
elif leftover == "]":
Expand All @@ -502,14 +541,6 @@ def i(self, leftover=None, repeat=1):
leftover = "{"
elif leftover == ">":
leftover = "<"
start_position = -1
for i in reversed(range(position)):
if text[i] == leftover:
start_position = i + 1
break
if start_position == -1:
return
# Find the matching character
stack = []
stack.append(leftover)
end_position = -1
Expand All @@ -526,7 +557,7 @@ def i(self, leftover=None, repeat=1):
stack.pop()
elif j == ">" and stack[-1] == "<":
stack.pop()
elif j in list("([{\"\'"):
elif j in list("([{<\"\'"):
stack.append(j)
if not stack:
end_position = i + start_position - 1
Expand Down Expand Up @@ -559,6 +590,39 @@ def a(self, leftover=None, repeat=1):
text = editor.toPlainText()
position = cursor.position()
# Find the starting position
start_position = -1
if leftover == "(":
leftover = ")"
elif leftover == "[":
leftover = "]"
elif leftover == "{":
leftover = "}"
elif leftover == "<":
leftover = ">"
stack = []
stack.append(leftover)
for i, j in enumerate(reversed(text[0:position])):
if j == "(" and stack[-1] == ")":
stack.pop()
elif j == "[" and stack[-1] == "]":
stack.pop()
elif j == "{" and stack[-1] == "}":
stack.pop()
elif j == "\"" and stack[-1] == "\"":
stack.pop()
elif j == "\'" and stack[-1] == "\'":
stack.pop()
elif j == "<" and stack[-1] == ">":
stack.pop()
elif j in list(")]}>\"\'"):
stack.append(j)
if not stack:
start_position = len(text[0:position]) - i
break
if start_position == -1:
return

# Find the matching character
if leftover == ")":
leftover = "("
elif leftover == "]":
Expand All @@ -567,14 +631,6 @@ def a(self, leftover=None, repeat=1):
leftover = "{"
elif leftover == ">":
leftover = "<"
start_position = -1
for i in reversed(range(position)):
if text[i] == leftover:
start_position = i + 1
break
if start_position == -1:
return
# Find the matching character
stack = []
stack.append(leftover)
end_position = -1
Expand All @@ -591,7 +647,7 @@ def a(self, leftover=None, repeat=1):
stack.pop()
elif j == ">" and stack[-1] == "<":
stack.pop()
elif j in list("([{\"\'"):
elif j in list("([{<\"\'"):
stack.append(j)
if not stack:
end_position = i + start_position - 1
Expand Down