Skip to content
forked from neovim/neovim

Commit

Permalink
fix(lua): stop pending highlight.on_yank timer, if any (neovim#18824)
Browse files Browse the repository at this point in the history
When yanking another range while previous yank is still highlighted, the
pending timer could clear the highlight almost immediately (especially
when using larger `timeout`, i.e. 2000)
  • Loading branch information
kraftwerk28 authored and clason committed Jun 16, 2022
1 parent 5243cb8 commit f15d609
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions runtime/lua/vim/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ 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('hlyank')
local yank_timer
--- Highlight the yanked region
---
--- use from init.vim via
Expand Down Expand Up @@ -113,6 +114,9 @@ function M.on_yank(opts)

local bufnr = api.nvim_get_current_buf()
api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1)
if yank_timer then
yank_timer:close()
end

local pos1 = vim.fn.getpos("'[")
local pos2 = vim.fn.getpos("']")
Expand All @@ -129,7 +133,8 @@ function M.on_yank(opts)
{ regtype = event.regtype, inclusive = event.inclusive, priority = M.priorities.user }
)

vim.defer_fn(function()
yank_timer = vim.defer_fn(function()
yank_timer = nil
if api.nvim_buf_is_valid(bufnr) then
api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1)
end
Expand Down

0 comments on commit f15d609

Please sign in to comment.