From d991a816015cd0a126dca09f39085d8c5ee843d2 Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 25 Sep 2024 00:36:08 +0200 Subject: [PATCH] feat: better user cursor highlights --- lua/codemp/utils.lua | 26 ++++---------------------- lua/codemp/workspace.lua | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/lua/codemp/utils.lua b/lua/codemp/utils.lua index 89c804f..c628c0f 100644 --- a/lua/codemp/utils.lua +++ b/lua/codemp/utils.lua @@ -165,26 +165,6 @@ local function buffer_set_content(buf, content, first, last) 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 count = 0 @@ -203,8 +183,10 @@ local function separator() end end +local hash_fn = nil +if native ~= nil then hash_fn = native.hash end + return { - multiline_highlight = multiline_highlight, cursor = { position = cursor_position, }, @@ -213,7 +195,7 @@ return { get_content = buffer_get_content, set_content = buffer_set_content, }, - hash = native.hash, + hash = hash_fn, available_colors = available_colors, color = color, poller = async_poller, diff --git a/lua/codemp/workspace.lua b/lua/codemp/workspace.lua index 17d99f0..0a76d04 100644 --- a/lua/codemp/workspace.lua +++ b/lua/codemp/workspace.lua @@ -88,6 +88,22 @@ local function register_cursor_handler(ws) buffers.users[event.user] = event.buffer local buffer_id = buffers.map_rev[event.buffer] 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( buffer_id, user_hl[event.user].ns,