From 850084b519e18122820478a71bb4bfa4c15e528a Mon Sep 17 00:00:00 2001 From: Maria José Solano Date: Mon, 13 Jan 2025 19:39:03 -0800 Subject: refactor: use nvim.foo.bar format for namespaces --- runtime/lua/vim/hl.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/hl.lua') diff --git a/runtime/lua/vim/hl.lua b/runtime/lua/vim/hl.lua index 099efa3c61..f5ace7fdc5 100644 --- a/runtime/lua/vim/hl.lua +++ b/runtime/lua/vim/hl.lua @@ -115,7 +115,7 @@ function M.range(bufnr, ns, higroup, start, finish, opts) end end -local yank_ns = api.nvim_create_namespace('hlyank') +local yank_ns = api.nvim_create_namespace('nvim.hlyank') local yank_timer --- @type uv.uv_timer_t? local yank_cancel --- @type fun()? -- cgit From af0ef2ac9dd19b7c4005a3197334d3e7d554646c Mon Sep 17 00:00:00 2001 From: Siddhant Agarwal <68201519+siddhantdev@users.noreply.github.com> Date: Wed, 22 Jan 2025 21:16:24 +0530 Subject: feat(lua): vim.hl.range() "timeout" #32012 Problem: `vim.hl.on_yank()` has a "timeout" behavior but this is not available for `vim.hl.range()`. Solution: Add `timeout` arg to `vim.hl.range()`. --- runtime/lua/vim/hl.lua | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'runtime/lua/vim/hl.lua') diff --git a/runtime/lua/vim/hl.lua b/runtime/lua/vim/hl.lua index f5ace7fdc5..070748d31e 100644 --- a/runtime/lua/vim/hl.lua +++ b/runtime/lua/vim/hl.lua @@ -17,6 +17,9 @@ M.priorities = { user = 200, } +local range_timer --- @type uv.uv_timer_t? +local range_hl_clear --- @type fun()? + --- @class vim.hl.range.Opts --- @inlinedoc --- @@ -31,6 +34,10 @@ M.priorities = { --- Highlight priority --- (default: `vim.hl.priorities.user`) --- @field priority? integer +--- +--- Time in ms before highlight is cleared +--- (default: -1 no timeout) +--- @field timeout? integer --- Apply highlight group to range of text. --- @@ -45,6 +52,7 @@ function M.range(bufnr, ns, higroup, start, finish, opts) local regtype = opts.regtype or 'v' local inclusive = opts.inclusive or false local priority = opts.priority or M.priorities.user + local timeout = opts.timeout or -1 local v_maxcol = vim.v.maxcol @@ -100,6 +108,19 @@ function M.range(bufnr, ns, higroup, start, finish, opts) end end + if range_timer and not range_timer:is_closing() then + range_timer:close() + assert(range_hl_clear) + range_hl_clear() + end + + range_hl_clear = function() + range_timer = nil + range_hl_clear = nil + pcall(vim.api.nvim_buf_clear_namespace, bufnr, ns, 0, -1) + pcall(vim.api.nvim__ns_set, { wins = {} }) + end + for _, res in ipairs(region) do local start_row = res[1][2] - 1 local start_col = res[1][3] - 1 @@ -113,11 +134,13 @@ function M.range(bufnr, ns, higroup, start, finish, opts) strict = false, }) end + + if timeout ~= -1 then + range_timer = vim.defer_fn(range_hl_clear, timeout) + end end local yank_ns = api.nvim_create_namespace('nvim.hlyank') -local yank_timer --- @type uv.uv_timer_t? -local yank_cancel --- @type fun()? --- Highlight the yanked text during a |TextYankPost| event. --- @@ -152,31 +175,17 @@ function M.on_yank(opts) end local higroup = opts.higroup or 'IncSearch' - local timeout = opts.timeout or 150 local bufnr = vim.api.nvim_get_current_buf() local winid = vim.api.nvim_get_current_win() - if yank_timer then - yank_timer:close() - assert(yank_cancel) - yank_cancel() - end vim.api.nvim__ns_set(yank_ns, { wins = { winid } }) M.range(bufnr, yank_ns, higroup, "'[", "']", { regtype = event.regtype, inclusive = event.inclusive, priority = opts.priority or M.priorities.user, + timeout = opts.timeout or 150, }) - - yank_cancel = function() - yank_timer = nil - yank_cancel = nil - pcall(vim.api.nvim_buf_clear_namespace, bufnr, yank_ns, 0, -1) - pcall(vim.api.nvim__ns_set, { wins = {} }) - end - - yank_timer = vim.defer_fn(yank_cancel, timeout) end return M -- cgit