diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-02-13 18:04:09 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-17 19:13:40 +1100 |
commit | 8a555dd62ebdb985041f421dca650b941007afc1 (patch) | |
tree | 79262b68dfa7013ffdc1380ff313a967f1adfe45 /pkg | |
parent | bef26b9634a6a4c85028dcb1577161ed2c662b4e (diff) |
refactor
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/controllers/files_controller.go | 28 | ||||
-rw-r--r-- | pkg/gui/controllers/files_remove_controller.go (renamed from pkg/gui/controllers/files_controller_remove.go) | 74 | ||||
-rw-r--r-- | pkg/gui/gui.go | 3 |
3 files changed, 75 insertions, 30 deletions
diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 6868586e6..018322f1a 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -99,12 +99,6 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types Description: self.c.Tr.LcIgnoreFile, }, { - Key: opts.GetKey(opts.Config.Universal.Remove), - Handler: self.checkSelectedFileNode(self.remove), - Description: self.c.Tr.LcViewDiscardOptions, - OpensMenu: true, - }, - { Key: opts.GetKey(opts.Config.Files.RefreshFiles), Handler: self.refresh, Description: self.c.Tr.LcRefreshFiles, @@ -619,28 +613,6 @@ func (self *FilesController) OpenMergeTool() error { }) } -func (self *FilesController) ResetSubmodule(submodule *models.SubmoduleConfig) error { - return self.c.WithWaitingStatus(self.c.Tr.LcResettingSubmoduleStatus, func() error { - self.c.LogAction(self.c.Tr.Actions.ResetSubmodule) - - file := self.helpers.WorkingTree.FileForSubmodule(submodule) - if file != nil { - if err := self.git.WorkingTree.UnStageFile(file.Names(), file.Tracked); err != nil { - return self.c.Error(err) - } - } - - if err := self.git.Submodule.Stash(submodule); err != nil { - return self.c.Error(err) - } - if err := self.git.Submodule.Reset(submodule); err != nil { - return self.c.Error(err) - } - - return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES, types.SUBMODULES}}) - }) -} - func (self *FilesController) handleStashSave(stashFunc func(message string) error) error { if !self.helpers.WorkingTree.IsWorkingTreeDirty() { return self.c.ErrorMsg(self.c.Tr.NoTrackedStagedFilesStash) diff --git a/pkg/gui/controllers/files_controller_remove.go b/pkg/gui/controllers/files_remove_controller.go index cdebf8914..521167c33 100644 --- a/pkg/gui/controllers/files_controller_remove.go +++ b/pkg/gui/controllers/files_remove_controller.go @@ -1,13 +1,44 @@ package controllers import ( + "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/filetree" "github.com/jesseduffield/lazygit/pkg/gui/types" ) // splitting this action out into its own file because it's self-contained -func (self *FilesController) remove(node *filetree.FileNode) error { +type FilesRemoveController struct { + baseController + *controllerCommon +} + +var _ types.IController = &FilesRemoveController{} + +func NewFilesRemoveController( + common *controllerCommon, +) *FilesRemoveController { + return &FilesRemoveController{ + baseController: baseController{}, + controllerCommon: common, + } +} + +func (self *FilesRemoveController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + bindings := []*types.Binding{ + { + Key: opts.GetKey(opts.Config.Universal.Remove), + Handler: self.checkSelectedFileNode(self.remove), + Description: self.c.Tr.LcViewDiscardOptions, + OpensMenu: true, + }, + } + + return bindings +} + +func (self *FilesRemoveController) remove(node *filetree.FileNode) error { var menuItems []*types.MenuItem if node.File == nil { menuItems = []*types.MenuItem{ @@ -83,3 +114,44 @@ func (self *FilesController) remove(node *filetree.FileNode) error { return self.c.Menu(types.CreateMenuOptions{Title: node.GetPath(), Items: menuItems}) } + +func (self *FilesRemoveController) ResetSubmodule(submodule *models.SubmoduleConfig) error { + return self.c.WithWaitingStatus(self.c.Tr.LcResettingSubmoduleStatus, func() error { + self.c.LogAction(self.c.Tr.Actions.ResetSubmodule) + + file := self.helpers.WorkingTree.FileForSubmodule(submodule) + if file != nil { + if err := self.git.WorkingTree.UnStageFile(file.Names(), file.Tracked); err != nil { + return self.c.Error(err) + } + } + + if err := self.git.Submodule.Stash(submodule); err != nil { + return self.c.Error(err) + } + if err := self.git.Submodule.Reset(submodule); err != nil { + return self.c.Error(err) + } + + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES, types.SUBMODULES}}) + }) +} + +func (self *FilesRemoveController) checkSelectedFileNode(callback func(*filetree.FileNode) error) func() error { + return func() error { + node := self.context().GetSelectedFileNode() + if node == nil { + return nil + } + + return callback(node) + } +} + +func (self *FilesRemoveController) Context() types.Context { + return self.context() +} + +func (self *FilesRemoveController) context() *context.WorkingTreeContext { + return self.contexts.Files +} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 4e5007f1a..069d8a5a7 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -564,6 +564,7 @@ func (gui *Gui) resetControllers() { } branchesController := controllers.NewBranchesController(common) + filesRemoveController := controllers.NewFilesRemoveController(common) switchToSubCommitsControllerFactory := controllers.NewSubCommitsSwitchControllerFactory( common, @@ -592,7 +593,7 @@ func (gui *Gui) resetControllers() { } controllers.AttachControllers(gui.State.Contexts.Branches, branchesController) - controllers.AttachControllers(gui.State.Contexts.Files, gui.Controllers.Files) + controllers.AttachControllers(gui.State.Contexts.Files, gui.Controllers.Files, filesRemoveController) controllers.AttachControllers(gui.State.Contexts.Tags, gui.Controllers.Tags) controllers.AttachControllers(gui.State.Contexts.Submodules, gui.Controllers.Submodules) controllers.AttachControllers(gui.State.Contexts.LocalCommits, gui.Controllers.LocalCommits, bisectController) |