feat: better user cursor highlights

This commit is contained in:
əlemi 2024-09-25 00:36:08 +02:00
parent ce65e7e01a
commit d991a81601
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 20 additions and 22 deletions

View file

@ -165,26 +165,6 @@ local function buffer_set_content(buf, content, first, last)
end end
end end
---@param buf integer buffer to highlight onto
---@param ns integer namespace for highlight
---@param group string highlight group
---@param start [integer, integer]
---@param fini [integer, integer]
local function multiline_highlight(buf, ns, group, start, fini)
for i=start[1],fini[1] do
if i == start[1] and i == fini[1] then
local fini_col = fini[2]
if start[2] == fini[2] then fini_col = fini_col + 1 end
vim.api.nvim_buf_add_highlight(buf, ns, group, i, start[2], fini_col)
elseif i == start[1] then
vim.api.nvim_buf_add_highlight(buf, ns, group, i, start[2], -1)
elseif i == fini[1] then
vim.api.nvim_buf_add_highlight(buf, ns, group, i, 0, fini[2])
else
vim.api.nvim_buf_add_highlight(buf, ns, group, i, 0, -1)
end
end
end
local function buffer_len(buf) local function buffer_len(buf)
local count = 0 local count = 0
@ -203,8 +183,10 @@ local function separator()
end end
end end
local hash_fn = nil
if native ~= nil then hash_fn = native.hash end
return { return {
multiline_highlight = multiline_highlight,
cursor = { cursor = {
position = cursor_position, position = cursor_position,
}, },
@ -213,7 +195,7 @@ return {
get_content = buffer_get_content, get_content = buffer_get_content,
set_content = buffer_set_content, set_content = buffer_set_content,
}, },
hash = native.hash, hash = hash_fn,
available_colors = available_colors, available_colors = available_colors,
color = color, color = color,
poller = async_poller, poller = async_poller,

View file

@ -88,6 +88,22 @@ local function register_cursor_handler(ws)
buffers.users[event.user] = event.buffer buffers.users[event.user] = event.buffer
local buffer_id = buffers.map_rev[event.buffer] local buffer_id = buffers.map_rev[event.buffer]
if buffer_id ~= nil then if buffer_id ~= nil then
vim.api.nvim_buf_set_extmark(
buffer_id,
user_hl[event.user].ns,
event.start[1],
event.start[2],
{
end_col = event.finish[1],
end_row = event.finish[2],
hl_group = user_hl[event.user].hi,
hl_eol = true,
virt_text_pos = "overlay",
virt_text = {
{ event.user, "InlayHint" }
},
}
)
utils.multiline_highlight( utils.multiline_highlight(
buffer_id, buffer_id,
user_hl[event.user].ns, user_hl[event.user].ns,