How to create a blocking picker? #2060
Answered
by
enderbeatt
enderbeatt
asked this question in
Q&A
-
I am pretty sure I am not the first person who wants to implement something like this: start a picker, select an item, and get an item in lua code. I basically want to have Is there a way to wait for the picker to finish in lua code? |
Beta Was this translation helpful? Give feedback.
Answered by
enderbeatt
Jul 18, 2025
Replies: 1 comment 4 replies
-
Have you heard of callbacks? Snacks.picker.pick('files', {
on_close = function(picker)
local items = picker:selected { fallback = true }
vim.print(items)
end,
}) You can also override the confirm action like this: Note Jump is the default action but some pickers may override it. See sources.lua. Snacks.picker.pick('files', {
confirm = function(picker, item, action)
local multi_selection = picker:selected { fallback = true }
vim.print { item, multi_selection }
Snacks.picker.actions.jump(picker, item, action)
end,
}) or like this: Snacks.picker.pick('files', {
actions = {
before_confirm = function(picker, item)
vim.print(item)
end,
},
win = {
input = {
keys = {
['<CR>'] = { { 'before_confirm', 'confirm' }, mode = { 'i', 'n' } },
},
},
},
}) There is also a drop-in replacement for the Snacks.picker.select(items, opts, on_choice) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I do not hate callbacks, it's just that nvim-dap requires a string or a function that returns a string to setup its arguments
Fortunately, turns out that if you pass nvim-dap a function, it will run it inside a coroutine, so that's how I implemented what I need:
https://github.com/enderbeatt/neovim-config/blob/master/lua/handmade/debug_helpers.lua#L40-L65
And that's where I use it:
https://github.com/enderbeatt/neovim-config/blob/master/lua/plugin/debug/config.lua#L40-L42