feat(vim.hl): allow multiple timed highlights simultaneously #33283

Problem: Currently vim.hl.range only allows one timed highlight.
Creating another one, removes the old one.

Solution: vim.hl.range now returns a timer and a function. The timer
keeps track of how much time is left in the highlight and the function
allows you to clear it, letting the user decide what to do with old
highlights.
This commit is contained in:
Siddhant Agarwal
2025-04-03 19:56:56 +05:30
committed by GitHub
parent 974a3aa2c4
commit eae2d3b145
4 changed files with 113 additions and 26 deletions

View File

@@ -139,6 +139,80 @@ describe('vim.hl.range', function()
|
]])
end)
it('shows multiple highlights with different timeouts simultaneously', function()
local timeout1 = 300
local timeout2 = 600
exec_lua(function()
local ns = vim.api.nvim_create_namespace('')
vim.hl.range(0, ns, 'Search', { 0, 0 }, { 4, 0 }, { timeout = timeout1 })
vim.hl.range(0, ns, 'Search', { 2, 6 }, { 3, 5 }, { timeout = timeout2 })
end)
screen:expect({
grid = [[
{10:^asdfghjkl}{100:$} |
{10:«口=口»}{100:$} |
{10:qwertyuiop}{100:$} |
{10:口口=口口}{1:$} |
zxcvbnm{1:$} |
|
]],
timeout = timeout1 / 3,
})
screen:expect({
grid = [[
^asdfghjkl{1:$} |
«口=口»{1:$} |
qwerty{10:uiop}{100:$} |
{10:口口}=口口{1:$} |
zxcvbnm{1:$} |
|
]],
timeout = timeout1 + ((timeout2 - timeout1) / 3),
})
screen:expect([[
^asdfghjkl{1:$} |
«口=口»{1:$} |
qwertyuiop{1:$} |
口口=口口{1:$} |
zxcvbnm{1:$} |
|
]])
end)
it('allows cancelling a highlight that has not timed out', function()
exec_lua(function()
local timeout = 3000
local range_timer
local range_hl_clear
local ns = vim.api.nvim_create_namespace('')
range_timer, range_hl_clear = vim.hl.range(
0,
ns,
'Search',
{ 0, 0 },
{ 4, 0 },
{ timeout = timeout }
)
if range_timer and not range_timer:is_closing() then
range_timer:close()
assert(range_hl_clear)
range_hl_clear()
range_hl_clear() -- Exercise redundant call
end
end)
screen:expect({
grid = [[
^asdfghjkl{1:$} |
«口=口»{1:$} |
qwertyuiop{1:$} |
口口=口口{1:$} |
zxcvbnm{1:$} |
|
]],
unchanged = true,
})
end)
end)
describe('vim.hl.on_yank', function()