From b455e0179b4288c69e6231bfcf8d1c132b78f2fc Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Sat, 15 Jan 2022 14:19:20 -0800 Subject: feat: use nvim_buf_set_extmark for vim.highlight (#16963) Closes https://github.com/neovim/neovim/issues/13647 This allows customizing the priority of the highlights. * Add default priority of 50 * Use priority of 200 for highlight on yank * use priority of 40 for highlight references (LSP) --- runtime/lua/vim/highlight.lua | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'runtime/lua/vim/highlight.lua') diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua index 236f3165f2..12faa0a6e1 100644 --- a/runtime/lua/vim/highlight.lua +++ b/runtime/lua/vim/highlight.lua @@ -25,16 +25,29 @@ end ---@param higroup highlight group to use for highlighting ---@param rtype type of range (:help setreg, default charwise) ---@param inclusive boolean indicating whether the range is end-inclusive (default false) -function highlight.range(bufnr, ns, higroup, start, finish, rtype, inclusive) +---@param priority number indicating priority of highlight (default 50) +function highlight.range(bufnr, ns, higroup, start, finish, rtype, inclusive, priority) rtype = rtype or 'v' inclusive = inclusive or false + priority = priority or 50 -- sanity check if start[2] < 0 or finish[1] < start[1] then return end local region = vim.region(bufnr, start, finish, rtype, inclusive) for linenr, cols in pairs(region) do - api.nvim_buf_add_highlight(bufnr, ns, higroup, linenr, cols[1], cols[2]) + local end_row + if cols[2] == -1 then + end_row = linenr + 1 + cols[2] = 0 + end + api.nvim_buf_set_extmark(bufnr, ns, linenr, cols[1], { + hl_group = higroup, + end_row = end_row, + end_col = cols[2], + priority = priority, + strict = false + }) end end @@ -82,7 +95,7 @@ function highlight.on_yank(opts) pos1 = {pos1[2] - 1, pos1[3] - 1 + pos1[4]} pos2 = {pos2[2] - 1, pos2[3] - 1 + pos2[4]} - highlight.range(bufnr, yank_ns, higroup, pos1, pos2, event.regtype, event.inclusive) + highlight.range(bufnr, yank_ns, higroup, pos1, pos2, event.regtype, event.inclusive, 200) vim.defer_fn( function() -- cgit From 10a46a20ce78efa80c1d1f7ac43b2a95ef92ea76 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Mon, 21 Feb 2022 21:21:42 +0100 Subject: refactor(highlight)!: optional arguments for highlight.range as table (#17462) also update documentation BREAKING CHANGE: signature of highlight.range is now vim.highlight.range(bufnr, ns, hlgroup, start, finish, { regtype = regtype, inclusive = inclusive, priority = priority }) Co-authored-by: Gregory Anders <8965202+gpanders@users.noreply.github.com> --- runtime/lua/vim/highlight.lua | 105 +++++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 38 deletions(-) (limited to 'runtime/lua/vim/highlight.lua') diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua index 12faa0a6e1..4105ef0675 100644 --- a/runtime/lua/vim/highlight.lua +++ b/runtime/lua/vim/highlight.lua @@ -1,9 +1,16 @@ local api = vim.api -local highlight = {} +local M = {} + +M.priorities = { + syntax = 50, + treesitter = 100, + diagnostics = 150, + user = 200, +} ---@private -function highlight.create(higroup, hi_info, default) +function M.create(higroup, hi_info, default) local options = {} -- TODO: Add validation for k, v in pairs(hi_info) do @@ -13,28 +20,33 @@ function highlight.create(higroup, hi_info, default) end ---@private -function highlight.link(higroup, link_to, force) +function M.link(higroup, link_to, force) vim.cmd(string.format([[highlight%s link %s %s]], force and "!" or " default", higroup, link_to)) end - --- Highlight range between two positions --- ---@param bufnr number of buffer to apply highlighting to ---@param ns namespace to add highlight to ---@param higroup highlight group to use for highlighting ----@param rtype type of range (:help setreg, default charwise) ----@param inclusive boolean indicating whether the range is end-inclusive (default false) ----@param priority number indicating priority of highlight (default 50) -function highlight.range(bufnr, ns, higroup, start, finish, rtype, inclusive, priority) - rtype = rtype or 'v' - inclusive = inclusive or false - priority = priority or 50 +---@param start first position (tuple {line,col}) +---@param finish second position (tuple {line,col}) +---@param opts table with options: +-- - regtype type of range (:help setreg, default charwise) +-- - inclusive boolean indicating whether the range is end-inclusive (default false) +-- - priority number indicating priority of highlight (default priorities.user) +function M.range(bufnr, ns, higroup, start, finish, opts) + opts = opts or {} + local regtype = opts.regtype or "v" + local inclusive = opts.inclusive or false + local priority = opts.priority or M.priorities.user -- sanity check - if start[2] < 0 or finish[1] < start[1] then return end + if start[2] < 0 or finish[1] < start[1] then + return + end - local region = vim.region(bufnr, start, finish, rtype, inclusive) + local region = vim.region(bufnr, start, finish, regtype, inclusive) for linenr, cols in pairs(region) do local end_row if cols[2] == -1 then @@ -46,13 +58,12 @@ function highlight.range(bufnr, ns, higroup, start, finish, rtype, inclusive, pr end_row = end_row, end_col = cols[2], priority = priority, - strict = false + strict = false, }) end - end -local yank_ns = api.nvim_create_namespace('hlyank') +local yank_ns = api.nvim_create_namespace("hlyank") --- Highlight the yanked region --- --- use from init.vim via @@ -62,26 +73,40 @@ local yank_ns = api.nvim_create_namespace('hlyank') --- customize conditions (here: do not highlight a visual selection) via --- au TextYankPost * lua vim.highlight.on_yank {on_visual=false} --- --- @param opts dictionary with options controlling the highlight: +-- @param opts table with options controlling the highlight: -- - higroup highlight group for yanked region (default "IncSearch") -- - timeout time in ms before highlight is cleared (default 150) -- - on_macro highlight when executing macro (default false) -- - on_visual highlight when yanking visual selection (default true) -- - event event structure (default vim.v.event) -function highlight.on_yank(opts) - vim.validate { - opts = { opts, - function(t) if t == nil then return true else return type(t) == 'table' end end, - 'a table or nil to configure options (see `:h highlight.on_yank`)', - }} +function M.on_yank(opts) + vim.validate({ + opts = { + opts, + function(t) + if t == nil then + return true + else + return type(t) == "table" + end + end, + "a table or nil to configure options (see `:h highlight.on_yank`)", + }, + }) opts = opts or {} local event = opts.event or vim.v.event local on_macro = opts.on_macro or false local on_visual = (opts.on_visual ~= false) - if (not on_macro) and vim.fn.reg_executing() ~= '' then return end - if event.operator ~= 'y' or event.regtype == '' then return end - if (not on_visual) and event.visual then return end + if not on_macro and vim.fn.reg_executing() ~= "" then + return + end + if event.operator ~= "y" or event.regtype == "" then + return + end + if not on_visual and event.visual then + return + end local higroup = opts.higroup or "IncSearch" local timeout = opts.timeout or 150 @@ -92,19 +117,23 @@ function highlight.on_yank(opts) local pos1 = vim.fn.getpos("'[") local pos2 = vim.fn.getpos("']") - pos1 = {pos1[2] - 1, pos1[3] - 1 + pos1[4]} - pos2 = {pos2[2] - 1, pos2[3] - 1 + pos2[4]} - - highlight.range(bufnr, yank_ns, higroup, pos1, pos2, event.regtype, event.inclusive, 200) + pos1 = { pos1[2] - 1, pos1[3] - 1 + pos1[4] } + pos2 = { pos2[2] - 1, pos2[3] - 1 + pos2[4] } - vim.defer_fn( - function() - if api.nvim_buf_is_valid(bufnr) then - api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1) - end - end, - timeout + M.range( + bufnr, + yank_ns, + higroup, + pos1, + pos2, + { regtype = event.regtype, inclusive = event.inclusive, priority = M.priorities.user } ) + + vim.defer_fn(function() + if api.nvim_buf_is_valid(bufnr) then + api.nvim_buf_clear_namespace(bufnr, yank_ns, 0, -1) + end + end, timeout) end -return highlight +return M -- cgit From aefdc6783cb77f09786542c90901a9e7120bea42 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Mon, 9 May 2022 11:23:51 +0200 Subject: chore: format runtime with stylua --- runtime/lua/vim/highlight.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'runtime/lua/vim/highlight.lua') diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua index 4105ef0675..22b67aee88 100644 --- a/runtime/lua/vim/highlight.lua +++ b/runtime/lua/vim/highlight.lua @@ -14,14 +14,14 @@ function M.create(higroup, hi_info, default) local options = {} -- TODO: Add validation for k, v in pairs(hi_info) do - table.insert(options, string.format("%s=%s", k, v)) + table.insert(options, string.format('%s=%s', k, v)) end - vim.cmd(string.format([[highlight %s %s %s]], default and "default" or "", higroup, table.concat(options, " "))) + vim.cmd(string.format([[highlight %s %s %s]], default and 'default' or '', higroup, table.concat(options, ' '))) end ---@private function M.link(higroup, link_to, force) - vim.cmd(string.format([[highlight%s link %s %s]], force and "!" or " default", higroup, link_to)) + vim.cmd(string.format([[highlight%s link %s %s]], force and '!' or ' default', higroup, link_to)) end --- Highlight range between two positions @@ -37,7 +37,7 @@ end -- - priority number indicating priority of highlight (default priorities.user) function M.range(bufnr, ns, higroup, start, finish, opts) opts = opts or {} - local regtype = opts.regtype or "v" + local regtype = opts.regtype or 'v' local inclusive = opts.inclusive or false local priority = opts.priority or M.priorities.user @@ -63,7 +63,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('hlyank') --- Highlight the yanked region --- --- use from init.vim via @@ -87,10 +87,10 @@ function M.on_yank(opts) if t == nil then return true else - return type(t) == "table" + return type(t) == 'table' end end, - "a table or nil to configure options (see `:h highlight.on_yank`)", + 'a table or nil to configure options (see `:h highlight.on_yank`)', }, }) opts = opts or {} @@ -98,17 +98,17 @@ function M.on_yank(opts) local on_macro = opts.on_macro or false local on_visual = (opts.on_visual ~= false) - if not on_macro and vim.fn.reg_executing() ~= "" then + if not on_macro and vim.fn.reg_executing() ~= '' then return end - if event.operator ~= "y" or event.regtype == "" then + if event.operator ~= 'y' or event.regtype == '' then return end if not on_visual and event.visual then return end - local higroup = opts.higroup or "IncSearch" + local higroup = opts.higroup or 'IncSearch' local timeout = opts.timeout or 150 local bufnr = api.nvim_get_current_buf() -- cgit From d5651a4183da8dfc71508850e429a7f0b99979f7 Mon Sep 17 00:00:00 2001 From: Wsevolod Date: Thu, 2 Jun 2022 10:49:57 +0300 Subject: fix(lua): stop pending highlight.on_yank timer, if any (#18824) 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) --- runtime/lua/vim/highlight.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/highlight.lua') diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua index 22b67aee88..36e3c2ad20 100644 --- a/runtime/lua/vim/highlight.lua +++ b/runtime/lua/vim/highlight.lua @@ -64,6 +64,7 @@ function M.range(bufnr, ns, higroup, start, finish, opts) end local yank_ns = api.nvim_create_namespace('hlyank') +local yank_timer --- Highlight the yanked region --- --- use from init.vim via @@ -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("']") @@ -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 -- cgit From aa4f9c5341f5280f16cce0630ea54b84eef717b3 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Thu, 7 Jul 2022 18:27:18 +0200 Subject: refactor(lua): reformat with stylua 0.14.0 (#19264) * reformat Lua runtime to make lint CI pass * reduce max line length to 100 --- runtime/lua/vim/highlight.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/highlight.lua') diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua index 36e3c2ad20..e72d45f11c 100644 --- a/runtime/lua/vim/highlight.lua +++ b/runtime/lua/vim/highlight.lua @@ -16,7 +16,14 @@ function M.create(higroup, hi_info, default) for k, v in pairs(hi_info) do table.insert(options, string.format('%s=%s', k, v)) end - vim.cmd(string.format([[highlight %s %s %s]], default and 'default' or '', higroup, table.concat(options, ' '))) + vim.cmd( + string.format( + [[highlight %s %s %s]], + default and 'default' or '', + higroup, + table.concat(options, ' ') + ) + ) end ---@private -- cgit From 6b1a8f23d7da60aa2fe53cd66760176d2f589690 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sun, 10 Jul 2022 00:40:32 +0800 Subject: refactor(lua): replace vim.cmd use with API calls (#19283) Signed-off-by: Raphael --- runtime/lua/vim/highlight.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'runtime/lua/vim/highlight.lua') diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua index e72d45f11c..ddd504a0e0 100644 --- a/runtime/lua/vim/highlight.lua +++ b/runtime/lua/vim/highlight.lua @@ -11,6 +11,7 @@ M.priorities = { ---@private function M.create(higroup, hi_info, default) + vim.deprecate('vim.highlight.create', 'vim.api.nvim_set_hl', '0.9') local options = {} -- TODO: Add validation for k, v in pairs(hi_info) do @@ -28,6 +29,7 @@ end ---@private function M.link(higroup, link_to, force) + vim.deprecate('vim.highlight.link', 'vim.api.nvim_set_hl', '0.9') vim.cmd(string.format([[highlight%s link %s %s]], force and '!' or ' default', higroup, link_to)) end -- cgit