Skip to content

Commit 573a783

Browse files
authored
feat(neovim): add forward query history cycling (cycle_forward_query) (#435)
Add a new keymap 'cycle_forward_query' (default <C-Down>) that allows navigating forward through the query history toward more recent queries. This complements the existing 'cycle_previous_query' (<C-Up>) which cycles backward, enabling full bidirectional navigation of query history without losing your place. - New config key: keymaps.cycle_forward_query (default: '<C-Down>') - New function M.cycle_forward_query() that decrements history_offset to walk forward through recent entries - Shares the history_offset state with recall_query_from_history for seamless bidirectional traversal
1 parent bcd3c76 commit 573a783

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

lua/fff/conf.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ local M = {}
3333
--- @field toggle_debug string
3434
--- @field cycle_grep_modes string
3535
--- @field cycle_previous_query string
36+
--- @field cycle_forward_query string
3637
--- @field toggle_select string
3738
--- @field send_to_quickfix string
3839
--- @field focus_list string
@@ -247,6 +248,8 @@ local function init()
247248
cycle_grep_modes = '<S-Tab>',
248249
-- goes to the previous query in history
249250
cycle_previous_query = '<C-Up>',
251+
-- goes to the next query in history (forward)
252+
cycle_forward_query = '<C-Down>',
250253
-- multi-select keymaps for quickfix
251254
toggle_select = '<Tab>',
252255
send_to_quickfix = '<C-q>',

lua/fff/picker_ui.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ function M.setup_keymaps()
933933
set_keymap('i', keymaps.move_up, M.move_up, input_opts)
934934
set_keymap('i', keymaps.move_down, M.move_down, input_opts)
935935
set_keymap('i', keymaps.cycle_previous_query, M.recall_query_from_history, input_opts)
936+
set_keymap('i', keymaps.cycle_forward_query, M.cycle_forward_query, input_opts)
936937
set_keymap('n', 'j', M.move_down, input_opts)
937938
set_keymap('n', 'k', M.move_up, input_opts)
938939
set_keymap('n', keymaps.focus_list, M.focus_list_win, input_opts)
@@ -2228,6 +2229,47 @@ function M.recall_query_from_history()
22282229
end)
22292230
end
22302231

2232+
--- Cycle forward through query history (toward more recent queries).
2233+
--- Complements recall_query_from_history which cycles backward.
2234+
--- Allows bidirectional navigation without losing your place.
2235+
function M.cycle_forward_query()
2236+
if not M.state.active then return end
2237+
2238+
-- Initialize offset on first press (start from most recent, same as backward)
2239+
if M.state.history_offset == nil then
2240+
M.state.history_offset = 0
2241+
elseif M.state.history_offset > 0 then
2242+
-- Decrement offset to move forward toward more recent queries
2243+
M.state.history_offset = M.state.history_offset - 1
2244+
else
2245+
-- At the most recent entry (offset 0), can't go further forward
2246+
return
2247+
end
2248+
2249+
-- Fetch query at current offset from Rust (grep and file picker have separate histories)
2250+
local fuzzy = require('fff.core').ensure_initialized()
2251+
local history_fn = M.state.mode == 'grep' and fuzzy.get_historical_grep_query or fuzzy.get_historical_query
2252+
local ok, query = pcall(history_fn, M.state.history_offset)
2253+
2254+
if not ok or not query then
2255+
-- Shouldn't happen since we validated the offset, but handle gracefully
2256+
M.state.history_offset = nil
2257+
return
2258+
end
2259+
2260+
if M.state.mode ~= 'grep' then M.state.next_search_force_combo_boost = true end
2261+
2262+
-- this is going to trigger the on_input_change handler with the normal search and render flow
2263+
vim.api.nvim_buf_set_lines(M.state.input_buf, 0, -1, false, { M.state.config.prompt .. query })
2264+
2265+
-- Position cursor at end
2266+
vim.schedule(function()
2267+
if M.state.active and M.state.input_win and vim.api.nvim_win_is_valid(M.state.input_win) then
2268+
vim.api.nvim_win_set_cursor(M.state.input_win, { 1, #M.state.config.prompt + #query })
2269+
end
2270+
end)
2271+
end
2272+
22312273
--- Check whether the given window has 'winfixbuf' enabled.
22322274
--- pcall-guarded so this stays safe on Neovim versions that predate the option.
22332275
--- @param win number Window ID

0 commit comments

Comments
 (0)