Skip to content

Commit 326e7e8

Browse files
authored
Strip the '+' and '-' characters when copying parts of a diff to the clipboard (#4519)
- **PR Description** This makes it easier to copy diff hunks and paste them into code. We only strip the prefixes if the copied lines are either all '+' or all '-' (possibly including context lines), otherwise we keep them. We also keep them when parts of a hunk header is included in the selection; this is useful for copying a diff hunk and pasting it into a github comment, for example. A not-quite-correct edge case is when you select the '--- a/file.txt' line of a diff header on its own; in this case we copy it as '-- a/file.txt' (same for the '+++' line). This is probably uncommon enough that it's not worth fixing (it's not trivial to fix because we don't know that we're in a header). Fixes #3859. Fixes #4511.
2 parents ac9b830 + 159bbb0 commit 326e7e8

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

pkg/gui/controllers/patch_explorer_controller.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package controllers
22

33
import (
4+
"strings"
5+
46
"github.com/jesseduffield/gocui"
57
"github.com/jesseduffield/lazygit/pkg/gui/types"
8+
"github.com/samber/lo"
69
)
710

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

297300
self.c.LogAction(self.c.Tr.Actions.CopySelectedTextToClipboard)
298-
if err := self.c.OS().CopyToClipboard(selected); err != nil {
301+
if err := self.c.OS().CopyToClipboard(dropDiffPrefix(selected)); err != nil {
299302
return err
300303
}
301304

302305
return nil
303306
}
304307

308+
// Removes '+' or '-' from the beginning of each line in the diff string, except
309+
// when both '+' and '-' lines are present, or diff header lines, in which case
310+
// the diff is returned unchanged. This is useful for copying parts of diffs to
311+
// the clipboard in order to paste them into code.
312+
func dropDiffPrefix(diff string) string {
313+
lines := strings.Split(strings.TrimRight(diff, "\n"), "\n")
314+
315+
const (
316+
PLUS int = iota
317+
MINUS
318+
CONTEXT
319+
OTHER
320+
)
321+
322+
linesByType := lo.GroupBy(lines, func(line string) int {
323+
switch {
324+
case strings.HasPrefix(line, "+"):
325+
return PLUS
326+
case strings.HasPrefix(line, "-"):
327+
return MINUS
328+
case strings.HasPrefix(line, " "):
329+
return CONTEXT
330+
}
331+
return OTHER
332+
})
333+
334+
hasLinesOfType := func(lineType int) bool { return len(linesByType[lineType]) > 0 }
335+
336+
keepPrefix := hasLinesOfType(OTHER) || (hasLinesOfType(PLUS) && hasLinesOfType(MINUS))
337+
if keepPrefix {
338+
return diff
339+
}
340+
341+
return strings.Join(lo.Map(lines, func(line string, _ int) string { return line[1:] + "\n" }), "")
342+
}
343+
305344
func (self *PatchExplorerController) isFocused() bool {
306345
return self.c.Context().Current().GetKey() == self.context.GetKey()
307346
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package controllers
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_dropDiffPrefix(t *testing.T) {
10+
scenarios := []struct {
11+
name string
12+
diff string
13+
expectedResult string
14+
}{
15+
{
16+
name: "empty string",
17+
diff: "",
18+
expectedResult: "",
19+
},
20+
{
21+
name: "only added lines",
22+
diff: `+line1
23+
+line2
24+
`,
25+
expectedResult: `line1
26+
line2
27+
`,
28+
},
29+
{
30+
name: "added lines with context",
31+
diff: ` line1
32+
+line2
33+
`,
34+
expectedResult: `line1
35+
line2
36+
`,
37+
},
38+
{
39+
name: "only deleted lines",
40+
diff: `-line1
41+
-line2
42+
`,
43+
expectedResult: `line1
44+
line2
45+
`,
46+
},
47+
{
48+
name: "deleted lines with context",
49+
diff: `-line1
50+
line2
51+
`,
52+
expectedResult: `line1
53+
line2
54+
`,
55+
},
56+
{
57+
name: "only context",
58+
diff: ` line1
59+
line2
60+
`,
61+
expectedResult: `line1
62+
line2
63+
`,
64+
},
65+
{
66+
name: "added and deleted lines",
67+
diff: `+line1
68+
-line2
69+
`,
70+
expectedResult: `+line1
71+
-line2
72+
`,
73+
},
74+
{
75+
name: "hunk header lines",
76+
diff: `@@ -1,8 +1,11 @@
77+
line1
78+
`,
79+
expectedResult: `@@ -1,8 +1,11 @@
80+
line1
81+
`,
82+
},
83+
}
84+
for _, s := range scenarios {
85+
t.Run(s.name, func(t *testing.T) {
86+
assert.Equal(t, s.expectedResult, dropDiffPrefix(s.diff))
87+
})
88+
}
89+
}

0 commit comments

Comments
 (0)