Skip to content

Conversation

@stefanhaller
Copy link
Collaborator

This fixes various problems with setting the cursor position, and adjusting the origin for editable views. See individual commit messages for details.

In particular, this allows the cursor position to be outside of a view's visible area, which is a normal and reasonable thing to do.

This improves the situation where you have an editable view whose content is
taller than the screen, and you scroll it with the mouse wheel so that the
cursor leaves the visible area of the view. Previously we would clamp the cursor
position to the visible area, which means we would effectively move the cursor
in the opposite direction when you scroll. (But only visually; once you then
type a character, the view would scroll to show the real cursor position, and
the visual cursor would jump back to its real position.)

Also, fix an off-by-two error where the cursor would still draw when it is on
the lower frame of the view, or one line below; the reason is that we were using
Size() rather than InnerSize() to determine the valid visible area.
It is allowed to set it to a position outside the visible portion of the view;
this is needed if the view is scrolled and the cursor position is in the portion
of the view that is scrolled off-screen.

It is even allowed to set the position to invalid values with respect to the
view content (e.g. less than zero, or y greater than the length of the viewlines
array). It might seem reasonable to clamp to valid values, but we can't do that
because the methods are often called at a time where the viewlines array is not
up to date. It is the caller's responsibility to clamp to valid values.

Note that errors in client code that gets this wrong are harmless. The cursor
position is only used to draw the cursor; if it is out of bounds, the worst that
will happen is that the cursor blinks in the wrong place. We don't have to be
worried about out-of-bounds access to the viewlines array, for example; it is
not used for that.
We had an off-by-one error here where if the cursor is equal to the size of the
view, we would leave it there. That's already outside of the visible area, so we
need to scroll the view up by one line in that case to make the cursor visible.

In lazygit this would happen in commit descriptions that are so long that they
show a scrollbar.
stefanhaller added a commit to jesseduffield/lazygit that referenced this pull request May 23, 2025
See jesseduffield/gocui#80.

This fixes selecting hunks in the staging view that are longer than the screen.
Copy link
Owner

@jesseduffield jesseduffield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@stefanhaller stefanhaller merged commit 319bd37 into master May 29, 2025
1 check passed
@stefanhaller stefanhaller deleted the fix-cursor-position-problems branch May 29, 2025 12:30
stefanhaller added a commit to jesseduffield/lazygit that referenced this pull request May 29, 2025
See jesseduffield/gocui#80.

This fixes selecting hunks in the staging view that are longer than the screen.
stefanhaller added a commit to jesseduffield/lazygit that referenced this pull request May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants