diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-03-23 21:39:30 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-04-30 13:19:53 +1000 |
commit | 71753770ad6da851b6584b6868fd03a0dba6e5c8 (patch) | |
tree | bbcc190fc3ddbc4f6e466c5f415e03dc9ddad3fd | |
parent | 820b1e811d09fce5b0e578a7a304cca9c781d42a (diff) |
move custom patch options menu action to controllers package
-rw-r--r-- | pkg/gui/controllers/custom_patch_options_menu_action.go | 219 | ||||
-rw-r--r-- | pkg/gui/controllers/global_controller.go | 10 | ||||
-rw-r--r-- | pkg/gui/custom_patch_options_panel.go | 215 | ||||
-rw-r--r-- | pkg/gui/keybindings.go | 7 |
4 files changed, 229 insertions, 222 deletions
diff --git a/pkg/gui/controllers/custom_patch_options_menu_action.go b/pkg/gui/controllers/custom_patch_options_menu_action.go new file mode 100644 index 000000000..ca566c88c --- /dev/null +++ b/pkg/gui/controllers/custom_patch_options_menu_action.go @@ -0,0 +1,219 @@ +package controllers + +import ( + "fmt" + + "github.com/jesseduffield/lazygit/pkg/commands/types/enums" + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type CustomPatchOptionsMenuAction struct { + c *ControllerCommon +} + +func (self *CustomPatchOptionsMenuAction) Call() error { + if !self.c.Git().Patch.PatchBuilder.Active() { + return self.c.ErrorMsg(self.c.Tr.NoPatchError) + } + + menuItems := []*types.MenuItem{ + { + Label: "reset patch", + OnPress: self.c.Helpers().PatchBuilding.Reset, + Key: 'c', + }, + { + Label: "apply patch", + OnPress: func() error { return self.handleApplyPatch(false) }, + Key: 'a', + }, + { + Label: "apply patch in reverse", + OnPress: func() error { return self.handleApplyPatch(true) }, + Key: 'r', + }, + } + + if self.c.Git().Patch.PatchBuilder.CanRebase && self.c.Git().Status.WorkingTreeState() == enums.REBASE_MODE_NONE { + menuItems = append(menuItems, []*types.MenuItem{ + { + Label: fmt.Sprintf("remove patch from original commit (%s)", self.c.Git().Patch.PatchBuilder.To), + OnPress: self.handleDeletePatchFromCommit, + Key: 'd', + }, + { + Label: "move patch out into index", + OnPress: self.handleMovePatchIntoWorkingTree, + Key: 'i', + }, + { + Label: "move patch into new commit", + OnPress: self.handlePullPatchIntoNewCommit, + Key: 'n', + }, + }...) + + if self.c.CurrentContext().GetKey() == self.c.Contexts().LocalCommits.GetKey() { + selectedCommit := self.c.Contexts().LocalCommits.GetSelected() + if selectedCommit != nil && self.c.Git().Patch.PatchBuilder.To != selectedCommit.Sha { + // adding this option to index 1 + menuItems = append( + menuItems[:1], + append( + []*types.MenuItem{ + { + Label: fmt.Sprintf("move patch to selected commit (%s)", selectedCommit.Sha), + OnPress: self.handleMovePatchToSelectedCommit, + Key: 'm', + }, + }, menuItems[1:]..., + )..., + ) + } + } + } + + menuItems = append(menuItems, []*types.MenuItem{ + { + Label: "copy patch to clipboard", + OnPress: func() error { return self.copyPatchToClipboard() }, + Key: 'y', + }, + }...) + + return self.c.Menu(types.CreateMenuOptions{Title: self.c.Tr.PatchOptionsTitle, Items: menuItems}) +} + +func (self *CustomPatchOptionsMenuAction) getPatchCommitIndex() int { + for index, commit := range self.c.Model().Commits { + if commit.Sha == self.c.Git().Patch.PatchBuilder.To { + return index + } + } + return -1 +} + +func (self *CustomPatchOptionsMenuAction) validateNormalWorkingTreeState() (bool, error) { + if self.c.Git().Status.WorkingTreeState() != enums.REBASE_MODE_NONE { + return false, self.c.ErrorMsg(self.c.Tr.CantPatchWhileRebasingError) + } + return true, nil +} + +func (self *CustomPatchOptionsMenuAction) returnFocusFromPatchExplorerIfNecessary() error { + if self.c.CurrentContext().GetKey() == self.c.Contexts().CustomPatchBuilder.GetKey() { + return self.c.Helpers().PatchBuilding.Escape() + } + return nil +} + +func (self *CustomPatchOptionsMenuAction) handleDeletePatchFromCommit() error { + if ok, err := self.validateNormalWorkingTreeState(); !ok { + return err + } + + if err := self.returnFocusFromPatchExplorerIfNecessary(); err != nil { + return err + } + + return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func() error { + commitIndex := self.getPatchCommitIndex() + self.c.LogAction(self.c.Tr.Actions.RemovePatchFromCommit) + err := self.c.Git().Patch.DeletePatchesFromCommit(self.c.Model().Commits, commitIndex) + return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err) + }) +} + +func (self *CustomPatchOptionsMenuAction) handleMovePatchToSelectedCommit() error { + if ok, err := self.validateNormalWorkingTreeState(); !ok { + return err + } + + if err := self.returnFocusFromPatchExplorerIfNecessary(); err != nil { + return err + } + + return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func() error { + commitIndex := self.getPatchCommitIndex() + self.c.LogAction(self.c.Tr.Actions.MovePatchToSelectedCommit) + err := self.c.Git().Patch.MovePatchToSelectedCommit(self.c.Model().Commits, commitIndex, self.c.Contexts().LocalCommits.GetSelectedLineIdx()) + return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err) + }) +} + +func (self *CustomPatchOptionsMenuAction) handleMovePatchIntoWorkingTree() error { + if ok, err := self.validateNormalWorkingTreeState(); !ok { + return err + } + + if err := self.returnFocusFromPatchExplorerIfNecessary(); err != nil { + return err + } + + pull := func(stash bool) error { + return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func() error { + commitIndex := self.getPatchCommitIndex() + self.c.LogAction(self.c.Tr.Actions.MovePatchIntoIndex) + err := self.c.Git().Patch.MovePatchIntoIndex(self.c.Model().Commits, commitIndex, stash) + return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err) + }) + } + + if self.c.Helpers().WorkingTree.IsWorkingTreeDirty() { + return self.c.Confirm(types.ConfirmOpts{ + Title: self.c.Tr.MustStashTitle, + Prompt: self.c.Tr.MustStashWarning, + HandleConfirm: func() error { + return pull(true) + }, + }) + } else { + return pull(false) + } +} + +func (self *CustomPatchOptionsMenuAction) handlePullPatchIntoNewCommit() error { + if ok, err := self.validateNormalWorkingTreeState(); !ok { + return err + } + + if err := self.returnFocusFromPatchExplorerIfNecessary(); err != nil { + return err + } + + return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func() error { + commitIndex := self.getPatchCommitIndex() + self.c.LogAction(self.c.Tr.Actions.MovePatchIntoNewCommit) + err := self.c.Git().Patch.PullPatchIntoNewCommit(self.c.Model().Commits, commitIndex) + return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err) + }) +} + +func (self *CustomPatchOptionsMenuAction) handleApplyPatch(reverse bool) error { + if err := self.returnFocusFromPatchExplorerIfNecessary(); err != nil { + return err + } + + action := self.c.Tr.Actions.ApplyPatch + if reverse { + action = "Apply patch in reverse" + } + self.c.LogAction(action) + if err := self.c.Git().Patch.PatchBuilder.ApplyPatches(reverse); err != nil { + return self.c.Error(err) + } + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) +} + +func (self *CustomPatchOptionsMenuAction) copyPatchToClipboard() error { + patch := self.c.Git().Patch.PatchBuilder.RenderAggregatedPatch(true) + + self.c.LogAction(self.c.Tr.Actions.CopyPatchToClipboard) + if err := self.c.OS().CopyToClipboard(patch); err != nil { + return self.c.Error(err) + } + + self.c.Toast(self.c.Tr.PatchCopiedToClipboard) + + return nil +} diff --git a/pkg/gui/controllers/global_controller.go b/pkg/gui/controllers/global_controller.go index 6a901d738..02d312331 100644 --- a/pkg/gui/controllers/global_controller.go +++ b/pkg/gui/controllers/global_controller.go @@ -25,6 +25,12 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type Handler: self.customCommand, Description: self.c.Tr.LcExecuteCustomCommand, }, + { + Key: opts.GetKey(opts.Config.Universal.CreatePatchOptionsMenu), + Handler: self.createCustomPatchOptionsMenu, + Description: self.c.Tr.ViewPatchOptions, + OpensMenu: true, + }, } } @@ -32,6 +38,10 @@ func (self *GlobalController) customCommand() error { return (&CustomCommandAction{c: self.c}).Call() } +func (self *GlobalController) createCustomPatchOptionsMenu() error { + return (&CustomPatchOptionsMenuAction{c: self.c}).Call() +} + func (self *GlobalController) Context() types.Context { return nil } diff --git a/pkg/gui/custom_patch_options_panel.go b/pkg/gui/custom_patch_options_panel.go deleted file mode 100644 index 837909deb..000000000 --- a/pkg/gui/custom_patch_options_panel.go +++ /dev/null @@ -1,215 +0,0 @@ -package gui - -import ( - "fmt" - - "github.com/jesseduffield/lazygit/pkg/commands/types/enums" - "github.com/jesseduffield/lazygit/pkg/gui/types" -) - -func (gui *Gui) handleCreatePatchOptionsMenu() error { - if !gui.git.Patch.PatchBuilder.Active() { - return gui.c.ErrorMsg(gui.c.Tr.NoPatchError) - } - - menuItems := []*types.MenuItem{ - { - Label: "reset patch", - OnPress: gui.helpers.PatchBuilding.Reset, - Key: 'c', - }, - { - Label: "apply patch", - OnPress: func() error { return gui.handleApplyPatch(false) }, - Key: 'a', - }, - { - Label: "apply patch in reverse", - OnPress: func() error { return gui.handleApplyPatch(true) }, - Key: 'r', - }, - } - - if gui.git.Patch.PatchBuilder.CanRebase && gui.git.Status.WorkingTreeState() == enums.REBASE_MODE_NONE { - menuItems = append(menuItems, []*types.MenuItem{ - { - Label: fmt.Sprintf("remove patch from original commit (%s)", gui.git.Patch.PatchBuilder.To), - OnPress: gui.handleDeletePatchFromCommit, - Key: 'd', - }, - { - Label: "move patch out into index", - OnPress: gui.handleMovePatchIntoWorkingTree, - Key: 'i', - }, - { - Label: "move patch into new commit", - OnPress: gui.handlePullPatchIntoNewCommit, - Key: 'n', - }, - }...) - - if gui.c.CurrentContext().GetKey() == gui.State.Contexts.LocalCommits.GetKey() { - selectedCommit := gui.State.Contexts.LocalCommits.GetSelected() - if selectedCommit != nil && gui.git.Patch.PatchBuilder.To != selectedCommit.Sha { - // adding this option to index 1 - menuItems = append( - menuItems[:1], - append( - []*types.MenuItem{ - { - Label: fmt.Sprintf("move patch to selected commit (%s)", selectedCommit.Sha), - OnPress: gui.handleMovePatchToSelectedCommit, - Key: 'm', - }, - }, menuItems[1:]..., - )..., - ) - } - } - } - - menuItems = append(menuItems, []*types.MenuItem{ - { - Label: "copy patch to clipboard", - OnPress: func() error { return gui.copyPatchToClipboard() }, - Key: 'y', - }, - }...) - - return gui.c.Menu(types.CreateMenuOptions{Title: gui.c.Tr.PatchOptionsTitle, Items: menuItems}) -} - -func (gui *Gui) getPatchCommitIndex() int { - for index, commit := range gui.State.Model.Commits { - if commit.Sha == gui.git.Patch.PatchBuilder.To { - return index - } - } - return -1 -} - -func (gui *Gui) validateNormalWorkingTreeState() (bool, error) { - if gui.git.Status.WorkingTreeState() != enums.REBASE_MODE_NONE { - return false, gui.c.ErrorMsg(gui.c.Tr.CantPatchWhileRebasingError) - } - return true, nil -} - -func (gui *Gui) returnFocusFromPatchExplorerIfNecessary() error { - if gui.c.CurrentContext().GetKey() == gui.State.Contexts.CustomPatchBuilder.GetKey() { - return gui.helpers.PatchBuilding.Escape() - } - return nil -} - -func (gui *Gui) handleDeletePatchFromCommit() error { - if ok, err := gui.validateNormalWorkingTreeState(); !ok { - return err - } - - if err := gui.returnFocusFromPatchExplorerIfNecessary(); err != nil { - return err - } - - return gui.c.WithWaitingStatus(gui.c.Tr.RebasingStatus, func() error { - commitIndex := gui.getPatchCommitIndex() - gui.c.LogAction(gui.c.Tr.Actions.RemovePatchFromCommit) - err := gui.git.Patch.DeletePatchesFromCommit(gui.State.Model.Commits, commitIndex) - return gui.helpers.MergeAndRebase.CheckMergeOrRebase(err) - }) -} - -func (gui *Gui) handleMovePatchToSelectedCommit() error { - if ok, err := gui.validateNormalWorkingTreeState(); !ok { - return err - } - - if err := gui.returnFocusFromPatchExplorerIfNecessary(); err != nil { - return err - } - - return gui.c.WithWaitingStatus(gui.c.Tr.RebasingStatus, func() error { - commitIndex := gui.getPatchCommitIndex() - gui.c.LogAction(gui.c.Tr.Actions.MovePatchToSelectedCommit) - err := gui.git.Patch.MovePatchToSelectedCommit(gui.State.Model.Commits, commitIndex, gui.State.Contexts.LocalCommits.GetSelectedLineIdx()) - return gui.helpers.MergeAndRebase.CheckMergeOrRebase(err) - }) -} - -func (gui *Gui) handleMovePatchIntoWorkingTree() error { - if ok, err := gui.validateNormalWorkingTreeState(); !ok { - return err - } - - if err := gui.returnFocusFromPatchExplorerIfNecessary(); err != nil { - return err - } - - pull := func(stash bool) error { - return gui.c.WithWaitingStatus(gui.c.Tr.RebasingStatus, func() error { - commitIndex := gui.getPatchCommitIndex() - gui.c.LogAction(gui.c.Tr.Actions.MovePatchIntoIndex) - err := gui.git.Patch.MovePatchIntoIndex(gui.State.Model.Commits, commitIndex, stash) - return gui.helpers.MergeAndRebase.CheckMergeOrRebase(err) - }) - } - - if gui.helpers.WorkingTree.IsWorkingTreeDirty() { - return gui.c.Confirm(types.ConfirmOpts{ - Title: gui.c.Tr.MustStashTitle, - Prompt: gui.c.Tr.MustStashWarning, - HandleConfirm: func() error { - return pull(true) - }, - }) - } else { - return pull(false) - } -} - -func (gui *Gui) handlePullPatchIntoNewCommit() error { - if ok, err := gui.validateNormalWorkingTreeState(); !ok { - return err - } - - if err := gui.returnFocusFromPatchExplorerIfNecessary(); err != nil { - return err - } - - return gui.c.WithWaitingStatus(gui.c.Tr.RebasingStatus, func() error { - commitIndex := gui.getPatchCommitIndex() - gui.c.LogAction(gui.c.Tr.Actions.MovePatchIntoNewCommit) - err := gui.git.Patch.PullPatchIntoNewCommit(gui.State.Model.Commits, commitIndex) - return gui.helpers.MergeAndRebase.CheckMergeOrRebase(err) - }) -} - -func (gui *Gui) handleApplyPatch(reverse bool) error { - if err := gui.returnFocusFromPatchExplorerIfNecessary(); err != nil { - return err - } - - action := gui.c.Tr.Actions.ApplyPatch - if reverse { - action = "Apply patch in reverse" - } - gui.c.LogAction(action) - if err := gui.git.Patch.PatchBuilder.ApplyPatches(reverse); err != nil { - return gui.c.Error(err) - } - return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) -} - -func (gui *Gui) copyPatchToClipboard() error { - patch := gui.git.Patch.PatchBuilder.RenderAggregatedPatch(true) - - gui.c.LogAction(gui.c.Tr.Actions.CopyPatchToClipboard) - if err := gui.os.CopyToClipboard(patch); err != nil { - return gui.c.Error(err) - } - - gui.c.Toast(gui.c.Tr.PatchCopiedToClipboard) - - return nil -} diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 55dcaa715..31ab09667 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -143,13 +143,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi }, { ViewName: "", - Key: opts.GetKey(opts.Config.Universal.CreatePatchOptionsMenu), - Handler: self.handleCreatePatchOptionsMenu, - Description: self.c.Tr.ViewPatchOptions, - OpensMenu: true, - }, - { - ViewName: "", Key: opts.GetKey(opts.Config.Universal.Refresh), Handler: self.handleRefresh, Description: self.c.Tr.LcRefresh, |