| local api = vim.api |
|
|
| local M = {} |
|
|
| |
| |
| |
| |
| |
| |
| |
| M.priorities = { |
| syntax = 50, |
| treesitter = 100, |
| semantic_tokens = 125, |
| diagnostics = 150, |
| user = 200, |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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 |
| local timeout = opts.timeout or -1 |
|
|
| local v_maxcol = vim.v.maxcol |
|
|
| local pos1 = type(start) == 'string' and vim.fn.getpos(start) |
| or { |
| bufnr, |
| start[1] + 1, |
| start[2] ~= -1 and start[2] ~= v_maxcol and start[2] + 1 or v_maxcol, |
| 0, |
| } |
| local pos2 = type(finish) == 'string' and vim.fn.getpos(finish) |
| or { |
| bufnr, |
| finish[1] + 1, |
| finish[2] ~= -1 and start[2] ~= v_maxcol and finish[2] + 1 or v_maxcol, |
| 0, |
| } |
|
|
| local buf_line_count = vim.api.nvim_buf_line_count(bufnr) |
| pos1[2] = math.min(pos1[2], buf_line_count) |
| pos2[2] = math.min(pos2[2], buf_line_count) |
|
|
| if pos1[2] <= 0 or pos1[3] <= 0 or pos2[2] <= 0 or pos2[3] <= 0 then |
| return |
| end |
|
|
| vim._with({ buf = bufnr }, function() |
| if pos1[3] ~= v_maxcol then |
| local max_col1 = vim.fn.col({ pos1[2], '$' }) |
| pos1[3] = math.min(pos1[3], max_col1) |
| end |
| if pos2[3] ~= v_maxcol then |
| local max_col2 = vim.fn.col({ pos2[2], '$' }) |
| pos2[3] = math.min(pos2[3], max_col2) |
| end |
| end) |
|
|
| local region = vim.fn.getregionpos(pos1, pos2, { |
| type = regtype, |
| exclusive = not inclusive, |
| eol = true, |
| }) |
| |
| if regtype == 'v' or regtype == 'V' then |
| region = { { region[1][1], region[#region][2] } } |
| if |
| regtype == 'V' |
| or region[1][2][2] == pos1[2] and pos1[3] == v_maxcol |
| or region[1][2][2] == pos2[2] and pos2[3] == v_maxcol |
| then |
| region[1][2][2] = region[1][2][2] + 1 |
| region[1][2][3] = 0 |
| end |
| end |
|
|
| local extmarks = {} |
| for _, res in ipairs(region) do |
| local start_row = res[1][2] - 1 |
| local start_col = res[1][3] - 1 |
| local end_row = res[2][2] - 1 |
| local end_col = res[2][3] |
| table.insert( |
| extmarks, |
| api.nvim_buf_set_extmark(bufnr, ns, start_row, start_col, { |
| hl_group = higroup, |
| end_row = end_row, |
| end_col = end_col, |
| priority = priority, |
| strict = false, |
| }) |
| ) |
| end |
|
|
| local range_hl_clear = function() |
| if not api.nvim_buf_is_valid(bufnr) then |
| return |
| end |
| for _, mark in ipairs(extmarks) do |
| api.nvim_buf_del_extmark(bufnr, ns, mark) |
| end |
| end |
|
|
| if timeout ~= -1 then |
| local range_timer = vim.defer_fn(range_hl_clear, timeout) |
| return range_timer, range_hl_clear |
| end |
| end |
|
|
| local yank_timer |
| local yank_hl_clear |
| local yank_ns = api.nvim_create_namespace('nvim.hlyank') |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.on_yank(opts) |
| vim.validate('opts', opts, 'table', true) |
| 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 |
|
|
| local higroup = opts.higroup or 'IncSearch' |
|
|
| local bufnr = vim.api.nvim_get_current_buf() |
| local winid = vim.api.nvim_get_current_win() |
|
|
| if yank_timer and not yank_timer:is_closing() then |
| yank_timer:close() |
| assert(yank_hl_clear) |
| yank_hl_clear() |
| end |
|
|
| vim.api.nvim__ns_set(yank_ns, { wins = { winid } }) |
| yank_timer, yank_hl_clear = M.range(bufnr, yank_ns, higroup, "'[", "']", { |
| regtype = event.regtype, |
| inclusive = true, |
| priority = opts.priority or M.priorities.user, |
| timeout = opts.timeout or 150, |
| }) |
| end |
|
|
| return M |
|
|