2020-05-18 06:49:50 -07:00
|
|
|
local api = vim.api
|
|
|
|
|
2022-02-21 13:21:42 -07:00
|
|
|
local M = {}
|
|
|
|
|
2023-07-18 07:42:30 -07:00
|
|
|
--- Table with default priorities used for highlighting:
|
2024-02-15 10:16:04 -07:00
|
|
|
--- - `syntax`: `50`, used for standard syntax highlighting
|
|
|
|
--- - `treesitter`: `100`, used for treesitter-based highlighting
|
|
|
|
--- - `semantic_tokens`: `125`, used for LSP semantic token highlighting
|
|
|
|
--- - `diagnostics`: `150`, used for code analysis such as diagnostics
|
|
|
|
--- - `user`: `200`, used for user-triggered highlights such as LSP document
|
|
|
|
--- symbols or `on_yank` autocommands
|
2022-02-21 13:21:42 -07:00
|
|
|
M.priorities = {
|
|
|
|
syntax = 50,
|
|
|
|
treesitter = 100,
|
2022-11-23 09:06:36 -07:00
|
|
|
semantic_tokens = 125,
|
2022-02-21 13:21:42 -07:00
|
|
|
diagnostics = 150,
|
|
|
|
user = 200,
|
|
|
|
}
|
2020-05-18 06:49:50 -07:00
|
|
|
|
2024-03-16 10:11:42 -07:00
|
|
|
--- @class vim.highlight.range.Opts
|
|
|
|
--- @inlinedoc
|
|
|
|
---
|
|
|
|
--- Type of range. See [setreg()]
|
|
|
|
--- (default: `'charwise'`)
|
|
|
|
--- @field regtype? string
|
|
|
|
---
|
|
|
|
--- Indicates whether the range is end-inclusive
|
|
|
|
--- (default: `false`)
|
|
|
|
--- @field inclusive? boolean
|
|
|
|
---
|
|
|
|
--- Indicates priority of highlight
|
|
|
|
--- (default: `vim.highlight.priorities.user`)
|
|
|
|
--- @field priority? integer
|
|
|
|
---
|
|
|
|
--- @field package _scoped? boolean
|
|
|
|
|
2023-07-15 08:55:32 -07:00
|
|
|
--- Apply highlight group to range of text.
|
2020-05-31 11:56:00 -07:00
|
|
|
---
|
2023-03-04 09:47:30 -07:00
|
|
|
---@param bufnr integer Buffer number to apply highlighting to
|
|
|
|
---@param ns integer Namespace to add highlight to
|
|
|
|
---@param higroup string Highlight group to use for highlighting
|
2023-04-11 07:28:46 -07:00
|
|
|
---@param start integer[]|string Start of region as a (line, column) tuple or string accepted by |getpos()|
|
|
|
|
---@param finish integer[]|string End of region as a (line, column) tuple or string accepted by |getpos()|
|
2024-03-16 10:11:42 -07:00
|
|
|
---@param opts? vim.highlight.range.Opts
|
2022-02-21 13:21:42 -07:00
|
|
|
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
|
2024-02-22 01:39:32 -07:00
|
|
|
local scoped = opts._scoped or false
|
2020-05-31 11:56:00 -07:00
|
|
|
|
feat(extmark): support proper multiline ranges
The removes the previous restriction that nvim_buf_set_extmark()
could not be used to highlight arbitrary multi-line regions
The problem can be summarized as follows: let's assume an extmark with a
hl_group is placed covering the region (5,0) to (50,0) Now, consider
what happens if nvim needs to redraw a window covering the lines 20-30.
It needs to be able to ask the marktree what extmarks cover this region,
even if they don't begin or end here.
Therefore the marktree needs to be augmented with the information covers
a point, not just what marks begin or end there. To do this, we augment
each node with a field "intersect" which is a set the ids of the
marks which overlap this node, but only if it is not part of the set of
any parent. This ensures the number of nodes that need to be explicitly
marked grows only logarithmically with the total number of explicitly
nodes (and thus the number of of overlapping marks).
Thus we can quickly iterate all marks which overlaps any query position
by looking up what leaf node contains that position. Then we only need
to consider all "start" marks within that leaf node, and the "intersect"
set of that node and all its parents.
Now, and the major source of complexity is that the tree restructuring
operations (to ensure that each node has T-1 <= size <= 2*T-1) also need
to update these sets. If a full inner node is split in two, one of the
new parents might start to completely overlap some ranges and its ids
will need to be moved from its children's sets to its own set.
Similarly, if two undersized nodes gets joined into one, it might no
longer completely overlap some ranges, and now the children which do
needs to have the have the ids in its set instead. And then there are
the pivots! Yes the pivot operations when a child gets moved from one
parent to another.
2020-11-22 02:10:37 -07:00
|
|
|
-- TODO: in case of 'v', 'V' (not block), this should calculate equivalent
|
|
|
|
-- bounds (row, col, end_row, end_col) as multiline regions are natively
|
|
|
|
-- supported now
|
2022-02-21 13:21:42 -07:00
|
|
|
local region = vim.region(bufnr, start, finish, regtype, inclusive)
|
2020-05-31 11:56:00 -07:00
|
|
|
for linenr, cols in pairs(region) do
|
2022-01-15 15:19:20 -07:00
|
|
|
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,
|
2022-02-21 13:21:42 -07:00
|
|
|
strict = false,
|
2024-02-22 01:39:32 -07:00
|
|
|
scoped = scoped,
|
2022-01-15 15:19:20 -07:00
|
|
|
})
|
2020-05-31 11:56:00 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-21 13:21:42 -07:00
|
|
|
local yank_ns = api.nvim_create_namespace('hlyank')
|
2024-03-16 10:11:42 -07:00
|
|
|
local yank_timer --- @type uv.uv_timer_t?
|
|
|
|
local yank_cancel --- @type fun()?
|
2023-07-15 08:55:32 -07:00
|
|
|
|
2024-04-30 04:30:21 -07:00
|
|
|
--- Highlight the yanked text during a |TextYankPost| event.
|
|
|
|
---
|
|
|
|
--- Add the following to your `init.vim`:
|
|
|
|
---
|
|
|
|
--- ```vim
|
|
|
|
--- autocmd TextYankPost * silent! lua vim.highlight.on_yank {higroup='Visual', timeout=300}
|
|
|
|
--- ```
|
2020-05-31 11:56:00 -07:00
|
|
|
---
|
2023-07-15 08:55:32 -07:00
|
|
|
--- @param opts table|nil Optional parameters
|
|
|
|
--- - 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)
|
|
|
|
--- - priority integer priority (default |vim.highlight.priorities|`.user`)
|
2022-02-21 13:21:42 -07:00
|
|
|
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`)',
|
|
|
|
},
|
|
|
|
})
|
2020-07-05 18:30:12 -07:00
|
|
|
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)
|
|
|
|
|
2022-02-21 13:21:42 -07:00
|
|
|
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
|
2020-07-05 18:30:12 -07:00
|
|
|
|
|
|
|
local higroup = opts.higroup or 'IncSearch'
|
|
|
|
local timeout = opts.timeout or 150
|
2020-05-18 06:49:50 -07:00
|
|
|
|
2024-02-22 01:39:32 -07:00
|
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
|
|
local winid = vim.api.nvim_get_current_win()
|
2022-06-02 00:49:57 -07:00
|
|
|
if yank_timer then
|
|
|
|
yank_timer:close()
|
2024-03-16 10:11:42 -07:00
|
|
|
assert(yank_cancel)
|
2024-02-22 01:39:32 -07:00
|
|
|
yank_cancel()
|
2022-06-02 00:49:57 -07:00
|
|
|
end
|
2020-05-18 06:49:50 -07:00
|
|
|
|
2024-05-12 14:12:25 -07:00
|
|
|
vim.api.nvim__win_add_ns(winid, yank_ns)
|
2023-05-06 12:53:36 -07:00
|
|
|
M.range(bufnr, yank_ns, higroup, "'[", "']", {
|
|
|
|
regtype = event.regtype,
|
|
|
|
inclusive = event.inclusive,
|
|
|
|
priority = opts.priority or M.priorities.user,
|
2024-02-22 01:39:32 -07:00
|
|
|
_scoped = true,
|
2023-05-06 12:53:36 -07:00
|
|
|
})
|
2022-02-21 13:21:42 -07:00
|
|
|
|
2024-02-22 01:39:32 -07:00
|
|
|
yank_cancel = function()
|
2022-06-02 00:49:57 -07:00
|
|
|
yank_timer = nil
|
2024-02-22 01:39:32 -07:00
|
|
|
yank_cancel = nil
|
|
|
|
pcall(vim.api.nvim_buf_clear_namespace, bufnr, yank_ns, 0, -1)
|
2024-05-12 14:12:25 -07:00
|
|
|
pcall(vim.api.nvim__win_del_ns, winid, yank_ns)
|
2024-02-22 01:39:32 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
yank_timer = vim.defer_fn(yank_cancel, timeout)
|
2020-05-18 06:49:50 -07:00
|
|
|
end
|
|
|
|
|
2022-02-21 13:21:42 -07:00
|
|
|
return M
|