diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-06-13 11:01:26 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-08-06 13:49:11 +1000 |
commit | 524bf83a4a681408c3fb57818f6968cab632e0ae (patch) | |
tree | 8858b4ee8d4670dcdd1637fe5fedf00ff080c154 /pkg/gui/controllers/context_lines_controller.go | |
parent | 6dfef08efc5c7f262194c0af35fd777428f33a1a (diff) |
refactor to only have one context per view
Diffstat (limited to 'pkg/gui/controllers/context_lines_controller.go')
-rw-r--r-- | pkg/gui/controllers/context_lines_controller.go | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/pkg/gui/controllers/context_lines_controller.go b/pkg/gui/controllers/context_lines_controller.go new file mode 100644 index 000000000..c90bd9c9f --- /dev/null +++ b/pkg/gui/controllers/context_lines_controller.go @@ -0,0 +1,116 @@ +package controllers + +import ( + "errors" + + "github.com/jesseduffield/lazygit/pkg/gui/context" + "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" +) + +// This controller lets you change the context size for diffs. The 'context' in 'context size' refers to the conventional meaning of the word 'context' in a diff, as opposed to lazygit's own idea of a 'context'. + +var CONTEXT_KEYS_SHOWING_DIFFS = []types.ContextKey{ + context.FILES_CONTEXT_KEY, + context.COMMIT_FILES_CONTEXT_KEY, + context.STASH_CONTEXT_KEY, + context.LOCAL_COMMITS_CONTEXT_KEY, + context.SUB_COMMITS_CONTEXT_KEY, + context.STAGING_MAIN_CONTEXT_KEY, + context.STAGING_SECONDARY_CONTEXT_KEY, + context.PATCH_BUILDING_MAIN_CONTEXT_KEY, + context.PATCH_BUILDING_SECONDARY_CONTEXT_KEY, +} + +type ContextLinesController struct { + baseController + *controllerCommon +} + +var _ types.IController = &ContextLinesController{} + +func NewContextLinesController( + common *controllerCommon, +) *ContextLinesController { + return &ContextLinesController{ + baseController: baseController{}, + controllerCommon: common, + } +} + +func (self *ContextLinesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + bindings := []*types.Binding{ + { + Key: opts.GetKey(opts.Config.Universal.IncreaseContextInDiffView), + Handler: self.Increase, + Description: self.c.Tr.IncreaseContextInDiffView, + }, + { + Key: opts.GetKey(opts.Config.Universal.DecreaseContextInDiffView), + Handler: self.Decrease, + Description: self.c.Tr.DecreaseContextInDiffView, + }, + } + + return bindings +} + +func (self *ContextLinesController) Context() types.Context { + return nil +} + +func (self *ContextLinesController) Increase() error { + if self.isShowingDiff() { + if err := self.checkCanChangeContext(); err != nil { + return self.c.Error(err) + } + + self.c.UserConfig.Git.DiffContextSize = self.c.UserConfig.Git.DiffContextSize + 1 + return self.applyChange() + } + + return nil +} + +func (self *ContextLinesController) Decrease() error { + old_size := self.c.UserConfig.Git.DiffContextSize + + if self.isShowingDiff() && old_size > 1 { + if err := self.checkCanChangeContext(); err != nil { + return self.c.Error(err) + } + + self.c.UserConfig.Git.DiffContextSize = old_size - 1 + return self.applyChange() + } + + return nil +} + +func (self *ContextLinesController) applyChange() error { + currentContext := self.c.CurrentStaticContext() + switch currentContext.GetKey() { + // we make an exception for our staging and patch building contexts because they actually need to refresh their state afterwards. + case context.PATCH_BUILDING_MAIN_CONTEXT_KEY: + return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.PATCH_BUILDING}}) + case context.STAGING_MAIN_CONTEXT_KEY, context.STAGING_SECONDARY_CONTEXT_KEY: + return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STAGING}}) + default: + return currentContext.HandleRenderToMain() + } +} + +func (self *ContextLinesController) checkCanChangeContext() error { + if self.git.Patch.PatchManager.Active() { + return errors.New(self.c.Tr.CantChangeContextSizeError) + } + + return nil +} + +func (self *ContextLinesController) isShowingDiff() bool { + return lo.Contains( + CONTEXT_KEYS_SHOWING_DIFFS, + self.c.CurrentStaticContext().GetKey(), + ) +} |