refactor: improve tracking and deletion of extmarks
This commit is contained in:
parent
440acf7900
commit
ef3818dcac
@ -4,6 +4,7 @@
|
||||
---@field private _attached boolean Whether or not VirtualIndent is attached for its buffer
|
||||
---@field private _bufnrs table<integer, VirtualIndent> Buffers with VirtualIndent attached
|
||||
---@field private _watcher_running boolean Whether or not VirtualIndent is reacting to `vim.b.org_indent_mode`
|
||||
---@field private _extmark_ids table<integer, integer[]> Mapping of line number to active extmarks
|
||||
---@field private _tree_utils table<string, function> Treesitter utilities for the given filetype
|
||||
---@field private _fallback_pattern string Pattern to search for if treesitter parser fails
|
||||
local VirtualIndent = {
|
||||
@ -43,6 +44,7 @@ function VirtualIndent:new(bufnr)
|
||||
local this = setmetatable({
|
||||
_bufnr = bufnr,
|
||||
_watcher_running = false,
|
||||
_extmark_ids = {},
|
||||
_attached = false,
|
||||
_fallback_pattern = ft_setting.fallback_pattern,
|
||||
_tree_utils = ft_setting.utils,
|
||||
@ -52,19 +54,12 @@ function VirtualIndent:new(bufnr)
|
||||
end
|
||||
|
||||
function VirtualIndent:_delete_old_extmarks(start_line, end_line)
|
||||
local ok, old_extmarks = pcall(
|
||||
vim.api.nvim_buf_get_extmarks,
|
||||
self._bufnr,
|
||||
self._ns_id,
|
||||
{ start_line, 0 },
|
||||
{ end_line, 0 },
|
||||
{ type = "virt_text" }
|
||||
)
|
||||
if not ok then
|
||||
old_extmarks = {}
|
||||
end
|
||||
for _, ext in ipairs(old_extmarks) do
|
||||
vim.api.nvim_buf_del_extmark(self._bufnr, self._ns_id, ext[1])
|
||||
for line = start_line, end_line do
|
||||
local line_extmark_ids = self._extmark_ids[line] or {}
|
||||
for idx, extmark_id in ipairs(line_extmark_ids) do
|
||||
vim.api.nvim_buf_del_extmark(self._bufnr, self._ns_id, extmark_id)
|
||||
line_extmark_ids[idx] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -126,7 +121,7 @@ function VirtualIndent:set_indent(start_line, end_line, ignore_ts)
|
||||
|
||||
if indent > 0 then
|
||||
-- NOTE: `ephemeral = true` is not implemented for `inline` virt_text_pos :(
|
||||
pcall(vim.api.nvim_buf_set_extmark, self._bufnr, self._ns_id, line, 0, {
|
||||
local new_extmark_id = vim.api.nvim_buf_set_extmark(self._bufnr, self._ns_id, line, 0, {
|
||||
-- HACK: The 'space' character below is not a space, it is actually a "Braille Pattern Blank"
|
||||
-- character, U+2800. This avoids issues with how `indentexpr` is calculated by not using
|
||||
-- spaces (which the `indentexpr` is looking for).
|
||||
@ -135,6 +130,11 @@ function VirtualIndent:set_indent(start_line, end_line, ignore_ts)
|
||||
right_gravity = false,
|
||||
priority = 110,
|
||||
})
|
||||
if not self._extmark_ids[line] then
|
||||
self._extmark_ids[line] = { new_extmark_id }
|
||||
else
|
||||
table.insert(self._extmark_ids[line], new_extmark_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user