summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pkg/gui/context/patch_explorer_context.go3
-rw-r--r--pkg/gui/patch_exploring/focus.go12
-rw-r--r--pkg/gui/patch_exploring/focus_test.go40
-rw-r--r--pkg/gui/patch_exploring/state.go4
4 files changed, 44 insertions, 15 deletions
diff --git a/pkg/gui/context/patch_explorer_context.go b/pkg/gui/context/patch_explorer_context.go
index 17ecae4ae..54a9356ac 100644
--- a/pkg/gui/context/patch_explorer_context.go
+++ b/pkg/gui/context/patch_explorer_context.go
@@ -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)
diff --git a/pkg/gui/patch_exploring/focus.go b/pkg/gui/patch_exploring/focus.go
index c3846235b..02fed7b28 100644
--- a/pkg/gui/patch_exploring/focus.go
+++ b/pkg/gui/patch_exploring/focus.go
@@ -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
diff --git a/pkg/gui/patch_exploring/focus_test.go b/pkg/gui/patch_exploring/focus_test.go
index eb3ed7c66..bbe4b2341 100644
--- a/pkg/gui/patch_exploring/focus_test.go
+++ b/pkg/gui/patch_exploring/focus_test.go
@@ -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))
})
}
}
diff --git a/pkg/gui/patch_exploring/state.go b/pkg/gui/patch_exploring/state.go
index ad5426805..356bff4f4 100644
--- a/pkg/gui/patch_exploring/state.go
+++ b/pkg/gui/patch_exploring/state.go
@@ -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)
}