From ff039bb024ca0a8753c6591fa8cb98312090f739 Mon Sep 17 00:00:00 2001 From: Volodymyr Chernetskyi <19735328+chernetskyi@users.noreply.github.com> Date: Tue, 15 Sep 2026 14:53:40 +0200 Subject: perf(hightlight): compute only bounds for non-blockwise highlights Problem: vim.hl.range() computes a segment for every selected line, then discards all but the outer endpoints for characterwise and linewise highlights. This makes highlighting large yanks and puts unnecessarily expensive. Solution: Use getregionpos() with bounds=true for these highlights. Also use bounds in the keymap example that only needs the first and last selected lines. AI-assisted --- runtime/lua/vim/hl.lua | 8 ++++---- runtime/lua/vim/keymap.lua | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/hl.lua b/runtime/lua/vim/hl.lua index 552285c7da..c02986b28e 100644 --- a/runtime/lua/vim/hl.lua +++ b/runtime/lua/vim/hl.lua @@ -94,15 +94,15 @@ function M.range(buf, ns, hlgroup, start, finish, opts) end end) + -- For non-blockwise selection, use a single extmark and only compute its bounds. + local bounds = regtype == 'v' or regtype == 'V' local region = vim.fn.getregionpos(pos1, pos2, { type = regtype, exclusive = not inclusive, eol = true, + bounds = bounds, }) - -- For non-blockwise selection, use a single extmark. - if regtype == 'v' or regtype == 'V' then - --- @type [ [integer, integer, integer, integer], [integer, integer, integer, integer]][] - region = { { assert(region[1])[1], assert(region[#region])[2] } } + if bounds then local region1 = assert(region[1]) if regtype == 'V' diff --git a/runtime/lua/vim/keymap.lua b/runtime/lua/vim/keymap.lua index 7451f42ad4..061d3340fe 100644 --- a/runtime/lua/vim/keymap.lua +++ b/runtime/lua/vim/keymap.lua @@ -39,9 +39,10 @@ local keymap = {} --- type = 'v', --- exclusive = false, --- eol = false, +--- bounds = true, --- }) --- local line1 = region[1][1][2] ---- local line2 = region[#region][1][2] +--- local line2 = region[1][2][2] --- vim.print({ line1, line2 }) --- end) --- -- cgit