@@ -1346,7 +1346,10 @@ local function format_file_display(item, max_width)
13461346 return filename , display_path
13471347end
13481348
1349- --- Adjust scroll for bottom prompt to eliminate gaps
1349+ --- Adjust scroll for bottom prompt to eliminate gaps.
1350+ --- When the cursor has moved above the bottom viewport (e.g. user scrolled up
1351+ --- through many results), follow the cursor instead of forcing the view to the
1352+ --- bottom — otherwise the selected item disappears off the top of the window.
13501353local function scroll_to_bottom ()
13511354 if not M .state .list_win or not vim .api .nvim_win_is_valid (M .state .list_win ) then return end
13521355
@@ -1355,8 +1358,21 @@ local function scroll_to_bottom()
13551358
13561359 vim .api .nvim_win_call (M .state .list_win , function ()
13571360 local view = vim .fn .winsaveview ()
1358- -- Force topline to show content at bottom
1359- view .topline = math.max (1 , buf_lines - win_height + 1 )
1361+ local bottom_topline = math.max (1 , buf_lines - win_height + 1 )
1362+ local cursor_line = vim .api .nvim_win_get_cursor (M .state .list_win )[1 ]
1363+
1364+ if cursor_line >= bottom_topline then
1365+ -- Cursor is visible when anchored to bottom — keep content near prompt
1366+ view .topline = bottom_topline
1367+ elseif cursor_line < view .topline then
1368+ -- Cursor scrolled above the current viewport — shift topline up just
1369+ -- enough to keep the cursor visible (1 line margin above)
1370+ view .topline = math.max (1 , cursor_line - 1 )
1371+ elseif cursor_line >= view .topline + win_height then
1372+ -- Cursor below viewport (shouldn't happen often) — snap to bottom
1373+ view .topline = bottom_topline
1374+ end
1375+ -- Otherwise cursor is already within the current viewport — don't move it
13601376 vim .fn .winrestview (view )
13611377 end )
13621378end
0 commit comments