stevearc / dressing.nvim

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

vim.ui.input reports nil for empty string #40

Closed Shatur closed 2 years ago

Shatur commented 2 years ago

Describe the bug

The following code

vim.ui.input({ prompt = 'Data: ' }, function(data) vim.notify(vim.inspect(data)) end)

prints nil for both cancelled dialog and empty input. I would expect to have an empty string on empty input.

System information

To Reproduce Steps to reproduce the behavior:

  1. :vim.ui.input({ prompt = 'Data: ' }, function(data) vim.notify(vim.inspect(data)) end)
  2. Press <CR>
stevearc commented 2 years ago

This is intentional behavior. The built-in vim.ui.input explicitly treats an empty return string as nil, so we have to do the same to preserve compatibility. https://github.com/neovim/neovim/blob/0adc66171a355a12494d87ebb767d509540c7ef9/runtime/lua/vim/ui.lua#L92-L96

Shatur commented 2 years ago

Oh, I see... But how can I know if a user canceled the dialog or set an empty string?

stevearc commented 2 years ago

Unfortunately, you cannot. I believe this is a limitation of the input() function, which seems to return an empty string when canceled. Since it can't distinguish between empty input and cancel, vim.ui.input can't either.