Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate keybinding suggestions in status bar after switching repos #3660

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/gui/context/base_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ func (self *BaseContext) AddMouseKeybindingsFn(fn types.MouseKeybindingsFn) {
self.mouseKeybindingsFns = append(self.mouseKeybindingsFns, fn)
}

func (self *BaseContext) ClearAllBindingsFn() {
self.keybindingsFns = []types.KeybindingsFn{}
self.mouseKeybindingsFns = []types.MouseKeybindingsFn{}
}

func (self *BaseContext) AddOnClickFn(fn func() error) {
if fn != nil {
self.onClickFn = fn
Expand Down
4 changes: 4 additions & 0 deletions pkg/gui/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (gui *Gui) Helpers() *helpers.Helpers {
// in the keybinding menu: the earlier that the controller is attached to a context,
// the lower in the list the keybindings will appear.
func (gui *Gui) resetHelpersAndControllers() {
for _, context := range gui.Contexts().Flatten() {
context.ClearAllBindingsFn()
}

helperCommon := gui.c
recordDirectoryHelper := helpers.NewRecordDirectoryHelper(helperCommon)
reposHelper := helpers.NewRecentReposHelper(helperCommon, recordDirectoryHelper, gui.onNewRepo)
Expand Down
1 change: 1 addition & 0 deletions pkg/gui/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type IBaseContext interface {

AddKeybindingsFn(KeybindingsFn)
AddMouseKeybindingsFn(MouseKeybindingsFn)
ClearAllBindingsFn()

// This is a bit of a hack at the moment: we currently only set an onclick function so that
// our list controller can come along and wrap it in a list-specific click handler.
Expand Down
6 changes: 6 additions & 0 deletions pkg/integration/components/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ func (self *Shell) Clone(repoName string) *Shell {
return self
}

func (self *Shell) CloneNonBare(repoName string) *Shell {
self.RunCommand([]string{"git", "clone", ".", "../" + repoName})

return self
}

func (self *Shell) SetBranchUpstream(branch string, upstream string) *Shell {
self.RunCommand([]string{"git", "branch", "--set-upstream-to=" + upstream, branch})

Expand Down
1 change: 1 addition & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ var tests = []*components.IntegrationTest{
ui.Accordion,
ui.DoublePopup,
ui.EmptyMenu,
ui.KeybindingSuggestionsWhenSwitchingRepos,
ui.ModeSpecificKeybindingSuggestions,
ui.OpenLinkFailure,
ui.RangeSelect,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ui

import (
"path/filepath"

"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var KeybindingSuggestionsWhenSwitchingRepos = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Show correct keybinding suggestions after switching between repos",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
otherRepo, _ := filepath.Abs("../other")
config.AppState.RecentRepos = []string{otherRepo}
},
SetupRepo: func(shell *Shell) {
shell.CloneNonBare("other")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
switchToRepo := func(repo string) {
t.GlobalPress(keys.Universal.OpenRecentRepos)
t.ExpectPopup().Menu().Title(Equals("Recent repositories")).
Lines(
Contains(repo).IsSelected(),
Contains("Cancel"),
).Confirm()
t.Views().Status().Content(Contains(repo + " → master"))
}

t.Views().Files().Focus()
t.Views().Options().Content(
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ? | Cancel: <esc>"))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little awkward that we depend on the concrete keybinding suggestions here, so we'll have to adapt the test when those change. It doesn't bother me too much though, it's still better than not having a test at all.

I did choose the panel that has the lowest number of keybindings.


switchToRepo("other")
switchToRepo("repo")

t.Views().Options().Content(
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ? | Cancel: <esc>"))
},
})
Loading