Skip to content

Commit

Permalink
Merge pull request #73 from ok97465/add_greater_less
Browse files Browse the repository at this point in the history
Add <<, >> command
  • Loading branch information
ccordoba12 committed Oct 26, 2020
2 parents 94a8ab7 + c896d6f commit 4d0bf82
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 1 deletion.
86 changes: 86 additions & 0 deletions spyder_vim/tests/test_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -2012,3 +2012,89 @@ def test_r_command(vim_bot, text, command_list, result, cursor_pos):

assert editor.toPlainText() == result
assert editor.textCursor().position() == cursor_pos


@pytest.mark.parametrize(
"text, command_list, result, cursor_pos",
[
('ab\ncdef\n', ['v', '>'], ' ab\ncdef\n', 4),
('ab\ncdef\n', ['V', '>'], ' ab\ncdef\n', 4),
('ab\n\ncdef\n \n', ['V', '3j', '>'], ' ab\n\n cdef\n \n', 4),
('ab\n\ncdef\n \n', ['l', 'v', '3j', '>'], ' ab\n\n cdef\n \n', 4)
]
)
def test_greater_visual_command(vim_bot, text, command_list, result,
cursor_pos):
"""Test >> command."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.set_text(text)

cmd_line = vim.get_focus_widget()
for command in command_list:
qtbot.keyClicks(cmd_line, command)

assert editor.toPlainText() == result
assert editor.textCursor().position() == cursor_pos


@pytest.mark.parametrize(
"text, command_list, result, cursor_pos",
[
('ab\ncdef\n', ['>>'], ' ab\ncdef\n', 4),
('ab\n\ncdef\n \n', ['4>>'], ' ab\n\n cdef\n \n', 4)
]
)
def test_greatergreater_command(vim_bot, text, command_list, result,
cursor_pos):
"""Test >> command."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.set_text(text)

cmd_line = vim.get_focus_widget()
for command in command_list:
qtbot.keyClicks(cmd_line, command)

assert editor.toPlainText() == result
assert editor.textCursor().position() == cursor_pos


