Repository: jesseduffield/lazygit
File: pkg/gui/services/custom_commands/keybinding_creator.go (Go)

Code before:
func (self *KeybindingCreator) call(customCommand config.CustomCommand, handler func() error) ([]*types.Binding, error) {
	if customCommand.Context == "" {
		return nil, formatContextNotProvidedError(customCommand)
	}

	viewNames, err := self.getViewNamesAndContexts(customCommand)
	if err != nil {
		return nil, err
	}

	description := customCommand.Description
	if description == "" {
		description = customCommand.Command
	}

	bindings := []*types.Binding{}
	for _, viewName := range viewNames {
		bindings = append(bindings, &types.Binding{
			ViewName:    viewName,
			Key:         keybindings.GetKey(customCommand.Key),
			Modifier:    gocui.ModNone,
			Handler:     handler,
			Description: description,
		})
	}
	return bindings, nil
}

func (self *KeybindingCreator) getViewNamesAndContexts(customCommand config.CustomCommand) ([]string, error) {
	if customCommand.Context == "global" {
		return []string{}, nil
	}

	contexts := strings.Split(customCommand.Context, ",")
	contexts = lo.Map(contexts, func(context string, _ int) string {
		return strings.TrimSpace(context)
	})

	viewNames := []string{}
	for _, context := range contexts {
		ctx, ok := self.contextForContextKey(types.ContextKey(context))
		if !ok {
			return []string{}, formatUnknownContextError(customCommand)
		}

		viewNames = append(viewNames, ctx.GetViewName())
	}

	return viewNames, nil
}


Review comment (suggestion):
Matter of taste, probably, but I'd write this block as
```go
	return lo.Map(viewNames, func(viewName string, _ int) *types.Binding {
		return &types.Binding{
			ViewName:    viewName,
			Key:         keybindings.GetKey(customCommand.Key),
			Modifier:    gocui.ModNone,
			Handler:     handler,
			Description: description,
		}
	}), nil
```

Code after:
func (self *KeybindingCreator) call(customCommand config.CustomCommand, handler func() error) ([]*types.Binding, error) {
	if customCommand.Context == "" {
		return nil, formatContextNotProvidedError(customCommand)
	}

	viewNames, err := self.getViewNamesAndContexts(customCommand)
	if err != nil {
		return nil, err
	}

	description := customCommand.Description
	if description == "" {
		description = customCommand.Command
	}

	return lo.Map(viewNames, func(viewName string, _ int) *types.Binding {
		return &types.Binding{
			ViewName:    viewName,
			Key:         keybindings.GetKey(customCommand.Key),
			Modifier:    gocui.ModNone,
			Handler:     handler,
			Description: description,
		}
	}), nil
}

func (self *KeybindingCreator) getViewNamesAndContexts(customCommand config.CustomCommand) ([]string, error) {
	if customCommand.Context == "global" {
		return []string{""}, nil
	}

	contexts := strings.Split(customCommand.Context, ",")
	contexts = lo.Map(contexts, func(context string, _ int) string {
		return strings.TrimSpace(context)
	})

	viewNames := []string{}
	for _, context := range contexts {
		ctx, ok := self.contextForContextKey(types.ContextKey(context))
		if !ok {
			return []string{}, formatUnknownContextError(customCommand)
		}

		viewNames = append(viewNames, ctx.GetViewName())
	}

	return viewNames, nil
}

func (self *KeybindingCreator) contextForContextKey(contextKey types.ContextKey) (types.Context, bool) {
	for _, context := range self.c.Contexts().Flatten() {