From a3ae1c8e4de057147547de2d5d7405b79067f212 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 11 Apr 2025 10:11:42 +0200 Subject: [PATCH 1/2] Refactor: extract function clampLineIdx And reimplement using lo.Clamp instead of comparing manually. --- pkg/gui/patch_exploring/state.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pkg/gui/patch_exploring/state.go b/pkg/gui/patch_exploring/state.go index 074793f8e..4bbe881a1 100644 --- a/pkg/gui/patch_exploring/state.go +++ b/pkg/gui/patch_exploring/state.go @@ -7,6 +7,7 @@ import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) // State represents the current state of the patch explorer context i.e. when @@ -177,15 +178,13 @@ func (s *State) SelectLine(newSelectedLineIdx int) { s.selectLineWithoutRangeCheck(newSelectedLineIdx) } +func (s *State) clampLineIdx(lineIdx int) int { + return lo.Clamp(lineIdx, 0, len(s.patchLineIndices)-1) +} + // This just moves the cursor without caring about range select func (s *State) selectLineWithoutRangeCheck(newSelectedLineIdx int) { - if newSelectedLineIdx < 0 { - newSelectedLineIdx = 0 - } else if newSelectedLineIdx > len(s.patchLineIndices)-1 { - newSelectedLineIdx = len(s.patchLineIndices) - 1 - } - - s.selectedLineIdx = newSelectedLineIdx + s.selectedLineIdx = s.clampLineIdx(newSelectedLineIdx) } func (s *State) SelectNewLineForRange(newSelectedLineIdx int) { From 825e5c23c1cfd385cb53300380d603cde0e70425 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 11 Apr 2025 10:14:04 +0200 Subject: [PATCH 2/2] Fix crash when dragging from below the end of the diff upwards and then staging the range We need to clamp the range start index of a selection range in the same way as we clamp the selection index. --- pkg/gui/patch_exploring/state.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/gui/patch_exploring/state.go b/pkg/gui/patch_exploring/state.go index 4bbe881a1..73ce0a756 100644 --- a/pkg/gui/patch_exploring/state.go +++ b/pkg/gui/patch_exploring/state.go @@ -188,7 +188,7 @@ func (s *State) selectLineWithoutRangeCheck(newSelectedLineIdx int) { } func (s *State) SelectNewLineForRange(newSelectedLineIdx int) { - s.rangeStartLineIdx = newSelectedLineIdx + s.rangeStartLineIdx = s.clampLineIdx(newSelectedLineIdx) s.selectMode = RANGE