1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-21 21:47:32 +02:00

Add SetSelection function for list contexts and use it in most places

The only time we should call SetSelectedLineIdx is when we are happy for a
select range to be retained which means things like moving the selected line
index to top top/bottom or up/down a page as the user navigates.

But in every other case we should now call SetSelection because that will
set the selected index and cancel the range which is almost always what we
want.
This commit is contained in:
Jesse Duffield 2024-01-14 09:50:52 +11:00
parent 8840c1a2b7
commit 54bd94ad24
27 changed files with 54 additions and 35 deletions

@ -63,7 +63,7 @@ func NewCommitFilesContext(c *ContextCommon) *CommitFilesContext {
}
ctx.GetView().SetOnSelectItem(ctx.SearchTrait.onSelectItemWrapper(func(selectedLineIdx int) error {
ctx.GetList().SetSelectedLineIdx(selectedLineIdx)
ctx.GetList().SetSelection(selectedLineIdx)
return ctx.HandleFocus(types.OnFocusOpts{})
}))

@ -31,5 +31,5 @@ func (self *FilteredListViewModel[T]) ClearFilter() {
self.FilteredList.ClearFilter()
self.SetSelectedLineIdx(unfilteredIndex)
self.SetSelection(unfilteredIndex)
}

@ -102,7 +102,7 @@ func (self *ListContextTrait) HandleRender() error {
}
func (self *ListContextTrait) OnSearchSelect(selectedLineIdx int) error {
self.GetList().SetSelectedLineIdx(selectedLineIdx)
self.GetList().SetSelection(selectedLineIdx)
return self.HandleFocus(types.OnFocusOpts{})
}

@ -85,7 +85,7 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
}
ctx.GetView().SetOnSelectItem(ctx.SearchTrait.onSelectItemWrapper(func(selectedLineIdx int) error {
ctx.GetList().SetSelectedLineIdx(selectedLineIdx)
ctx.GetList().SetSelection(selectedLineIdx)
return ctx.HandleFocus(types.OnFocusOpts{})
}))

@ -134,7 +134,7 @@ func NewSubCommitsContext(
}
ctx.GetView().SetOnSelectItem(ctx.SearchTrait.onSelectItemWrapper(func(selectedLineIdx int) error {
ctx.GetList().SetSelectedLineIdx(selectedLineIdx)
ctx.GetList().SetSelection(selectedLineIdx)
return ctx.HandleFocus(types.OnFocusOpts{})
}))

@ -74,7 +74,7 @@ func (self *SuggestionsContext) GetSelectedItemId() string {
func (self *SuggestionsContext) SetSuggestions(suggestions []*types.Suggestion) {
self.State.Suggestions = suggestions
self.SetSelectedLineIdx(0)
self.SetSelection(0)
self.c.ResetViewOrigin(self.GetView())
_ = self.HandleRender()
}

@ -45,10 +45,25 @@ func (self *ListCursor) GetSelectedLineIdx() int {
return self.selectedIdx
}
// Sets the selected line index. Note, you probably don't want to use this directly,
// because it doesn't affect the range select mode or range start index. You should only
// use this for navigation situations where e.g. the user wants to jump to the top of
// a list while in range select mode so that the selection ends up being between
// the top of the list and the previous selection
func (self *ListCursor) SetSelectedLineIdx(value int) {
self.selectedIdx = self.clampValue(value)
}
// Sets the selected index and cancels the range. You almost always want to use
// this instead of SetSelectedLineIdx. For example, if you want to jump the cursor
// to the top of a list after checking out a branch, you should use this method,
// or you may end up with a large range selection from the previous cursor position
// to the top of the list.
func (self *ListCursor) SetSelection(value int) {
self.selectedIdx = self.clampValue(value)
self.CancelRangeSelect()
}
func (self *ListCursor) clampValue(value int) int {
clampedValue := -1
if self.list.Len() > 0 {

@ -50,7 +50,7 @@ func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext {
}
ctx.GetView().SetOnSelectItem(ctx.SearchTrait.onSelectItemWrapper(func(selectedLineIdx int) error {
ctx.GetList().SetSelectedLineIdx(selectedLineIdx)
ctx.GetList().SetSelection(selectedLineIdx)
return ctx.HandleFocus(types.OnFocusOpts{})
}))

@ -265,7 +265,7 @@ func (self *BisectController) selectCurrentBisectCommit() {
// find index of commit with that sha, move cursor to that.
for i, commit := range self.c.Model().Commits {
if commit.Sha == info.GetCurrentSha() {
self.context().SetSelectedLineIdx(i)
self.context().SetSelection(i)
_ = self.context().HandleFocus(types.OnFocusOpts{})
break
}

@ -424,7 +424,7 @@ func (self *BranchesController) createNewBranchWithName(newBranchName string) er
return self.c.Error(err)
}
self.context().SetSelectedLineIdx(0)
self.context().SetSelection(0)
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
}
@ -627,7 +627,7 @@ func (self *BranchesController) createSortMenu() error {
if self.c.GetAppState().LocalBranchSortOrder != sortOrder {
self.c.GetAppState().LocalBranchSortOrder = sortOrder
self.c.SaveAppStateAndLogError()
self.c.Contexts().Branches.SetSelectedLineIdx(0)
self.c.Contexts().Branches.SetSelection(0)
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.BRANCHES}})
}
return nil
@ -658,7 +658,7 @@ func (self *BranchesController) rename(branch *models.Branch) error {
// now that we've got our stuff again we need to find that branch and reselect it.
for i, newBranch := range self.c.Model().Branches {
if newBranch.Name == newBranchName {
self.context().SetSelectedLineIdx(i)
self.context().SetSelection(i)
if err := self.context().HandleRender(); err != nil {
return err
}

@ -73,7 +73,7 @@ func (self *FilteringMenuAction) setFiltering(path string) error {
}
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.COMMITS}, Then: func() {
self.c.Contexts().LocalCommits.SetSelectedLineIdx(0)
self.c.Contexts().LocalCommits.SetSelection(0)
self.c.Contexts().LocalCommits.FocusLine()
}})
}

