From 28d5bdbc08af3f15b6d9ddec208bd997a408193d Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 1 Sep 2026 09:30:39 +0200 Subject: [PATCH] Move the diff-copy helper out of the explorer controller The next commit deletes the controller this lives in, while the helper itself is still wanted: it is what makes a copied selection paste straight into code. Move it first, so that the deletion is a deletion. Co-authored-by: Claude Opus 5 (1M context) --- pkg/gui/controllers/diff_copy.go | 43 +++++++++++++++++++ .../controllers/patch_explorer_controller.go | 39 ----------------- 2 files changed, 43 insertions(+), 39 deletions(-) create mode 100644 pkg/gui/controllers/diff_copy.go diff --git a/pkg/gui/controllers/diff_copy.go b/pkg/gui/controllers/diff_copy.go new file mode 100644 index 000000000..f2e75b097 --- /dev/null +++ b/pkg/gui/controllers/diff_copy.go @@ -0,0 +1,43 @@ +package controllers + +import ( + "strings" + + "github.com/samber/lo" +) + +// 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" }), "") +} diff --git a/pkg/gui/controllers/patch_explorer_controller.go b/pkg/gui/controllers/patch_explorer_controller.go index 70dfb8f4e..7a897dd1f 100644 --- a/pkg/gui/controllers/patch_explorer_controller.go +++ b/pkg/gui/controllers/patch_explorer_controller.go @@ -1,12 +1,9 @@ package controllers import ( - "strings" - "github.com/jesseduffield/lazygit/pkg/gocui" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" "github.com/jesseduffield/lazygit/pkg/gui/types" - "github.com/samber/lo" ) type PatchExplorerControllerFactory struct { @@ -351,42 +348,6 @@ func (self *PatchExplorerController) CopySelectedToClipboard() error { 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() }