From 9a88af1b888b2955f64d9c9e9a84835991c8cc4f Mon Sep 17 00:00:00 2001 From: ArijanJ Date: Thu, 11 Apr 2024 00:57:39 +0200 Subject: [PATCH] feat: optional timeout on pattern mode --- doc/hop.txt | 7 +++++++ lua/hop/defaults.lua | 1 + lua/hop/init.lua | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/doc/hop.txt b/doc/hop.txt index 988bac9..309dbc2 100644 --- a/doc/hop.txt +++ b/doc/hop.txt @@ -851,6 +851,13 @@ below. Defaults:~ `multi_windows = false` +`timeout` *hop-config-timeout* + Starts a timer after the first valid key in pattern mode which submits the + query after `timeout` milliseconds if no other keys are pressed. + + Defaults:~ + `timeout = nil` + `excluded_filetypes` Skip hinting windows with the excluded filetypes. Those windows to check filetypes are collected only when you enable `multi_windows` or execute diff --git a/lua/hop/defaults.lua b/lua/hop/defaults.lua index e0bfc95..c5e2df2 100644 --- a/lua/hop/defaults.lua +++ b/lua/hop/defaults.lua @@ -26,5 +26,6 @@ M.hint_type = hint.HintType.OVERLAY ---@type HintType M.excluded_filetypes = {} M.match_mappings = {} M.extensions = { 'hop-yank', 'hop-treesitter' } +M.timeout = nil return M diff --git a/lua/hop/init.lua b/lua/hop/init.lua index 39ba142..50d1ce9 100644 --- a/lua/hop/init.lua +++ b/lua/hop/init.lua @@ -149,6 +149,10 @@ function M.get_input_pattern(prompt, maxchar, opts) ---@type string|nil local pat = '' + local timer = nil + local redraw = true + local timeout_reached = false + while true do pat = vim.fn.join(pat_keys, '') @@ -162,16 +166,20 @@ function M.get_input_pattern(prompt, maxchar, opts) end end - api.nvim_echo({}, false, {}) - vim.cmd.redraw() - api.nvim_echo({ { prompt, 'Question' }, { pat } }, false, {}) + if redraw then + api.nvim_echo({}, false, {}) + vim.cmd.redraw() + api.nvim_echo({ { prompt, 'Question' }, { pat } }, false, {}) + end - local ok, key = pcall(vim.fn.getcharstr) + local ok, key = pcall(vim.fn.getcharstr, 0) if not ok then -- Interrupted by pat = nil break end + redraw = false + if key == K_Esc then pat = nil break @@ -179,12 +187,28 @@ function M.get_input_pattern(prompt, maxchar, opts) break elseif key == K_BS or key == K_C_H then pat_keys[#pat_keys] = nil - else + redraw = true + elseif key ~= '' then pat_keys[#pat_keys + 1] = key + redraw = true + end + + if opts and opts.timeout then + if key ~= '' then + timeout_reached = false + if timer then timer:stop() end + + timer = vim.loop.new_timer() + timer:start(opts.timeout, 0, function() + timeout_reached = true + timer:stop() + end) + end end - if maxchar and #pat_keys >= maxchar then + if (maxchar and #pat_keys >= maxchar) or timeout_reached then pat = vim.fn.join(pat_keys, '') + redraw = true break end end