diff options
author | notomo <notomo.motono@gmail.com> | 2021-08-26 23:37:36 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-26 16:37:36 +0200 |
commit | 274a3504a790a799b28ee89c75e29fb4dbdff41f (patch) | |
tree | caae17c39de08393d111a97a1c17f534032d17da | |
parent | 6ff1e3fa1f6c88e8a8ce4e2e5a017bea8b5e381e (diff) | |
download | rneovim-274a3504a790a799b28ee89c75e29fb4dbdff41f.tar.gz rneovim-274a3504a790a799b28ee89c75e29fb4dbdff41f.tar.bz2 rneovim-274a3504a790a799b28ee89c75e29fb4dbdff41f.zip |
fix(lua): verify buffer in highlight.on_yank (#15482)
Resolve an issue with deferred clearing of highlight failing if the
buffer is deleted before the timeout by checking whether the
buffer is valid first.
-rw-r--r-- | runtime/lua/vim/highlight.lua | 6 | ||||
-rw-r--r-- | test/functional/lua/highlight_spec.lua | 26 |
2 files changed, 31 insertions, 1 deletions
diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua index 4cb1994419..236f3165f2 100644 --- a/runtime/lua/vim/highlight.lua +++ b/runtime/lua/vim/highlight.lua @@ -85,7 +85,11 @@ function highlight.on_yank(opts) highlight.range(bufnr, yank_ns, higroup, pos1, pos2, event.regtype, event.inclusive) vim.defer_fn( - function() api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1) end, + function() + if api.nvim_buf_is_valid(bufnr) then + api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1) + end + end, timeout ) end diff --git a/test/functional/lua/highlight_spec.lua b/test/functional/lua/highlight_spec.lua new file mode 100644 index 0000000000..853b2991e8 --- /dev/null +++ b/test/functional/lua/highlight_spec.lua @@ -0,0 +1,26 @@ +local helpers = require('test.functional.helpers')(after_each) +local funcs = helpers.funcs +local exec_lua = helpers.exec_lua +local command = helpers.command +local clear = helpers.clear + +describe('vim.highlight.on_yank', function() + + before_each(function() + clear() + end) + + it('does not show errors even if buffer is wiped before timeout', function() + command('new') + local bufnr = funcs.bufnr("%") + exec_lua[[ + vim.highlight.on_yank({timeout = 10, on_macro = true, event = {operator = "y", regtype = "v"}}) + vim.cmd('bwipeout!') + ]] + exec_lua[[vim.wait(10)]] + local pattern = [[vim/highlight.lua:%d+: Invalid buffer id: ]] .. bufnr + local exists = pcall(helpers.assert_log, pattern) + assert.is_false(exists, string.format("%q should not be in log", pattern)) + end) + +end) |