Skip to content

Commit c5d846b

Browse files
committed
feat(custom command)!: support multiple contexts within one command
1 parent f598da0 commit c5d846b

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

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: 29 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,40 @@ 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+
bindings := []*types.Binding{}
43+
for _, viewName := range viewNames {
44+
bindings = append(bindings, &types.Binding{
45+
ViewName: viewName,
46+
Key: keybindings.GetKey(customCommand.Key),
47+
Modifier: gocui.ModNone,
48+
Handler: handler,
49+
Description: description,
50+
})
51+
}
52+
return bindings, nil
4953
}
5054

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

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

61-
viewName := ctx.GetViewName()
62-
return viewName, nil
75+
return viewNames, nil
6376
}
6477

6578
func (self *KeybindingCreator) contextForContextKey(contextKey types.ContextKey) (types.Context, bool) {

0 commit comments

Comments
 (0)