feat: Improve search to start with first match in or below current view

This commit is contained in:
n loewen (aider) 2025-06-08 09:58:48 +01:00
parent f2158e0321
commit bfba10be02
1 changed files with 18 additions and 4 deletions

22
gtm
View File

@ -461,11 +461,25 @@ def perform_search(state: AppState) -> AppState:
new_state = replace(state, search_matches=matches)
# If we have matches, select the first one
# If we have matches, select the first one that's in view or below current scroll
if matches:
new_state = replace(new_state, current_match_idx=0)
# Scroll to the first match
new_state = replace(new_state, right_scroll_offset=max(0, matches[0] - 3))
current_scroll = state.right_scroll_offset
visible_height = state.height - state.status_bar_height
# Find the first match that's in the current view or below it
first_visible_match_idx = 0
for idx, match_line in enumerate(matches):
if match_line >= current_scroll:
first_visible_match_idx = idx
break
# If all matches are above current view, wrap around to the first match
new_state = replace(new_state, current_match_idx=first_visible_match_idx)
# Scroll to the match if it's not already visible
match_line = matches[first_visible_match_idx]
if match_line < current_scroll or match_line >= current_scroll + visible_height:
new_state = replace(new_state, right_scroll_offset=max(0, match_line - 3))
else:
new_state = replace(new_state, current_match_idx=-1)