Select the line in the middle of the content, not the viewport

Pressing space in the focused main view starts the selection at the middle
row of the viewport. When the diff is shorter than the viewport that row is
empty space below the content, so the selection clamps onto the last line
instead of landing somewhere useful. Anchor on the middle of the visible
content instead; once the content fills the view this is the same row as
before.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Stefan Haller 2026-06-16 16:53:54 +02:00
parent 9db3094e80
commit 4c6af62424
2 changed files with 12 additions and 2 deletions

View file

@ -2095,6 +2095,16 @@ func (v *View) SelectedLineIdx() int {
return seletedLineIdx
}
// MiddleVisibleLineIdx returns the index of the view line at the middle of the
// content currently on screen. When the content is taller than the viewport this is
// the middle row of the viewport; when it's shorter, it's the middle of the content,
// so the result lands within the content rather than in the empty space below it.
func (v *View) MiddleVisibleLineIdx() int {
top := v.OriginY()
bottom := min(top+v.InnerHeight(), v.ViewLinesHeight())
return (top + bottom) / 2
}
// expected to only be used in tests
func (v *View) SelectedLine() string {
v.writeMutex.Lock()

View file

@ -203,8 +203,8 @@ func (self *MainViewController) toggleSelection() error {
v.Highlight = false
return nil
}
// Start the selection in the middle of the visible area.
showSelectionAtLine(v, v.OriginY()+v.InnerHeight()/2, false)
// Start the selection in the middle of the visible content.
showSelectionAtLine(v, v.MiddleVisibleLineIdx(), false)
return nil
}