feat(vim.ui): vim.ui.open, "gx" without netrw

Co-authored-by: Mathias Fußenegger <mfussenegger@users.noreply.github.com>
Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
Co-authored-by: ii14 <59243201+ii14@users.noreply.github.com>
This commit is contained in:
marshmallow 2023-04-30 15:53:02 +10:00 committed by Justin M. Keyes
parent 3ecd45ded0
commit af6e6ccf3d
6 changed files with 115 additions and 11 deletions

View File

@ -2343,6 +2343,30 @@ input({opts}, {on_confirm}) *vim.ui.input()*
typed (it might be an empty string if nothing was
entered), or `nil` if the user aborted the dialog.
open({path}) *vim.ui.open()*
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.
Notifies the user if unsuccessful
Example: >lua
vim.ui.open("https://neovim.io/")
vim.ui.open("/path/to/file")
<
Parameters: ~
• {path} (string) Path to be opened
Return: ~
SystemCompleted|nil result Result of command, if an appropriate one
could be found.
See also: ~
• |vim.system|
select({items}, {opts}, {on_choice}) *vim.ui.select()*
Prompts the user to pick from a list of items, allowing arbitrary
(potentially asynchronous) work until `on_choice`.

View File

@ -107,6 +107,9 @@ The following new APIs and features were added.
• Bundled treesitter parser and queries (highlight, folds) for Markdown,
Python, and Bash.
• |vim.ui.open()| opens URIs using the system default handler (macOS `open`,
Windows `explorer`, Linux `xdg-open`, etc.)
==============================================================================
CHANGED FEATURES *news-changed*
@ -143,6 +146,9 @@ The following changes to existing APIs or features add new behavior.
• |:Man| now respects 'wrapmargin'
• The |gx| command now uses |vim.ui.open()| and not netrw. Continue using
netrw with `vim.g.use_lua_gx = false`.
==============================================================================
REMOVED FEATURES *news-removed*

View File

@ -97,6 +97,14 @@ g8 Print the hex values of the bytes used in the
cursor is halfway through a multibyte character the
command won't move the cursor.
*gx*
gx Open the current path or URL under the cursor in the
system's default handler with |vim.ui.open|.
To use the netrw keymap, set `use_lua_gx` to false:
>lua
vim.g.use_lua_gx = false
<
*:p* *:pr* *:print* *E749*
:[range]p[rint] [flags]
Print [range] lines (default current line).

View File

@ -573,22 +573,15 @@ M['window/showDocument'] = function(_, result, ctx, _)
if result.external then
-- TODO(lvimuser): ask the user for confirmation
local cmd
if vim.fn.has('win32') == 1 then
cmd = { 'cmd.exe', '/c', 'start', '""', uri }
elseif vim.fn.has('macunix') == 1 then
cmd = { 'open', uri }
else
cmd = { 'xdg-open', uri }
end
local ret = vim.fn.system(cmd)
if vim.v.shell_error ~= 0 then
local ret = vim.ui.open(uri)
if ret.code ~= 0 or ret == nil then
return {
success = false,
error = {
code = protocol.ErrorCodes.UnknownErrorCode,
message = ret,
message = ret and ret.stderr or 'No handler could be found',
},
}
end

View File

@ -104,4 +104,69 @@ 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.
---
--- Notifies the user if unsuccessful
---
---@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:
--- <pre>lua
--- vim.ui.open("https://neovim.io/")
---
--- vim.ui.open("/path/to/file")
--- </pre>
function M.open(path)
if not path or path == '' then
vim.notify('os_open: No path provided', vim.log.levels.ERROR)
return nil
end
local cmd
if vim.fn.has('macunix') == 1 then
cmd = { 'open', path }
elseif vim.fn.has('win32') == 1 then
cmd = { 'explorer', 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
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)
end
return ret
end
return M

View File

@ -18,3 +18,11 @@ vim.api.nvim_create_user_command('InspectTree', function(cmd)
vim.treesitter.inspect_tree()
end
end, { desc = 'Inspect treesitter language tree for buffer', count = true })
if vim.g.use_lua_gx == nil or vim.g.use_lua_gx == true then
vim.keymap.set({ 'n', 'x' }, 'gx', function()
local uri = vim.fn.expand('<cfile>')
vim.ui.open(uri)
end, { desc = 'Open URI under cursor with system app' })
end