Skip to content

Commit

Permalink
Some cleanups for APIs related to contexts (#3808)
Browse files Browse the repository at this point in the history
- **PR Description**

Some cleanups for APIs related to contexts. Most of these were triggered
by a TODO comment in the code.
  • Loading branch information
stefanhaller authored Aug 17, 2024
2 parents d89dc96 + 1eb5d89 commit 1cb29ce
Show file tree
Hide file tree
Showing 57 changed files with 115 additions and 202 deletions.
46 changes: 8 additions & 38 deletions pkg/gui/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (self *ContextMgr) Replace(c types.Context) error {

defer self.Unlock()

return self.ActivateContext(c, types.OnFocusOpts{})
return self.Activate(c, types.OnFocusOpts{})
}

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

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

return self.ActivateContext(contextToActivate, singleOpts)
return self.Activate(contextToActivate, singleOpts)
}

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

self.Unlock()

if err := self.deactivateContext(currentContext, types.OnFocusLostOpts{NewContextKey: newContext.GetKey()}); err != nil {
if err := self.deactivate(currentContext, types.OnFocusLostOpts{NewContextKey: newContext.GetKey()}); err != nil {
return err
}

return self.ActivateContext(newContext, types.OnFocusOpts{})
return self.Activate(newContext, types.OnFocusOpts{})
}

func (self *ContextMgr) RemoveContexts(contextsToRemove []types.Context) error {
self.Lock()

if len(self.ContextStack) == 1 {
self.Unlock()
return nil
}

rest := lo.Filter(self.ContextStack, func(context types.Context, _ int) bool {
for _, contextToRemove := range contextsToRemove {
if context.GetKey() == contextToRemove.GetKey() {
return false
}
}
return true
})
self.ContextStack = rest
contextToActivate := rest[len(rest)-1]
self.Unlock()

for _, context := range contextsToRemove {
if err := self.deactivateContext(context, types.OnFocusLostOpts{NewContextKey: contextToActivate.GetKey()}); err != nil {
return err
}
}

// activate the item at the top of the stack
return self.ActivateContext(contextToActivate, types.OnFocusOpts{})
}

