Skip to content

Strip the '+' and '-' characters when copying parts of a diff to the clipboard #4519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion pkg/gui/controllers/patch_explorer_controller.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package controllers

import (
"strings"

"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/samber/lo"
)

type PatchExplorerControllerFactory struct {
Expand Down Expand Up @@ -295,13 +298,49 @@ func (self *PatchExplorerController) CopySelectedToClipboard() error {
selected := self.context.GetState().PlainRenderSelected()

self.c.LogAction(self.c.Tr.Actions.CopySelectedTextToClipboard)
if err := self.c.OS().CopyToClipboard(selected); err != nil {
if err := self.c.OS().CopyToClipboard(dropDiffPrefix(selected)); err != nil {
return err
}

return nil
}

// Removes '+' or '-' from the beginning of each line in the diff string, except
// when both '+' and '-' lines are present, or diff header lines, in which case
// the diff is returned unchanged. This is useful for copying parts of diffs to
// the clipboard in order to paste them into code.
func dropDiffPrefix(diff string) string {
lines := strings.Split(strings.TrimRight(diff, "\n"), "\n")

const (
PLUS int = iota
MINUS
CONTEXT
OTHER
)

linesByType := lo.GroupBy(lines, func(line string) int {
switch {
case strings.HasPrefix(line, "+"):
return PLUS
case strings.HasPrefix(line, "-"):
return MINUS
case strings.HasPrefix(line, " "):
return CONTEXT
}
return OTHER
})

hasLinesOfType := func(lineType int) bool { return len(linesByType[lineType]) > 0 }

keepPrefix := hasLinesOfType(OTHER) || (hasLinesOfType(PLUS) && hasLinesOfType(MINUS))
if keepPrefix {
return diff
}

return strings.Join(lo.Map(lines, func(line string, _ int) string { return line[1:] + "\n" }), "")
}

func (self *PatchExplorerController) isFocused() bool {
return self.c.Context().Current().GetKey() == self.context.GetKey()
}
Expand Down
89 changes: 89 additions & 0 deletions pkg/gui/controllers/patch_explorer_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package controllers

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_dropDiffPrefix(t *testing.T) {
scenarios := []struct {
name string
diff string
expectedResult string
}{
{
name: "empty string",
diff: "",
expectedResult: "",
},
{
name: "only added lines",
diff: `+line1
+line2
`,
expectedResult: `line1
line2
`,
},
{
name: "added lines with context",
diff: ` line1
+line2
`,
expectedResult: `line1
line2
`,
},
{
name: "only deleted lines",
diff: `-line1
-line2
`,
expectedResult: `line1
line2
`,
},
{
name: "deleted lines with context",
diff: `-line1
line2
`,
expectedResult: `line1
line2
`,
},
{
name: "only context",
diff: ` line1
line2
`,
expectedResult: `line1
line2
`,
},
{
name: "added and deleted lines",
diff: `+line1
-line2
`,
expectedResult: `+line1
-line2
`,
},
{
name: "hunk header lines",
diff: `@@ -1,8 +1,11 @@
line1
`,
expectedResult: `@@ -1,8 +1,11 @@
line1
`,
},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
assert.Equal(t, s.expectedResult, dropDiffPrefix(s.diff))
})
}
}