fix(vim.ui.input): accept nil or empty "opts" #19109

Fix #18143
This commit is contained in:
0x74696d6d79
2022-06-28 05:53:15 -04:00
committed by GitHub
parent 014a88799a
commit ee6b21e843
3 changed files with 19 additions and 4 deletions

View File

@@ -1935,8 +1935,7 @@ input({opts}, {on_confirm}) *vim.ui.input()*
Parameters: ~ Parameters: ~
{opts} (table) Additional options. See |input()| {opts} (table) Additional options. See |input()|
• prompt (string|nil) Text of the prompt. • prompt (string|nil) Text of the prompt
Defaults to `Input:`.
• default (string|nil) Default reply to the • default (string|nil) Default reply to the
input input
• completion (string|nil) Specifies type of • completion (string|nil) Specifies type of

View File

@@ -59,7 +59,7 @@ end
--- ---
---@param opts table Additional options. See |input()| ---@param opts table Additional options. See |input()|
--- - prompt (string|nil) --- - prompt (string|nil)
--- Text of the prompt. Defaults to `Input: `. --- Text of the prompt
--- - default (string|nil) --- - default (string|nil)
--- Default reply to the input --- Default reply to the input
--- - completion (string|nil) --- - completion (string|nil)
@@ -87,7 +87,7 @@ function M.input(opts, on_confirm)
on_confirm = { on_confirm, 'function', false }, on_confirm = { on_confirm, 'function', false },
}) })
opts = opts or {} opts = (opts and not vim.tbl_isempty(opts)) and opts or vim.empty_dict()
local input = vim.fn.input(opts) local input = vim.fn.input(opts)
if #input > 0 then if #input > 0 then
on_confirm(input) on_confirm(input)

View File

@@ -2,6 +2,8 @@ local helpers = require('test.functional.helpers')(after_each)
local eq = helpers.eq local eq = helpers.eq
local exec_lua = helpers.exec_lua local exec_lua = helpers.exec_lua
local clear = helpers.clear local clear = helpers.clear
local feed = helpers.feed
local eval = helpers.eval
describe('vim.ui', function() describe('vim.ui', function()
before_each(function() before_each(function()
@@ -67,5 +69,19 @@ describe('vim.ui', function()
eq('Inputted text', result[1]) eq('Inputted text', result[1])
eq('Input: ', result[2]) eq('Input: ', result[2])
end) end)
it('can input text on nil opt', function()
feed(':lua vim.ui.input(nil, function(input) result = input end)<cr>')
eq('', eval('v:errmsg'))
feed('Inputted text<cr>')
eq('Inputted text', exec_lua('return result'))
end)
it('can input text on {} opt', function()
feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
eq('', eval('v:errmsg'))
feed('abcdefg<cr>')
eq('abcdefg', exec_lua('return result'))
end)
end) end)
end) end)