From 3621939fd0d76dee978fbac21d0e90cb4e534ebc Mon Sep 17 00:00:00 2001 From: yuxino Date: Thu, 12 Nov 2026 21:06:56 +0800 Subject: [PATCH] Fix cursor visibility when scrolling wrapped lines --- src/prompt_toolkit/layout/containers.py | 12 ++-- tests/test_layout.py | 75 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/src/prompt_toolkit/layout/containers.py b/src/prompt_toolkit/layout/containers.py index fbc54d8c0..a59cf48f3 100644 --- a/src/prompt_toolkit/layout/containers.py +++ b/src/prompt_toolkit/layout/containers.py @@ -2349,25 +2349,25 @@ def get_line_height(lineno: int) -> int: # scroll to this line. line_height = get_line_height(ui_content.cursor_position.y) if line_height > height - scroll_offsets_top: - # Calculate the height of the text before the cursor (including - # line prefixes). - text_before_height = ui_content.get_height_for_line( + # Include the character under the cursor, which can wrap onto + # the next line even when all preceding text fits. + cursor_height = ui_content.get_height_for_line( ui_content.cursor_position.y, width, self.get_line_prefix, - slice_stop=ui_content.cursor_position.x, + slice_stop=ui_content.cursor_position.x + 1, ) # Adjust scroll offset. self.vertical_scroll = ui_content.cursor_position.y self.vertical_scroll_2 = min( - text_before_height - 1, # Keep the cursor visible. + cursor_height - 1, # Keep the cursor visible. line_height - height, # Avoid blank lines at the bottom when scrolling up again. self.vertical_scroll_2, ) self.vertical_scroll_2 = max( - 0, text_before_height - height, self.vertical_scroll_2 + 0, cursor_height - height, self.vertical_scroll_2 ) return else: diff --git a/tests/test_layout.py b/tests/test_layout.py index cbbbcd0bd..896faa0fa 100644 --- a/tests/test_layout.py +++ b/tests/test_layout.py @@ -2,9 +2,15 @@ import pytest +from prompt_toolkit import Application +from prompt_toolkit.data_structures import Point, Size +from prompt_toolkit.input.defaults import create_pipe_input from prompt_toolkit.layout import InvalidLayoutError, Layout from prompt_toolkit.layout.containers import HSplit, VSplit, Window from prompt_toolkit.layout.controls import BufferControl +from prompt_toolkit.output import DummyOutput +from prompt_toolkit.utils import get_cwidth +from prompt_toolkit.widgets import TextArea def test_layout_class(): @@ -51,3 +57,72 @@ def test_layout_class(): def test_create_invalid_layout(): with pytest.raises(InvalidLayoutError): Layout(HSplit([])) + + +@pytest.mark.parametrize("height", [1, 2]) +@pytest.mark.parametrize( + "prompt,prefix,character", + [ + ("", "", " "), + (">>> ", "", " "), + ("界> ", "", " "), + ("", ".. ", " "), + (">>> ", ".. ", " "), + (">>> ", "界 ", " "), + (">>> ", "", "界"), + (">>> ", ".. ", "界"), + ], +) +def test_wrapped_text_area_cursor(height, prompt, prefix, character): + class Output(DummyOutput): + def get_size(self): + return Size(rows=height, columns=20) + + prefix_width = get_cwidth(prefix) + text = "x" * (20 - get_cwidth(prompt) - prefix_width - 1) + text += "x" * ((height - 1) * (20 - prefix_width)) + area = TextArea( + multiline=height > 1, + height=height, + wrap_lines=True, + prompt=prompt, + get_line_prefix=(lambda lineno, wrap_count: prefix) if prefix else None, + ) + # Fill the window, type at the wrap boundary, then move back and insert. + keys = iter([text, "x", character, "\x1b[D", "X"]) + positions = [] + last_document = None + + with create_pipe_input() as inp: + + def after_render(app): + nonlocal last_document + if app.is_done or area.document == last_document: + return + last_document = area.document + info = area.window.render_info + positions.append((info.cursor_position, area.window.vertical_scroll_2)) + key = next(keys, None) + if key is None: + app.exit() + else: + inp.send_text(key) + + app = Application( + layout=Layout(area), + input=inp, + output=Output(), + full_screen=True, + after_render=after_render, + ) + app.run() + + assert positions[1:] == [ + (Point(x=19, y=height - 1), 0), + (Point(x=prefix_width, y=height - 1), 1), + (Point(x=prefix_width + get_cwidth(character), y=height - 1), 1), + (Point(x=prefix_width, y=height - 1), 1), + (Point(x=prefix_width + 1, y=height - 1), 1), + ] + assert area.text == text + "xX" + character + assert area.buffer.cursor_position == len(text) + 2