fix(picker.lsp): lazy resolve item lsp locations. Fixes #650

This commit is contained in:
Folke Lemaitre 2025-01-20 09:56:10 +01:00
parent f7fddf8dfb
commit d0a0046e37
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
5 changed files with 101 additions and 39 deletions

View file

@ -181,6 +181,7 @@ function M.parse(str)
return t, args
end
--- Resolves the item if it has a resolve function
---@param item snacks.picker.Item
function M.resolve(item)
if item and item.resolve then
@ -190,6 +191,31 @@ function M.resolve(item)
return item
end
--- Resolves the location of an item to byte positions
---@param item snacks.picker.Item
---@param buf? number
function M.resolve_loc(item, buf)
if not item or not item.loc or item.loc.resolved then
return item
end
local lines = {} ---@type string[]
if buf and vim.api.nvim_buf_is_valid(buf) then
lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
else
lines = vim.fn.readfile(item.file)
end
---@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
end
item.pos = resolve(item.loc.range["start"])
item.end_pos = resolve(item.loc.range["end"]) or item.end_pos
item.loc.resolved = true
return item
end
--- Returns the relative time from a given time
--- as ... ago
---@param time number in seconds