fix(picker.actions): when only 1 win, pick_win will select that automatically. Show warning when no windows. See #623

This commit is contained in:
Folke Lemaitre 2025-01-30 20:57:17 +01:00
parent 71070b78f0
commit ba5a70b84d
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 27 additions and 16 deletions

View file

@ -132,25 +132,36 @@ function M.pick_win(picker, item, action)
local overlays = {} ---@type snacks.win[]
picker.layout:hide()
local chars = "asdfghjkl"
local wins = {} ---@type number[]
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_config(win).relative == "" then
local c = chars:sub(1, 1)
chars = chars:sub(2)
overlays[c] = Snacks.win({
backdrop = false,
win = win,
focusable = false,
enter = false,
relative = "win",
width = 7,
height = 3,
text = (" \n %s \n "):format(c),
wo = {
winhighlight = "NormalFloat:SnacksPickerPickWin" .. (win == picker.main and "Current" or ""),
},
})
wins[#wins + 1] = win
end
end
if #wins == 1 then
picker.main = wins[1]
return
elseif #wins == 0 then
Snacks.notify.warn("No windows to pick from", { title = "Snacks Picker" })
return
end
for _, win in ipairs(wins) do
local c = chars:sub(1, 1)
chars = chars:sub(2)
overlays[c] = Snacks.win({
backdrop = false,
win = win,
focusable = false,
enter = false,
relative = "win",
width = 7,
height = 3,
text = (" \n %s \n "):format(c),
wo = {
winhighlight = "NormalFloat:SnacksPickerPickWin" .. (win == picker.main and "Current" or ""),
},
})
end
vim.cmd([[redraw!]])
local char = vim.fn.getcharstr()
for _, overlay in pairs(overlays) do

View file

@ -186,7 +186,7 @@ function State:setup(opts, ctx)
if self.all then
local picker = self.picker()
if not picker then
return
return {}
end
picker.list:set_target()
self.on_find = function()