diff options
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r-- | runtime/lua/vim/lsp/handlers.lua | 6 | ||||
-rw-r--r-- | runtime/lua/vim/ui.lua | 75 |
2 files changed, 33 insertions, 48 deletions
diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua index 9b102c0f84..79d3f7aab0 100644 --- a/runtime/lua/vim/lsp/handlers.lua +++ b/runtime/lua/vim/lsp/handlers.lua @@ -576,12 +576,12 @@ M['window/showDocument'] = function(_, result, ctx, _) local ret = vim.ui.open(uri) - if ret.code ~= 0 or ret == nil then + if ret == nil or ret.code ~= 0 then return { success = false, error = { code = protocol.ErrorCodes.UnknownErrorCode, - message = ret and ret.stderr or 'No handler could be found', + message = ret and ret.stderr or 'No handler found', }, } end @@ -593,7 +593,7 @@ M['window/showDocument'] = function(_, result, ctx, _) local client = vim.lsp.get_client_by_id(client_id) local client_name = client and client.name or string.format('id=%d', client_id) if not client then - err_message({ 'LSP[', client_name, '] client has shut down after sending ', ctx.method }) + err_message('LSP[', client_name, '] client has shut down after sending ', ctx.method) return vim.NIL end diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index 2200ee7bc3..3ffa329f74 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -104,69 +104,54 @@ function M.input(opts, on_confirm) end end ---- Opens a path in the system's default handler. ---- This function utilizes `xdg-open`, `wslview`, `explorer`, or `open` commands ---- depending on the system to open the provided path. +--- Opens `path` with the system default handler (macOS `open`, Windows `explorer.exe`, Linux +--- `xdg-open`, …), or shows a message on failure. --- ---- Notifies the user if unsuccessful +--- Expands "~/" and environment variables in filesystem paths. --- ----@param path string Path to be opened ---- ----@return SystemCompleted|nil result Result of command, if an appropriate one ----could be found. ---- ----@see |vim.system| ---- ---- Example: +--- Examples: --- <pre>lua --- vim.ui.open("https://neovim.io/") ---- ---- vim.ui.open("/path/to/file") +--- vim.ui.open("~/path/to/file") +--- vim.ui.open("$VIMRUNTIME") --- </pre> +--- +---@param path string Path or URL to open +--- +---@return SystemCompleted|nil result Command result, or nil if not found. +--- +---@see |vim.system()| function M.open(path) - if not path or path == '' then - vim.notify('os_open: No path provided', vim.log.levels.ERROR) - return nil + vim.validate{ + path={path, 'string'} + } + local is_uri = path:match('%w+:') + if not is_uri then + path = vim.fn.expand(path) end local cmd - if vim.fn.has('macunix') == 1 then + if vim.fn.has('mac') == 1 then cmd = { 'open', path } elseif vim.fn.has('win32') == 1 then cmd = { 'explorer', path } + elseif vim.fn.executable('wslview') == 1 then + cmd = { 'wslview', path } + elseif vim.fn.executable('xdg-open') == 1 then + cmd = { 'xdg-open', path } else - if vim.fn.executable('wslview') == 1 then - cmd = { 'wslview', path } - elseif vim.fn.executable('xdg-open') == 1 then - cmd = { 'xdg-open', path } - else - vim.notify( - 'os_open: Could not find an appropriate command to use (Is xdg-open installed?)', - vim.log.levels.ERROR - ) - - return nil - end + vim.notify('vim.ui.open: no handler found (tried: wslview, xdg-open)', vim.log.levels.ERROR) + return nil end - local ret = vim - .system(cmd, { - text = true, - detach = true, - }) - :wait() - - if ret.code ~= 0 then - local msg = { - 'Failed to open path', - ret, - vim.inspect(cmd), - } - vim.notify(table.concat(msg, '\n'), vim.log.levels.ERROR) + local rv = vim.system(cmd, { text = true, detach = true, }):wait() + if rv.code ~= 0 then + local msg = ('vim.ui.open: command failed (%d): %s'):format(rv.code, vim.inspect(cmd)) + vim.notify(msg, vim.log.levels.ERROR) end - return ret + return rv end return M |