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

Code before:
	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
}

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

Review comment (bug):
This is a bug, it breaks custom commands for the "global" context. You need to change this to `return []string{""}` to fix that.

Unfortunately we don't have a test that catches this; it would be great to add one. We have several existing tests for custom commands, you could pick one of those as an example to follow, e.g. `pkg/integration/tests/custom_commands/show_output_in_panel.go`.

Code after:
	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() {
		if context.GetKey() == contextKey {
			return context, true
		}
	}