mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-11-12 15:53:33 -04:00
Check the auto-wrap config inside the helper that removes hard line breaks
All three callers of TryRemoveHardLineBreaks check the AutoWrapCommitMessage config first and pass the AutoWrapWidth config. We are about to add a fourth caller, so move both into a CommitsHelper method. This also makes it impossible to call it with the wrong width. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
671dfa394e
commit
8b8e3c05aa
@@ -6,7 +6,6 @@ import (
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
|
||||
"github.com/jesseduffield/lazygit/pkg/gocui"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
@@ -166,9 +165,7 @@ func (self *CommitMessageController) setCommitMessageAtIndex(index int) (bool, e
|
||||
}
|
||||
return false, errors.New(self.c.Tr.CommitWithoutMessageErr)
|
||||
}
|
||||
if self.c.UserConfig().Git.Commit.AutoWrapCommitMessage {
|
||||
commitMessage = helpers.TryRemoveHardLineBreaks(commitMessage, self.c.UserConfig().Git.Commit.AutoWrapWidth)
|
||||
}
|
||||
commitMessage = self.c.Helpers().Commits.TryRemoveHardLineBreaksIfAutoWrapping(commitMessage)
|
||||
self.c.Helpers().Commits.SetMessageAndDescriptionInView(commitMessage)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -89,7 +89,18 @@ func (self *CommitsHelper) JoinCommitMessageAndUnwrappedDescription() string {
|
||||
return self.getCommitSummary() + "\n" + self.getUnwrappedCommitDescription()
|
||||
}
|
||||
|
||||
func TryRemoveHardLineBreaks(message string, autoWrapWidth int) string {
|
||||
// TryRemoveHardLineBreaksIfAutoWrapping replaces the hard line breaks of a
|
||||
// message with spaces wherever auto-wrapping would break the line at the same
|
||||
// place, so that the paragraphs reflow when the user edits them in the panel.
|
||||
func (self *CommitsHelper) TryRemoveHardLineBreaksIfAutoWrapping(message string) string {
|
||||
commitConfig := self.c.UserConfig().Git.Commit
|
||||
if !commitConfig.AutoWrapCommitMessage {
|
||||
return message
|
||||
}
|
||||
return tryRemoveHardLineBreaks(message, commitConfig.AutoWrapWidth)
|
||||
}
|
||||
|
||||
func tryRemoveHardLineBreaks(message string, autoWrapWidth int) string {
|
||||
lastHardLineStart := 0
|
||||
result := message
|
||||
for i, b := range message {
|
||||
|
||||
@@ -34,7 +34,7 @@ func TestTryRemoveHardLineBreaks(t *testing.T) {
|
||||
}
|
||||
for _, s := range scenarios {
|
||||
t.Run(s.name, func(t *testing.T) {
|
||||
actualResult := TryRemoveHardLineBreaks(s.message, s.autoWrapWidth)
|
||||
actualResult := tryRemoveHardLineBreaks(s.message, s.autoWrapWidth)
|
||||
assert.Equal(t, s.expectedResult, actualResult)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -884,9 +884,7 @@ func (self *LocalCommitsController) reword(commit *models.Commit) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if self.c.UserConfig().Git.Commit.AutoWrapCommitMessage {
|
||||
commitMessage = helpers.TryRemoveHardLineBreaks(commitMessage, self.c.UserConfig().Git.Commit.AutoWrapWidth)
|
||||
}
|
||||
commitMessage = self.c.Helpers().Commits.TryRemoveHardLineBreaksIfAutoWrapping(commitMessage)
|
||||
self.c.Helpers().Commits.OpenCommitMessagePanel(
|
||||
&helpers.OpenCommitMessagePanelOpts{
|
||||
CommitIndex: commitIdx,
|
||||
@@ -1579,9 +1577,7 @@ func (self *LocalCommitsController) createAmendCommit(commit *models.Commit, inc
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if self.c.UserConfig().Git.Commit.AutoWrapCommitMessage {
|
||||
commitMessage = helpers.TryRemoveHardLineBreaks(commitMessage, self.c.UserConfig().Git.Commit.AutoWrapWidth)
|
||||
}
|
||||
commitMessage = self.c.Helpers().Commits.TryRemoveHardLineBreaksIfAutoWrapping(commitMessage)
|
||||
originalSubject, _, _ := strings.Cut(commitMessage, "\n")
|
||||
self.c.Helpers().Commits.OpenCommitMessagePanel(
|
||||
&helpers.OpenCommitMessagePanelOpts{
|
||||
|
||||
Reference in New Issue
Block a user