def _scroll_when_linewrapping(self, ui_content, width, height, cli):
        scroll_offsets_bottom = self.scroll_offsets.bottom
        scroll_offsets_top = self.scroll_offsets.top

        # We don't have horizontal scrolling.
        self.horizontal_scroll = 0

        # If the current line consumes more than the whole window height,
        # then we have to scroll vertically inside this line. (We don't take
        # the scroll offsets into account for this.)
        # Also, ignore the scroll offsets in this case. Just set the vertical
        # scroll to this line.
        if ui_content.get_height_for_line(ui_content.cursor_position.y, width) > height - scroll_offsets_top:
            # Calculate the height of the text before the cursor, with the line
            # containing the cursor included, and the character belowe the
            # cursor included as well.
            line = explode_tokens(ui_content.get_line(ui_content.cursor_position.y))
            text_before_cursor = token_list_to_text(line[:ui_content.cursor_position.x + 1])
            text_before_height = UIContent.get_height_for_text(text_before_cursor, width)

            # Adjust scroll offset.
            self.vertical_scroll = ui_content.cursor_position.y
            self.vertical_scroll_2 = min(text_before_height - 1, self.vertical_scroll_2)
            self.vertical_scroll_2 = max(0, text_before_height - height, self.vertical_scroll_2)
            return
        else:
            self.vertical_scroll_2 = 0

        # Current line doesn't consume the whole height. Take scroll offsets into account.
        def get_min_vertical_scroll():
            # Make sure that the cursor line is not below the bottom.
            # (Calculate how many lines can be shown between the cursor and the .)
            used_height = 0
            prev_lineno = ui_content.cursor_position.y

            for lineno in range(ui_content.cursor_position.y, -1, -1):
                used_height += ui_content.get_height_for_line(lineno, width)

                if used_height > height - scroll_offsets_bottom:
                    return prev_lineno
                else:
                    prev_lineno = lineno
            return 0

        def get_max_vertical_scroll():
            # Make sure that the cursor line is not above the top.
            prev_lineno = ui_content.cursor_position.y
            used_height = 0

            for lineno in range(ui_content.cursor_position.y - 1, -1, -1):
                used_height += ui_content.get_height_for_line(lineno, width)

                if used_height > scroll_offsets_top:
                    return prev_lineno
                else:
                    prev_lineno = lineno
            return prev_lineno

        def get_topmost_visible():
            """
            Calculate the upper most line that can be visible, while the bottom
            is still visible. We should not allow scroll more than this if
            `allow_scroll_beyond_bottom` is false.
            """
            prev_lineno = ui_content.line_count - 1
            used_height = 0
            for lineno in range(ui_content.line_count - 1, -1, -1):
                used_height += ui_content.get_height_for_line(lineno, width)
                if used_height > height:
                    return prev_lineno
                else:
                    prev_lineno = lineno
            return prev_lineno

        # Scroll vertically. (Make sure that the whole line which contains the
        # cursor is visible.
        topmost_visible = get_topmost_visible()

            # Note: the `min(topmost_visible, ...)` is to make sure that we
            # don't require scrolling up because of the bottom scroll offset,
            # when we are at the end of the document.
        self.vertical_scroll = max(self.vertical_scroll, min(topmost_visible, get_min_vertical_scroll()))
        self.vertical_scroll = min(self.vertical_scroll, get_max_vertical_scroll())

        # Disallow scrolling beyond bottom?
        if not self.allow_scroll_beyond_bottom(cli):
            self.vertical_scroll = min(self.vertical_scroll, topmost_visible)
Scroll to make sure the cursor position is visible and that we maintain
dataset/ETHPy150Open jonathanslenders/python-prompt-toolkit/prompt_toolkit/layout/containers.py/Window._scroll_when_linewrapping