Fix textarea scrolling past end of content#105
Open
only-keval wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes jesseduffield/lazygit#5633
Problem
When a textarea's content overflows the view and is then deleted or shortened, the view remains scrolled past the end of the content, showing blank space instead of snapping back to display the remaining content. This affects both horizontal scrolling in single-line textareas and vertical scrolling in multi-line textareas.
Root Cause
The
updatedCursorAndOrigin()function only considered the cursor position when calculating the view's origin (scroll offset), not the actual content length. When content was deleted, the cursor position might still satisfy the scrolling condition (cursor > origin + usableSize mathematically), leaving the view scrolled even though no content existed in that region.Additionally, when content ended with a newline, the blank line that should follow wasn't being rendered because the dimension calculation didn't account for it.
Solution
Cache content dimensions: Added
maxContentXandmaxContentYfields toTextAreathat are updated whenever cells are rebuilt. This provides O(1) access to content dimensions during rendering.Clamp origin to content bounds: Modified
updatedCursorAndOrigin()to accept acontentLenparameter and ensure the origin never exceedscontentLen - usableSize. This prevents scrolling into blank space past the actual content.Account for trailing newlines: When content ends with a newline, increment the max Y dimension to account for the blank line that follows, ensuring proper rendering and cursor positioning.
Changes
TextArea: AddedmaxContentX,maxContentYcaching withcomputeMaxDimensions()andGetContentDimensions()updatedCursorAndOrigin(): Now takes content length parameter and clamps origin appropriatelyRenderTextArea(): Passes content dimensions toupdatedCursorAndOrigin()Testing