Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions spyder_vim/tests/test_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -1943,3 +1943,28 @@ def test_gt_command(vim_bot):
cmd_line = vim.get_focus_widget()
qtbot.keyClicks(cmd_line, 'gT')
assert 0 == editor_stack.get_stack_index()


@pytest.mark.parametrize(
"text, command_list, result, cursor_pos",
[
('abc\ndef\nghi\n', ['J'], 'abc def\nghi\n', 3),
('a\nb\nc\n', ['V', 'J'], 'a b\nc\n', 1),
('a\nb\n\nc\n', ['V', '2j', 'J'], 'a b\nc\n', 1),
('a\nb\nc\n', ['V', 'j', 'J'], 'a b\nc\n', 1),
('a\nb\nc\n', ['V', '2j', 'J'], 'a b c\n', 3),
('a\nb\n\n\n \nc\n', ['j', 'V', '4j', 'J'], 'a\nb c\n', 3)
]
)
def test_J_command(vim_bot, text, command_list, result, cursor_pos):
"""Test J 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

64 changes: 63 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 = "bdehHjklLnNpPGyw$^0 \r\b%"
VIM_VISUAL_OPS = "bdehHjJklLnNpPGyw$^0 \r\b%"
VIM_VISUAL_PREFIX = "agi"
VIM_ARG_PREFIX = "fF\""

Expand Down Expand Up @@ -922,6 +922,68 @@ def D(self, repeat):
editor.cut()
self._widget.update_vim_cursor()

def J(self, repeat):
"""Join lines, with a minimum of two lines."""
editor = self._widget.editor()
cursor = editor.textCursor()
n_block = editor.blockCount()

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)
no_block_start = selection.cursor.blockNumber()

selection.cursor.setPosition(cursor_pos_end)
no_block_end = selection.cursor.blockNumber()

self.exit_visual_mode()

if no_block_start == no_block_end:
no_block_end += 1

cursor_pos_start = min([cursor_pos_start, cursor_pos_end])
else:
no_block_start = cursor.blockNumber()
no_block_end = cursor.blockNumber() + repeat
cursor_pos_start = cursor.position()

if no_block_end >= n_block - 1:
no_block_end = n_block - 1

if no_block_start == no_block_end:
return

n_line = no_block_end - no_block_start + 1
text_list = ['']
cursor.setPosition(cursor_pos_start)
for _ in range(n_line - 1):
cursor.movePosition(QTextCursor.NextBlock)
text = cursor.block().text().lstrip()
if text:
text_list.append(text)

# Replace text
cursor.setPosition(cursor_pos_start)
cursor.movePosition(QTextCursor.EndOfLine)
cursor.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor,
n_line - 1)
cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
cursor.insertText(' '.join(text_list))

# Move position of cursor
cursor.movePosition(QTextCursor.EndOfBlock)
cursor.movePosition(QTextCursor.Left, n=len(text_list[-1]) + 1)
editor.setTextCursor(cursor)

self._widget.update_vim_cursor()

def _cut_word(self, repeat, move_operation):
"""Cut words."""
editor = self._widget.editor()
Expand Down