fix: keep moved-line selections in view - #4156
Open
mvanhorn wants to merge 1 commit into
Open
Conversation
MoveLinesUp applies its own newline-boundary compensation to the selection after the buffer edit event updates coordinates. When the cursor sits on the compensated endpoint, Cursor.Loc is left one line away from the endpoint it should track, so Relocate scrolls to a stale position and the moved selection can end up above the viewport. Record which selection endpoint the cursor is on before the move and restore Cursor.Loc to that endpoint after compensation, in both MoveLinesUp and MoveLinesDown. Fixes micro-editor#4134
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.
Summary
MoveLinesUpnow keeps the moved selection in view by re-pointing the cursor at the selection endpoint it was already on before the view relocates. The same synchronization is applied toMoveLinesDown, whose compensation path has the identical structure.Why this matters
Moving a multi-line selection upward can push the selected text above the viewport while the view stays put, so the selection scrolls out of sight (#4134).
MoveLinesDowndoes not show the same behavior, which is what makes the bug feel arbitrary in normal editing.The cause is a disagreement between two coordinate updates. The buffer edit event updates cursor and selection coordinates, and then
MoveLinesUpapplies its own newline-boundary compensation toCurSelection[sel]. When the cursor happens to sit on the endpoint being compensated,Cursor.Locis left one line away from the endpoint it should track.Relocatescrolls toCursor.Loc, so the view follows a stale position and the moved lines can end up off screen.Changes
Cursor.Locto that endpoint after the compensation step, in bothMoveLinesUpandMoveLinesDown.internal/action/movelines_test.gowith regression coverage for both directions and both selection orientations.Behavior is unchanged when there is no selection, at the first and last buffer line, and for selections taller than the viewport: the active endpoint is followed, with no attempt to fit an oversized selection on screen.
Testing
New tests cover:
Verified against
master: the new tests fail without the change (cursor = {0 9}, active selection endpoint = {0 8}) and pass with it.Fixes #4134