@pytest.mark.parametrize(
"text, command_list, result, cursor_pos",
[
(' ab\ncdef\n', ['l', 'v', '<'], 'ab\ncdef\n', 0),
(' ab\ncdef\n', ['V', '<'], 'ab\ncdef\n', 0),
(' ab\n \n cdef\n \n', ['j', 'l', 'v', '2j', '<'], ' ab\n\ncdef\n \n', 4),
(' ab\n \n cdef\n \n', ['j', 'V', '2j', '<'], ' ab\n\ncdef\n \n', 4)
]
)
def test_less_visual_command(vim_bot, text, command_list, result, cursor_pos):
"""Test << command."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.set_text(text)

cmd_line = vim.get_focus_widget()
for command in command_list:
qtbot.keyClicks(cmd_line, command)

assert editor.toPlainText() == result
assert editor.textCursor().position() == cursor_pos


@pytest.mark.parametrize(
"text, command_list, result, cursor_pos",
[
(' ab\ncdef\n', ['l', '<<'], 'ab\ncdef\n', 0),
(' ab\n \n cdef\n \n', ['j', '3<<'], ' ab\n\ncdef\n \n', 4)
]
)
def test_lessless_command(vim_bot, text, command_list, result, cursor_pos):
"""Test << command."""
main, editor_stack, editor, vim, qtbot = vim_bot
editor.set_text(text)

cmd_line = vim.get_focus_widget()
for command in command_list:
qtbot.keyClicks(cmd_line, command)

assert editor.toPlainText() == result
assert editor.textCursor().position() == cursor_pos
131 changes: 130 additions & 1 deletion spyder_vim/vim_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 = "bdehHjJklLnNpPGyw$^0 \r\b%~"
VIM_VISUAL_OPS = "bdehHjJklLnNpPGyw$^0 \r\b%~<>"
VIM_VISUAL_PREFIX = "agi"
VIM_ARG_PREFIX = "fFr\""

Expand All @@ -49,6 +49,7 @@
"%": "PERCENT",
"~": "TILDE"
}
INDENT = " "


# %% Vim shortcuts
Expand Down Expand Up @@ -1276,6 +1277,134 @@ def TILDE(self, repeat):

self._widget.update_vim_cursor()

def _get_selected_block_number(self, repeat=1):
"""Return selected block number."""
editor = self._widget.editor()
cursor = self._editor_cursor()
if self.visual_mode:
selection = editor.get_extra_selections('vim_visual')[0]

cursor_pos_start, cursor_pos_end = self._get_selection_positions()
cursor_pos_start, cursor_pos_end = sorted([cursor_pos_start,
cursor_pos_end])
if self.visual_mode == 'line':
cursor_pos_end -= 1

selection.cursor.setPosition(cursor_pos_start)
start_block_no = selection.cursor.blockNumber()

selection.cursor.setPosition(cursor_pos_end)
end_block_no = selection.cursor.blockNumber()
else:
start_block_no = cursor.block().blockNumber()
end_block_no = start_block_no + repeat - 1
if end_block_no >= editor.blockCount():
end_block_no = editor.blockCount() - 1

return start_block_no, end_block_no

def _get_pos_of_block(self, block_no):
"""Return start position & end position of block."""
editor = self._widget.editor()
block = editor.document().findBlockByNumber(block_no)
start_pos = block.position()
end_pos = start_pos + block.length() - 1
return start_pos, end_pos

def _get_text_of_blocks(self, start_block_no, end_block_no):
"""Return text of blocks."""
text_list = []
editor = self._widget.editor()
for no in range(start_block_no, end_block_no + 1):
block = editor.document().findBlockByNumber(no)
text_list.append(block.text())

return text_list

def GREATER(self, repeat=1):
"""Shift lines rightwards in visual mode."""
cursor_pos_start, _ = self._get_selection_positions()
start_block_no, end_block_no = self._get_selected_block_number(1)
n_line = end_block_no - start_block_no + 1
self.exit_visual_mode()

editor = self._widget.editor()
cursor = self._editor_cursor()
cursor.setPosition(cursor_pos_start)
editor.setTextCursor(cursor)

self.GREATERGREATER(repeat=n_line)

def GREATERGREATER(self, repeat=1):
"""Shift lines rightwards."""
editor = self._widget.editor()
cursor = self._editor_cursor()

start_block_no, end_block_no = self._get_selected_block_number(repeat)
cursor_start_pos, __ = self._get_pos_of_block(start_block_no)
__, end_pos = self._get_pos_of_block(end_block_no)

text_list = self._get_text_of_blocks(start_block_no, end_block_no)
text_list_indent = []
for text in text_list:
if text:
text_list_indent.append(INDENT + text)
else:
text_list_indent.append(text)
text_indent = '\n'.join(text_list_indent)

# Replace text
cursor.setPosition(cursor_start_pos)
cursor.setPosition(end_pos, QTextCursor.KeepAnchor)
cursor.insertText(text_indent)

# Move cursor position
cursor.setPosition(cursor_start_pos)
editor.setTextCursor(cursor)
self.CARET()

def LESS(self, repeat=1):
"""Shift lines leftwards in visual mode."""
cursor_pos_start, _ = self._get_selection_positions()
start_block_no, end_block_no = self._get_selected_block_number(1)
n_line = end_block_no - start_block_no + 1
self.exit_visual_mode()

editor = self._widget.editor()
cursor = self._editor_cursor()
cursor.setPosition(cursor_pos_start)
editor.setTextCursor(cursor)

self.LESSLESS(repeat=n_line)

def LESSLESS(self, repeat=1):
"""Shift lines leftwards."""
editor = self._widget.editor()
cursor = self._editor_cursor()

start_block_no, end_block_no = self._get_selected_block_number(repeat)
cusor_start_pos, __ = self._get_pos_of_block(start_block_no)
__, end_pos = self._get_pos_of_block(end_block_no)

text_list = self._get_text_of_blocks(start_block_no, end_block_no)
text_list_indent = []
len_indent = len(INDENT)
for text in text_list:
n_space = len(text) - len(text.lstrip())
idx_discard = min([n_space, len_indent])
text_list_indent.append(text[idx_discard:])
text_indent = '\n'.join(text_list_indent)

# Replace text
cursor.setPosition(cusor_start_pos)
cursor.setPosition(end_pos, QTextCursor.KeepAnchor)
cursor.insertText(text_indent)

# Move cursor position
start_pos, __ = self._get_pos_of_block(start_block_no)
cursor.setPosition(cusor_start_pos)
editor.setTextCursor(cursor)
self.CARET()

# %% Vim commands
class VimCommands(object):
Expand Down

0 comments on commit 4d0bf82

Please sign in to comment.