From d84986880effd20130e7c6bd43e388c7e19e2377 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 29 Nov 2024 16:24:12 +0100 Subject: [PATCH] Extract helper methods We'll reuse them in the next commit. --- .../controllers/local_commits_controller.go | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 8d2d31700..0d6121d70 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -9,6 +9,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/gui/context" + "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/style" @@ -532,10 +533,7 @@ func (self *LocalCommitsController) startInteractiveRebaseWithEdit( ) error { return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func(gocui.Task) error { self.c.LogAction(self.c.Tr.Actions.EditCommit) - selectedIdx, rangeStartIdx, rangeSelectMode := self.context().GetSelectionRangeAndMode() - commits := self.c.Model().Commits - selectedHash := commits[selectedIdx].Hash - rangeStartHash := commits[rangeStartIdx].Hash + selectionRangeAndMode := self.getSelectionRangeAndMode() err := self.c.Git().Rebase.EditRebase(commitsToEdit[len(commitsToEdit)-1].Hash) return self.c.Helpers().MergeAndRebase.CheckMergeOrRebaseWithRefreshOptions( err, @@ -554,23 +552,41 @@ func (self *LocalCommitsController) startInteractiveRebaseWithEdit( } } - // We need to select the same commit range again because after starting a rebase, - // new lines can be added for update-ref commands in the TODO file, due to - // stacked branches. So the selected commits may be in different positions in the list. - _, newSelectedIdx, ok1 := lo.FindIndexOf(self.c.Model().Commits, func(c *models.Commit) bool { - return c.Hash == selectedHash - }) - _, newRangeStartIdx, ok2 := lo.FindIndexOf(self.c.Model().Commits, func(c *models.Commit) bool { - return c.Hash == rangeStartHash - }) - if ok1 && ok2 { - self.context().SetSelectionRangeAndMode(newSelectedIdx, newRangeStartIdx, rangeSelectMode) - } + self.restoreSelectionRangeAndMode(selectionRangeAndMode) return nil }}) }) } +type SelectionRangeAndMode struct { + selectedHash string + rangeStartHash string + mode traits.RangeSelectMode +} + +func (self *LocalCommitsController) getSelectionRangeAndMode() SelectionRangeAndMode { + selectedIdx, rangeStartIdx, rangeSelectMode := self.context().GetSelectionRangeAndMode() + commits := self.c.Model().Commits + selectedHash := commits[selectedIdx].Hash + rangeStartHash := commits[rangeStartIdx].Hash + return SelectionRangeAndMode{selectedHash, rangeStartHash, rangeSelectMode} +} + +func (self *LocalCommitsController) restoreSelectionRangeAndMode(selectionRangeAndMode SelectionRangeAndMode) { + // We need to select the same commit range again because after starting a rebase, + // new lines can be added for update-ref commands in the TODO file, due to + // stacked branches. So the selected commits may be in different positions in the list. + _, newSelectedIdx, ok1 := lo.FindIndexOf(self.c.Model().Commits, func(c *models.Commit) bool { + return c.Hash == selectionRangeAndMode.selectedHash + }) + _, newRangeStartIdx, ok2 := lo.FindIndexOf(self.c.Model().Commits, func(c *models.Commit) bool { + return c.Hash == selectionRangeAndMode.rangeStartHash + }) + if ok1 && ok2 { + self.context().SetSelectionRangeAndMode(newSelectedIdx, newRangeStartIdx, selectionRangeAndMode.mode) + } +} + func (self *LocalCommitsController) findCommitForQuickStartInteractiveRebase() (*models.Commit, error) { commit, index, ok := lo.FindIndexOf(self.c.Model().Commits, func(c *models.Commit) bool { return c.IsMerge() || c.Status == models.StatusMerged