1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-07 01:09:45 +02:00

Escape out of hunk mode only if it was turned on by the user

If hunk mode is on by default because of the config, then it's annoying for
escape to go to line mode.
This commit is contained in:
Stefan Haller
2025-07-04 14:35:48 +02:00
parent 2961c991a4
commit 0a73123a66
3 changed files with 27 additions and 10 deletions

View File

@ -170,7 +170,7 @@ func (self *PatchBuildingController) Escape() error {
context := self.c.Contexts().CustomPatchBuilder context := self.c.Contexts().CustomPatchBuilder
state := context.GetState() state := context.GetState()
if state.SelectingRange() || state.SelectingHunk() { if state.SelectingRange() || state.SelectingHunkEnabledByUser() {
state.SetLineSelectMode() state.SetLineSelectMode()
self.c.PostRefreshUpdate(context) self.c.PostRefreshUpdate(context)
return nil return nil

View File

@ -168,7 +168,7 @@ func (self *StagingController) EditFile() error {
} }
func (self *StagingController) Escape() error { func (self *StagingController) Escape() error {
if self.context.GetState().SelectingRange() || self.context.GetState().SelectingHunk() { if self.context.GetState().SelectingRange() || self.context.GetState().SelectingHunkEnabledByUser() {
self.context.GetState().SetLineSelectMode() self.context.GetState().SetLineSelectMode()
self.c.PostRefreshUpdate(self.context) self.c.PostRefreshUpdate(self.context)
return nil return nil

View File

@ -28,6 +28,12 @@ type State struct {
viewLineIndices []int viewLineIndices []int
// Array of indices of the original patch lines indexed by a wrapped view line index // Array of indices of the original patch lines indexed by a wrapped view line index
patchLineIndices []int patchLineIndices []int
// whether the user has switched to hunk mode manually; if hunk mode is on
// but this is false, then hunk mode was enabled because the config makes it
// on by default.
// this makes a difference for whether we want to escape out of hunk mode
userEnabledHunkMode bool
} }
// these represent what select mode we're in // these represent what select mode we're in
@ -65,6 +71,11 @@ func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *Stat
selectMode = HUNK selectMode = HUNK
} }
userEnabledHunkMode := false
if oldState != nil {
userEnabledHunkMode = oldState.userEnabledHunkMode
}
// if we have clicked from the outside to focus the main view we'll pass in a non-negative line index so that we can instantly select that line // if we have clicked from the outside to focus the main view we'll pass in a non-negative line index so that we can instantly select that line
if selectedLineIdx >= 0 { if selectedLineIdx >= 0 {
// Clamp to the number of wrapped view lines; index might be out of // Clamp to the number of wrapped view lines; index might be out of
@ -84,14 +95,15 @@ func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *Stat
} }
return &State{ return &State{
patch: patch, patch: patch,
selectedLineIdx: selectedLineIdx, selectedLineIdx: selectedLineIdx,
selectMode: selectMode, selectMode: selectMode,
rangeStartLineIdx: rangeStartLineIdx, rangeStartLineIdx: rangeStartLineIdx,
rangeIsSticky: false, rangeIsSticky: false,
diff: diff, diff: diff,
viewLineIndices: viewLineIndices, viewLineIndices: viewLineIndices,
patchLineIndices: patchLineIndices, patchLineIndices: patchLineIndices,
userEnabledHunkMode: userEnabledHunkMode,
} }
} }
@ -129,6 +141,7 @@ func (s *State) ToggleSelectHunk() {
s.selectMode = LINE s.selectMode = LINE
} else { } else {
s.selectMode = HUNK s.selectMode = HUNK
s.userEnabledHunkMode = true
// If we are not currently on a change line, select the next one (or the // If we are not currently on a change line, select the next one (or the
// previous one if there is no next one): // previous one if there is no next one):
@ -159,6 +172,10 @@ func (s *State) SelectingHunk() bool {
return s.selectMode == HUNK return s.selectMode == HUNK
} }
func (s *State) SelectingHunkEnabledByUser() bool {
return s.selectMode == HUNK && s.userEnabledHunkMode
}
func (s *State) SelectingRange() bool { func (s *State) SelectingRange() bool {
return s.selectMode == RANGE && (s.rangeIsSticky || s.rangeStartLineIdx != s.selectedLineIdx) return s.selectMode == RANGE && (s.rangeIsSticky || s.rangeStartLineIdx != s.selectedLineIdx)
} }