Skip to content

Commit fc54d78

Browse files
committed
fixup! Move to next stageable line after adding a line to a custom patch
1 parent efeff86 commit fc54d78

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

pkg/commands/patch/patch.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,41 @@ func (self *Patch) HunkContainingLine(idx int) int {
115115
return -1
116116
}
117117

118-
// Returns the patch line index of the next change (i.e. addition or deletion).
119-
func (self *Patch) GetNextChangeIdx(idx int) int {
118+
// Returns the patch line index of the next change (i.e. addition or deletion)
119+
// that matches the same "included" state, given the includedLines. If you don't
120+
// care about included states, pass nil for includedLines and false for included.
121+
func (self *Patch) GetNextChangeIdxOfSameIncludedState(idx int, includedLines []int, included bool) (int, bool) {
120122
idx = lo.Clamp(idx, 0, self.LineCount()-1)
121123

122124
lines := self.Lines()
123125

126+
isMatch := func(i int, line *PatchLine) bool {
127+
sameIncludedState := lo.Contains(includedLines, i) == included
128+
return line.IsChange() && sameIncludedState
129+
}
130+
124131
for i, line := range lines[idx:] {
125-
if line.isChange() {
126-
return i + idx
132+
if isMatch(i+idx, line) {
133+
return i + idx, true
127134
}
128135
}
129136

130137
// there are no changes from the cursor onwards so we'll instead
131138
// return the index of the last change
132139
for i := len(lines) - 1; i >= 0; i-- {
133140
line := lines[i]
134-
if line.isChange() {
135-
return i
141+
if isMatch(i, line) {
142+
return i, true
136143
}
137144
}
138145

139-
// should not be possible
140-
return 0
146+
return 0, false
147+
}
148+
149+
// Returns the patch line index of the next change (i.e. addition or deletion).
150+
func (self *Patch) GetNextChangeIdx(idx int) int {
151+
result, _ := self.GetNextChangeIdxOfSameIncludedState(idx, nil, false)
152+
return result
141153
}
142154

143155
// Returns the length of the patch in lines

pkg/gui/controllers/patch_building_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (self *PatchBuildingController) toggleSelection() error {
161161
state.SetLineSelectMode()
162162
}
163163

164-
state.SelectNextStageableLine()
164+
state.SelectNextStageableLineOfSameIncludedState(self.context().GetIncludedLineIndices(), firstSelectedChangeLineIsStaged)
165165

166166
return nil
167167
}

pkg/gui/patch_exploring/state.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,10 @@ func wrapPatchLines(diff string, view *gocui.View) ([]int, []int) {
339339
return viewLineIndices, patchLineIndices
340340
}
341341

342-
func (s *State) SelectNextStageableLine() {
342+
func (s *State) SelectNextStageableLineOfSameIncludedState(includedLines []int, included bool) {
343343
_, lastLineIdx := s.SelectedPatchRange()
344-
patchLineIdx := s.patch.GetNextChangeIdx(lastLineIdx + 1)
345-
s.SelectLine(s.viewLineIndices[patchLineIdx])
344+
patchLineIdx, found := s.patch.GetNextChangeIdxOfSameIncludedState(lastLineIdx+1, includedLines, included)
345+
if found {
346+
s.SelectLine(s.viewLineIndices[patchLineIdx])
347+
}
346348
}

0 commit comments

Comments
 (0)