If selected line is outside, move it to the middle of the view

Previously, the current line was only moved as much as necessary so that it's in
view again. This had the problem that when jumping downwards from hunk to hunk
with the right-arrow key, only the first line of the new hunk was shown at the
bottom of the window. I prefer to put the selected line in the middle of the
view in this case, so that I can see more of the newly selected hunk.

This has the consequence that when scrolling through the view line by line using
down-arrow, the view jumps by half a screen whenever I reach the bottom. I can
see how some users might be opposed to this change, but I happen to like it too,
because it allows me to see more context of what's ahead.
This commit is contained in:
Stefan Haller 2023-03-12 17:17:31 +01:00
parent 4a4afc4639
commit 79c11a0458
4 changed files with 44 additions and 15 deletions

View file

@ -109,8 +109,9 @@ func (self *PatchExplorerContext) FocusSelection() {
_, viewHeight := view.Size()
bufferHeight := viewHeight - 1
_, origin := view.Origin()
numLines := view.LinesHeight()
newOriginY := state.CalculateOrigin(origin, bufferHeight)
newOriginY := state.CalculateOrigin(origin, bufferHeight, numLines)
_ = view.SetOriginY(newOriginY)

View file

@ -2,21 +2,19 @@ package patch_exploring
import "github.com/jesseduffield/lazygit/pkg/utils"
func calculateOrigin(currentOrigin int, bufferHeight int, firstLineIdx int, lastLineIdx int, selectedLineIdx int, mode selectMode) int {
func calculateOrigin(currentOrigin int, bufferHeight int, numLines int, firstLineIdx int, lastLineIdx int, selectedLineIdx int, mode selectMode) int {
needToSeeIdx, wantToSeeIdx := getNeedAndWantLineIdx(firstLineIdx, lastLineIdx, selectedLineIdx, mode)
return calculateNewOriginWithNeededAndWantedIdx(currentOrigin, bufferHeight, needToSeeIdx, wantToSeeIdx)
return calculateNewOriginWithNeededAndWantedIdx(currentOrigin, bufferHeight, numLines, needToSeeIdx, wantToSeeIdx)
}
// we want to scroll our origin so that the index we need to see is in view
// and the other index we want to see (e.g. the other side of a line range)
// is as close to being in view as possible.
func calculateNewOriginWithNeededAndWantedIdx(currentOrigin int, bufferHeight int, needToSeeIdx int, wantToSeeIdx int) int {
func calculateNewOriginWithNeededAndWantedIdx(currentOrigin int, bufferHeight int, numLines int, needToSeeIdx int, wantToSeeIdx int) int {
origin := currentOrigin
if needToSeeIdx < currentOrigin {
origin = needToSeeIdx
} else if needToSeeIdx > currentOrigin+bufferHeight {
origin = needToSeeIdx - bufferHeight
if needToSeeIdx < currentOrigin || needToSeeIdx > currentOrigin+bufferHeight {
origin = utils.Max(utils.Min(needToSeeIdx-bufferHeight/2, numLines-bufferHeight-1), 0)
}
bottom := origin + bufferHeight

View file

@ -11,6 +11,7 @@ func TestNewOrigin(t *testing.T) {
name string
origin int
bufferHeight int
numLines int
firstLineIdx int
lastLineIdx int
selectedLineIdx int
@ -20,29 +21,54 @@ func TestNewOrigin(t *testing.T) {
scenarios := []scenario{
{
name: "selection above scroll window",
name: "selection above scroll window, enough room to put it in the middle",
origin: 250,
bufferHeight: 100,
numLines: 500,
firstLineIdx: 210,
lastLineIdx: 210,
selectedLineIdx: 210,
selectMode: LINE,
expected: 160,
},
{
name: "selection above scroll window, not enough room to put it in the middle",
origin: 50,
bufferHeight: 100,
numLines: 500,
firstLineIdx: 10,
lastLineIdx: 10,
selectedLineIdx: 10,
selectMode: LINE,
expected: 10,
expected: 0,
},
{
name: "selection below scroll window",
name: "selection below scroll window, enough room to put it in the middle",
origin: 0,
bufferHeight: 100,
numLines: 500,
firstLineIdx: 150,
lastLineIdx: 150,
selectedLineIdx: 150,
selectMode: LINE,
expected: 50,
expected: 100,
},
{
name: "selection below scroll window, not enough room to put it in the middle",
origin: 0,
bufferHeight: 100,
numLines: 200,
firstLineIdx: 199,
lastLineIdx: 199,
selectedLineIdx: 199,
selectMode: LINE,
expected: 99,
},
{
name: "selection within scroll window",
origin: 0,
bufferHeight: 100,
numLines: 500,
firstLineIdx: 50,
lastLineIdx: 50,
selectedLineIdx: 50,
@ -53,6 +79,7 @@ func TestNewOrigin(t *testing.T) {
name: "range ending below scroll window with selection at end of range",
origin: 0,
bufferHeight: 100,
numLines: 500,
firstLineIdx: 40,
lastLineIdx: 150,
selectedLineIdx: 150,
@ -63,6 +90,7 @@ func TestNewOrigin(t *testing.T) {
name: "range ending below scroll window with selection at beginning of range",
origin: 0,
bufferHeight: 100,
numLines: 500,
firstLineIdx: 40,
lastLineIdx: 150,
selectedLineIdx: 40,
@ -73,6 +101,7 @@ func TestNewOrigin(t *testing.T) {
name: "range starting above scroll window with selection at beginning of range",
origin: 50,
bufferHeight: 100,
numLines: 500,
firstLineIdx: 40,
lastLineIdx: 150,
selectedLineIdx: 40,
@ -83,6 +112,7 @@ func TestNewOrigin(t *testing.T) {
name: "hunk extending beyond both bounds of scroll window",
origin: 50,
bufferHeight: 100,
numLines: 500,
firstLineIdx: 40,
lastLineIdx: 200,
selectedLineIdx: 70,
@ -94,7 +124,7 @@ func TestNewOrigin(t *testing.T) {
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
assert.EqualValues(t, s.expected, calculateOrigin(s.origin, s.bufferHeight, s.firstLineIdx, s.lastLineIdx, s.selectedLineIdx, s.selectMode))
assert.EqualValues(t, s.expected, calculateOrigin(s.origin, s.bufferHeight, s.numLines, s.firstLineIdx, s.lastLineIdx, s.selectedLineIdx, s.selectMode))
})
}
}

View file

@ -216,8 +216,8 @@ func (s *State) SelectTop() {
s.SelectLine(0)
}
func (s *State) CalculateOrigin(currentOrigin int, bufferHeight int) int {
func (s *State) CalculateOrigin(currentOrigin int, bufferHeight int, numLines int) int {
firstLineIdx, lastLineIdx := s.SelectedRange()
return calculateOrigin(currentOrigin, bufferHeight, firstLineIdx, lastLineIdx, s.GetSelectedLineIdx(), s.selectMode)
return calculateOrigin(currentOrigin, bufferHeight, numLines, firstLineIdx, lastLineIdx, s.GetSelectedLineIdx(), s.selectMode)
}