Keep the current search match when a view without a selection refreshes

If a view doesn't show a selection, for example a branch's commit log
in the focused main view, n and N step through the matches without
moving a cursor. A refresh then resets the search to the first match,
so the "x of y" status goes back to "1 of y", and the next n starts
over from the top.

Only reset the current match when a new search starts, and keep it when
the search runs again over refreshed content.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Stefan Haller
2026-11-07 19:54:00 +01:00
co-authored by Claude Opus 5.5
parent 5f5d6a1dd6
commit 4aed79b954
2 changed files with 6 additions and 5 deletions
+6 -2
View File
@@ -560,6 +560,7 @@ func (v *View) GetSearchStatus() (int, int) {
// UpdateSearchResults runs the search for str again, for content or model search results
// that have changed. It doesn't move the view. The match at the cursor becomes the
// current one, in the same way as when the cursor moves; see SetNearestSearchPosition.
// If the view doesn't show a selection, the current match stays the same.
//
// modelSearchResults is optional; pass nil to search the view. If non-nil,
// these positions will be used for highlighting search results. Even in this
@@ -606,9 +607,11 @@ func (v *View) startSearch(str string, modelSearchResults []SearchPosition) bool
if len(v.searcher.searchPositions) == 0 {
return false
}
// If the view doesn't show a selection, the search starts at the first match.
if v.Highlight {
v.searcher.currentSearchIndex = v.firstSearchPositionAfterCursor()
} else {
// If the view doesn't show a selection, the search starts at the first match.
v.searcher.currentSearchIndex = 0
}
return true
}
@@ -726,11 +729,12 @@ func calculateNewOrigin(selectedLine int, oldOrigin int, lineCount int, viewHeig
return oldOrigin
}
// search sets what to search for. It leaves the current match for the caller to choose
// once the positions have been worked out.
func (s *searcher) search(str string, modelSearchResults []SearchPosition) {
s.searchString = str
s.searchPositions = []SearchPosition{}
s.modelSearchResults = modelSearchResults
s.currentSearchIndex = 0
}
func (s *searcher) clearSearch() {
@@ -28,10 +28,7 @@ var SearchOverACommitLogKeepsTheCurrentMatchOnRefresh = NewIntegrationTest(NewIn
t.GlobalPress(keys.Universal.Refresh)
t.Views().Search().Content(
/* EXPECTED:
Contains("matches for 'commit-0' (2 of 3)"))
ACTUAL: */
Contains("matches for 'commit-0' (1 of 3)"))
})
},
})