MunifTanjim / nui.nvim

UI Component Library for Neovim.
MIT License
1.62k stars 57 forks source link

Why is the 'on_submit' callback of 'nui.input' being executed twice? #287

Closed LaBatata101 closed 1 year ago

LaBatata101 commented 1 year ago

In the following code, I have created an input that closes the current buffer when the user types 'y'. However, I am encountering an issue where the 'on_submit' callback is being executed twice. This causes the code to attempt to delete the same buffer twice, resulting in an error as shown in the image below.

local Input = require("nui.input")
local event = require("nui.utils.autocmd").event

local prompt_msg = "Close current file?"

local bufnr = vim.api.nvim_get_current_buf()

local input = Input({
  position = "50%",
  size = {
    width = #prompt_msg + 4,
  },
  border = {
    style = "single",
    text = {
      top = prompt_msg,
      top_align = "center",
    },
  },
  win_options = {
    winhighlight = "Normal:Normal,FloatBorder:Normal",
  },
}, {
  prompt = "> ",
  default_value = "N",
  on_close = function()
    print("Input Closed!")
  end,
  on_submit = function(value)
    print("START - on_submit")
    if value:lower() == "y" then
      vim.api.nvim_buf_delete(bufnr, { force = true })
    end
    print("END - on_submit")
  end,
})

input:mount()

input:on(event.BufLeave, function()
  input:unmount()
end)

image

MunifTanjim commented 1 year ago

Looks like you might be using an old version? Can you update nui.nvim and try again?

LaBatata101 commented 1 year ago

That fixed, thanks!