mirror of
https://github.com/folke/snacks.nvim
synced 2025-12-23 08:47:57 +00:00
fix(picker.actions): drop and tabdrop should never reload existing buffers (#2368)
## Description When using `tabdrop` as the `jump` action and jumping to a location in the current buffer, the existing implementation always triggers a reload of the current buffer because the `vim.cmd` is unconditional. This causes unnecessary triggering of `BufRead*` and `LspAttach` auto-commands, which can then trigger a lot of things that can be CPU-intensive (linters, LSP, etc.). This PR skips calling the `cmd` when jumping to the current buffer, and hence avoids the excessive triggering of those auto-commands. --------- Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
parent
404027c973
commit
6cf2fee619
1 changed files with 41 additions and 35 deletions
|
|
@ -53,6 +53,7 @@ function M.jump(picker, _, action)
|
||||||
local win = vim.api.nvim_get_current_win()
|
local win = vim.api.nvim_get_current_win()
|
||||||
|
|
||||||
local current_buf = vim.api.nvim_get_current_buf()
|
local current_buf = vim.api.nvim_get_current_buf()
|
||||||
|
local current_tab = vim.api.nvim_get_current_tabpage()
|
||||||
local current_empty = vim.bo[current_buf].buftype == ""
|
local current_empty = vim.bo[current_buf].buftype == ""
|
||||||
and vim.bo[current_buf].filetype == ""
|
and vim.bo[current_buf].filetype == ""
|
||||||
and vim.api.nvim_buf_line_count(current_buf) == 1
|
and vim.api.nvim_buf_line_count(current_buf) == 1
|
||||||
|
|
@ -77,48 +78,53 @@ function M.jump(picker, _, action)
|
||||||
end
|
end
|
||||||
|
|
||||||
local cmd = edit_cmd[action.cmd] or edit_cmd.edit
|
local cmd = edit_cmd[action.cmd] or edit_cmd.edit
|
||||||
|
local is_drop = cmd:find("drop") ~= nil
|
||||||
|
|
||||||
if cmd:find("drop") then
|
-- load the buffers
|
||||||
local drop = {} ---@type string[]
|
local first_buf ---@type number
|
||||||
for _, item in ipairs(items) do
|
for _, item in ipairs(items) do
|
||||||
local path = item.buf and vim.api.nvim_buf_get_name(item.buf) or Snacks.picker.util.path(item)
|
local buf = item.buf ---@type number
|
||||||
if not path then
|
if not buf then
|
||||||
Snacks.notify.error("Either item.buf or item.file is required", { title = "Snacks Picker" })
|
local path = assert(Snacks.picker.util.path(item), "Either item.buf or item.file is required")
|
||||||
return
|
buf = vim.fn.bufadd(path)
|
||||||
end
|
|
||||||
drop[#drop + 1] = vim.fn.fnameescape(path)
|
|
||||||
end
|
end
|
||||||
vim.cmd(cmd .. " " .. table.concat(drop, " "))
|
vim.bo[buf].buflisted = true
|
||||||
win = vim.api.nvim_get_current_win()
|
first_buf = first_buf or buf
|
||||||
else
|
end
|
||||||
for i, item in ipairs(items) do
|
|
||||||
-- load the buffer
|
|
||||||
local buf = item.buf ---@type number
|
|
||||||
if not buf then
|
|
||||||
local path = assert(Snacks.picker.util.path(item), "Either item.buf or item.file is required")
|
|
||||||
buf = vim.fn.bufadd(path)
|
|
||||||
end
|
|
||||||
vim.bo[buf].buflisted = true
|
|
||||||
|
|
||||||
-- use an existing window if possible
|
-- find an existing window showing the first buffer in the current tab
|
||||||
if cmd == "buffer" and #items == 1 and picker.opts.jump.reuse_win and buf ~= current_buf then
|
---@param in_tab? boolean
|
||||||
for _, w in ipairs(vim.fn.win_findbuf(buf)) do
|
local function find_win(in_tab)
|
||||||
if vim.api.nvim_win_get_config(w).relative == "" then
|
if first_buf == current_buf then
|
||||||
win = w
|
return true
|
||||||
vim.api.nvim_set_current_win(win)
|
end
|
||||||
break
|
for _, w in ipairs(vim.fn.win_findbuf(first_buf)) do
|
||||||
end
|
if
|
||||||
end
|
vim.api.nvim_win_get_config(w).relative == ""
|
||||||
end
|
and (in_tab ~= true or vim.api.nvim_win_get_tabpage(w) == current_tab)
|
||||||
|
then
|
||||||
-- open the first buffer
|
win = w
|
||||||
if i == 1 then
|
vim.api.nvim_set_current_win(win)
|
||||||
vim.cmd(("%s %d"):format(cmd, buf))
|
return true
|
||||||
win = vim.api.nvim_get_current_win()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- use an existing window if reuse_win or drop
|
||||||
|
if is_drop then
|
||||||
|
if find_win() or cmd == "drop" then
|
||||||
|
cmd = "buffer!"
|
||||||
|
else
|
||||||
|
cmd = "tab sbuffer"
|
||||||
|
end
|
||||||
|
elseif cmd == "buffer!" and #items == 1 and picker.opts.jump.reuse_win then
|
||||||
|
find_win(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- open the first buffer
|
||||||
|
vim.cmd(("%s %d"):format(cmd, first_buf))
|
||||||
|
win = vim.api.nvim_get_current_win()
|
||||||
|
|
||||||
-- set the cursor
|
-- set the cursor
|
||||||
local item = items[1]
|
local item = items[1]
|
||||||
local pos = item.pos
|
local pos = item.pos
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue