stevearc / dressing.nvim

Neovim plugin to improve the default vim.ui interfaces
MIT License
1.7k stars 32 forks source link

Window was closed immediately #92

Closed mrtnvgr closed 1 year ago

mrtnvgr commented 1 year ago

Describe the bug When I try to open multiple windows at once, I get this error. How can I spawn multiple selection windows?

System information

vim.api.nvim_create_autocmd("FileType", {
    pattern = "DressingSelect",
    callback = function(args)
        vim.keymap.set("n", "<Tab>", "j", { buffer = args.buf })
        vim.keymap.set("n", "<S-Tab>", "k", { buffer = args.buf })
    end,
})

require("dressing").setup({
    input = { border = initlua.settings.ui.border },
    select = {
        nui = {
            border = { style = initlua.settings.ui.border },
        },
        builtin = {
            border = initlua.settings.ui.border,
        },
    },
})

To Reproduce

vim.ui.select({ "Yes", "No" }, { prompt = "1" }, function(choice) end)
vim.ui.select({ "Yes", "No" }, { prompt = "1" }, function(choice) end)
vim.ui.select({ "Yes", "No" }, { prompt = "1" }, function(choice) end)

Additional context

Error executing vim.schedule lua callback: .../nvim/lazy/dressing.nvim/lua/dressing/select/builtin.lua:73: Window was closed immediately
stack traceback:
    [C]: in function 'nvim_open_win'
    .../nvim/lazy/dressing.nvim/lua/dressing/select/builtin.lua:73: in function 'select'
    ...are/nvim/lazy/dressing.nvim/lua/dressing/select/init.lua:70: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>
stevearc commented 1 year ago

What are you trying to accomplish? You cannot open more than one selection at a time because there is no way for the user to interact with more than one window at a time. If you need to perform multiple selections, you'll need to do that in sequence.

vim.ui.select({ "Yes", "No" }, { prompt = "1" }, function(choice)
  vim.ui.select({ "Yes", "No" }, { prompt = "1" }, function(choice)
    vim.ui.select({ "Yes", "No" }, { prompt = "1" }, function(choice) end)
  end)
end)
mrtnvgr commented 1 year ago

What are you trying to accomplish? You cannot open more than one selection at a time because there is no way for the user to interact with more than one window at a time. If you need to perform multiple selections, you'll need to do that in sequence.

vim.ui.select({ "Yes", "No" }, { prompt = "1" }, function(choice)
  vim.ui.select({ "Yes", "No" }, { prompt = "1" }, function(choice)
    vim.ui.select({ "Yes", "No" }, { prompt = "1" }, function(choice) end)
  end)
end)

Yes, that's what I need! Is there a way doing this without nested functions? Ideally I would want to call them in a for loop.

for _, name in ipairs(names) do
  vim.ui.select({ "Yes", "No" }, { prompt = "Do you want to enable " .. name .. "?" }, function(choice) ... end)
end
stevearc commented 1 year ago

If you want to call async functions as though they were synchronous, you'll need to use lua's coroutines. It's possible to use raw coroutines for this, but it would probably be easier to use an async lib. The most popular one I know of is plenary.async, but there are others out there.

mrtnvgr commented 1 year ago

If you want to call async functions as though they were synchronous, you'll need to use lua's coroutines. It's possible to use raw coroutines for this, but it would probably be easier to use an async lib. The most popular one I know of is plenary.async, but there are others out there.

Thank you so much! I will try it