fix(vim.ui)!: change open() to return pcall-like values #28502

Problem:
`vim.ui.open` unnecessarily invents a different success/failure
convention. Its return type was changed in 57adf8c6e0, so we might as
well change it to have a more conventional form.

Solution:
Change the signature to use the `pcall` convention of `status, result`.
This commit is contained in:
Justin M. Keyes 2024-04-25 04:15:58 -07:00 committed by GitHub
parent a1c2da56ec
commit e0d92b9cc2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 25 additions and 22 deletions

View File

@ -2560,8 +2560,8 @@ vim.ui.open({path}) *vim.ui.open()*
vim.ui.open("https://neovim.io/") vim.ui.open("https://neovim.io/")
vim.ui.open("~/path/to/file") vim.ui.open("~/path/to/file")
-- Synchronous (wait until the process exits). -- Synchronous (wait until the process exits).
local cmd, err = vim.ui.open("$VIMRUNTIME") local ok, cmd = vim.ui.open("$VIMRUNTIME")
if cmd then if ok then
cmd:wait() cmd:wait()
end end
< <
@ -2570,8 +2570,8 @@ vim.ui.open({path}) *vim.ui.open()*
• {path} (`string`) Path or URL to open • {path} (`string`) Path or URL to open
Return (multiple): ~ Return (multiple): ~
(`vim.SystemObj?`) Command object, or nil if not found. (`boolean`) false if command not found, else true.
(`string?`) Error message on failure (`vim.SystemObj|string`) Command object, or error message on failure
See also: ~ See also: ~
• |vim.system()| • |vim.system()|

View File

@ -157,6 +157,8 @@ unreleased features on Nvim HEAD.
• Renamed vim.tbl_isarray() to vim.isarray(). • Renamed vim.tbl_isarray() to vim.isarray().
• Changed |vim.ui.open()| return-signature to match pcall() convention.
============================================================================== ==============================================================================
NEW FEATURES *news-features* NEW FEATURES *news-features*

View File

@ -98,18 +98,19 @@ do
--- Map |gx| to call |vim.ui.open| on the <cfile> at cursor. --- Map |gx| to call |vim.ui.open| on the <cfile> at cursor.
do do
local function do_open(uri) local function do_open(uri)
local cmd, err = vim.ui.open(uri) local ok, cmd_or_err = vim.ui.open(uri)
local rv = cmd and cmd:wait(1000) or nil local rv = ok and (cmd_or_err --[[@as vim.SystemObj]]):wait(1000) or nil
if cmd and rv and rv.code ~= 0 then if rv and rv.code ~= 0 then
err = ('vim.ui.open: command %s (%d): %s'):format( ok = false
cmd_or_err = ('vim.ui.open: command %s (%d): %s'):format(
(rv.code == 124 and 'timeout' or 'failed'), (rv.code == 124 and 'timeout' or 'failed'),
rv.code, rv.code,
vim.inspect(cmd.cmd) vim.inspect(cmd_or_err.cmd)
) )
end end
if err then if not ok then
vim.notify(err, vim.log.levels.ERROR) vim.notify(cmd_or_err --[[@as string]], vim.log.levels.ERROR)
end end
end end

View File

@ -654,15 +654,15 @@ M[ms.window_showDocument] = function(_, result, ctx, _)
if result.external then if result.external then
-- TODO(lvimuser): ask the user for confirmation -- TODO(lvimuser): ask the user for confirmation
local cmd, err = vim.ui.open(uri) local ok, cmd_or_err = vim.ui.open(uri)
local ret = cmd and cmd:wait(2000) or nil local ret = ok and (cmd_or_err --[[@as vim.SystemObj]]):wait(2000) or nil
if ret == nil or ret.code ~= 0 then if ret == nil or ret.code ~= 0 then
return { return {
success = false, success = false,
error = { error = {
code = protocol.ErrorCodes.UnknownErrorCode, code = protocol.ErrorCodes.UnknownErrorCode,
message = ret and ret.stderr or err, message = ret and ret.stderr or cmd_or_err,
}, },
} }
end end

View File

@ -118,16 +118,16 @@ end
--- vim.ui.open("https://neovim.io/") --- vim.ui.open("https://neovim.io/")
--- vim.ui.open("~/path/to/file") --- vim.ui.open("~/path/to/file")
--- -- Synchronous (wait until the process exits). --- -- Synchronous (wait until the process exits).
--- local cmd, err = vim.ui.open("$VIMRUNTIME") --- local ok, cmd = vim.ui.open("$VIMRUNTIME")
--- if cmd then --- if ok then
--- cmd:wait() --- cmd:wait()
--- end --- end
--- ``` --- ```
--- ---
---@param path string Path or URL to open ---@param path string Path or URL to open
--- ---
---@return vim.SystemObj|nil # Command object, or nil if not found. ---@return boolean # false if command not found, else true.
---@return string|nil # Error message on failure ---@return vim.SystemObj|string # Command object, or error message on failure
--- ---
---@see |vim.system()| ---@see |vim.system()|
function M.open(path) function M.open(path)
@ -147,7 +147,7 @@ function M.open(path)
if vim.fn.executable('rundll32') == 1 then if vim.fn.executable('rundll32') == 1 then
cmd = { 'rundll32', 'url.dll,FileProtocolHandler', path } cmd = { 'rundll32', 'url.dll,FileProtocolHandler', path }
else else
return nil, 'vim.ui.open: rundll32 not found' return false, 'vim.ui.open: rundll32 not found'
end end
elseif vim.fn.executable('wslview') == 1 then elseif vim.fn.executable('wslview') == 1 then
cmd = { 'wslview', path } cmd = { 'wslview', path }
@ -156,10 +156,10 @@ function M.open(path)
elseif vim.fn.executable('xdg-open') == 1 then elseif vim.fn.executable('xdg-open') == 1 then
cmd = { 'xdg-open', path } cmd = { 'xdg-open', path }
else else
return nil, 'vim.ui.open: no handler found (tried: wslview, explorer.exe, xdg-open)' return false, 'vim.ui.open: no handler found (tried: wslview, explorer.exe, xdg-open)'
end end
return vim.system(cmd, { text = true, detach = true }), nil return true, vim.system(cmd, { text = true, detach = true })
end end
return M return M

View File

@ -144,7 +144,7 @@ describe('vim.ui', function()
end end
if not is_os('bsd') then if not is_os('bsd') then
local rv = local rv =
exec_lua [[local cmd = vim.ui.open('non-existent-file'); return cmd:wait(100).code]] exec_lua [[local _, cmd = vim.ui.open('non-existent-file'); return cmd:wait(100).code]]
ok(type(rv) == 'number' and rv ~= 0, 'nonzero exit code', rv) ok(type(rv) == 'number' and rv ~= 0, 'nonzero exit code', rv)
end end