Skip to content

Commit 0a73123

Browse files
committed
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.
1 parent 2961c99 commit 0a73123

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

pkg/gui/controllers/patch_building_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (self *PatchBuildingController) Escape() error {
170170
context := self.c.Contexts().CustomPatchBuilder
171171
state := context.GetState()
172172

173-
if state.SelectingRange() || state.SelectingHunk() {
173+
if state.SelectingRange() || state.SelectingHunkEnabledByUser() {
174174
state.SetLineSelectMode()
175175
self.c.PostRefreshUpdate(context)
176176
return nil

pkg/gui/controllers/staging_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (self *StagingController) EditFile() error {
168168
}
169169

170170
func (self *StagingController) Escape() error {
171-
if self.context.GetState().SelectingRange() || self.context.GetState().SelectingHunk() {
171+
if self.context.GetState().SelectingRange() || self.context.GetState().SelectingHunkEnabledByUser() {
172172
self.context.GetState().SetLineSelectMode()
173173
self.c.PostRefreshUpdate(self.context)
174174
return nil

pkg/gui/patch_exploring/state.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ type State struct {
2828
viewLineIndices []int
2929
// Array of indices of the original patch lines indexed by a wrapped view line index
3030
patchLineIndices []int
31+
32+
// whether the user has switched to hunk mode manually; if hunk mode is on
33+
// but this is false, then hunk mode was enabled because the config makes it
34+
// on by default.
35+
// this makes a difference for whether we want to escape out of hunk mode
36+
userEnabledHunkMode bool
3137
}
3238

3339
// these represent what select mode we're in
@@ -65,6 +71,11 @@ func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *Stat
6571
selectMode = HUNK
6672
}
6773

74+
userEnabledHunkMode := false
75+
if oldState != nil {
76+
userEnabledHunkMode = oldState.userEnabledHunkMode
77+
}
78+
6879
// 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
6980
if selectedLineIdx >= 0 {
7081
// 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
8495
}
8596

8697
return &State{
87-
patch: patch,
88-
selectedLineIdx: selectedLineIdx,
89-
selectMode: selectMode,
90-
rangeStartLineIdx: rangeStartLineIdx,
91-
rangeIsSticky: false,
92-
diff: diff,
93-
viewLineIndices: viewLineIndices,
94-
patchLineIndices: patchLineIndices,
98+
patch: patch,
99+
selectedLineIdx: selectedLineIdx,
100+
selectMode: selectMode,
101+
rangeStartLineIdx: rangeStartLineIdx,
102+
rangeIsSticky: false,
103+
diff: diff,
104+
viewLineIndices: viewLineIndices,
105+
patchLineIndices: patchLineIndices,
106+
userEnabledHunkMode: userEnabledHunkMode,
95107
}
96108
}
97109

@@ -129,6 +141,7 @@ func (s *State) ToggleSelectHunk() {
129141
s.selectMode = LINE
130142
} else {
131143
s.selectMode = HUNK
144+
s.userEnabledHunkMode = true
132145

133146
// If we are not currently on a change line, select the next one (or the
134147
// previous one if there is no next one):
@@ -159,6 +172,10 @@ func (s *State) SelectingHunk() bool {
159172
return s.selectMode == HUNK
160173
}
161174

175+
func (s *State) SelectingHunkEnabledByUser() bool {
176+
return s.selectMode == HUNK && s.userEnabledHunkMode
177+
}
178+
162179
func (s *State) SelectingRange() bool {
163180
return s.selectMode == RANGE && (s.rangeIsSticky || s.rangeStartLineIdx != s.selectedLineIdx)
164181
}

0 commit comments

Comments
 (0)