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)
This commit is contained in:
Michael Lingelbach
2022-01-15 14:19:20 -08:00
committed by GitHub
parent f3193c7b54
commit b455e0179b
3 changed files with 32 additions and 16 deletions

View File

@@ -25,16 +25,29 @@ end
---@param higroup highlight group to use for highlighting ---@param higroup highlight group to use for highlighting
---@param rtype type of range (:help setreg, default charwise) ---@param rtype type of range (:help setreg, default charwise)
---@param inclusive boolean indicating whether the range is end-inclusive (default false) ---@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' rtype = rtype or 'v'
inclusive = inclusive or false inclusive = inclusive or false
priority = priority or 50
-- sanity check -- 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, rtype, inclusive)
for linenr, cols in pairs(region) do 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
end end
@@ -82,7 +95,7 @@ function highlight.on_yank(opts)
pos1 = {pos1[2] - 1, pos1[3] - 1 + pos1[4]} pos1 = {pos1[2] - 1, pos1[3] - 1 + pos1[4]}
pos2 = {pos2[2] - 1, pos2[3] - 1 + pos2[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( vim.defer_fn(
function() function()

View File

@@ -1550,7 +1550,10 @@ do --[[ References ]]
reference_ns, reference_ns,
document_highlight_kind[kind], document_highlight_kind[kind],
{ start_line, start_idx }, { start_line, start_idx },
{ end_line, end_idx }) { end_line, end_idx },
nil,
false,
40)
end end
end end
end end

View File

@@ -208,10 +208,10 @@ describe('vim.diagnostic', function()
eq(all_highlights, exec_lua [[ eq(all_highlights, exec_lua [[
local ns_1_diags = { local ns_1_diags = {
make_error("Error 1", 1, 1, 1, 5), make_error("Error 1", 1, 1, 1, 5),
make_warning("Warning on Server 1", 2, 1, 2, 5), make_warning("Warning on Server 1", 2, 1, 2, 3),
} }
local ns_2_diags = { local ns_2_diags = {
make_warning("Warning 1", 2, 1, 2, 5), make_warning("Warning 1", 2, 1, 2, 3),
} }
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags)
@@ -255,10 +255,10 @@ describe('vim.diagnostic', function()
eq({0, 2}, exec_lua [[ eq({0, 2}, exec_lua [[
local ns_1_diags = { local ns_1_diags = {
make_error("Error 1", 1, 1, 1, 5), make_error("Error 1", 1, 1, 1, 5),
make_warning("Warning on Server 1", 2, 1, 2, 5), make_warning("Warning on Server 1", 2, 1, 2, 3),
} }
local ns_2_diags = { local ns_2_diags = {
make_warning("Warning 1", 2, 1, 2, 5), make_warning("Warning 1", 2, 1, 2, 3),
} }
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags)
@@ -599,10 +599,10 @@ describe('vim.diagnostic', function()
eq(all_highlights, exec_lua [[ eq(all_highlights, exec_lua [[
local ns_1_diags = { local ns_1_diags = {
make_error("Error 1", 1, 1, 1, 5), make_error("Error 1", 1, 1, 1, 5),
make_warning("Warning on Server 1", 2, 1, 2, 5), make_warning("Warning on Server 1", 2, 1, 2, 3),
} }
local ns_2_diags = { local ns_2_diags = {
make_warning("Warning 1", 2, 1, 2, 5), make_warning("Warning 1", 2, 1, 2, 3),
} }
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags)
@@ -787,7 +787,7 @@ describe('vim.diagnostic', function()
eq(2, exec_lua [[ eq(2, exec_lua [[
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_error("Error 1", 1, 1, 1, 5), make_error("Error 1", 1, 1, 1, 5),
make_warning("Warning on Server 1", 1, 1, 2, 5), make_warning("Warning on Server 1", 1, 1, 2, 3),
}) })
return #vim.diagnostic.get(diagnostic_bufnr) return #vim.diagnostic.get(diagnostic_bufnr)
@@ -798,9 +798,9 @@ describe('vim.diagnostic', function()
eq({2, 3, 2}, exec_lua [[ eq({2, 3, 2}, exec_lua [[
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_error("Error 1", 1, 1, 1, 5), make_error("Error 1", 1, 1, 1, 5),
make_warning("Warning on Server 1", 1, 1, 2, 5), make_warning("Warning on Server 1", 1, 1, 2, 3),
make_info("Ignored information", 1, 1, 2, 5), make_info("Ignored information", 1, 1, 2, 3),
make_hint("Here's a hint", 1, 1, 2, 5), make_hint("Here's a hint", 1, 1, 2, 3),
}) })
return { return {
@@ -820,8 +820,8 @@ describe('vim.diagnostic', function()
eq(1, exec_lua [[ eq(1, exec_lua [[
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_error("Error 1", 1, 1, 1, 5), make_error("Error 1", 1, 1, 1, 5),
make_warning("Warning on Server 1", 1, 1, 2, 5), make_warning("Warning on Server 1", 1, 1, 2, 3),
make_info("Ignored information", 1, 1, 2, 5), make_info("Ignored information", 1, 1, 2, 3),
make_error("Error On Other Line", 2, 1, 1, 5), make_error("Error On Other Line", 2, 1, 1, 5),
}) })