Handle text pasted into the commit message panel in one piece

Use the new OnPaste handler for the summary and the description. This
replaces the IsPasting checks in the keybindings for confirming and for
switching between summary and description. These checks only worked for
the default keys of those keybindings. If a user had bound Enter or Tab
to something else, pasting a newline or a tab ran that command.

Otherwise the behavior stays the same, except that the panel now drops
Esc and control characters in pasted text. Before, it executed them;
for example, a pasted Esc closed the panel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Stefan Haller
2026-11-12 10:22:50 +01:00
co-authored by Claude Opus 5.5
parent 0111d9bd61
commit f2a63e2de0
4 changed files with 41 additions and 70 deletions
@@ -4,7 +4,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
)
type CommitDescriptionController struct {
@@ -27,7 +26,7 @@ func (self *CommitDescriptionController) GetKeybindings(opts types.KeybindingsOp
bindings := []*types.Binding{
{
Keys: opts.GetKeys(opts.Config.Universal.TogglePanel),
Handler: self.handleTogglePanel,
Handler: self.switchToCommitMessage,
},
{
Keys: opts.GetKeys(opts.Config.Universal.Return),
@@ -86,32 +85,6 @@ func (self *CommitDescriptionController) switchToCommitMessage() error {
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 && lo.Contains(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.NewKeyRune(' '))
}
return nil
}
return self.switchToCommitMessage()
}
func (self *CommitDescriptionController) close() error {
self.c.Helpers().Commits.CloseCommitMessagePanel()
return nil
@@ -7,7 +7,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/samber/lo"
)
type CommitMessageController struct {
@@ -48,7 +47,7 @@ func (self *CommitMessageController) GetKeybindings(opts types.KeybindingsOpts)
},
{
Keys: opts.GetKeys(opts.Config.Universal.TogglePanel),
Handler: self.handleTogglePanel,
Handler: self.switchToCommitDescription,
},
{
Keys: opts.GetKeys(opts.Config.CommitMessage.CommitMenu),
@@ -112,32 +111,6 @@ func (self *CommitMessageController) switchToCommitDescription() error {
return nil
}
func (self *CommitMessageController) 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 && lo.Contains(self.c.UserConfig().Keybinding.Universal.TogglePanel, "<tab>") {
// It is unlikely that a pasted commit message contains a tab in the
// subject line, so it shouldn't matter too much how we handle it.
// Simply insert 4 spaces instead; all that matters is that we don't
// switch to the description panel.
view := self.context().GetView()
for range 4 {
view.Editor.Edit(view, gocui.NewKeyRune(' '))
}
return nil
}
return self.switchToCommitDescription()
}
func (self *CommitMessageController) handleCommitIndexChange(value int) error {
currentIndex := self.context().GetSelectedIndex()
newIndex := currentIndex + value
@@ -171,20 +144,6 @@ func (self *CommitMessageController) setCommitMessageAtIndex(index int) (bool, e
}
func (self *CommitMessageController) confirm() error {
// The default keybinding for this action is "<enter>", which means that we
// also get here when pasting multi-line text that contains newlines. In
// that case we don't want to confirm the commit, but switch to the
// description panel instead so that the rest of the pasted text goes there.
//
// Only do this if the SubmitEditorText command is actually mapped to
// "<enter>" (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 "<enter>", then we're totally
// out of luck.
if self.c.GocuiGui().IsPasting && lo.Contains(self.c.UserConfig().Keybinding.Universal.SubmitEditorText, "<enter>") {
return self.switchToCommitDescription()
}
return self.c.Helpers().Commits.HandleCommitConfirm()
}
@@ -320,6 +320,37 @@ func (self *CommitsHelper) copyCommitMessageToClipboard() error {
return nil
}
// HandlePasteIntoSummary inserts text that the user pasted into the summary
// with the terminal's paste command. If the text has several lines, the rest
// of them goes into the description.
func (self *CommitsHelper) HandlePasteIntoSummary(text string) {
summary, rest, hasMoreLines := strings.Cut(text, "\n")
view := self.c.Views().CommitMessage
view.TextArea.TypeString(replaceTabsInPastedText(summary))
view.RenderTextArea()
self.c.Contexts().CommitMessage.RenderSubtitle()
if hasMoreLines {
self.c.Context().Replace(self.c.Contexts().CommitDescription)
self.HandlePasteIntoDescription(rest)
}
}
// HandlePasteIntoDescription inserts text that the user pasted into the
// description with the terminal's paste command.
func (self *CommitsHelper) HandlePasteIntoDescription(text string) {
view := self.c.Views().CommitDescription
view.TextArea.TypeString(replaceTabsInPastedText(text))
view.RenderTextArea()
}
// Replace each tab with four spaces, without aligning to tab stops. This works
// well enough for leading indentation, which is common in pasted code snippets.
func replaceTabsInPastedText(text string) string {
return strings.ReplaceAll(text, "\t", " ")
}
func stripTrailingWhitespace(message string) string {
lines := strings.Split(message, "\n")
for i, line := range lines {
+8
View File
@@ -119,10 +119,18 @@ func (gui *Gui) createAllViews() error {
gui.Views.CommitMessage.Visible = false
gui.Views.CommitMessage.Editable = true
gui.Views.CommitMessage.Editor = gocui.EditorFunc(gui.commitMessageEditor)
gui.Views.CommitMessage.OnPaste = func(text string) error {
gui.helpers.Commits.HandlePasteIntoSummary(text)
return nil
}
gui.Views.CommitDescription.Visible = false
gui.Views.CommitDescription.Editable = true
gui.Views.CommitDescription.Editor = gocui.EditorFunc(gui.commitDescriptionEditor)
gui.Views.CommitDescription.OnPaste = func(text string) error {
gui.helpers.Commits.HandlePasteIntoDescription(text)
return nil
}
gui.Views.Confirmation.Visible = false
gui.Views.Confirmation.Wrap = true