Skip to content
Open
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
9 changes: 4 additions & 5 deletions src/prompt_toolkit/key_binding/bindings/vi.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,10 @@ def cut(self, buffer: Buffer) -> tuple[Document, ClipboardData]:
from_ += buffer.cursor_position
to += buffer.cursor_position

# For Vi mode, the SelectionState does include the upper position,
# while `self.operator_range` does not. So, go one to the left, unless
# we're in the line mode, then we don't want to risk going to the
# previous line, and missing one line in the selection.
if self.type != TextObjectType.LINEWISE:
# Characterwise operator ranges have an exclusive upper bound, while
# Vi selections include it. Linewise and block selections must keep
# their endpoints to avoid shifting the selected rows or columns.
if self.type in (TextObjectType.EXCLUSIVE, TextObjectType.INCLUSIVE):
to -= 1

document = Document(
Expand Down
34 changes: 34 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from prompt_toolkit.key_binding.bindings.named_commands import prefix_meta
from prompt_toolkit.key_binding.key_bindings import KeyBindings
from prompt_toolkit.output import DummyOutput
from prompt_toolkit.selection import SelectionType
from prompt_toolkit.shortcuts import PromptSession


Expand Down Expand Up @@ -735,6 +736,39 @@ def test_vi_block_editing_empty_lines():
assert result.text == "***\n***\n***\n\n\n"


@pytest.mark.parametrize("operator", ["d", "c", "y"])
@pytest.mark.parametrize(
"text, start, movement, remaining, selected",
[
("abcd\nefgh\nijkl", "gg0", "jj", "bcd\nfgh\njkl", "a\ne\ni"),
("abcd\nefgh\nijkl", "G0", "kk", "bcd\nfgh\njkl", "a\ne\ni"),
("abcd\nefgh\nijkl", "gg0l", "jjl", "ad\neh\nil", "bc\nfg\njk"),
("abcd\nefgh\nijkl", "G0ll", "kkh", "ad\neh\nil", "bc\nfg\njk"),
("abcd\nefgh\nijkl", "gg0l", "", "acd\nefgh\nijkl", "b"),
("abcd\n\nx\nijkl", "gg0", "jjj", "bcd\n\n\njkl", "a\n\nx\ni"),
],
)
def test_vi_visual_block_operators(
text, start, movement, remaining, selected, operator
):
result, app = _feed_cli_with_input(
text.replace("\n", "\r")
+ "\x1b"
+ start
+ "\x16"
+ movement
+ operator
+ "\x1b\r",
editing_mode=EditingMode.VI,
multiline=True,
)

assert result.text == (text if operator == "y" else remaining)
clipboard_data = app.clipboard.get_data()
assert clipboard_data.text == selected
assert clipboard_data.type == SelectionType.BLOCK


def test_vi_visual_line_copy():
feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, multiline=True)

Expand Down
Loading