@ -87,7 +87,7 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error {
_ = self.c.Refresh(types.RefreshOptions{Mode: types.SYNC, Scope: []types.RefreshableView{types.FILES}})
}
self.c.Contexts().LocalCommits.SetSelectedLineIdx(index)
self.c.Contexts().LocalCommits.SetSelection(index)
return self.c.PushContext(self.c.Contexts().LocalCommits)
}

@ -44,9 +44,9 @@ func (self *RefsHelper) CheckoutRef(ref string, options types.CheckoutRefOptions
cmdOptions := git_commands.CheckoutOptions{Force: false, EnvVars: options.EnvVars}
onSuccess := func() {
self.c.Contexts().Branches.SetSelectedLineIdx(0)
self.c.Contexts().ReflogCommits.SetSelectedLineIdx(0)
self.c.Contexts().LocalCommits.SetSelectedLineIdx(0)
self.c.Contexts().Branches.SetSelection(0)
self.c.Contexts().ReflogCommits.SetSelection(0)
self.c.Contexts().LocalCommits.SetSelection(0)
// loading a heap of commits is slow so we limit them whenever doing a reset
self.c.Contexts().LocalCommits.SetLimitCommits(true)
}
@ -107,8 +107,8 @@ func (self *RefsHelper) ResetToRef(ref string, strength string, envVars []string
return self.c.Error(err)
}
self.c.Contexts().LocalCommits.SetSelectedLineIdx(0)
self.c.Contexts().ReflogCommits.SetSelectedLineIdx(0)
self.c.Contexts().LocalCommits.SetSelection(0)
self.c.Contexts().ReflogCommits.SetSelection(0)
// loading a heap of commits is slow so we limit them whenever doing a reset
self.c.Contexts().LocalCommits.SetLimitCommits(true)
@ -215,8 +215,8 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest
}
}
self.c.Contexts().LocalCommits.SetSelectedLineIdx(0)
self.c.Contexts().Branches.SetSelectedLineIdx(0)
self.c.Contexts().LocalCommits.SetSelection(0)
self.c.Contexts().Branches.SetSelection(0)
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
},

@ -216,7 +216,7 @@ func (self *SearchHelper) OnPromptContentChanged(searchString string) {
state := self.searchState()
switch context := state.Context.(type) {
case types.IFilterableContext:
context.SetSelectedLineIdx(0)
context.SetSelection(0)
_ = context.GetView().SetOriginY(0)
context.SetFilter(searchString)
_ = self.c.PostRefreshUpdate(context)
@ -232,7 +232,7 @@ func (self *SearchHelper) ReApplyFilter(context types.Context) {
if context == state.Context {
filterableContext, ok := context.(types.IFilterableContext)
if ok {
filterableContext.SetSelectedLineIdx(0)
filterableContext.SetSelection(0)
_ = filterableContext.GetView().SetOriginY(0)
filterableContext.ReApplyFilter()
}

@ -53,7 +53,7 @@ func (self *SubCommitsHelper) ViewSubCommits(opts ViewSubCommitsOpts) error {
self.refreshHelper.RefreshAuthors(commits)
subCommitsContext := self.c.Contexts().SubCommits
subCommitsContext.SetSelectedLineIdx(0)
subCommitsContext.SetSelection(0)
subCommitsContext.SetParentContext(opts.Context)
subCommitsContext.SetWindowName(opts.Context.GetWindowName())
subCommitsContext.SetTitleRef(utils.TruncateWithEllipsis(opts.TitleRef, 50))

@ -160,7 +160,7 @@ func (self *ListController) HandleClick(opts gocui.ViewMouseBindingOpts) error {
return nil
}
self.context.GetList().SetSelectedLineIdx(newSelectedLineIdx)
self.context.GetList().SetSelection(newSelectedLineIdx)
if prevSelectedLineIdx == newSelectedLineIdx && alreadyFocused && self.context.GetOnClick() != nil {
return self.context.GetOnClick()()

@ -459,7 +459,7 @@ func (self *LocalCommitsController) startInteractiveRebaseWithEdit(
return c.Sha == selectedCommit.Sha
})
if ok {
self.context().SetSelectedLineIdx(index)
self.context().SetSelection(index)
}
}})
})

