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/services/custom_commands"
2023-03-23 09:47:29 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/status"
2023-01-18 18:14:03 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
2022-02-26 10:26:39 +02:00
)
2023-03-23 09:47:29 +02:00
func ( gui * Gui ) Helpers ( ) * helpers . Helpers {
return gui . helpers
}
2023-04-15 06:51:11 +02:00
func ( gui * Gui ) resetHelpersAndControllers ( ) {
2022-02-26 10:26:39 +02:00
helperCommon := gui . c
2023-07-16 11:39:53 +02:00
recordDirectoryHelper := helpers . NewRecordDirectoryHelper ( helperCommon )
reposHelper := helpers . NewRecentReposHelper ( helperCommon , recordDirectoryHelper , gui . onNewRepo )
2023-03-23 03:53:18 +02:00
refsHelper := helpers . NewRefsHelper ( helperCommon )
2023-07-16 12:38:22 +02:00
suggestionsHelper := helpers . NewSuggestionsHelper ( helperCommon )
worktreeHelper := helpers . NewWorktreeHelper ( helperCommon , reposHelper , refsHelper , suggestionsHelper )
2022-02-26 10:26:39 +02:00
2023-03-23 03:53:18 +02:00
rebaseHelper := helpers . NewMergeAndRebaseHelper ( helperCommon , refsHelper )
2023-01-21 13:38:14 +02:00
setCommitSummary := gui . getCommitMessageSetTextareaTextFn ( func ( ) * gocui . View { return gui . Views . CommitMessage } )
setCommitDescription := gui . getCommitMessageSetTextareaTextFn ( func ( ) * gocui . View { return gui . Views . CommitDescription } )
getCommitSummary := func ( ) string {
return strings . TrimSpace ( gui . Views . CommitMessage . TextArea . GetContent ( ) )
2022-11-27 02:10:24 +02:00
}
2023-01-21 13:38:14 +02:00
getCommitDescription := func ( ) string {
return strings . TrimSpace ( gui . Views . CommitDescription . TextArea . GetContent ( ) )
}
commitsHelper := helpers . NewCommitsHelper ( helperCommon ,
getCommitSummary ,
setCommitSummary ,
getCommitDescription ,
setCommitDescription ,
)
2023-03-23 03:53:18 +02:00
gpgHelper := helpers . NewGpgHelper ( helperCommon )
2022-12-30 14:24:24 +02:00
viewHelper := helpers . NewViewHelper ( helperCommon , gui . State . Contexts )
2023-03-23 03:53:18 +02:00
patchBuildingHelper := helpers . NewPatchBuildingHelper ( helperCommon )
stagingHelper := helpers . NewStagingHelper ( helperCommon )
mergeConflictsHelper := helpers . NewMergeConflictsHelper ( helperCommon )
2023-07-16 11:39:53 +02:00
2022-09-02 08:50:34 +02:00
refreshHelper := helpers . NewRefreshHelper (
helperCommon ,
refsHelper ,
rebaseHelper ,
patchBuildingHelper ,
stagingHelper ,
mergeConflictsHelper ,
worktreeHelper ,
)
2023-03-23 09:47:29 +02:00
diffHelper := helpers . NewDiffHelper ( helperCommon )
cherryPickHelper := helpers . NewCherryPickHelper (
helperCommon ,
rebaseHelper ,
)
bisectHelper := helpers . NewBisectHelper ( helperCommon )
2023-03-23 09:55:41 +02:00
windowHelper := helpers . NewWindowHelper ( helperCommon , viewHelper )
modeHelper := helpers . NewModeHelper (
helperCommon ,
diffHelper ,
patchBuildingHelper ,
cherryPickHelper ,
rebaseHelper ,
bisectHelper ,
)
appStatusHelper := helpers . NewAppStatusHelper (
helperCommon ,
func ( ) * status . StatusManager { return gui . statusManager } ,
)
2022-02-26 10:26:39 +02:00
gui . helpers = & helpers . Helpers {
2023-03-23 09:47:29 +02:00
Refs : refsHelper ,
Host : helpers . NewHostHelper ( helperCommon ) ,
PatchBuilding : patchBuildingHelper ,
Staging : stagingHelper ,
Bisect : bisectHelper ,
Suggestions : suggestionsHelper ,
Files : helpers . NewFilesHelper ( helperCommon ) ,
2023-01-21 13:38:14 +02:00
WorkingTree : helpers . NewWorkingTreeHelper ( helperCommon , refsHelper , commitsHelper , gpgHelper ) ,
2023-07-22 06:05:42 +02:00
Tags : helpers . NewTagsHelper ( helperCommon , commitsHelper ) ,
2023-03-23 09:47:29 +02:00
GPG : helpers . NewGpgHelper ( helperCommon ) ,
MergeAndRebase : rebaseHelper ,
MergeConflicts : mergeConflictsHelper ,
CherryPick : cherryPickHelper ,
2023-03-23 03:53:18 +02:00
Upstream : helpers . NewUpstreamHelper ( helperCommon , suggestionsHelper . GetRemoteBranchesSuggestionsFunc ) ,
AmendHelper : helpers . NewAmendHelper ( helperCommon , gpgHelper ) ,
2023-01-21 13:38:14 +02:00
Commits : commitsHelper ,
2022-12-30 14:24:24 +02:00
Snake : helpers . NewSnakeHelper ( helperCommon ) ,
2023-03-23 09:47:29 +02:00
Diff : diffHelper ,
2023-07-16 06:14:09 +02:00
Repos : reposHelper ,
2022-12-30 14:24:24 +02:00
RecordDirectory : recordDirectoryHelper ,
Update : helpers . NewUpdateHelper ( helperCommon , gui . Updater ) ,
2023-03-23 09:55:41 +02:00
Window : windowHelper ,
2022-12-30 14:24:24 +02:00
View : viewHelper ,
Refresh : refreshHelper ,
2023-03-23 03:53:18 +02:00
Confirmation : helpers . NewConfirmationHelper ( helperCommon ) ,
2023-03-23 09:55:41 +02:00
Mode : modeHelper ,
AppStatus : appStatusHelper ,
WindowArrangement : helpers . NewWindowArrangementHelper (
gui . c ,
windowHelper ,
modeHelper ,
appStatusHelper ,
2023-03-23 09:47:29 +02:00
) ,
2022-09-02 08:50:34 +02:00
Search : helpers . NewSearchHelper ( helperCommon ) ,
Worktree : worktreeHelper ,
2022-02-26 10:26:39 +02:00
}
gui . CustomCommandsClient = custom_commands . NewClient (
helperCommon ,
gui . helpers ,
)
2023-03-23 09:47:29 +02:00
common := controllers . NewControllerCommon ( helperCommon , gui )
2022-02-26 10:26:39 +02:00
syncController := controllers . NewSyncController (
common ,
)
2022-12-30 14:24:24 +02:00
submodulesController := controllers . NewSubmodulesController ( common )
2022-02-26 10:26:39 +02:00
bisectController := controllers . NewBisectController ( common )
commitMessageController := controllers . NewCommitMessageController (
common ,
2023-01-21 13:38:14 +02:00
)
commitDescriptionController := controllers . NewCommitDescriptionController (
common ,
2022-02-26 10:26:39 +02:00
)
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 ,
)
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 } ,
)
2022-09-01 20:25:41 +02:00
worktreesController := controllers . NewWorktreesController ( common )
2022-03-26 06:52:35 +02:00
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 14:24:24 +02:00
snakeController := controllers . NewSnakeController ( common )
2023-03-23 04:04:57 +02:00
reflogCommitsController := controllers . NewReflogCommitsController ( common )
subCommitsController := controllers . NewSubCommitsController ( common )
2022-12-30 14:24:24 +02:00
statusController := controllers . NewStatusController ( common )
2023-03-21 11:57:52 +02:00
commandLogController := controllers . NewCommandLogController ( common )
confirmationController := controllers . NewConfirmationController ( common )
suggestionsController := controllers . NewSuggestionsController ( common )
2023-03-26 07:33:52 +02:00
jumpToSideWindowController := controllers . NewJumpToSideWindowController ( common )
sideWindowControllerFactory := controllers . NewSideWindowControllerFactory ( common )
2023-05-27 06:14:43 +02:00
filterControllerFactory := controllers . NewFilterControllerFactory ( common )
for _ , context := range gui . c . Context ( ) . AllFilterable ( ) {
controllers . AttachControllers ( context , filterControllerFactory . Create ( context ) )
}
searchControllerFactory := controllers . NewSearchControllerFactory ( common )
for _ , context := range gui . c . Context ( ) . AllSearchable ( ) {
controllers . AttachControllers ( context , searchControllerFactory . Create ( context ) )
}
2023-03-26 07:33:52 +02:00
// allow for navigating between side window contexts
for _ , context := range [ ] types . Context {
gui . State . Contexts . Status ,
gui . State . Contexts . Remotes ,
2022-09-01 20:25:41 +02:00
gui . State . Contexts . Worktrees ,
2023-03-26 07:33:52 +02:00
gui . State . Contexts . Tags ,
gui . State . Contexts . Branches ,
gui . State . Contexts . RemoteBranches ,
gui . State . Contexts . Files ,
gui . State . Contexts . Submodules ,
gui . State . Contexts . ReflogCommits ,
gui . State . Contexts . LocalCommits ,
gui . State . Contexts . CommitFiles ,
gui . State . Contexts . SubCommits ,
gui . State . Contexts . Stash ,
} {
controllers . AttachControllers ( context , sideWindowControllerFactory . Create ( context ) )
}
2022-02-26 10:26:39 +02:00
2023-02-10 14:44:15 +02:00
setSubCommits := func ( commits [ ] * models . Commit ) {
gui . Mutexes . SubCommitsMutex . Lock ( )
defer gui . Mutexes . SubCommitsMutex . Unlock ( )
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 (
2022-12-30 14:24:24 +02:00
common , context , gui . State . Contexts . CommitFiles ,
2022-03-26 08:03:30 +02:00
) )
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 ) )
2023-07-16 11:39:53 +02:00
}
2023-07-17 11:59:51 +02:00
for _ , context := range [ ] controllers . CanViewWorktreeOptions {
gui . State . Contexts . LocalCommits ,
gui . State . Contexts . ReflogCommits ,
gui . State . Contexts . SubCommits ,
gui . State . Contexts . Stash ,
gui . State . Contexts . Branches ,
gui . State . Contexts . RemoteBranches ,
gui . State . Contexts . Tags ,
} {
controllers . AttachControllers ( context , controllers . NewWorktreeOptionsController ( common , context ) )
2022-03-26 07:42:56 +02:00
}
2022-12-30 14:24:24 +02:00
controllers . AttachControllers ( gui . State . Contexts . ReflogCommits ,
reflogCommitsController ,
)
controllers . AttachControllers ( gui . State . Contexts . SubCommits ,
subCommitsController ,
)
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 ,
)
2022-09-01 20:25:41 +02:00
controllers . AttachControllers ( gui . State . Contexts . Worktrees ,
worktreesController ,
)
2022-06-13 03:01:26 +02:00
controllers . AttachControllers ( gui . State . Contexts . Stash ,
stashController ,
)
controllers . AttachControllers ( gui . State . Contexts . Menu ,
menuController ,
)
controllers . AttachControllers ( gui . State . Contexts . CommitMessage ,
commitMessageController ,
)
2023-01-21 13:38:14 +02:00
controllers . AttachControllers ( gui . State . Contexts . CommitDescription ,
commitDescriptionController ,
)
2022-06-13 03:01:26 +02:00
controllers . AttachControllers ( gui . State . Contexts . RemoteBranches ,
remoteBranchesController ,
)
2022-12-30 14:24:24 +02:00
controllers . AttachControllers ( gui . State . Contexts . Status ,
statusController ,
)
2023-03-21 11:57:52 +02:00
controllers . AttachControllers ( gui . State . Contexts . CommandLog ,
commandLogController ,
)
controllers . AttachControllers ( gui . State . Contexts . Confirmation ,
confirmationController ,
)
controllers . AttachControllers ( gui . State . Contexts . Suggestions ,
suggestionsController ,
)
2023-05-27 06:14:43 +02:00
controllers . AttachControllers ( gui . State . Contexts . Search ,
controllers . NewSearchPromptController ( common ) ,
)
2022-06-13 03:01:26 +02:00
controllers . AttachControllers ( gui . State . Contexts . Global ,
syncController ,
undoController ,
globalController ,
contextLinesController ,
2023-03-26 07:33:52 +02:00
jumpToSideWindowController ,
2022-06-13 03:01:26 +02:00
)
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
2023-03-23 03:35:07 +02:00
listControllerFactory := controllers . NewListControllerFactory ( common )
2023-03-23 13:05:25 +02:00
for _ , context := range gui . c . Context ( ) . AllList ( ) {
2022-02-26 10:26:39 +02:00
controllers . AttachControllers ( context , listControllerFactory . Create ( context ) )
}
}
2022-12-30 14:24:24 +02:00
2023-01-21 13:38:14 +02:00
func ( gui * Gui ) getCommitMessageSetTextareaTextFn ( getView func ( ) * gocui . View ) func ( string ) {
2022-12-30 14:24:24 +02:00
return func ( text string ) {
// using a getView function so that we don't need to worry about when the view is created
view := getView ( )
view . ClearTextArea ( )
view . TextArea . TypeString ( text )
2023-01-21 13:38:14 +02:00
gui . helpers . Confirmation . ResizeCommitMessagePanels ( )
2022-12-30 14:24:24 +02:00
view . RenderTextArea ( )
}
}