func (self *ContextMgr) deactivateContext(c types.Context, opts types.OnFocusLostOpts) error {
func (self *ContextMgr) deactivate(c types.Context, opts types.OnFocusLostOpts) error {
view, _ := self.gui.c.GocuiGui().View(c.GetViewName())

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

func (self *ContextMgr) ActivateContext(c types.Context, opts types.OnFocusOpts) error {
func (self *ContextMgr) Activate(c types.Context, opts types.OnFocusOpts) error {
viewName := c.GetViewName()
v, err := self.gui.c.GocuiGui().View(viewName)
if err != nil {
Expand Down Expand Up @@ -392,7 +362,7 @@ func (self *ContextMgr) ContextForKey(key types.ContextKey) types.Context {
return nil
}

func (self *ContextMgr) PopupContexts() []types.Context {
func (self *ContextMgr) CurrentPopup() []types.Context {
self.RLock()
defer self.RUnlock()

Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/context/local_commits_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
getDisplayStrings := func(startIdx int, endIdx int) [][]string {
selectedCommitHash := ""

if c.CurrentContext().GetKey() == LOCAL_COMMITS_CONTEXT_KEY {
if c.Context().Current().GetKey() == LOCAL_COMMITS_CONTEXT_KEY {
selectedCommit := viewModel.GetSelected()
if selectedCommit != nil {
selectedCommitHash = selectedCommit.Hash
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/context/menu_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error {
return nil
}

if err := self.c.PopContext(); err != nil {
if err := self.c.Context().Pop(); err != nil {
return err
}

Expand Down
7 changes: 2 additions & 5 deletions pkg/gui/context/parent_context_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import "github.com/jesseduffield/lazygit/pkg/gui/types"

type ParentContextMgr struct {
ParentContext types.Context
// 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
hasParent bool
}

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

func (self *ParentContextMgr) SetParentContext(context types.Context) {
self.ParentContext = context
self.hasParent = true
}

func (self *ParentContextMgr) GetParentContext() (types.Context, bool) {
return self.ParentContext, self.hasParent
func (self *ParentContextMgr) GetParentContext() types.Context {
return self.ParentContext
}
2 changes: 1 addition & 1 deletion pkg/gui/context/patch_explorer_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewPatchExplorerContext(
func(selectedLineIdx int) error {
ctx.GetMutex().Lock()
defer ctx.GetMutex().Unlock()
return ctx.NavigateTo(ctx.c.IsCurrentContext(ctx), selectedLineIdx)
return ctx.NavigateTo(ctx.c.Context().IsCurrent(ctx), selectedLineIdx)
}),
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/context/sub_commits_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewSubCommitsContext(
}

selectedCommitHash := ""
if c.CurrentContext().GetKey() == SUB_COMMITS_CONTEXT_KEY {
if c.Context().Current().GetKey() == SUB_COMMITS_CONTEXT_KEY {
selectedCommit := viewModel.GetSelected()
if selectedCommit != nil {
selectedCommitHash = selectedCommit.Hash
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/controllers/commit_description_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (self *CommitDescriptionController) context() *context.CommitMessageContext
}

func (self *CommitDescriptionController) switchToCommitMessage() error {
return self.c.ReplaceContext(self.c.Contexts().CommitMessage)
return self.c.Context().Replace(self.c.Contexts().CommitMessage)
}

func (self *CommitDescriptionController) close() error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/controllers/commit_message_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (self *CommitMessageController) handleNextCommit() error {
}

func (self *CommitMessageController) switchToCommitDescription() error {
if err := self.c.ReplaceContext(self.c.Contexts().CommitDescription); err != nil {
if err := self.c.Context().Replace(self.c.Contexts().CommitDescription); err != nil {
return err
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/gui/controllers/commits_files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error
}

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

Expand Down Expand Up @@ -354,7 +354,7 @@ func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode
}
}

return self.c.PushContext(self.c.Contexts().CustomPatchBuilder, opts)
return self.c.Context().Push(self.c.Contexts().CustomPatchBuilder, opts)
}

if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.To != self.context().GetRef().RefName() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/controllers/confirmation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (self *ConfirmationController) GetKeybindings(opts types.KeybindingsOpts) [
self.c.UserConfig.Keybinding.Universal.Remove, self.c.UserConfig.Keybinding.Universal.Edit)
}
self.c.Views().Suggestions.Subtitle = subtitle
return self.c.ReplaceContext(self.c.Contexts().Suggestions)
return self.c.Context().Replace(self.c.Contexts().Suggestions)
}
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/controllers/context_lines_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (self *ContextLinesController) applyChange() error {
self.c.Toast(fmt.Sprintf(self.c.Tr.DiffContextSizeChanged, self.c.AppState.DiffContextSize))
self.c.SaveAppStateAndLogError()

currentContext := self.c.CurrentStaticContext()
currentContext := self.c.Context().CurrentStatic()
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:
Expand All @@ -117,6 +117,6 @@ func (self *ContextLinesController) checkCanChangeContext() error {
func (self *ContextLinesController) isShowingDiff() bool {
return lo.Contains(
CONTEXT_KEYS_SHOWING_DIFFS,
self.c.CurrentStaticContext().GetKey(),
self.c.Context().CurrentStatic().GetKey(),
)
}
6 changes: 3 additions & 3 deletions pkg/gui/controllers/custom_patch_options_menu_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (self *CustomPatchOptionsMenuAction) Call() error {
},
}...)

if self.c.CurrentContext().GetKey() == self.c.Contexts().LocalCommits.GetKey() {
if self.c.Context().Current().GetKey() == self.c.Contexts().LocalCommits.GetKey() {
selectedCommit := self.c.Contexts().LocalCommits.GetSelected()
if selectedCommit != nil && self.c.Git().Patch.PatchBuilder.To != selectedCommit.Hash {

Expand Down Expand Up @@ -122,7 +122,7 @@ func (self *CustomPatchOptionsMenuAction) validateNormalWorkingTreeState() (bool
}

func (self *CustomPatchOptionsMenuAction) returnFocusFromPatchExplorerIfNecessary() error {
if self.c.CurrentContext().GetKey() == self.c.Contexts().CustomPatchBuilder.GetKey() {
if self.c.Context().Current().GetKey() == self.c.Contexts().CustomPatchBuilder.GetKey() {
return self.c.Helpers().PatchBuilding.Escape()
}
return nil
Expand Down Expand Up @@ -220,7 +220,7 @@ func (self *CustomPatchOptionsMenuAction) handlePullPatchIntoNewCommit() error {
if err := self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err); err != nil {
return err
}
return self.c.PushContext(self.c.Contexts().LocalCommits)
return self.c.Context().Push(self.c.Contexts().LocalCommits)
})
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func (self *FilesController) EnterFile(opts types.OnFocusOpts) error {
return errors.New(self.c.Tr.FileStagingRequirements)
}

return self.c.PushContext(self.c.Contexts().Staging, opts)
return self.c.Context().Push(self.c.Contexts().Staging, opts)
}

func (self *FilesController) toggleStagedAll() error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/controllers/filtering_menu_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type FilteringMenuAction struct {
func (self *FilteringMenuAction) Call() error {
fileName := ""
author := ""
switch self.c.CurrentSideContext() {
switch self.c.Context().CurrentSide() {
case self.c.Contexts().Files:
node := self.c.Contexts().Files.GetSelected()
if node != nil {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (self *FilteringMenuAction) setFiltering() error {
repoState.SetScreenMode(types.SCREEN_HALF)
}

if err := self.c.PushContext(self.c.Contexts().LocalCommits); err != nil {
if err := self.c.Context().Push(self.c.Contexts().LocalCommits); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/controllers/helpers/commits_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOp

self.UpdateCommitPanelView(opts.InitialMessage)

return self.c.PushContext(self.c.Contexts().CommitMessage)
return self.c.Context().Push(self.c.Contexts().CommitMessage)
}

func (self *CommitsHelper) OnCommitSuccess() {
Expand Down Expand Up @@ -193,7 +193,7 @@ func (self *CommitsHelper) CloseCommitMessagePanel() error {
self.c.Views().CommitMessage.Visible = false
self.c.Views().CommitDescription.Visible = false

return self.c.PopContext()
return self.c.Context().Pop()
}

func (self *CommitsHelper) OpenCommitMenu(suggestionFunc func(string) []*types.Suggestion) error {
Expand Down
8 changes: 4 additions & 4 deletions pkg/gui/controllers/helpers/confirmation_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (self *ConfirmationHelper) wrappedConfirmationFunction(cancel goContext.Can
return func() error {
cancel()

if err := self.c.PopContext(); err != nil {
if err := self.c.Context().Pop(); err != nil {
return err
}

Expand Down Expand Up @@ -241,7 +241,7 @@ func (self *ConfirmationHelper) CreatePopupPanel(ctx goContext.Context, opts typ

self.c.State().GetRepoState().SetCurrentPopupOpts(&opts)

return self.c.PushContext(self.c.Contexts().Confirmation)
return self.c.Context().Push(self.c.Contexts().Confirmation)
}

func underlineLinks(text string) string {
Expand Down Expand Up @@ -325,7 +325,7 @@ func (self *ConfirmationHelper) getSelectedSuggestionValue() string {

func (self *ConfirmationHelper) ResizeCurrentPopupPanels() {
var parentPopupContext types.Context
for _, c := range self.c.CurrentPopupContexts() {
for _, c := range self.c.Context().CurrentPopup() {
switch c {
case self.c.Contexts().Menu:
self.resizeMenu(parentPopupContext)
Expand Down Expand Up @@ -431,7 +431,7 @@ func (self *ConfirmationHelper) IsPopupPanel(context types.Context) bool {
}

func (self *ConfirmationHelper) IsPopupPanelFocused() bool {
return self.IsPopupPanel(self.c.CurrentContext())
return self.IsPopupPanel(self.c.Context().Current())
}

func (self *ConfirmationHelper) TooltipForMenuItem(menuItem *types.MenuItem) string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/controllers/helpers/diff_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (self *DiffHelper) RenderDiff() error {
// which becomes an option when you bring up the diff menu, but when you're just
// flicking through branches it will be using the local branch name.
func (self *DiffHelper) CurrentDiffTerminals() []string {
c := self.c.CurrentSideContext()
c := self.c.Context().CurrentSide()

if c.GetKey() == "" {
return nil
Expand All @@ -93,7 +93,7 @@ func (self *DiffHelper) currentDiffTerminal() string {
}

func (self *DiffHelper) currentlySelectedFilename() string {
currentContext := self.c.CurrentContext()
currentContext := self.c.Context().Current()

switch currentContext := currentContext.(type) {
case types.IListContext:
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/controllers/helpers/fixup_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error {
}

self.c.Contexts().LocalCommits.SetSelection(index)
return self.c.PushContext(self.c.Contexts().LocalCommits)
return self.c.Context().Push(self.c.Contexts().LocalCommits)
}

if warnAboutAddedLines {
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/controllers/helpers/merge_and_rebase_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (self *MergeAndRebaseHelper) PromptForConflictHandling() error {
{
Label: self.c.Tr.ViewConflictsMenuItem,
OnPress: func() error {
return self.c.PushContext(self.c.Contexts().Files)
return self.c.Context().Push(self.c.Contexts().Files)
},
},
{
Expand Down Expand Up @@ -316,7 +316,7 @@ func (self *MergeAndRebaseHelper) RebaseOntoRef(ref string) error {
if err = self.ResetMarkedBaseCommit(); err != nil {
return err
}
return self.c.PushContext(self.c.Contexts().LocalCommits)
return self.c.Context().Push(self.c.Contexts().LocalCommits)
},
},
{
Expand Down
8 changes: 4 additions & 4 deletions pkg/gui/controllers/helpers/merge_conflicts_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (self *MergeConflictsHelper) EscapeMerge() error {
// to continue the merge/rebase. In that case, we don't want to then push the
// files context over it.
// So long as both places call OnUIThread, we're fine.
if self.c.IsCurrentContext(self.c.Contexts().MergeConflicts) {
return self.c.PushContext(self.c.Contexts().Files)
if self.c.Context().IsCurrent(self.c.Contexts().MergeConflicts) {
return self.c.Context().Push(self.c.Contexts().Files)
}
return nil
})
Expand Down Expand Up @@ -93,7 +93,7 @@ func (self *MergeConflictsHelper) SwitchToMerge(path string) error {
}
}

return self.c.PushContext(self.c.Contexts().MergeConflicts)
return self.c.Context().Push(self.c.Contexts().MergeConflicts)
}

func (self *MergeConflictsHelper) context() *context.MergeConflictsContext {
Expand Down Expand Up @@ -123,7 +123,7 @@ func (self *MergeConflictsHelper) RefreshMergeState() error {
self.c.Contexts().MergeConflicts.GetMutex().Lock()
defer self.c.Contexts().MergeConflicts.GetMutex().Unlock()

if self.c.CurrentContext().GetKey() != context.MERGE_CONFLICTS_CONTEXT_KEY {
if self.c.Context().Current().GetKey() != context.MERGE_CONFLICTS_CONTEXT_KEY {
return nil
}

Expand Down
Loading

0 comments on commit 1cb29ce

Please sign in to comment.