Skip to content

Commit 7d92260

Browse files
authored
Fix commit searching during rebase or in divergence from upstream view (#4730)
- **PR Description** Fix search results being off by two lines during a rebase or in the divergence from upstream view.
2 parents 228d442 + 2b41a27 commit 7d92260

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

pkg/gui/context/list_renderer.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,19 @@ func (self *ListRenderer) renderLines(startIdx int, endIdx int) string {
9999

100100
func (self *ListRenderer) prepareConversionArrays(nonModelItems []*NonModelItem) {
101101
self.numNonModelItems = len(nonModelItems)
102-
self.viewIndicesByModelIndex = lo.Range(self.list.Len() + 1)
103-
self.modelIndicesByViewIndex = lo.Range(self.list.Len() + 1)
102+
viewIndicesByModelIndex := lo.Range(self.list.Len() + 1)
103+
modelIndicesByViewIndex := lo.Range(self.list.Len() + 1)
104104
offset := 0
105105
for _, item := range nonModelItems {
106106
for i := item.Index; i <= self.list.Len(); i++ {
107-
self.viewIndicesByModelIndex[i]++
107+
viewIndicesByModelIndex[i]++
108108
}
109-
self.modelIndicesByViewIndex = slices.Insert(
110-
self.modelIndicesByViewIndex, item.Index+offset, self.modelIndicesByViewIndex[item.Index+offset])
109+
modelIndicesByViewIndex = slices.Insert(
110+
modelIndicesByViewIndex, item.Index+offset, modelIndicesByViewIndex[item.Index+offset])
111111
offset++
112112
}
113+
self.viewIndicesByModelIndex = viewIndicesByModelIndex
114+
self.modelIndicesByViewIndex = modelIndicesByViewIndex
113115
}
114116

115117
func (self *ListRenderer) insertNonModelItems(

pkg/gui/context/local_commits_context.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (self *LocalCommitsContext) RefForAdjustingLineNumberInDiff() string {
223223
}
224224

225225
func (self *LocalCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
226-
return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr)
226+
return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), self.ModelIndexToViewIndex, searchStr)
227227
}
228228

229229
func (self *LocalCommitsViewModel) SetLimitCommits(value bool) {
@@ -266,7 +266,9 @@ func shouldShowGraph(c *ContextCommon) bool {
266266
return false
267267
}
268268

269-
func searchModelCommits(caseSensitive bool, commits []*models.Commit, columnPositions []int, searchStr string) []gocui.SearchPosition {
269+
func searchModelCommits(caseSensitive bool, commits []*models.Commit, columnPositions []int,
270+
modelToViewIndex func(int) int, searchStr string,
271+
) []gocui.SearchPosition {
270272
if columnPositions == nil {
271273
// This should never happen. We are being called at a time where our
272274
// entire view content is scrolled out of view, so that we didn't draw
@@ -283,7 +285,7 @@ func searchModelCommits(caseSensitive bool, commits []*models.Commit, columnPosi
283285
// searching for a commit hash that is longer than the truncated hash
284286
// that we render. So we just set the XStart and XEnd values to the
285287
// start and end of the commit hash column, which is the second one.
286-
result := gocui.SearchPosition{XStart: columnPositions[1], XEnd: columnPositions[2] - 1, Y: idx}
288+
result := gocui.SearchPosition{XStart: columnPositions[1], XEnd: columnPositions[2] - 1, Y: modelToViewIndex(idx)}
287289
return result, strings.Contains(normalize(commit.Hash()), searchStr) ||
288290
strings.Contains(normalize(commit.Name), searchStr) ||
289291
strings.Contains(normalize(commit.ExtraInfo), searchStr) // allow searching for tags

pkg/gui/context/sub_commits_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (self *SubCommitsContext) RefForAdjustingLineNumberInDiff() string {
225225
}
226226

227227
func (self *SubCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
228-
return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr)
228+
return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), self.ModelIndexToViewIndex, searchStr)
229229
}
230230

231231
func (self *SubCommitsContext) IndexForGotoBottom() int {

0 commit comments

Comments
 (0)