From 8f164f7bc536ca711e531d49f96348fbae54b860 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 12 Mar 2023 18:31:45 +0100 Subject: [PATCH] Stop cycling hunks when reaching the end Previously, when pressing right-arrow when the cursor is already in the last hunk, it would jump back to the beginning of that hunk. This can be confusing if the hunk is long, maybe the start of the hunk is already scrolled off the top of the window, and then pressing right-arrow actually scrolls *backwards*, which is counter-intuitive. It's better to do nothing in this case. Same for left-arrow when the cursor is already in the first hunk, although here the problem is not so severe (unless diff context was increased by a huge amount, and the start of the first hunk is scrolled off the bottom of the window). --- pkg/commands/patch/patch.go | 5 +++++ pkg/gui/patch_exploring/state.go | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/commands/patch/patch.go b/pkg/commands/patch/patch.go index 55dd8f80d..049334727 100644 --- a/pkg/commands/patch/patch.go +++ b/pkg/commands/patch/patch.go @@ -149,3 +149,8 @@ func (self *Patch) LineCount() int { } return count } + +// Returns the number of hunks of the patch +func (self *Patch) HunkCount() int { + return len(self.hunks) +} diff --git a/pkg/gui/patch_exploring/state.go b/pkg/gui/patch_exploring/state.go index 356bff4f4..1c82d59cb 100644 --- a/pkg/gui/patch_exploring/state.go +++ b/pkg/gui/patch_exploring/state.go @@ -143,8 +143,13 @@ func (s *State) CycleHunk(forward bool) { } hunkIdx := s.patch.HunkContainingLine(s.selectedLineIdx) - start := s.patch.HunkStartIdx(hunkIdx + change) - s.selectedLineIdx = s.patch.GetNextChangeIdx(start) + if hunkIdx != -1 { + newHunkIdx := hunkIdx + change + if newHunkIdx >= 0 && newHunkIdx < s.patch.HunkCount() { + start := s.patch.HunkStartIdx(newHunkIdx) + s.selectedLineIdx = s.patch.GetNextChangeIdx(start) + } + } } func (s *State) CycleLine(forward bool) {