Skip to content

Commit 94d7291

Browse files
author
haxibami
authored
add support for multi byte chars (#19)
1 parent 088c91e commit 94d7291

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lua/lsplinks.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,26 @@ function M.get(bufnr)
204204
return links_by_buf[bufnr] or {}
205205
end
206206

207+
--- Translate a character index to a byte index
208+
---@param lnum integer
209+
---@param index integer
210+
---@return integer
211+
local function translate(lnum, index)
212+
local line = vim.api.nvim_buf_get_lines(0, lnum, lnum + 1, true)
213+
return vim.fn.byteidx(line[1], index)
214+
end
215+
207216
--- Highlight links in the current buffer
208217
function M.display()
209218
api.nvim_buf_clear_namespace(0, ns, 0, -1)
210219
for _, link in ipairs(M.get()) do
220+
local start_lnum = link.range.start.line
221+
local end_lnum = link.range["end"].line
211222
-- sometimes the buffer is changed before we get here and the link
212223
-- ranges are invalid, so we ignore the error.
213-
pcall(api.nvim_buf_set_extmark, 0, ns, link.range.start.line, link.range.start.character, {
214-
end_row = link.range["end"].line,
215-
end_col = link.range["end"].character,
224+
pcall(api.nvim_buf_set_extmark, 0, ns, start_lnum, translate(start_lnum, link.range.start.character), {
225+
end_row = end_lnum,
226+
end_col = translate(end_lnum, link.range["end"].character),
216227
hl_group = options.hl_group,
217228
})
218229
end

0 commit comments

Comments
 (0)