Keep the current search match when it is on the selected line

If the selected line has several search matches and n has gone to the
second one, a refresh makes the first match on the line the current one
again. The "x of y" status changes although the user did nothing, and
the next n goes to the second match on the same line once more. Focusing
the view again does the same.

If the current match is on the selected line, keep it.

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 029f939252
commit 8fb62f85b6
2 changed files with 7 additions and 6 deletions
+7
View File
@@ -638,8 +638,15 @@ func (v *View) IsSearching() bool {
return v.searcher.searchString != ""
}
// nearestSearchPosition returns the current match if it is on the cursor's line.
// Otherwise it returns the first match on that line, or else the last match above it,
// or the first match if there is none above.
func (v *View) nearestSearchPosition() int {
currentLineIndex := v.cy + v.oy
if v.searcher.searchPositions[v.searcher.currentSearchIndex].Y == currentLineIndex {
return v.searcher.currentSearchIndex
}
lastSearchPos := 0
for i, pos := range v.searcher.searchPositions {
if pos.Y == currentLineIndex {
@@ -37,17 +37,11 @@ var SearchKeepsTheSecondMatchOnALineOnRefresh = NewIntegrationTest(NewIntegratio
t.GlobalPress(keys.Universal.Refresh)
t.Views().Search().Content(
/* EXPECTED:
Contains("matches for 'needle' (2 of 3)"))
ACTUAL: */
Contains("matches for 'needle' (1 of 3)"))
}).
Press(keys.Universal.NextMatch).
SelectedLines(
/* EXPECTED:
Contains("commit 02 needle"),
ACTUAL: */
Contains("commit 04 needle and needle"),
)
},
})