diff options
author | Jongwook Choi <wookayin@gmail.com> | 2022-11-07 19:15:15 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-08 08:15:15 +0800 |
commit | 59ff4691f67fc1ddd3d1b7240a2f2eb095e58281 (patch) | |
tree | 09f43712485108b3770891986a1c1ffc015c85a0 /runtime | |
parent | 8147d3df284a075f89746f9d5e948b5220c45f0b (diff) | |
download | rneovim-59ff4691f67fc1ddd3d1b7240a2f2eb095e58281.tar.gz rneovim-59ff4691f67fc1ddd3d1b7240a2f2eb095e58281.tar.bz2 rneovim-59ff4691f67fc1ddd3d1b7240a2f2eb095e58281.zip |
fix(vim.ui.input): return empty string when inputs nothing (#20883)
fix(vim.ui.input): return empty string when inputs nothing
The previous behavior of `vim.ui.input()` when typing <CR> with
no text input (with an intention of having the empty string as input)
was to execute `on_confirm(nil)`, conflicting with its documentation.
Inputting an empty string should now correctly execute `on_confirm('')`.
This should be clearly distinguished from cancelling or aborting the
input UI, in which case `on_confirm(nil)` is executed as before.
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/doc/lua.txt | 3 | ||||
-rw-r--r-- | runtime/lua/vim/ui.lua | 15 |
2 files changed, 13 insertions, 5 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index c4e139bca7..cab2f49d94 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -1979,7 +1979,8 @@ input({opts}, {on_confirm}) *vim.ui.input()* highlighting user inputs. • {on_confirm} (function) ((input|nil) -> ()) Called once the user confirms or abort the input. `input` is what the user - typed. `nil` if the user aborted the dialog. + typed (it might be an empty string if nothing was + entered), or `nil` if the user aborted the dialog. select({items}, {opts}, {on_choice}) *vim.ui.select()* Prompts the user to pick a single item from a collection of entries diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index 6f1ce3089d..d9a3963afc 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -73,7 +73,8 @@ end --- user inputs. ---@param on_confirm function ((input|nil) -> ()) --- Called once the user confirms or abort the input. ---- `input` is what the user typed. +--- `input` is what the user typed (it might be +--- an empty string if nothing was entered), or --- `nil` if the user aborted the dialog. --- --- Example: @@ -88,11 +89,17 @@ function M.input(opts, on_confirm) }) opts = (opts and not vim.tbl_isempty(opts)) and opts or vim.empty_dict() + + -- Note that vim.fn.input({}) returns an empty string when cancelled. + -- vim.ui.input() should distinguish aborting from entering an empty string. + local _canceled = vim.NIL + opts = vim.tbl_extend('keep', opts, { cancelreturn = _canceled }) + local input = vim.fn.input(opts) - if #input > 0 then - on_confirm(input) - else + if input == _canceled then on_confirm(nil) + else + on_confirm(input) end end |