Skip to content

Commit 466509b

Browse files
committed
In hunk staging mode, select blocks of changes rather than actual hunks
Also, pressing right or left arrow moves between blocks of changes rather than actual hunks. I find this to be much more useful.
1 parent 08ad8a0 commit 466509b

File tree

5 files changed

+87
-133
lines changed

5 files changed

+87
-133
lines changed

pkg/gui/controllers/patch_explorer_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ func (self *PatchExplorerController) HandleNextLineRange() error {
223223
}
224224

225225
func (self *PatchExplorerController) HandlePrevHunk() error {
226-
self.context.GetState().CycleHunk(false)
226+
self.context.GetState().SelectPreviousHunk()
227227

228228
return nil
229229
}
230230

231231
func (self *PatchExplorerController) HandleNextHunk() error {
232-
self.context.GetState().CycleHunk(true)
232+
self.context.GetState().SelectNextHunk()
233233

234234
return nil
235235
}

pkg/gui/patch_exploring/state.go

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ func (s *State) ToggleSelectHunk() {
125125
s.selectMode = LINE
126126
} else {
127127
s.selectMode = HUNK
128+
129+
// If we are not currently on a change line, select the next one (or the
130+
// previous one if there is no next one):
131+
s.selectedLineIdx = s.viewLineIndices[s.patch.GetNextChangeIdx(
132+
s.patchLineIndices[s.selectedLineIdx])]
128133
}
129134
}
130135

