Skip to content

Commit 542030f

Browse files
yam-liustefanhaller
authored andcommitted
Support multiple contexts within one command, add tests, update doc
1 parent 206b2c6 commit 542030f

File tree

5 files changed

+93
-18
lines changed

5 files changed

+93
-18
lines changed

docs/Custom_Command_Keybindings.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ The permitted contexts are:
8787
| stash | The 'Stash' tab |
8888
| global | This keybinding will take affect everywhere |
8989

90+
> **Bonus**
91+
>
92+
> You can use a comma-separated string, such as `context: 'commits, subCommits'`, to make it effective in multiple contexts.
93+
94+
9095
## Prompts
9196

9297
### Common fields

pkg/gui/services/custom_commands/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ func (self *Client) GetCustomCommandKeybindings() ([]*types.Binding, error) {
3939
bindings := []*types.Binding{}
4040
for _, customCommand := range self.customCommands {
4141
handler := self.handlerCreator.call(customCommand)
42-
binding, err := self.keybindingCreator.call(customCommand, handler)
42+
compoundBindings, err := self.keybindingCreator.call(customCommand, handler)
4343
if err != nil {
4444
return nil, err
4545
}
46-
bindings = append(bindings, binding)
46+
bindings = append(bindings, compoundBindings...)
4747
}
4848

4949
return bindings, nil

pkg/gui/services/custom_commands/keybinding_creator.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ func NewKeybindingCreator(c *helpers.HelperCommon) *KeybindingCreator {
2424
}
2525
}
2626

27-
func (self *KeybindingCreator) call(customCommand config.CustomCommand, handler func() error) (*types.Binding, error) {
27+
func (self *KeybindingCreator) call(customCommand config.CustomCommand, handler func() error) ([]*types.Binding, error) {
2828
if customCommand.Context == "" {
2929
return nil, formatContextNotProvidedError(customCommand)
3030
}
3131

32-
viewName, err := self.getViewNameAndContexts(customCommand)
32+
viewNames, err := self.getViewNamesAndContexts(customCommand)
3333
if err != nil {
3434
return nil, err
3535
}
@@ -39,27 +39,38 @@ func (self *KeybindingCreator) call(customCommand config.CustomCommand, handler
3939
description = customCommand.Command
4040
}
4141

42-
return &types.Binding{
43-
ViewName: viewName,
44-
Key: keybindings.GetKey(customCommand.Key),
45-
Modifier: gocui.ModNone,
46-
Handler: handler,
47-
Description: description,
48-
}, nil
42+
return lo.Map(viewNames, func(viewName string, _ int) *types.Binding {
43+
return &types.Binding{
44+
ViewName: viewName,
45+
Key: keybindings.GetKey(customCommand.Key),
46+
Modifier: gocui.ModNone,
47+
Handler: handler,
48+
Description: description,
49+
}
50+
}), nil
4951
}
5052

51-
func (self *KeybindingCreator) getViewNameAndContexts(customCommand config.CustomCommand) (string, error) {
53+
func (self *KeybindingCreator) getViewNamesAndContexts(customCommand config.CustomCommand) ([]string, error) {
5254
if customCommand.Context == "global" {
53-
return "", nil
55+
return []string{""}, nil
5456
}
5557

56-
ctx, ok := self.contextForContextKey(types.ContextKey(customCommand.Context))
57-
if !ok {
58-
return "", formatUnknownContextError(customCommand)
58+
contexts := strings.Split(customCommand.Context, ",")
59+
contexts = lo.Map(contexts, func(context string, _ int) string {
60+
return strings.TrimSpace(context)
61+
})
62+
63+
viewNames := []string{}
64+
for _, context := range contexts {
65+
ctx, ok := self.contextForContextKey(types.ContextKey(context))
66+
if !ok {
67+
return []string{}, formatUnknownContextError(customCommand)
68+
}
69+
70+
viewNames = append(viewNames, ctx.GetViewName())
5971
}
6072

61-
viewName := ctx.GetViewName()
62-
return viewName, nil
73+
return viewNames, nil
6374
}
6475

6576
func (self *KeybindingCreator) contextForContextKey(contextKey types.ContextKey) (types.Context, bool) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package custom_commands
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var MultipleContexts = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Test that multiple contexts works",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupRepo: func(shell *Shell) {
13+
shell.EmptyCommit("my change")
14+
},
15+
SetupConfig: func(cfg *config.AppConfig) {
16+
cfg.UserConfig.CustomCommands = []config.CustomCommand{
17+
{
18+
Key: "X",
19+
Context: "commits, reflogCommits",
20+
Command: "touch myfile",
21+
ShowOutput: false,
22+
},
23+
}
24+
},
25+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
26+
// commits
27+
t.Views().Commits().
28+
Focus().
29+
Press("X")
30+
31+
t.Views().Files().
32+
Focus().
33+
Lines(Contains("myfile"))
34+
35+
t.Shell().DeleteFile("myfile")
36+
t.GlobalPress(keys.Files.RefreshFiles)
37+
38+
// branches
39+
t.Views().Branches().
40+
Focus().
41+
Press("X")
42+
43+
t.Views().Files().
44+
Focus().
45+
IsEmpty()
46+
47+
// files
48+
t.Views().ReflogCommits().
49+
Focus().
50+
Press("X")
51+
52+
t.Views().Files().
53+
Focus().
54+
Lines(Contains("myfile"))
55+
56+
t.Shell().DeleteFile("myfile")
57+
},
58+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ var tests = []*components.IntegrationTest{
126126
custom_commands.History,
127127
custom_commands.MenuFromCommand,
128128
custom_commands.MenuFromCommandsOutput,
129+
custom_commands.MultipleContexts,
129130
custom_commands.MultiplePrompts,
130131
custom_commands.OmitFromHistory,
131132
custom_commands.ShowOutputInPanel,

0 commit comments

Comments
 (0)