Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optional timeout on pattern mode #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/hop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lua/hop/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 30 additions & 6 deletions lua/hop/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')

Expand All @@ -162,29 +166,49 @@ 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 <C-c>
pat = nil
break
end

redraw = false

if key == K_Esc then
pat = nil
break
elseif key == K_CR or key == K_NL then
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
Expand Down
Loading