@@ -203,25 +208,48 @@ func (s *State) DragSelectLine(newSelectedLineIdx int) {
203208

204209
func (s *State) CycleSelection(forward bool) {
205210
if s.SelectingHunk() {
206-
s.CycleHunk(forward)
211+
if forward {
212+
s.SelectNextHunk()
213+
} else {
214+
s.SelectPreviousHunk()
215+
}
207216
} else {
208217
s.CycleLine(forward)
209218
}
210219
}
211220

212-
func (s *State) CycleHunk(forward bool) {
213-
change := 1
214-
if !forward {
215-
change = -1
221+
func (s *State) SelectPreviousHunk() {
222+
patchLines := s.patch.Lines()
223+
patchLineIdx := s.patchLineIndices[s.selectedLineIdx]
224+
nextNonChangeLine := patchLineIdx
225+
for nextNonChangeLine >= 0 && patchLines[nextNonChangeLine].IsChange() {
226+
nextNonChangeLine--
216227
}
217-
218-
hunkIdx := s.patch.HunkContainingLine(s.patchLineIndices[s.selectedLineIdx])
219-
if hunkIdx != -1 {
220-
newHunkIdx := hunkIdx + change
221-
if newHunkIdx >= 0 && newHunkIdx < s.patch.HunkCount() {
222-
start := s.patch.HunkStartIdx(newHunkIdx)
223-
s.selectedLineIdx = s.viewLineIndices[s.patch.GetNextChangeIdx(start)]
228+
nextChangeLine := nextNonChangeLine
229+
for nextChangeLine >= 0 && !patchLines[nextChangeLine].IsChange() {
230+
nextChangeLine--
231+
}
232+
if nextChangeLine >= 0 {
233+
for nextChangeLine > 0 && patchLines[nextChangeLine-1].IsChange() {
234+
nextChangeLine--
224235
}
236+
s.selectedLineIdx = s.viewLineIndices[nextChangeLine]
237+
}
238+
}
239+
240+
func (s *State) SelectNextHunk() {
241+
patchLines := s.patch.Lines()
242+
patchLineIdx := s.patchLineIndices[s.selectedLineIdx]
243+
nextNonChangeLine := patchLineIdx
244+
for nextNonChangeLine < len(patchLines) && patchLines[nextNonChangeLine].IsChange() {
245+
nextNonChangeLine++
246+
}
247+
nextChangeLine := nextNonChangeLine
248+
for nextChangeLine < len(patchLines) && !patchLines[nextChangeLine].IsChange() {
249+
nextChangeLine++
250+
}
251+
if nextChangeLine < len(patchLines) {
252+
s.selectedLineIdx = s.viewLineIndices[nextChangeLine]
225253
}
226254
}
227255

@@ -259,11 +287,34 @@ func (s *State) CurrentHunkBounds() (int, int) {
259287
return start, end
260288
}
261289

290+
func (s *State) selectionRangeForCurrentBlockOfChanges() (int, int) {
291+
patchLines := s.patch.Lines()
292+
patchLineIdx := s.patchLineIndices[s.selectedLineIdx]
293+
294+
patchStart := patchLineIdx
295+
for patchStart > 0 && patchLines[patchStart-1].IsChange() {
296+
patchStart--
297+
}
298+
299+
patchEnd := patchLineIdx
300+
for patchEnd < len(patchLines)-1 && patchLines[patchEnd+1].IsChange() {
301+
patchEnd++
302+
}
303+
304+
viewStart, viewEnd := s.viewLineIndices[patchStart], s.viewLineIndices[patchEnd]
305+
306+
// Increase viewEnd in case the last patch line is wrapped
307+
for viewEnd < len(s.patchLineIndices)-1 && s.patchLineIndices[viewEnd] == s.patchLineIndices[viewEnd+1] {
308+
viewEnd++
309+
}
310+
311+
return viewStart, viewEnd
312+
}
313+
262314
func (s *State) SelectedViewRange() (int, int) {
263315
switch s.selectMode {
264316
case HUNK:
265-
start, end := s.CurrentHunkBounds()
266-
return s.viewLineIndices[start], s.viewLineIndices[end]
317+
return s.selectionRangeForCurrentBlockOfChanges()
267318
case RANGE:
268319
if s.rangeStartLineIdx > s.selectedLineIdx {
269320
return s.selectedLineIdx, s.rangeStartLineIdx

pkg/integration/tests/patch_building/specific_selection.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,13 @@ var SpecificSelection = NewIntegrationTest(NewIntegrationTestArgs{
5555
).
5656
Press(keys.Main.ToggleSelectHunk).
5757
SelectedLines(
58-
Contains(`@@ -1,6 +1,6 @@`),
5958
Contains(`-1a`),
6059
Contains(`+aa`),
61-
Contains(` 1b`),
62-
Contains(`-1c`),
63-
Contains(`+cc`),
64-
Contains(` 1d`),
65-
Contains(` 1e`),
66-
Contains(` 1f`),
6760
).
6861
PressPrimaryAction().
6962
SelectedLines(
70-
Contains(`@@ -17,9 +17,9 @@`),
71-
Contains(` 1q`),
72-
Contains(` 1r`),
73-
Contains(` 1s`),
74-
Contains(`-1t`),
75-
Contains(`-1u`),
76-
Contains(`-1v`),
77-
Contains(`+tt`),
78-
Contains(`+uu`),
79-
Contains(`+vv`),
80-
Contains(` 1w`),
81-
Contains(` 1x`),
82-
Contains(` 1y`),
63+
Contains(`-1c`),
64+
Contains(`+cc`),
8365
).
8466
Tap(func() {
8567
t.Views().Information().Content(Contains("Building patch"))
@@ -154,8 +136,7 @@ var SpecificSelection = NewIntegrationTest(NewIntegrationTestArgs{
154136
Contains(`-1a`),
155137
Contains(`+aa`),
156138
Contains(` 1b`),
157-
Contains(`-1c`),
158-
Contains(`+cc`),
139+
Contains(` 1c`),
159140
Contains(` 1d`),
160141
Contains(` 1e`),
161142
Contains(` 1f`),

pkg/integration/tests/staging/diff_context_change.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,67 +52,40 @@ var DiffContextChange = NewIntegrationTest(NewIntegrationTestArgs{
5252
IsFocused().
5353
Press(keys.Main.ToggleSelectHunk).
5454
SelectedLines(
55-
Contains(`@@ -1,6 +1,6 @@`),
56-
Contains(` 1a`),
57-
Contains(` 2a`),
5855
Contains(`-3a`),
5956
Contains(`+3b`),
60-
Contains(` 4a`),
61-
Contains(` 5a`),
62-
Contains(` 6a`),
6357
).
6458
Press(keys.Universal.IncreaseContextInDiffView).
6559
Tap(func() {
6660
t.ExpectToast(Equals("Changed diff context size to 4"))
6761
}).
6862
SelectedLines(
69-
Contains(`@@ -1,7 +1,7 @@`),
70-
Contains(` 1a`),
71-
Contains(` 2a`),
7263
Contains(`-3a`),
7364
Contains(`+3b`),
74-
Contains(` 4a`),
75-
Contains(` 5a`),
76-
Contains(` 6a`),
77-
Contains(` 7a`),
7865
).
7966
Press(keys.Universal.DecreaseContextInDiffView).
8067
Tap(func() {
8168
t.ExpectToast(Equals("Changed diff context size to 3"))
8269
}).
8370
SelectedLines(
84-
Contains(`@@ -1,6 +1,6 @@`),
85-
Contains(` 1a`),
86-
Contains(` 2a`),
8771
Contains(`-3a`),
8872
Contains(`+3b`),
89-
Contains(` 4a`),
90-
Contains(` 5a`),
91-
Contains(` 6a`),
9273
).
9374
Press(keys.Universal.DecreaseContextInDiffView).
9475
Tap(func() {
9576
t.ExpectToast(Equals("Changed diff context size to 2"))
9677
}).
9778
SelectedLines(
98-
Contains(`@@ -1,5 +1,5 @@`),
99-
Contains(` 1a`),
100-
Contains(` 2a`),
10179
Contains(`-3a`),
10280
Contains(`+3b`),
103-
Contains(` 4a`),
104-
Contains(` 5a`),
10581
).
10682
Press(keys.Universal.DecreaseContextInDiffView).
10783
Tap(func() {
10884
t.ExpectToast(Equals("Changed diff context size to 1"))
10985
}).
11086
SelectedLines(
111-
Contains(`@@ -2,3 +2,3 @@`),
112-
Contains(` 2a`),
11387
Contains(`-3a`),
11488
Contains(`+3b`),
115-
Contains(` 4a`),
11689
).
11790
PressPrimaryAction().
11891
Press(keys.Universal.TogglePanel)
@@ -121,18 +94,14 @@ var DiffContextChange = NewIntegrationTest(NewIntegrationTestArgs{
12194
IsFocused().
12295
Press(keys.Main.ToggleSelectHunk).
12396
SelectedLines(
124-
Contains(`@@ -2,3 +2,3 @@`),
125-
Contains(` 2a`),
12697
Contains(`-3a`),
12798
Contains(`+3b`),
128-
Contains(` 4a`),
12999
).
130100
Press(keys.Universal.DecreaseContextInDiffView).
131101
Tap(func() {
132102
t.ExpectToast(Equals("Changed diff context size to 0"))
133103
}).
134104
SelectedLines(
135-
Contains(`@@ -3,1 +3 @@`),
136105
Contains(`-3a`),
137106
Contains(`+3b`),
138107
).
@@ -141,24 +110,16 @@ var DiffContextChange = NewIntegrationTest(NewIntegrationTestArgs{
141110
t.ExpectToast(Equals("Changed diff context size to 1"))
142111
}).
143112
SelectedLines(
144-
Contains(`@@ -2,3 +2,3 @@`),
145-
Contains(` 2a`),
146113
Contains(`-3a`),
147114
Contains(`+3b`),
148-
Contains(` 4a`),
149115
).
150116
Press(keys.Universal.IncreaseContextInDiffView).
151117
Tap(func() {
152118
t.ExpectToast(Equals("Changed diff context size to 2"))
153119
}).
154120
SelectedLines(
155-
Contains(`@@ -1,5 +1,5 @@`),
156-
Contains(` 1a`),
157-
Contains(` 2a`),
158121
Contains(`-3a`),
159122
Contains(`+3b`),
160-
Contains(` 4a`),
161-
Contains(` 5a`),
162123
)
163124
},
164125
})

0 commit comments

Comments
 (0)