diff --git a/lua/fff/picker_ui/picker_ui.lua b/lua/fff/picker_ui/picker_ui.lua index d9c3d625..49ceeba0 100644 --- a/lua/fff/picker_ui/picker_ui.lua +++ b/lua/fff/picker_ui/picker_ui.lua @@ -169,8 +169,11 @@ local function restore_from_state(state, source_label) M.state.suggestion_items = state.suggestion_items M.state.suggestion_source = state.suggestion_source - -- Set the query text in the input buffer + -- Writing the query below triggers on_lines -> on_input_change, which re-runs + -- the search (results may have changed since close). Stash the saved cursor so + -- that re-search restores the position instead of resetting it to the top. if state.query and state.query ~= '' then + M.state.pending_restore_cursor = M.state.cursor vim.api.nvim_buf_set_lines(M.state.input_buf, 0, -1, false, { M.state.config.prompt .. state.query }) end @@ -186,7 +189,7 @@ local function restore_from_state(state, source_label) if M.state.active and M.state.input_win and vim.api.nvim_win_is_valid(M.state.input_win) then local prompt_len = #M.state.config.prompt vim.api.nvim_win_set_cursor(M.state.input_win, { 1, prompt_len + #state.query }) - vim.cmd('startinsert!') + vim.cmd('stopinsert') end end) diff --git a/lua/fff/picker_ui/picker_ui_state.lua b/lua/fff/picker_ui/picker_ui_state.lua index 5c4e0f5f..dec4a47f 100644 --- a/lua/fff/picker_ui/picker_ui_state.lua +++ b/lua/fff/picker_ui/picker_ui_state.lua @@ -30,6 +30,10 @@ M.state = { last_render_ctx = nil, location = nil, + -- Cursor index to restore after the next search completes (set on resume). + -- Lets the re-search run for fresh results while keeping the saved position. + pending_restore_cursor = nil, + -- History cycling state history_offset = nil, next_search_force_combo_boost = false, @@ -117,6 +121,7 @@ function M.reset_state() M.state.item_to_lines = {} M.state.last_render_ctx = nil M.state.location = nil + M.state.pending_restore_cursor = nil M.reset_history_state() diff --git a/lua/fff/picker_ui/search_manager.lua b/lua/fff/picker_ui/search_manager.lua index b6907c93..544bfdb3 100644 --- a/lua/fff/picker_ui/search_manager.lua +++ b/lua/fff/picker_ui/search_manager.lua @@ -101,7 +101,14 @@ function M.update_results_sync() if S.suggestion_items and #S.suggestion_items > 0 then S.filtered_items = S.suggestion_items end - S.cursor = 1 + -- On resume, restore the saved cursor onto the fresh results (clamped, since + -- the result set may have changed since close). Otherwise reset to the top. + if S.pending_restore_cursor then + S.cursor = math.max(1, math.min(S.pending_restore_cursor, #S.filtered_items)) + S.pending_restore_cursor = nil + else + S.cursor = 1 + end P.render_debounced() end