Skip to content

feat: preserve cursor position across compact diff toggle - #274

Merged
umputun merged 2 commits into
masterfrom
feat/compact-preserve-position
Jul 17, 2026
Merged

umputun merged 2 commits into
masterfrom
feat/compact-preserve-position

Conversation

@umputun

@umputun umputun commented Jul 17, 2026

Copy link
Copy Markdown
Owner

preserves the diff cursor position across the compact toggle (C) instead of resetting to the first hunk.

what changed: toggleCompactMode captures a semantic anchor (source line + change type, with a nearest-hunk fallback) before the re-fetch and restores it in handleFileLoaded via applyCompactAnchor. The anchor is seq-tagged to the exact reload, so an intervening file switch or R reload drops a stale anchor. Compact→full always resolves the exact line (full is a superset); full→compact falls back to the nearest hunk when a context line is dropped. In collapsed mode adjustCursorIfHidden keeps the restored cursor off a hidden removed line.

scope: this keeps the cursor on the same line and centers the view on it, not the exact scroll offset. The compact and full views show different content, so an identical on-screen position often can't exist (e.g. 20 rows down in full but only 5 rows above the hunk in compact), and top/bottom hunks clamp to the edge. So it's "same hunk visible, cursor on the right line" rather than pixel-exact scroll restoration.

Related to #271.

umputun added 2 commits July 16, 2026 18:27
toggling compact mode (C) re-fetches the current file and used to reset
the cursor to the first hunk via the shared handleFileLoaded path. now the
cursor position is captured as a semantic anchor (source line plus change
type, with a nearest-hunk fallback) before the toggle and restored after
the re-fetch, so the reviewer stays on the same hunk instead of jumping to
the top. the anchor is seq-tagged to the exact reload so an intervening
file switch or R reload discards it.

Related to #271
- [major] [app/ui/loaders.go] applyCompactAnchor could land the cursor on a
  collapsed-hidden line (removed-line anchor or a modify hunk's removed start
  after expandedHunks is cleared); call adjustCursorIfHidden in both direct-set
  branches so the restored cursor stays visible in collapsed mode. doc note in
  CLAUDE.md + godoc updated.
- [test-gap] [app/ui/loaders_test.go] add stale-anchor seq-drop test
- [test-gap] [app/ui/loaders_test.go] make the divider-anchor subtest discriminate
  hunk-fallback from skipInitialDividers
- [test-gap] [app/ui/loaders_test.go] exercise applyCompactAnchor with a removed-line anchor
- [test-gap] [app/ui/loaders_test.go] cover collapsed+compact keeps the restored cursor visible
- [test-gap] [app/ui/handlers_test.go] add compact->full C-key integration test
Copilot AI review requested due to automatic review settings July 17, 2026 01:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@umputun
umputun merged commit adfa202 into master Jul 17, 2026
5 checks passed
@umputun
umputun deleted the feat/compact-preserve-position branch July 17, 2026 01:20
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.

2 participants