Skip to content

Commit 1cb29ce

Browse files
authored
Some cleanups for APIs related to contexts (#3808)
- **PR Description** Some cleanups for APIs related to contexts. Most of these were triggered by a TODO comment in the code.
2 parents d89dc96 + 1eb5d89 commit 1cb29ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+115
-202
lines changed

pkg/gui/context.go

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (self *ContextMgr) Replace(c types.Context) error {
5353

5454
defer self.Unlock()
5555

56-
return self.ActivateContext(c, types.OnFocusOpts{})
56+
return self.Activate(c, types.OnFocusOpts{})
5757
}
5858

5959
func (self *ContextMgr) Push(c types.Context, opts ...types.OnFocusOpts) error {
@@ -74,7 +74,7 @@ func (self *ContextMgr) Push(c types.Context, opts ...types.OnFocusOpts) error {
7474
contextsToDeactivate, contextToActivate := self.pushToContextStack(c)
7575

7676
for _, contextToDeactivate := range contextsToDeactivate {
77-
if err := self.deactivateContext(contextToDeactivate, types.OnFocusLostOpts{NewContextKey: c.GetKey()}); err != nil {
77+
if err := self.deactivate(contextToDeactivate, types.OnFocusLostOpts{NewContextKey: c.GetKey()}); err != nil {
7878
return err
7979
}
8080
}
@@ -83,7 +83,7 @@ func (self *ContextMgr) Push(c types.Context, opts ...types.OnFocusOpts) error {
8383
return nil
8484
}
8585

86-
return self.ActivateContext(contextToActivate, singleOpts)
86+
return self.Activate(contextToActivate, singleOpts)
8787
}
8888

8989
// Adjusts the context stack based on the context that's being pushed and
@@ -160,44 +160,14 @@ func (self *ContextMgr) Pop() error {
160160

161161
self.Unlock()
162162

163-
if err := self.deactivateContext(currentContext, types.OnFocusLostOpts{NewContextKey: newContext.GetKey()}); err != nil {
163+
if err := self.deactivate(currentContext, types.OnFocusLostOpts{NewContextKey: newContext.GetKey()}); err != nil {
164164
return err
165165
}
166166

167-
return self.ActivateContext(newContext, types.OnFocusOpts{})
167+
return self.Activate(newContext, types.OnFocusOpts{})
168168
}
169169

170-
func (self *ContextMgr) RemoveContexts(contextsToRemove []types.Context) error {
171-
self.Lock()
172-
173-
if len(self.ContextStack) == 1 {
174-
self.Unlock()
175-
return nil
176-
}
177-
178-
rest := lo.Filter(self.ContextStack, func(context types.Context, _ int) bool {
179-
for _, contextToRemove := range contextsToRemove {
180-
if context.GetKey() == contextToRemove.GetKey() {
181-
return false
182-
}
183-
}
184-
return true
185-
})
186-
self.ContextStack = rest
187-
contextToActivate := rest[len(rest)-1]
188-
self.Unlock()
189-
190-
for _, context := range contextsToRemove {
191-
if err := self.deactivateContext(context, types.OnFocusLostOpts{NewContextKey: contextToActivate.GetKey()}); err != nil {
192-
return err
193-
}
194-
}
195-
196-
// activate the item at the top of the stack
197-
return self.ActivateContext(contextToActivate, types.OnFocusOpts{})
198-
}
199-
200-
func (self *ContextMgr) deactivateContext(c types.Context, opts types.OnFocusLostOpts) error {
170+
func (self *ContextMgr) deactivate(c types.Context, opts types.OnFocusLostOpts) error {
201171
view, _ := self.gui.c.GocuiGui().View(c.GetViewName())
202172

203173
if opts.NewContextKey != context.SEARCH_CONTEXT_KEY {
@@ -220,7 +190,7 @@ func (self *ContextMgr) deactivateContext(c types.Context, opts types.OnFocusLos
220190
return nil
221191
}
222192

223-
func (self *ContextMgr) ActivateContext(c types.Context, opts types.OnFocusOpts) error {
193+
func (self *ContextMgr) Activate(c types.Context, opts types.OnFocusOpts) error {
224194
viewName := c.GetViewName()
225195
v, err := self.gui.c.GocuiGui().View(viewName)
226196
if err != nil {
@@ -392,7 +362,7 @@ func (self *ContextMgr) ContextForKey(key types.ContextKey) types.Context {
392362
return nil
393363
}
394364

395-
func (self *ContextMgr) PopupContexts() []types.Context {
365+
func (self *ContextMgr) CurrentPopup() []types.Context {
396366
self.RLock()
397367
defer self.RUnlock()
398368

pkg/gui/context/local_commits_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
3434
getDisplayStrings := func(startIdx int, endIdx int) [][]string {
3535
selectedCommitHash := ""
3636

37-
if c.CurrentContext().GetKey() == LOCAL_COMMITS_CONTEXT_KEY {
37+
if c.Context().Current().GetKey() == LOCAL_COMMITS_CONTEXT_KEY {
3838
selectedCommit := viewModel.GetSelected()
3939
if selectedCommit != nil {
4040
selectedCommitHash = selectedCommit.Hash

pkg/gui/context/menu_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error {
197197
return nil
198198
}
199199

200-
if err := self.c.PopContext(); err != nil {
200+
if err := self.c.Context().Pop(); err != nil {
201201
return err
202202
}
203203

pkg/gui/context/parent_context_mgr.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import "github.com/jesseduffield/lazygit/pkg/gui/types"
44

55
type ParentContextMgr struct {
66
ParentContext types.Context
7-
// we can't know on the calling end whether a Context is actually a nil value without reflection, so we're storing this flag here to tell us. There has got to be a better way around this
8-
hasParent bool
97
}
108

119
var _ types.ParentContexter = (*ParentContextMgr)(nil)
1210

1311
func (self *ParentContextMgr) SetParentContext(context types.Context) {
1412
self.ParentContext = context
15-
self.hasParent = true
1613
}
1714

18-
func (self *ParentContextMgr) GetParentContext() (types.Context, bool) {
19-
return self.ParentContext, self.hasParent
15+
func (self *ParentContextMgr) GetParentContext() types.Context {
16+
return self.ParentContext
2017
}

pkg/gui/context/patch_explorer_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func NewPatchExplorerContext(
5353
func(selectedLineIdx int) error {
5454
ctx.GetMutex().Lock()
5555
defer ctx.GetMutex().Unlock()
56-
return ctx.NavigateTo(ctx.c.IsCurrentContext(ctx), selectedLineIdx)
56+
return ctx.NavigateTo(ctx.c.Context().IsCurrent(ctx), selectedLineIdx)
5757
}),
5858
)
5959

pkg/gui/context/sub_commits_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func NewSubCommitsContext(
4747
}
4848

4949
selectedCommitHash := ""
50-
if c.CurrentContext().GetKey() == SUB_COMMITS_CONTEXT_KEY {
50+
if c.Context().Current().GetKey() == SUB_COMMITS_CONTEXT_KEY {
5151
selectedCommit := viewModel.GetSelected()
5252
if selectedCommit != nil {
5353
selectedCommitHash = selectedCommit.Hash

pkg/gui/controllers/commit_description_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (self *CommitDescriptionController) context() *context.CommitMessageContext
5353
}
5454

5555
func (self *CommitDescriptionController) switchToCommitMessage() error {
56-
return self.c.ReplaceContext(self.c.Contexts().CommitMessage)
56+
return self.c.Context().Replace(self.c.Contexts().CommitMessage)
5757
}
5858

5959
func (self *CommitDescriptionController) close() error {

pkg/gui/controllers/commit_message_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (self *CommitMessageController) handleNextCommit() error {
8585
}
8686

8787
func (self *CommitMessageController) switchToCommitDescription() error {
88-
if err := self.c.ReplaceContext(self.c.Contexts().CommitDescription); err != nil {
88+
if err := self.c.Context().Replace(self.c.Contexts().CommitDescription); err != nil {
8989
return err
9090
}
9191
return nil

pkg/gui/controllers/commits_files_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error
178178
}
179179

180180
func (self *CommitFilesController) discard(selectedNodes []*filetree.CommitFileNode) error {
181-
parentContext, ok := self.c.CurrentContext().GetParentContext()
182-
if !ok || parentContext.GetKey() != context.LOCAL_COMMITS_CONTEXT_KEY {
181+
parentContext := self.c.Context().Current().GetParentContext()
182+
if parentContext == nil || parentContext.GetKey() != context.LOCAL_COMMITS_CONTEXT_KEY {
183183
return errors.New(self.c.Tr.CanOnlyDiscardFromLocalCommits)
184184
}
185185

@@ -354,7 +354,7 @@ func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode
354354
}
355355
}
356356

357-
return self.c.PushContext(self.c.Contexts().CustomPatchBuilder, opts)
357+
return self.c.Context().Push(self.c.Contexts().CustomPatchBuilder, opts)
358358
}
359359

360360
if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.To != self.context().GetRef().RefName() {

pkg/gui/controllers/confirmation_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (self *ConfirmationController) GetKeybindings(opts types.KeybindingsOpts) [
4949
self.c.UserConfig.Keybinding.Universal.Remove, self.c.UserConfig.Keybinding.Universal.Edit)
5050
}
5151
self.c.Views().Suggestions.Subtitle = subtitle
52-
return self.c.ReplaceContext(self.c.Contexts().Suggestions)
52+
return self.c.Context().Replace(self.c.Contexts().Suggestions)
5353
}
5454
return nil
5555
},

0 commit comments

Comments
 (0)