feat(picker): on_show + impl for lines to show with current cursor

This commit is contained in:
Folke Lemaitre 2025-01-14 11:51:17 +01:00
parent e390b7d874
commit 9a7020fd18
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 12 additions and 0 deletions

View file

@ -75,7 +75,9 @@ local M = {}
---@field icons? snacks.picker.icons
---@field source? string
---@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 layouts? table<string, snacks.picker.Layout>
---@field main? snacks.picker.main.Config
local defaults = {
prompt = "",
sources = {},

View file

@ -266,6 +266,13 @@ M.lines = {
preview = "main",
preset = "ivy",
},
---@param picker snacks.Picker
on_show = function(picker)
local cursor = vim.api.nvim_win_get_cursor(picker.main)
local info = vim.api.nvim_win_call(picker.main, vim.fn.winsaveview)
picker.list:view(cursor[1], info.topline)
picker:show_preview()
end,
}
-- Loclist

View file

@ -263,6 +263,9 @@ function M:show()
self.preview.win:show()
end
self.input.win:focus()
if self.opts.on_show then
self.opts.on_show(self)
end
end
---@return fun():snacks.picker.Item?