mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-03 00:57:52 +02:00
fix lbl scrolling
This commit is contained in:
52
pkg/gui/lbl/focus.go
Normal file
52
pkg/gui/lbl/focus.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package lbl
|
||||||
|
|
||||||
|
import "github.com/jesseduffield/lazygit/pkg/utils"
|
||||||
|
|
||||||
|
func calculateOrigin(currentOrigin int, bufferHeight int, firstLineIdx int, lastLineIdx int, selectedLineIdx int, mode selectMode) int {
|
||||||
|
needToSeeIdx, wantToSeeIdx := getNeedAndWantLineIdx(firstLineIdx, lastLineIdx, selectedLineIdx, mode)
|
||||||
|
|
||||||
|
return calculateNewOriginWithNeededAndWantedIdx(currentOrigin, bufferHeight, 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 in as close to being in view as possible.
|
||||||
|
func calculateNewOriginWithNeededAndWantedIdx(currentOrigin int, bufferHeight int, needToSeeIdx int, wantToSeeIdx int) int {
|
||||||
|
origin := currentOrigin
|
||||||
|
if needToSeeIdx < currentOrigin {
|
||||||
|
origin = needToSeeIdx
|
||||||
|
} else if needToSeeIdx > currentOrigin+bufferHeight {
|
||||||
|
origin = needToSeeIdx - bufferHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
bottom := origin + bufferHeight
|
||||||
|
|
||||||
|
if wantToSeeIdx < origin {
|
||||||
|
requiredChange := origin - wantToSeeIdx
|
||||||
|
allowedChange := bottom - needToSeeIdx
|
||||||
|
return origin - utils.Min(requiredChange, allowedChange)
|
||||||
|
} else if wantToSeeIdx > origin+bufferHeight {
|
||||||
|
requiredChange := wantToSeeIdx - bottom
|
||||||
|
allowedChange := needToSeeIdx - origin
|
||||||
|
return origin + utils.Min(requiredChange, allowedChange)
|
||||||
|
} else {
|
||||||
|
return origin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNeedAndWantLineIdx(firstLineIdx int, lastLineIdx int, selectedLineIdx int, mode selectMode) (int, int) {
|
||||||
|
switch mode {
|
||||||
|
case LINE:
|
||||||
|
return selectedLineIdx, selectedLineIdx
|
||||||
|
case RANGE:
|
||||||
|
if selectedLineIdx == firstLineIdx {
|
||||||
|
return firstLineIdx, lastLineIdx
|
||||||
|
} else {
|
||||||
|
return lastLineIdx, firstLineIdx
|
||||||
|
}
|
||||||
|
case HUNK:
|
||||||
|
return firstLineIdx, lastLineIdx
|
||||||
|
default:
|
||||||
|
panic("unknown mode")
|
||||||
|
}
|
||||||
|
}
|
100
pkg/gui/lbl/focus_test.go
Normal file
100
pkg/gui/lbl/focus_test.go
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
package lbl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewOrigin(t *testing.T) {
|
||||||
|
type scenario struct {
|
||||||
|
name string
|
||||||
|
origin int
|
||||||
|
bufferHeight int
|
||||||
|
firstLineIdx int
|
||||||
|
lastLineIdx int
|
||||||
|
selectedLineIdx int
|
||||||
|
selectMode selectMode
|
||||||
|
expected int
|
||||||
|
}
|
||||||
|
|
||||||
|
scenarios := []scenario{
|
||||||
|
{
|
||||||
|
name: "selection above scroll window",
|
||||||
|
origin: 50,
|
||||||
|
bufferHeight: 100,
|
||||||
|
firstLineIdx: 10,
|
||||||
|
lastLineIdx: 10,
|
||||||
|
selectedLineIdx: 10,
|
||||||
|
selectMode: LINE,
|
||||||
|
expected: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "selection below scroll window",
|
||||||
|
origin: 0,
|
||||||
|
bufferHeight: 100,
|
||||||
|
firstLineIdx: 150,
|
||||||
|
lastLineIdx: 150,
|
||||||
|
selectedLineIdx: 150,
|
||||||
|
selectMode: LINE,
|
||||||
|
expected: 50,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "selection within scroll window",
|
||||||
|
origin: 0,
|
||||||
|
bufferHeight: 100,
|
||||||
|
firstLineIdx: 50,
|
||||||
|
lastLineIdx: 50,
|
||||||
|
selectedLineIdx: 50,
|
||||||
|
selectMode: LINE,
|
||||||
|
expected: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "range ending below scroll window with selection at end of range",
|
||||||
|
origin: 0,
|
||||||
|
bufferHeight: 100,
|
||||||
|
firstLineIdx: 40,
|
||||||
|
lastLineIdx: 150,
|
||||||
|
selectedLineIdx: 150,
|
||||||
|
selectMode: RANGE,
|
||||||
|
expected: 50,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "range ending below scroll window with selection at beginning of range",
|
||||||
|
origin: 0,
|
||||||
|
bufferHeight: 100,
|
||||||
|
firstLineIdx: 40,
|
||||||
|
lastLineIdx: 150,
|
||||||
|
selectedLineIdx: 40,
|
||||||
|
selectMode: RANGE,
|
||||||
|
expected: 40,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "range starting above scroll window with selection at beginning of range",
|
||||||
|
origin: 50,
|
||||||
|
bufferHeight: 100,
|
||||||
|
firstLineIdx: 40,
|
||||||
|
lastLineIdx: 150,
|
||||||
|
selectedLineIdx: 40,
|
||||||
|
selectMode: RANGE,
|
||||||
|
expected: 40,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "hunk extending beyond both bounds of scroll window",
|
||||||
|
origin: 50,
|
||||||
|
bufferHeight: 100,
|
||||||
|
firstLineIdx: 40,
|
||||||
|
lastLineIdx: 200,
|
||||||
|
selectedLineIdx: 70,
|
||||||
|
selectMode: HUNK,
|
||||||
|
expected: 40,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -189,3 +189,9 @@ func (s *State) SelectTop() {
|
|||||||
s.SetLineSelectMode()
|
s.SetLineSelectMode()
|
||||||
s.SelectLine(0)
|
s.SelectLine(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *State) CalculateOrigin(currentOrigin int, bufferHeight int) int {
|
||||||
|
firstLineIdx, lastLineIdx := s.SelectedRange()
|
||||||
|
|
||||||
|
return calculateOrigin(currentOrigin, bufferHeight, firstLineIdx, lastLineIdx, s.GetSelectedLineIdx(), s.selectMode)
|
||||||
|
}
|
||||||
|
@ -155,25 +155,16 @@ func (gui *Gui) focusSelection(state *LblPanelState) error {
|
|||||||
bufferHeight := viewHeight - 1
|
bufferHeight := viewHeight - 1
|
||||||
_, origin := stagingView.Origin()
|
_, origin := stagingView.Origin()
|
||||||
|
|
||||||
firstLineIdx, lastLineIdx := state.SelectedRange()
|
selectedLineIdx := state.GetSelectedLineIdx()
|
||||||
|
|
||||||
margin := 0 // we may want to have a margin in place to show context but right now I'm thinking we keep this at zero
|
newOrigin := state.CalculateOrigin(origin, bufferHeight)
|
||||||
|
|
||||||
var newOrigin int
|
|
||||||
if firstLineIdx-origin < margin {
|
|
||||||
newOrigin = firstLineIdx - margin
|
|
||||||
} else if lastLineIdx-origin > bufferHeight-margin {
|
|
||||||
newOrigin = lastLineIdx - bufferHeight + margin
|
|
||||||
} else {
|
|
||||||
newOrigin = origin
|
|
||||||
}
|
|
||||||
|
|
||||||
gui.g.Update(func(*gocui.Gui) error {
|
gui.g.Update(func(*gocui.Gui) error {
|
||||||
if err := stagingView.SetOrigin(0, newOrigin); err != nil {
|
if err := stagingView.SetOrigin(0, newOrigin); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return stagingView.SetCursor(0, state.GetSelectedLineIdx()-newOrigin)
|
return stagingView.SetCursor(0, selectedLineIdx-newOrigin)
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Reference in New Issue
Block a user