Skip to content

Commit d70a405

Browse files
Fix crash with drag selecting and staging (#4480)
- **PR Description** Fix a crash in the staging view when clicking below the end of the diff, dragging upwards into the diff to select a range, and pressing space to stage. Fixes #4479. - **Please check if the PR fulfills these requirements** * [x] Cheatsheets are up-to-date (run `go generate ./...`) * [x] Code has been formatted (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting)) * [ ] Tests have been added/updated (see [here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md) for the integration test guide) * [ ] Text is internationalised (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation)) * [ ] If a new UserConfig entry was added, make sure it can be hot-reloaded (see [here](https://github.com/jesseduffield/lazygit/blob/master/docs/dev/Codebase_Guide.md#using-userconfig)) * [ ] Docs have been updated if necessary * [x] You've read through your own file changes for silly mistakes etc
2 parents 61636d8 + 825e5c2 commit d70a405

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

Diff for: pkg/gui/patch_exploring/state.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/jesseduffield/gocui"
88
"github.com/jesseduffield/lazygit/pkg/commands/patch"
99
"github.com/jesseduffield/lazygit/pkg/utils"
10+
"github.com/samber/lo"
1011
)
1112

1213
// State represents the current state of the patch explorer context i.e. when
@@ -177,19 +178,17 @@ func (s *State) SelectLine(newSelectedLineIdx int) {
177178
s.selectLineWithoutRangeCheck(newSelectedLineIdx)
178179
}
179180

181+
func (s *State) clampLineIdx(lineIdx int) int {
182+
return lo.Clamp(lineIdx, 0, len(s.patchLineIndices)-1)
183+
}
184+
180185
// This just moves the cursor without caring about range select
181186
func (s *State) selectLineWithoutRangeCheck(newSelectedLineIdx int) {
182-
if newSelectedLineIdx < 0 {
183-
newSelectedLineIdx = 0
184-
} else if newSelectedLineIdx > len(s.patchLineIndices)-1 {
185-
newSelectedLineIdx = len(s.patchLineIndices) - 1
186-
}
187-
188-
s.selectedLineIdx = newSelectedLineIdx
187+
s.selectedLineIdx = s.clampLineIdx(newSelectedLineIdx)
189188
}
190189

191190
func (s *State) SelectNewLineForRange(newSelectedLineIdx int) {
192-
s.rangeStartLineIdx = newSelectedLineIdx
191+
s.rangeStartLineIdx = s.clampLineIdx(newSelectedLineIdx)
193192

194193
s.selectMode = RANGE
195194

0 commit comments

Comments
 (0)