@ -132,7 +132,7 @@ func (self *RemoteBranchesController) createSortMenu() error {
if self.c.GetAppState().RemoteBranchSortOrder != sortOrder {
self.c.GetAppState().RemoteBranchSortOrder = sortOrder
self.c.SaveAppStateAndLogError()
self.c.Contexts().RemoteBranches.SetSelectedLineIdx(0)
self.c.Contexts().RemoteBranches.SetSelection(0)
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.REMOTES}})
}
return nil

@ -106,7 +106,7 @@ func (self *RemotesController) enter(remote *models.Remote) error {
newSelectedLine = -1
}
remoteBranchesContext := self.c.Contexts().RemoteBranches
remoteBranchesContext.SetSelectedLineIdx(newSelectedLine)
remoteBranchesContext.SetSelection(newSelectedLine)
remoteBranchesContext.SetTitleRef(remote.Name)
remoteBranchesContext.SetParentContext(self.Context())
remoteBranchesContext.GetView().TitlePrefix = self.Context().GetView().TitlePrefix

@ -189,7 +189,7 @@ func (self *StashController) handleRenameStashEntry(stashEntry *models.StashEntr
if err != nil {
return err
}
self.context().SetSelectedLineIdx(0) // Select the renamed stash
self.context().SetSelection(0) // Select the renamed stash
self.context().FocusLine()
return nil
},

@ -77,7 +77,7 @@ func (self *SwitchToDiffFilesController) Context() types.Context {
func (self *SwitchToDiffFilesController) viewFiles(opts SwitchToCommitFilesContextOpts) error {
diffFilesContext := self.diffFilesContext
diffFilesContext.SetSelectedLineIdx(0)
diffFilesContext.SetSelection(0)
diffFilesContext.SetRef(opts.Ref)
diffFilesContext.SetTitleRef(opts.Ref.Description())
diffFilesContext.SetCanRebase(opts.CanRebase)

@ -210,7 +210,9 @@ func (self *TagsController) createResetMenu(tag *models.Tag) error {
func (self *TagsController) create() error {
// leaving commit SHA blank so that we're just creating the tag for the current commit
return self.c.Helpers().Tags.OpenCreateTagPrompt("", func() { self.context().SetSelectedLineIdx(0) })
return self.c.Helpers().Tags.OpenCreateTagPrompt("", func() {
self.context().SetSelection(0)
})
}
func (self *TagsController) withSelectedTag(f func(tag *models.Tag) error) func() error {

@ -106,6 +106,6 @@ func (self *CommitFileTreeViewModel) ToggleShowTree() {
index, found := self.GetIndexForPath(path)
if found {
self.SetSelectedLineIdx(index)
self.SetSelection(index)
}
}

@ -81,7 +81,7 @@ func (self *FileTreeViewModel) SetTree() {
newNodes := self.GetAllItems()
newIdx := self.findNewSelectedIdx(prevNodes[prevSelectedLineIdx:], newNodes)
if newIdx != -1 && newIdx != prevSelectedLineIdx {
self.SetSelectedLineIdx(newIdx)
self.SetSelection(newIdx)
}
}
@ -128,7 +128,7 @@ func (self *FileTreeViewModel) findNewSelectedIdx(prevNodes []*FileNode, currNod
func (self *FileTreeViewModel) SetStatusFilter(filter FileTreeDisplayFilter) {
self.IFileTree.SetStatusFilter(filter)
self.IListCursor.SetSelectedLineIdx(0)
self.IListCursor.SetSelection(0)
}
// If we're going from flat to tree we want to select the same file.

@ -329,7 +329,7 @@ func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, contextKey types.Context
// because e.g. with worktrees, we'll show the current worktree at the top of the list.
listContext, ok := contextToPush.(types.IListContext)
if ok {
listContext.GetList().SetSelectedLineIdx(0)
listContext.GetList().SetSelection(0)
}
}

@ -43,7 +43,7 @@ func (gui *Gui) createMenu(opts types.CreateMenuOptions) error {
}
gui.State.Contexts.Menu.SetMenuItems(opts.Items, opts.ColumnAlignment)
gui.State.Contexts.Menu.SetSelectedLineIdx(0)
gui.State.Contexts.Menu.SetSelection(0)
gui.Views.Menu.Title = opts.Title
gui.Views.Menu.FgColor = theme.GocuiDefaultTextColor

@ -224,6 +224,7 @@ type IList interface {
type IListCursor interface {
GetSelectedLineIdx() int
SetSelectedLineIdx(value int)
SetSelection(value int)
MoveSelectedLine(delta int)
ClampSelection()
CancelRangeSelect()
@ -236,6 +237,7 @@ type IListCursor interface {
type IListPanelState interface {
SetSelectedLineIdx(int)
SetSelection(int)
GetSelectedLineIdx() int
}