1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-31 14:24:25 +03:00

Standardise display of range selection across views

We're not fully standardising here: different contexts can store their range state however
they like. What we are standardising on is that now the view is always responsible for
highlighting the selected lines, meaning the context/controller needs to tell the view
where the range start is.

Two convenient benefits from this change:
1) we no longer need bespoke code in integration tests for asserting on selected lines because
we can just ask the view
2) line selection in staging/patch-building/merge-conflicts views now look the same as in
list views i.e. the highlight applies to the whole line (including trailing space)

I also noticed a bug with merge conflicts not rendering the selection on focus though I suspect
it wasn't a bug with any real consequences when the view wasn't displaying the selection.

I'm going to scrap the selectedRangeBgColor config and just let it use the single line
background color. Hopefully nobody cares, but there's really no need for an extra config.
This commit is contained in:
Jesse Duffield
2024-01-14 00:18:05 +11:00
parent c0c3aac02e
commit f3eb180f75
28 changed files with 255 additions and 287 deletions

View File

@ -2,11 +2,8 @@ package components
import (
"fmt"
"strings"
"github.com/go-errors/errors"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/context"
)
type Views struct {
@ -30,95 +27,19 @@ func (self *Views) Secondary() *ViewDriver {
}
func (self *Views) regularView(viewName string) *ViewDriver {
return self.newStaticViewDriver(viewName, nil, nil, nil)
}
func (self *Views) patchExplorerViewByName(viewName string) *ViewDriver {
return self.newStaticViewDriver(
viewName,
func() ([]string, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.PatchExplorerContext)
state := ctx.GetState()
if state == nil {
return nil, errors.New("Expected patch explorer to be activated")
}
selectedContent := state.PlainRenderSelected()
// the above method returns a string with a trailing newline so we need to remove that before splitting
selectedLines := strings.Split(strings.TrimSuffix(selectedContent, "\n"), "\n")
return selectedLines, nil
},
func() (int, int, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.PatchExplorerContext)
state := ctx.GetState()
if state == nil {
return 0, 0, errors.New("Expected patch explorer to be activated")
}
startIdx, endIdx := state.SelectedRange()
return startIdx, endIdx, nil
},
func() (int, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.PatchExplorerContext)
state := ctx.GetState()
if state == nil {
return 0, errors.New("Expected patch explorer to be activated")
}
return state.GetSelectedLineIdx(), nil
},
)
}
// 'static' because it'll always refer to the same view, as opposed to the 'main' view which could actually be
// one of several views, or the 'current' view which depends on focus.
func (self *Views) newStaticViewDriver(
viewName string,
getSelectedLinesFn func() ([]string, error),
getSelectedLineRangeFn func() (int, int, error),
getSelectedLineIdxFn func() (int, error),
) *ViewDriver {
return &ViewDriver{
context: fmt.Sprintf("%s view", viewName),
getView: func() *gocui.View { return self.t.gui.View(viewName) },
getSelectedLinesFn: getSelectedLinesFn,
getSelectedRangeFn: getSelectedLineRangeFn,
getSelectedLineIdxFn: getSelectedLineIdxFn,
t: self.t,
context: fmt.Sprintf("%s view", viewName),
getView: func() *gocui.View { return self.t.gui.View(viewName) },
t: self.t,
}
}
func (self *Views) MergeConflicts() *ViewDriver {
viewName := "mergeConflicts"
return self.newStaticViewDriver(
viewName,
func() ([]string, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.MergeConflictsContext)
state := ctx.GetState()
if state == nil {
return nil, errors.New("Expected patch explorer to be activated")
}
selectedContent := strings.Split(state.PlainRenderSelected(), "\n")
func (self *Views) patchExplorerViewByName(viewName string) *ViewDriver {
return self.regularView(viewName)
}
return selectedContent, nil
},
func() (int, int, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.MergeConflictsContext)
state := ctx.GetState()
if state == nil {
return 0, 0, errors.New("Expected patch explorer to be activated")
}
startIdx, endIdx := state.GetSelectedRange()
return startIdx, endIdx, nil
},
// there is no concept of a cursor in the merge conflicts panel so we just return the start of the selection
func() (int, error) {
ctx := self.t.gui.ContextForView(viewName).(*context.MergeConflictsContext)
state := ctx.GetState()
if state == nil {
return 0, errors.New("Expected patch explorer to be activated")
}
startIdx, _ := state.GetSelectedRange()
return startIdx, nil
},
)
func (self *Views) MergeConflicts() *ViewDriver {
return self.regularView("mergeConflicts")
}
func (self *Views) Commits() *ViewDriver {