feat(picker): added jump options jumplist(true for all), reuse_win & tagstack (true for lsp). Closes #589. Closes #568

This commit is contained in:
Folke Lemaitre 2025-01-17 21:28:14 +01:00
parent ab3595a3f6
commit 84c3738fb0
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 39 additions and 7 deletions

View file

@ -5,15 +5,10 @@ local M = {}
local SCROLL_WHEEL_DOWN = Snacks.util.keycode("<ScrollWheelDown>")
local SCROLL_WHEEL_UP = Snacks.util.keycode("<ScrollWheelUp>")
function M.edit(picker)
function M.jump(picker)
picker:close()
local win = vim.api.nvim_get_current_win()
-- save position in jump list
vim.api.nvim_win_call(win, function()
vim.cmd("normal! m'")
end)
local current_buf = vim.api.nvim_get_current_buf()
local current_empty = vim.bo[current_buf].buftype == ""
and vim.bo[current_buf].filetype == ""
@ -21,7 +16,25 @@ function M.edit(picker)
and vim.api.nvim_buf_get_lines(current_buf, 0, -1, false)[1] == ""
and vim.api.nvim_buf_get_name(current_buf) == ""
if not current_empty then
-- save position in jump list
if picker.opts.jump.jumplist then
vim.api.nvim_win_call(win, function()
vim.cmd("normal! m'")
end)
end
-- save position in tag stack
if picker.opts.jump.tagstack then
local from = vim.fn.getpos(".")
from[1] = current_buf
local tagstack = { { tagname = vim.fn.expand("<cword>"), from = from } }
vim.fn.settagstack(vim.fn.win_getid(win), { items = tagstack }, "t")
end
end
local items = picker:selected({ fallback = true })
for _, item in ipairs(items) do
-- load the buffer
local buf = item.buf ---@type number
@ -30,6 +43,12 @@ function M.edit(picker)
buf = vim.fn.bufadd(path)
end
-- use an existing window if possible
if #items == 1 and picker.opts.jump.reuse_win and buf ~= current_buf then
win = vim.fn.win_findbuf(buf)[1] or win
vim.api.nvim_set_current_win(win)
end
if not vim.api.nvim_buf_is_loaded(buf) then
vim.api.nvim_buf_call(buf, function()
vim.cmd("edit")
@ -67,7 +86,8 @@ end
M.cancel = function() end
M.confirm = M.edit
M.edit = M.jump
M.confirm = M.jump
function M.toggle_maximize(picker)
picker.layout:maximize()

View file

@ -84,6 +84,7 @@ local M = {}
---@field main? snacks.picker.main.Config main editor window config
---@field on_change? fun(picker:snacks.Picker, item:snacks.picker.Item) called when the cursor changes
---@field on_show? fun(picker:snacks.Picker) called when the picker is shown
---@field jump? snacks.picker.jump.Config|{}
--- Other
---@field debug? snacks.picker.debug|{}
local defaults = {
@ -131,6 +132,12 @@ local defaults = {
},
man_pager = nil, ---@type string? MANPAGER env to use for `man` preview
},
---@class snacks.picker.jump.Config
jump = {
jumplist = true, -- save the current position in the jumplist
tagstack = false, -- save the current position in the tagstack
reuse_win = false, -- reuse an existing window if the buffer is already open
},
win = {
-- input window
input = {

View file

@ -317,6 +317,7 @@ M.lsp_declarations = {
format = "file",
include_current = false,
auto_confirm = true,
jump = { tagstack = true, reuse_win = true },
}
-- LSP definitions
@ -326,6 +327,7 @@ M.lsp_definitions = {
format = "file",
include_current = false,
auto_confirm = true,
jump = { tagstack = true, reuse_win = true },
}
-- LSP implementations
@ -335,6 +337,7 @@ M.lsp_implementations = {
format = "file",
include_current = false,
auto_confirm = true,
jump = { tagstack = true, reuse_win = true },
}
-- LSP references
@ -346,6 +349,7 @@ M.lsp_references = {
include_declaration = true,
include_current = false,
auto_confirm = true,
jump = { tagstack = true, reuse_win = true },
}
-- LSP document symbols
@ -401,6 +405,7 @@ M.lsp_type_definitions = {
format = "file",
include_current = false,
auto_confirm = true,
jump = { tagstack = true, reuse_win = true },
}
M.man = {