feat(picker.lsp): use existing buffers for preview when opened

This commit is contained in:
Folke Lemaitre 2025-02-04 22:06:00 +01:00
parent 6d3c135235
commit d4e63531c9
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -168,6 +168,19 @@ function M.fix_locs(locs)
end
end
function M.bufmap()
local bufmap = {} ---@type table<string,number>
for _, b in ipairs(vim.api.nvim_list_bufs()) do
if vim.bo[b].buflisted and vim.bo[b].buftype == "" and vim.api.nvim_buf_is_loaded(b) then
local name = vim.api.nvim_buf_get_name(b)
if name ~= "" then
bufmap[name] = b
end
end
end
return bufmap
end
---@param method string
---@param opts snacks.picker.lsp.Config|{context?:lsp.ReferenceContext}
---@param filter snacks.picker.Filter
@ -177,6 +190,7 @@ function M.get_locations(method, opts, filter)
local fname = vim.api.nvim_buf_get_name(buf)
fname = vim.fs.normalize(fname)
local cursor = vim.api.nvim_win_get_cursor(win)
local bufmap = M.bufmap()
---@async
---@param cb async fun(item: snacks.picker.finder.Item)
@ -218,7 +232,7 @@ function M.get_locations(method, opts, filter)
---@type snacks.picker.finder.Item
local item = {
text = loc.filename .. " " .. loc.text,
buf = loc.bufnr,
buf = bufmap[loc.filename],
file = loc.filename,
pos = { loc.lnum, loc.col - 1 },
end_pos = loc.end_lnum and loc.end_col and { loc.end_lnum, loc.end_col - 1 } or nil,
@ -341,6 +355,7 @@ function M.symbols(opts, ctx)
end
end
local bufmap = M.bufmap()
local filter = opts.filter[vim.bo[buf].filetype]
if filter == nil then
filter = opts.filter.default
@ -370,6 +385,7 @@ function M.symbols(opts, ctx)
})
for _, item in ipairs(items) do
item.tree = opts.tree
item.buf = bufmap[item.file]
---@diagnostic disable-next-line: await-in-sync
cb(item)
end