1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-13 01:30:53 +02:00

Make SelectedLine/SelectedLineIdx work in staging/stagingSecondary views

While we try to keep the view's cursor position in sync with the context state's
selectedLineIdx (at least when pressing up or down), there are enough situations
where the two run out of sync; for example when initially opening the view, or
after staging a hunk, or when scrolling the view using the wheel. While it would
be possible to fix these situations to keep them always in sync, it doesn't seem
worth it, because the view's cursor position isn't really used for anything
else. So we rather special-case the SelectedLine/SelectedLineIdx functions of
ViewDriver to query the context state's selectedLineIdx directly if it is a
patch explorer context.
This commit is contained in:
stk
2023-02-15 21:04:12 +01:00
parent 31fcec16d9
commit ff2a799200
5 changed files with 34 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import (
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/samber/lo"
)
@ -88,6 +89,9 @@ func (self *ViewDriver) Content(matcher *matcher) *ViewDriver {
func (self *ViewDriver) SelectedLine(matcher *matcher) *ViewDriver {
self.t.matchString(matcher, fmt.Sprintf("%s: Unexpected selected line.", self.context),
func() string {
if idx, ok := self.selectedLineIdxInPatchExplorer(); ok {
return self.getView().BufferLines()[idx]
}
return self.getView().SelectedLine()
},
)
@ -98,13 +102,25 @@ func (self *ViewDriver) SelectedLine(matcher *matcher) *ViewDriver {
// asserts on the index of the selected line. 0 is the first index, representing the line at the top of the view.
func (self *ViewDriver) SelectedLineIdx(expected int) *ViewDriver {
self.t.assertWithRetries(func() (bool, string) {
actual := self.getView().SelectedLineIdx()
actual, ok := self.selectedLineIdxInPatchExplorer()
if !ok {
actual = self.getView().SelectedLineIdx()
}
return expected == actual, fmt.Sprintf("%s: Expected selected line index to be %d, got %d", self.context, expected, actual)
})
return self
}
func (self *ViewDriver) selectedLineIdxInPatchExplorer() (int, bool) {
context := self.t.gui.ContextForView(self.getView().Name())
patchExplorerContext, ok := context.(types.IPatchExplorerContext)
if ok && patchExplorerContext.GetState() != nil {
return patchExplorerContext.GetState().GetSelectedLineIdx(), true
}
return 0, false
}
// focus the view (assumes the view is a side-view)
func (self *ViewDriver) Focus() *ViewDriver {
viewName := self.getView().Name()