1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03: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

View File

@ -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
}

View File

@ -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
}

View File

@ -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()
}})
}

View File

@ -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)
}

View File

@ -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})
},

View File

@ -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()
}

View File

@ -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))

View File

@ -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()()

View File

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

View File

@ -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

View File

@ -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

View File

@ -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
},

View File

@ -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)

View File

@ -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 {