diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2024-10-11 18:09:17 +0200 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2024-10-13 17:25:57 +0200 |
commit | 29a4e99988304da31a46d33bd50dfc8ea33d2fff (patch) | |
tree | 3756dc59b47cba265e39403f896ae489e2a95278 | |
parent | b14dfb0726e0410aa4c5160b59be1ec182ae592e (diff) |
Extract some functions from CommitFilesController to a new CommitFilesHelper
-rw-r--r-- | pkg/gui/controllers.go | 1 | ||||
-rw-r--r-- | pkg/gui/controllers/commits_files_controller.go | 67 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/commit_files_helper.go | 79 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/helpers.go | 2 |
4 files changed, 86 insertions, 63 deletions
diff --git a/pkg/gui/controllers.go b/pkg/gui/controllers.go index ce9deebcb..b5dc4785e 100644 --- a/pkg/gui/controllers.go +++ b/pkg/gui/controllers.go @@ -116,6 +116,7 @@ func (gui *Gui) resetHelpersAndControllers() { AmendHelper: helpers.NewAmendHelper(helperCommon, gpgHelper), FixupHelper: helpers.NewFixupHelper(helperCommon), Commits: commitsHelper, + CommitFiles: helpers.NewCommitFilesHelper(helperCommon), Snake: helpers.NewSnakeHelper(helperCommon), Diff: diffHelper, Repos: reposHelper, diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index df703eea2..4c0803bfb 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -194,7 +194,7 @@ func (self *CommitFilesController) onClickMain(opts gocui.ViewMouseBindingOpts) } } - return self.enterCommitFile(node, types.OnFocusOpts{ClickedWindowName: "main", ClickedViewLineIdx: opts.Y, ClickedViewRealLineIdx: line}) + return self.c.Helpers().CommitFiles.EnterCommitFile(node, types.OnFocusOpts{ClickedWindowName: "main", ClickedViewLineIdx: opts.Y, ClickedViewRealLineIdx: line}) } func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error { @@ -299,7 +299,7 @@ func (self *CommitFilesController) toggleForPatch(selectedNodes []*filetree.Comm toggle := func() error { return self.c.WithWaitingStatus(self.c.Tr.UpdatingPatch, func(gocui.Task) error { if !self.c.Git().Patch.PatchBuilder.Active() { - if err := self.startPatchBuilder(); err != nil { + if err := self.c.Helpers().CommitFiles.StartPatchBuilder(); err != nil { return err } } @@ -338,7 +338,7 @@ func (self *CommitFilesController) toggleForPatch(selectedNodes []*filetree.Comm }) } - from, to, reverse := self.currentFromToReverseForPatchBuilding() + from, to, reverse := self.c.Helpers().CommitFiles.CurrentFromToReverseForPatchBuilding() if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.NewPatchRequired(from, to, reverse) { self.c.Confirm(types.ConfirmOpts{ Title: self.c.Tr.DiscardPatch, @@ -360,67 +360,8 @@ func (self *CommitFilesController) toggleAllForPatch(_ *filetree.CommitFileNode) return self.toggleForPatch([]*filetree.CommitFileNode{root}) } -func (self *CommitFilesController) startPatchBuilder() error { - commitFilesContext := self.context() - - canRebase := commitFilesContext.GetCanRebase() - from, to, reverse := self.currentFromToReverseForPatchBuilding() - - self.c.Git().Patch.PatchBuilder.Start(from, to, reverse, canRebase) - return nil -} - -func (self *CommitFilesController) currentFromToReverseForPatchBuilding() (string, string, bool) { - commitFilesContext := self.context() - - from, to := commitFilesContext.GetFromAndToForDiff() - from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(from) - return from, to, reverse -} - func (self *CommitFilesController) enter(node *filetree.CommitFileNode) error { - return self.enterCommitFile(node, types.OnFocusOpts{ClickedWindowName: "", ClickedViewLineIdx: -1, ClickedViewRealLineIdx: -1}) -} - -func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode, opts types.OnFocusOpts) error { - if node.File == nil { - return self.handleToggleCommitFileDirCollapsed(node) - } - - enterTheFile := func() error { - if !self.c.Git().Patch.PatchBuilder.Active() { - if err := self.startPatchBuilder(); err != nil { - return err - } - } - - self.c.Context().Push(self.c.Contexts().CustomPatchBuilder, opts) - return nil - } - - from, to, reverse := self.currentFromToReverseForPatchBuilding() - if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.NewPatchRequired(from, to, reverse) { - self.c.Confirm(types.ConfirmOpts{ - Title: self.c.Tr.DiscardPatch, - Prompt: self.c.Tr.DiscardPatchConfirm, - HandleConfirm: func() error { - self.c.Git().Patch.PatchBuilder.Reset() - return enterTheFile() - }, - }) - - return nil - } - - return enterTheFile() -} - -func (self *CommitFilesController) handleToggleCommitFileDirCollapsed(node *filetree.CommitFileNode) error { - self.context().CommitFileTreeViewModel.ToggleCollapsed(node.GetPath()) - - self.c.PostRefreshUpdate(self.context()) - - return nil + return self.c.Helpers().CommitFiles.EnterCommitFile(node, types.OnFocusOpts{ClickedWindowName: "", ClickedViewLineIdx: -1, ClickedViewRealLineIdx: -1}) } // NOTE: this is very similar to handleToggleFileTreeView, could be DRY'd with generics diff --git a/pkg/gui/controllers/helpers/commit_files_helper.go b/pkg/gui/controllers/helpers/commit_files_helper.go new file mode 100644 index 000000000..247b51a61 --- /dev/null +++ b/pkg/gui/controllers/helpers/commit_files_helper.go @@ -0,0 +1,79 @@ +package helpers + +import ( + "github.com/jesseduffield/lazygit/pkg/gui/context" + "github.com/jesseduffield/lazygit/pkg/gui/filetree" + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type CommitFilesHelper struct { + c *HelperCommon +} + +func NewCommitFilesHelper(c *HelperCommon) *CommitFilesHelper { + return &CommitFilesHelper{ + c: c, + } +} + +func (self *CommitFilesHelper) EnterCommitFile(node *filetree.CommitFileNode, opts types.OnFocusOpts) error { + if node.File == nil { + self.handleToggleCommitFileDirCollapsed(node) + return nil + } + + enterTheFile := func() error { + if !self.c.Git().Patch.PatchBuilder.Active() { + if err := self.StartPatchBuilder(); err != nil { + return err + } + } + + self.c.Context().Push(self.c.Contexts().CustomPatchBuilder, opts) + return nil + } + + from, to, reverse := self.CurrentFromToReverseForPatchBuilding() + if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.NewPatchRequired(from, to, reverse) { + self.c.Confirm(types.ConfirmOpts{ + Title: self.c.Tr.DiscardPatch, + Prompt: self.c.Tr.DiscardPatchConfirm, + HandleConfirm: func() error { + self.c.Git().Patch.PatchBuilder.Reset() + return enterTheFile() + }, + }) + + return nil + } + + return enterTheFile() +} + +func (self *CommitFilesHelper) context() *context.CommitFilesContext { + return self.c.Contexts().CommitFiles +} + +func (self *CommitFilesHelper) handleToggleCommitFileDirCollapsed(node *filetree.CommitFileNode) { + self.context().CommitFileTreeViewModel.ToggleCollapsed(node.GetPath()) + + self.c.PostRefreshUpdate(self.context()) +} + +func (self *CommitFilesHelper) StartPatchBuilder() error { + commitFilesContext := self.context() + + canRebase := commitFilesContext.GetCanRebase() + from, to, reverse := self.CurrentFromToReverseForPatchBuilding() + + self.c.Git().Patch.PatchBuilder.Start(from, to, reverse, canRebase) + return nil +} + +func (self *CommitFilesHelper) CurrentFromToReverseForPatchBuilding() (string, string, bool) { + commitFilesContext := self.context() + + from, to := commitFilesContext.GetFromAndToForDiff() + from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(from) + return from, to, reverse +} diff --git a/pkg/gui/controllers/helpers/helpers.go b/pkg/gui/controllers/helpers/helpers.go index 1f1050dc9..ddfe0170c 100644 --- a/pkg/gui/controllers/helpers/helpers.go +++ b/pkg/gui/controllers/helpers/helpers.go @@ -35,6 +35,7 @@ type Helpers struct { AmendHelper *AmendHelper FixupHelper *FixupHelper Commits *CommitsHelper + CommitFiles *CommitFilesHelper Snake *SnakeHelper // lives in context package because our contexts need it to render to main Diff *DiffHelper @@ -73,6 +74,7 @@ func NewStubHelpers() *Helpers { AmendHelper: &AmendHelper{}, FixupHelper: &FixupHelper{}, Commits: &CommitsHelper{}, + CommitFiles: &CommitFilesHelper{}, Snake: &SnakeHelper{}, Diff: &DiffHelper{}, Repos: &ReposHelper{}, |