2022-02-26 10:26:39 +02:00
package gui
import (
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/controllers"
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
"github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking"
"github.com/jesseduffield/lazygit/pkg/gui/services/custom_commands"
2022-12-30 02:34:01 +02:00
"github.com/jesseduffield/lazygit/pkg/snake"
2022-02-26 10:26:39 +02:00
)
func ( gui * Gui ) resetControllers ( ) {
helperCommon := gui . c
osCommand := gui . os
model := gui . State . Model
refsHelper := helpers . NewRefsHelper (
helperCommon ,
gui . git ,
gui . State . Contexts ,
model ,
)
2022-08-06 10:05:00 +02:00
rebaseHelper := helpers . NewMergeAndRebaseHelper ( helperCommon , gui . State . Contexts , gui . git , refsHelper )
2022-04-08 17:06:07 +02:00
suggestionsHelper := helpers . NewSuggestionsHelper ( helperCommon , model , gui . refreshSuggestions )
2022-11-27 02:10:24 +02:00
setCommitMessage := gui . getSetTextareaTextFn ( func ( ) * gocui . View { return gui . Views . CommitMessage } )
getSavedCommitMessage := func ( ) string {
return gui . State . savedCommitMessage
}
2022-02-26 10:26:39 +02:00
gui . helpers = & helpers . Helpers {
Refs : refsHelper ,
2022-03-03 10:39:33 +02:00
Host : helpers . NewHostHelper ( helperCommon , gui . git ) ,
2022-06-13 03:01:26 +02:00
PatchBuilding : helpers . NewPatchBuildingHelper ( helperCommon , gui . git , gui . State . Contexts ) ,
2022-02-26 10:26:39 +02:00
Bisect : helpers . NewBisectHelper ( helperCommon , gui . git ) ,
2022-04-08 17:06:07 +02:00
Suggestions : suggestionsHelper ,
2022-02-26 10:26:39 +02:00
Files : helpers . NewFilesHelper ( helperCommon , gui . git , osCommand ) ,
2022-11-27 02:10:24 +02:00
WorkingTree : helpers . NewWorkingTreeHelper ( helperCommon , gui . git , gui . State . Contexts , refsHelper , model , setCommitMessage , getSavedCommitMessage ) ,
2022-02-26 10:26:39 +02:00
Tags : helpers . NewTagsHelper ( helperCommon , gui . git ) ,
GPG : helpers . NewGpgHelper ( helperCommon , gui . os , gui . git ) ,
MergeAndRebase : rebaseHelper ,
2022-08-06 10:50:52 +02:00
MergeConflicts : helpers . NewMergeConflictsHelper ( helperCommon , gui . State . Contexts , gui . git ) ,
2022-02-26 10:26:39 +02:00
CherryPick : helpers . NewCherryPickHelper (
helperCommon ,
gui . git ,
gui . State . Contexts ,
func ( ) * cherrypicking . CherryPicking { return gui . State . Modes . CherryPicking } ,
rebaseHelper ,
) ,
2022-04-08 17:06:07 +02:00
Upstream : helpers . NewUpstreamHelper ( helperCommon , model , suggestionsHelper . GetRemoteBranchesSuggestionsFunc ) ,
2022-02-26 10:26:39 +02:00
}
gui . CustomCommandsClient = custom_commands . NewClient (
helperCommon ,
gui . os ,
gui . git ,
gui . State . Contexts ,
gui . helpers ,
)
common := controllers . NewControllerCommon (
helperCommon ,
osCommand ,
gui . git ,
gui . helpers ,
model ,
gui . State . Contexts ,
gui . State . Modes ,
2022-07-31 05:52:56 +02:00
& gui . Mutexes ,
2022-02-26 10:26:39 +02:00
)
syncController := controllers . NewSyncController (
common ,
)
submodulesController := controllers . NewSubmodulesController (
common ,
gui . enterSubmodule ,
)
bisectController := controllers . NewBisectController ( common )
getCommitMessage := func ( ) string {
return strings . TrimSpace ( gui . Views . CommitMessage . TextArea . GetContent ( ) )
}
onCommitAttempt := func ( message string ) {
gui . State . savedCommitMessage = message
gui . Views . CommitMessage . ClearTextArea ( )
}
onCommitSuccess := func ( ) {
gui . State . savedCommitMessage = ""
}
commitMessageController := controllers . NewCommitMessageController (
common ,
getCommitMessage ,
onCommitAttempt ,
onCommitSuccess ,
)
remoteBranchesController := controllers . NewRemoteBranchesController ( common )
2022-03-26 06:52:35 +02:00
menuController := controllers . NewMenuController ( common )
localCommitsController := controllers . NewLocalCommitsController ( common , syncController . HandlePull )
tagsController := controllers . NewTagsController ( common )
filesController := controllers . NewFilesController (
common ,
gui . enterSubmodule ,
setCommitMessage ,
getSavedCommitMessage ,
)
2022-08-06 10:50:52 +02:00
mergeConflictsController := controllers . NewMergeConflictsController ( common )
2022-03-26 06:52:35 +02:00
remotesController := controllers . NewRemotesController (
common ,
func ( branches [ ] * models . RemoteBranch ) { gui . State . Model . RemoteBranches = branches } ,
)
undoController := controllers . NewUndoController ( common )
globalController := controllers . NewGlobalController ( common )
2022-06-13 03:01:26 +02:00
contextLinesController := controllers . NewContextLinesController ( common )
2022-12-20 15:25:49 +02:00
verticalScrollControllerFactory := controllers . NewVerticalScrollControllerFactory ( common , & gui . viewBufferManagerMap )
2022-06-13 03:01:26 +02:00
2022-02-26 10:26:39 +02:00
branchesController := controllers . NewBranchesController ( common )
gitFlowController := controllers . NewGitFlowController ( common )
filesRemoveController := controllers . NewFilesRemoveController ( common )
stashController := controllers . NewStashController ( common )
commitFilesController := controllers . NewCommitFilesController ( common )
2022-06-13 03:01:26 +02:00
patchExplorerControllerFactory := controllers . NewPatchExplorerControllerFactory ( common )
stagingController := controllers . NewStagingController ( common , gui . State . Contexts . Staging , gui . State . Contexts . StagingSecondary , false )
stagingSecondaryController := controllers . NewStagingController ( common , gui . State . Contexts . StagingSecondary , gui . State . Contexts . Staging , true )
patchBuildingController := controllers . NewPatchBuildingController ( common )
2022-12-30 02:34:01 +02:00
snakeController := controllers . NewSnakeController ( common , func ( ) * snake . Game { return gui . snakeGame } )
2022-02-26 10:26:39 +02:00
2022-03-26 08:03:30 +02:00
setSubCommits := func ( commits [ ] * models . Commit ) { gui . State . Model . SubCommits = commits }
2022-02-26 10:26:39 +02:00
2022-03-26 08:08:23 +02:00
for _ , context := range [ ] controllers . CanSwitchToSubCommits {
2022-02-26 10:26:39 +02:00
gui . State . Contexts . Branches ,
gui . State . Contexts . RemoteBranches ,
gui . State . Contexts . Tags ,
2022-03-24 13:07:30 +02:00
gui . State . Contexts . ReflogCommits ,
2022-02-26 10:26:39 +02:00
} {
2022-03-26 08:03:30 +02:00
controllers . AttachControllers ( context , controllers . NewSwitchToSubCommitsController (
common , setSubCommits , context ,
) )
2022-02-26 10:26:39 +02:00
}
2022-03-26 08:03:30 +02:00
for _ , context := range [ ] controllers . CanSwitchToDiffFiles {
2022-02-26 10:26:39 +02:00
gui . State . Contexts . LocalCommits ,
gui . State . Contexts . SubCommits ,
gui . State . Contexts . Stash ,
} {
2022-03-26 08:03:30 +02:00
controllers . AttachControllers ( context , controllers . NewSwitchToDiffFilesController (
common , gui . SwitchToCommitFilesContext , context ,
) )
2022-02-26 10:26:39 +02:00
}
2022-03-26 07:42:56 +02:00
for _ , context := range [ ] controllers . ContainsCommits {
gui . State . Contexts . LocalCommits ,
gui . State . Contexts . ReflogCommits ,
gui . State . Contexts . SubCommits ,
} {
2022-03-26 08:03:30 +02:00
controllers . AttachControllers ( context , controllers . NewBasicCommitsController ( common , context ) )
2022-03-26 07:42:56 +02:00
}
2022-06-13 03:01:26 +02:00
// TODO: add scroll controllers for main panels (need to bring some more functionality across for that e.g. reading more from the currently displayed git command)
controllers . AttachControllers ( gui . State . Contexts . Staging ,
stagingController ,
patchExplorerControllerFactory . Create ( gui . State . Contexts . Staging ) ,
verticalScrollControllerFactory . Create ( gui . State . Contexts . Staging ) ,
)
controllers . AttachControllers ( gui . State . Contexts . StagingSecondary ,
stagingSecondaryController ,
patchExplorerControllerFactory . Create ( gui . State . Contexts . StagingSecondary ) ,
verticalScrollControllerFactory . Create ( gui . State . Contexts . StagingSecondary ) ,
)
controllers . AttachControllers ( gui . State . Contexts . CustomPatchBuilder ,
patchBuildingController ,
patchExplorerControllerFactory . Create ( gui . State . Contexts . CustomPatchBuilder ) ,
verticalScrollControllerFactory . Create ( gui . State . Contexts . CustomPatchBuilder ) ,
)
controllers . AttachControllers ( gui . State . Contexts . CustomPatchBuilderSecondary ,
verticalScrollControllerFactory . Create ( gui . State . Contexts . CustomPatchBuilder ) ,
)
2022-08-06 10:50:52 +02:00
controllers . AttachControllers ( gui . State . Contexts . MergeConflicts ,
mergeConflictsController ,
)
2022-06-13 03:01:26 +02:00
controllers . AttachControllers ( gui . State . Contexts . Files ,
filesController ,
filesRemoveController ,
)
controllers . AttachControllers ( gui . State . Contexts . Tags ,
tagsController ,
)
controllers . AttachControllers ( gui . State . Contexts . Submodules ,
submodulesController ,
)
controllers . AttachControllers ( gui . State . Contexts . LocalCommits ,
localCommitsController ,
bisectController ,
)
controllers . AttachControllers ( gui . State . Contexts . Branches ,
branchesController ,
gitFlowController ,
)
controllers . AttachControllers ( gui . State . Contexts . LocalCommits ,
localCommitsController ,
bisectController ,
)
controllers . AttachControllers ( gui . State . Contexts . CommitFiles ,
commitFilesController ,
)
controllers . AttachControllers ( gui . State . Contexts . Remotes ,
remotesController ,
)
controllers . AttachControllers ( gui . State . Contexts . Stash ,
stashController ,
)
controllers . AttachControllers ( gui . State . Contexts . Menu ,
menuController ,
)
controllers . AttachControllers ( gui . State . Contexts . CommitMessage ,
commitMessageController ,
)
controllers . AttachControllers ( gui . State . Contexts . RemoteBranches ,
remoteBranchesController ,
)
controllers . AttachControllers ( gui . State . Contexts . Global ,
syncController ,
undoController ,
globalController ,
contextLinesController ,
)
2022-02-26 10:26:39 +02:00
2022-12-30 02:34:01 +02:00
controllers . AttachControllers ( gui . State . Contexts . Snake ,
snakeController ,
)
2022-02-27 02:42:22 +02:00
// this must come last so that we've got our click handlers defined against the context
2022-02-26 10:26:39 +02:00
listControllerFactory := controllers . NewListControllerFactory ( gui . c )
for _ , context := range gui . getListContexts ( ) {
controllers . AttachControllers ( context , listControllerFactory . Create ( context ) )
}
}