fix(picker.lsp): backward compat with Neovim 0.95

This commit is contained in:
Folke Lemaitre 2025-01-20 10:14:05 +01:00
parent f24b437228
commit 3df2408713
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 17 additions and 2 deletions

View file

@ -191,6 +191,18 @@ function M.resolve(item)
return item
end
---@param s string
---@param index number
---@param encoding string
function M.str_byteindex(s, index, encoding)
if vim.lsp.util._str_byteindex_enc then
return vim.lsp.util._str_byteindex_enc(s, index, encoding)
elseif vim._str_byteindex then
return vim._str_byteindex(s, index, encoding == "utf-16")
end
return vim.str_byteindex(s, index, encoding == "utf-16")
end
--- Resolves the location of an item to byte positions
---@param item snacks.picker.Item
---@param buf? number
@ -198,6 +210,7 @@ function M.resolve_loc(item, buf)
if not item or not item.loc or item.loc.resolved then
return item
end
-- return vim._str_byteindex(s, index, encoding == 'utf-16')
local lines = {} ---@type string[]
if buf and vim.api.nvim_buf_is_valid(buf) then
@ -208,7 +221,7 @@ function M.resolve_loc(item, buf)
---@param pos lsp.Position?
local function resolve(pos)
return pos and { pos.line + 1, vim.str_byteindex(lines[pos.line + 1], item.loc.encoding, pos.character) } or nil
return pos and { pos.line + 1, M.str_byteindex(lines[pos.line + 1], pos.character, item.loc.encoding) } or nil
end
item.pos = resolve(item.loc.range["start"])
item.end_pos = resolve(item.loc.range["end"]) or item.end_pos