From f736ab6228f2b50183f62e829f29dae8b461f09f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=91=D1=8B=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=B2?= Date: Wed, 11 Nov 2026 18:53:47 +0500 Subject: [PATCH] Fix Vi visual block operator boundaries Keep both corners of a visual block selection intact when converting it to clipboard data. Adjusting the upper endpoint as for characterwise ranges changes the selected columns and can omit the final row. --- src/prompt_toolkit/key_binding/bindings/vi.py | 9 +++-- tests/test_cli.py | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/prompt_toolkit/key_binding/bindings/vi.py b/src/prompt_toolkit/key_binding/bindings/vi.py index e3fca1b98..7b3604c68 100644 --- a/src/prompt_toolkit/key_binding/bindings/vi.py +++ b/src/prompt_toolkit/key_binding/bindings/vi.py @@ -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( diff --git a/tests/test_cli.py b/tests/test_cli.py index a876f2993..91598f696 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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 @@ -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)