mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-10-08 22:52:12 +02: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:
@@ -10,45 +10,24 @@ import (
|
||||
|
||||
type ViewDriver struct {
|
||||
// context is prepended to any error messages e.g. 'context: "current view"'
|
||||
context string
|
||||
getView func() *gocui.View
|
||||
t *TestDriver
|
||||
getSelectedLinesFn func() ([]string, error)
|
||||
getSelectedRangeFn func() (int, int, error)
|
||||
getSelectedLineIdxFn func() (int, error)
|
||||
context string
|
||||
getView func() *gocui.View
|
||||
t *TestDriver
|
||||
}
|
||||
|
||||
func (self *ViewDriver) getSelectedLines() ([]string, error) {
|
||||
if self.getSelectedLinesFn == nil {
|
||||
view := self.t.gui.View(self.getView().Name())
|
||||
|
||||
return []string{view.SelectedLine()}, nil
|
||||
}
|
||||
|
||||
return self.getSelectedLinesFn()
|
||||
func (self *ViewDriver) getSelectedLines() []string {
|
||||
view := self.t.gui.View(self.getView().Name())
|
||||
return view.SelectedLines()
|
||||
}
|
||||
|
||||
func (self *ViewDriver) getSelectedRange() (int, int, error) {
|
||||
if self.getSelectedRangeFn == nil {
|
||||
view := self.t.gui.View(self.getView().Name())
|
||||
idx := view.SelectedLineIdx()
|
||||
|
||||
return idx, idx, nil
|
||||
}
|
||||
|
||||
return self.getSelectedRangeFn()
|
||||
func (self *ViewDriver) getSelectedRange() (int, int) {
|
||||
view := self.t.gui.View(self.getView().Name())
|
||||
return view.SelectedLineRange()
|
||||
}
|
||||
|
||||
// even if you have a selected range, there may still be a line within that range
|
||||
// which the cursor points at. This function returns that line index.
|
||||
func (self *ViewDriver) getSelectedLineIdx() (int, error) {
|
||||
if self.getSelectedLineIdxFn == nil {
|
||||
view := self.t.gui.View(self.getView().Name())
|
||||
|
||||
return view.SelectedLineIdx(), nil
|
||||
}
|
||||
|
||||
return self.getSelectedLineIdxFn()
|
||||
func (self *ViewDriver) getSelectedLineIdx() int {
|
||||
view := self.t.gui.View(self.getView().Name())
|
||||
return view.SelectedLineIdx()
|
||||
}
|
||||
|
||||
// asserts that the view has the expected title
|
||||
@@ -105,7 +84,7 @@ func (self *ViewDriver) ContainsLines(matchers ...*TextMatcher) *ViewDriver {
|
||||
content := self.getView().Buffer()
|
||||
lines := strings.Split(content, "\n")
|
||||
|
||||
startIdx, endIdx, err := self.getSelectedRange()
|
||||
startIdx, endIdx := self.getSelectedRange()
|
||||
|
||||
for i := 0; i < len(lines)-len(matchers)+1; i++ {
|
||||
matches := true
|
||||
@@ -118,10 +97,6 @@ func (self *ViewDriver) ContainsLines(matchers ...*TextMatcher) *ViewDriver {
|
||||
break
|
||||
}
|
||||
if checkIsSelected {
|
||||
if err != nil {
|
||||
matches = false
|
||||
break
|
||||
}
|
||||
if lineIdx < startIdx || lineIdx > endIdx {
|
||||
matches = false
|
||||
break
|
||||
@@ -181,10 +156,7 @@ func (self *ViewDriver) SelectedLines(matchers ...*TextMatcher) *ViewDriver {
|
||||
self.validateEnoughLines(matchers)
|
||||
|
||||
self.t.assertWithRetries(func() (bool, string) {
|
||||
selectedLines, err := self.getSelectedLines()
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
selectedLines := self.getSelectedLines()
|
||||
|
||||
selectedContent := strings.Join(selectedLines, "\n")
|
||||
expectedContent := expectedContentFromMatchers(matchers)
|
||||
@@ -251,19 +223,13 @@ func (self *ViewDriver) assertLines(offset int, matchers ...*TextMatcher) *ViewD
|
||||
|
||||
if checkIsSelected {
|
||||
self.t.assertWithRetries(func() (bool, string) {
|
||||
startIdx, endIdx, err := self.getSelectedRange()
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
startIdx, endIdx := self.getSelectedRange()
|
||||
|
||||
if lineIdx < startIdx || lineIdx > endIdx {
|
||||
if startIdx == endIdx {
|
||||
return false, fmt.Sprintf("Unexpected selected line index in view '%s'. Expected %d, got %d", view.Name(), lineIdx, startIdx)
|
||||
} else {
|
||||
lines, err := self.getSelectedLines()
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
lines := self.getSelectedLines()
|
||||
return false, fmt.Sprintf("Unexpected selected line index in view '%s'. Expected line %d to be in range %d to %d. Selected lines:\n---\n%s\n---\n\nExpected line: '%s'", view.Name(), lineIdx, startIdx, endIdx, strings.Join(lines, "\n"), matcher.name())
|
||||
}
|
||||
}
|
||||
@@ -286,15 +252,11 @@ func (self *ViewDriver) Content(matcher *TextMatcher) *ViewDriver {
|
||||
return self
|
||||
}
|
||||
|
||||
// asserts on the selected line of the view. If your view has multiple lines selected,
|
||||
// but also has a concept of a cursor position, this will assert on the line that
|
||||
// the cursor is on. Otherwise it will assert on the first line of the selection.
|
||||
// asserts on the selected line of the view. If you are selecting a range,
|
||||
// you should use the SelectedLines method instead.
|
||||
func (self *ViewDriver) SelectedLine(matcher *TextMatcher) *ViewDriver {
|
||||
self.t.assertWithRetries(func() (bool, string) {
|
||||
selectedLineIdx, err := self.getSelectedLineIdx()
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
selectedLineIdx := self.getSelectedLineIdx()
|
||||
|
||||
viewLines := self.getView().BufferLines()
|
||||
|
||||
@@ -480,11 +442,7 @@ func (self *ViewDriver) NavigateToLine(matcher *TextMatcher) *ViewDriver {
|
||||
}
|
||||
})
|
||||
|
||||
selectedLineIdx, err := self.getSelectedLineIdx()
|
||||
if err != nil {
|
||||
self.t.fail(err.Error())
|
||||
return self
|
||||
}
|
||||
selectedLineIdx := self.getSelectedLineIdx()
|
||||
if selectedLineIdx == matchIndex {
|
||||
return self.SelectedLine(matcher)
|
||||
}
|
||||
@@ -507,11 +465,7 @@ func (self *ViewDriver) NavigateToLine(matcher *TextMatcher) *ViewDriver {
|
||||
|
||||
for i := 0; i < maxNumKeyPresses; i++ {
|
||||
keyPress()
|
||||
idx, err := self.getSelectedLineIdx()
|
||||
if err != nil {
|
||||
self.t.fail(err.Error())
|
||||
return self
|
||||
}
|
||||
idx := self.getSelectedLineIdx()
|
||||
if ok, _ := matcher.test(lines[idx]); ok {
|
||||
return self
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
Reference in New Issue
Block a user