Repository: jesseduffield/lazygit
File: pkg/gui/controllers/commit_description_controller.go (Go)

Code before:

func (self *CommitDescriptionController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
	return []*gocui.ViewMouseBinding{
		{
			ViewName: self.Context().GetViewName(),
			Key:      gocui.MouseLeft,
			Handler:  self.onClick,
		},
	}
}

func (self *CommitDescriptionController) GetOnFocus() func(types.OnFocusOpts) {
	return func(types.OnFocusOpts) {
		self.c.Views().CommitDescription.Footer = utils.ResolvePlaceholderString(self.c.Tr.CommitDescriptionFooter,
			map[string]string{
				"confirmInEditorKeybinding": keybindings.Label(self.c.UserConfig().Keybinding.Universal.ConfirmInEditor),
			})
	}
}

func (self *CommitDescriptionController) switchToCommitMessage() error {
	self.c.Context().Replace(self.c.Contexts().CommitMessage)
	return nil
}

func (self *CommitDescriptionController) handleTogglePanel() error {
	if self.c.GocuiGui().IsPasting {
		// Handling tabs in pasted commit messages is not optimal, but hopefully
		// good enough for now. We simply insert 4 spaces without worrying about
		// column alignment. This works well enough for leading indentation,
		// which is common in pasted code snippets.
		view := self.Context().GetView()
		for range 4 {
			view.Editor.Edit(view, gocui.KeySpace, ' ', 0)
		}
		return nil
	}

	return self.switchToCommitMessage()
}

func (self *CommitDescriptionController) close() error {
	self.c.Helpers().Commits.CloseCommitMessagePanel()
	return nil
}

func (self *CommitDescriptionController) confirm() error {
	return self.c.Helpers().Commits.HandleCommitConfirm()
}

func (self *CommitDescriptionController) openCommitMenu() error {

Review comment (suggestion):
Something that is not clear here is that `opts.Config.Universal.TogglePanel` defaults to `tab`. So a reader could get confused as to why we're conditionally inserting spaces. A comment should clear up that confusion.

Things probably get awkward when a user remaps these keys, and I'm not sure of a good way of dealing with that (doesn't need to be solved in this PR)

Code after:

func (self *CommitDescriptionController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
	return []*gocui.ViewMouseBinding{
		{
			ViewName: self.Context().GetViewName(),
			Key:      gocui.MouseLeft,
			Handler:  self.onClick,
		},
	}
}

func (self *CommitDescriptionController) GetOnFocus() func(types.OnFocusOpts) {
	return func(types.OnFocusOpts) {
		self.c.Views().CommitDescription.Footer = utils.ResolvePlaceholderString(self.c.Tr.CommitDescriptionFooter,
			map[string]string{
				"confirmInEditorKeybinding": keybindings.Label(self.c.UserConfig().Keybinding.Universal.ConfirmInEditor),
			})
	}
}

func (self *CommitDescriptionController) switchToCommitMessage() error {
	self.c.Context().Replace(self.c.Contexts().CommitMessage)
	return nil
}

func (self *CommitDescriptionController) handleTogglePanel() error {
	// The default keybinding for this action is "<tab>", which means that we
	// also get here when pasting multi-line text that contains tabs. In that
	// case we don't want to toggle the panel, but insert the tab as a character
	// (somehow, see below).
	//
	// Only do this if the TogglePanel command is actually mapped to "<tab>"
	// (the default). If it's not, we can only hope that it's mapped to some
	// ctrl key or fn key, which is unlikely to occur in pasted text. And if
	// they mapped some *other* command to "<tab>", then we're totally out of
	// luck.
	if self.c.GocuiGui().IsPasting && self.c.UserConfig().Keybinding.Universal.TogglePanel == "<tab>" {
		// Handling tabs in pasted commit messages is not optimal, but hopefully
		// good enough for now. We simply insert 4 spaces without worrying about
		// column alignment. This works well enough for leading indentation,
		// which is common in pasted code snippets.
		view := self.Context().GetView()
		for range 4 {
			view.Editor.Edit(view, gocui.KeySpace, ' ', 0)
		}
		return nil
	}

	return self.switchToCommitMessage()
}