1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-11 11:42:12 +02:00

refactor to make code clearer

This commit is contained in:
Jesse Duffield 2022-03-26 17:03:30 +11:00
parent 45dab51214
commit 077b6eb8a3
4 changed files with 42 additions and 83 deletions

@ -129,41 +129,35 @@ func (gui *Gui) resetControllers() {
stashController := controllers.NewStashController(common)
commitFilesController := controllers.NewCommitFilesController(common)
switchToSubCommitsControllerFactory := controllers.NewSubCommitsSwitchControllerFactory(
common,
func(commits []*models.Commit) { gui.State.Model.SubCommits = commits },
)
setSubCommits := func(commits []*models.Commit) { gui.State.Model.SubCommits = commits }
for _, context := range []controllers.ContextWithRefName{
gui.State.Contexts.Branches,
gui.State.Contexts.RemoteBranches,
gui.State.Contexts.Tags,
} {
controllers.AttachControllers(context, switchToSubCommitsControllerFactory.Create(context))
controllers.AttachControllers(context, controllers.NewSwitchToSubCommitsController(
common, setSubCommits, context,
))
}
commitishControllerFactory := controllers.NewCommitishControllerFactory(
common,
gui.SwitchToCommitFilesContext,
)
for _, context := range []controllers.Commitish{
for _, context := range []controllers.CanSwitchToDiffFiles{
gui.State.Contexts.LocalCommits,
gui.State.Contexts.ReflogCommits,
gui.State.Contexts.SubCommits,
gui.State.Contexts.Stash,
} {
controllers.AttachControllers(context, commitishControllerFactory.Create(context))
controllers.AttachControllers(context, controllers.NewSwitchToDiffFilesController(
common, gui.SwitchToCommitFilesContext, context,
))
}
basicCommitsControllerFactory := controllers.NewBasicCommitsControllerFactory(common)
for _, context := range []controllers.ContainsCommits{
gui.State.Contexts.LocalCommits,
gui.State.Contexts.ReflogCommits,
gui.State.Contexts.SubCommits,
} {
controllers.AttachControllers(context, basicCommitsControllerFactory.Create(context))
controllers.AttachControllers(context, controllers.NewBasicCommitsController(common, context))
}
controllers.AttachControllers(gui.State.Contexts.Branches, branchesController, gitFlowController)

@ -8,10 +8,6 @@ import (
// This controller is for all contexts that contain a list of commits.
type BasicCommitsControllerFactory struct {
controllerCommon *controllerCommon
}
var _ types.IController = &BasicCommitsController{}
type ContainsCommits interface {
@ -27,18 +23,10 @@ type BasicCommitsController struct {
context ContainsCommits
}
func NewBasicCommitsControllerFactory(
common *controllerCommon,
) *BasicCommitsControllerFactory {
return &BasicCommitsControllerFactory{
controllerCommon: common,
}
}
func (self *BasicCommitsControllerFactory) Create(context ContainsCommits) *BasicCommitsController {
func NewBasicCommitsController(controllerCommon *controllerCommon, context ContainsCommits) *BasicCommitsController {
return &BasicCommitsController{
baseController: baseController{},
controllerCommon: self.controllerCommon,
controllerCommon: controllerCommon,
context: context,
}
}

@ -6,47 +6,35 @@ import (
// This controller is for all contexts that contain commit files.
type CommitishControllerFactory struct {
controllerCommon *controllerCommon
viewFiles func(SwitchToCommitFilesContextOpts) error
}
var _ types.IController = &SwitchToDiffFilesController{}
var _ types.IController = &CommitishController{}
type Commitish interface {
type CanSwitchToDiffFiles interface {
types.Context
CanRebase() bool
GetSelectedRefName() string
}
type CommitishController struct {
type SwitchToDiffFilesController struct {
baseController
*controllerCommon
context Commitish
context CanSwitchToDiffFiles
viewFiles func(SwitchToCommitFilesContextOpts) error
}
func NewCommitishControllerFactory(
common *controllerCommon,
func NewSwitchToDiffFilesController(
controllerCommon *controllerCommon,
viewFiles func(SwitchToCommitFilesContextOpts) error,
) *CommitishControllerFactory {
return &CommitishControllerFactory{
controllerCommon: common,
context CanSwitchToDiffFiles,
) *SwitchToDiffFilesController {
return &SwitchToDiffFilesController{
baseController: baseController{},
controllerCommon: controllerCommon,
context: context,
viewFiles: viewFiles,
}
}
func (self *CommitishControllerFactory) Create(context Commitish) *CommitishController {
return &CommitishController{
baseController: baseController{},
controllerCommon: self.controllerCommon,
context: context,
viewFiles: self.viewFiles,
}
}
func (self *CommitishController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
func (self *SwitchToDiffFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Universal.GoInto),
@ -58,11 +46,11 @@ func (self *CommitishController) GetKeybindings(opts types.KeybindingsOpts) []*t
return bindings
}
func (self *CommitishController) GetOnClick() func() error {
func (self *SwitchToDiffFilesController) GetOnClick() func() error {
return self.checkSelected(self.enter)
}
func (self *CommitishController) checkSelected(callback func(string) error) func() error {
func (self *SwitchToDiffFilesController) checkSelected(callback func(string) error) func() error {
return func() error {
refName := self.context.GetSelectedRefName()
if refName == "" {
@ -73,7 +61,7 @@ func (self *CommitishController) checkSelected(callback func(string) error) func
}
}
func (self *CommitishController) enter(refName string) error {
func (self *SwitchToDiffFilesController) enter(refName string) error {
return self.viewFiles(SwitchToCommitFilesContextOpts{
RefName: refName,
CanRebase: self.context.CanRebase(),
@ -81,6 +69,6 @@ func (self *CommitishController) enter(refName string) error {
})
}
func (self *CommitishController) Context() types.Context {
func (self *SwitchToDiffFilesController) Context() types.Context {
return self.context
}

@ -6,19 +6,14 @@ import (
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type SubCommitsSwitchControllerFactory struct {
controllerCommon *controllerCommon
setSubCommits func([]*models.Commit)
}
var _ types.IController = &SubCommitsSwitchController{}
var _ types.IController = &SwitchToSubCommitsController{}
type ContextWithRefName interface {
types.Context
GetSelectedRefName() string
}
type SubCommitsSwitchController struct {
type SwitchToSubCommitsController struct {
baseController
*controllerCommon
context ContextWithRefName
@ -26,26 +21,20 @@ type SubCommitsSwitchController struct {
setSubCommits func([]*models.Commit)
}
func NewSubCommitsSwitchControllerFactory(
common *controllerCommon,
func NewSwitchToSubCommitsController(
controllerCommon *controllerCommon,
setSubCommits func([]*models.Commit),
) *SubCommitsSwitchControllerFactory {
return &SubCommitsSwitchControllerFactory{
controllerCommon: common,
context ContextWithRefName,
) *SwitchToSubCommitsController {
return &SwitchToSubCommitsController{
baseController: baseController{},
controllerCommon: controllerCommon,
context: context,
setSubCommits: setSubCommits,
}
}
func (self *SubCommitsSwitchControllerFactory) Create(context ContextWithRefName) *SubCommitsSwitchController {
return &SubCommitsSwitchController{
baseController: baseController{},
controllerCommon: self.controllerCommon,
context: context,
setSubCommits: self.setSubCommits,
}
}
func (self *SubCommitsSwitchController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
func (self *SwitchToSubCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Handler: self.viewCommits,
@ -57,11 +46,11 @@ func (self *SubCommitsSwitchController) GetKeybindings(opts types.KeybindingsOpt
return bindings
}
func (self *SubCommitsSwitchController) GetOnClick() func() error {
func (self *SwitchToSubCommitsController) GetOnClick() func() error {
return self.viewCommits
}
func (self *SubCommitsSwitchController) viewCommits() error {
func (self *SwitchToSubCommitsController) viewCommits() error {
refName := self.context.GetSelectedRefName()
if refName == "" {
return nil
@ -87,6 +76,6 @@ func (self *SubCommitsSwitchController) viewCommits() error {
return self.c.PushContext(self.contexts.SubCommits)
}
func (self *SubCommitsSwitchController) Context() types.Context {
func (self *SwitchToSubCommitsController) Context() types.Context {
return self.context
}