mirror of
https://github.com/neovim/neovim.git
synced 2024-12-23 20:55:18 -07:00
fix(gx): visual selection, expand env vars
--- Rejected experiment: move vim.ui.open() to vim.env.open() Problem: `vim.ui` is where user-interface "providers" live, which can be overridden. It would also be useful to have a "providers" namespace for platform-specific features such as "open", clipboard, python, and the other providers listed in `:help providers`. We could overload `vim.ui` to serve that purpose as the single "providers" namespace, but `vim.ui.nodejs()` for example seems awkward. Solution: `vim.env` currently has too narrow of a purpose. Overload it to also be a namespace for `vim.env.open`. diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua index 913f1fe20348..17d05ff37595 100644 --- a/runtime/lua/vim/_meta.lua +++ b/runtime/lua/vim/_meta.lua @@ -37,8 +37,28 @@ local options_info = setmetatable({}, { end, }) -vim.env = setmetatable({}, { - __index = function(_, k) +vim.env = setmetatable({ + open = setmetatable({}, { + __call = function(_, uri) + print('xxxxx'..uri) + return true + end, + __tostring = function() + local v = vim.fn.getenv('open') + if v == vim.NIL then + return nil + end + return v + end, + }) + }, + { + __index = function(t, k, ...) + if k == 'open' then + error() + -- vim.print({...}) + -- return rawget(t, k) + end local v = vim.fn.getenv(k) if v == vim.NIL then return nil
This commit is contained in:
parent
af6e6ccf3d
commit
67b2ed1004
@ -2344,28 +2344,26 @@ input({opts}, {on_confirm}) *vim.ui.input()*
|
|||||||
entered), or `nil` if the user aborted the dialog.
|
entered), or `nil` if the user aborted the dialog.
|
||||||
|
|
||||||
open({path}) *vim.ui.open()*
|
open({path}) *vim.ui.open()*
|
||||||
Opens a path in the system's default handler. This function utilizes
|
Opens `path` with the system default handler (macOS `open`, Windows
|
||||||
`xdg-open`, `wslview`, `explorer`, or `open` commands depending on the
|
`explorer.exe`, Linux `xdg-open`, …), or shows a message on failure.
|
||||||
system to open the provided path.
|
|
||||||
|
|
||||||
Notifies the user if unsuccessful
|
Expands "~/" and environment variables in filesystem paths.
|
||||||
|
|
||||||
Example: >lua
|
Examples: >lua
|
||||||
|
|
||||||
vim.ui.open("https://neovim.io/")
|
vim.ui.open("https://neovim.io/")
|
||||||
|
vim.ui.open("~/path/to/file")
|
||||||
vim.ui.open("/path/to/file")
|
vim.ui.open("$VIMRUNTIME")
|
||||||
<
|
<
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {path} (string) Path to be opened
|
• {path} (string) Path or URL to open
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
SystemCompleted|nil result Result of command, if an appropriate one
|
SystemCompleted|nil result Command result, or nil if not found.
|
||||||
could be found.
|
|
||||||
|
|
||||||
See also: ~
|
See also: ~
|
||||||
• |vim.system|
|
• |vim.system()|
|
||||||
|
|
||||||
select({items}, {opts}, {on_choice}) *vim.ui.select()*
|
select({items}, {opts}, {on_choice}) *vim.ui.select()*
|
||||||
Prompts the user to pick from a list of items, allowing arbitrary
|
Prompts the user to pick from a list of items, allowing arbitrary
|
||||||
|
@ -105,10 +105,10 @@ The following new APIs and features were added.
|
|||||||
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint
|
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint
|
||||||
|
|
||||||
• Bundled treesitter parser and queries (highlight, folds) for Markdown,
|
• Bundled treesitter parser and queries (highlight, folds) for Markdown,
|
||||||
Python, and Bash.
|
Python, and Bash.
|
||||||
|
|
||||||
• |vim.ui.open()| opens URIs using the system default handler (macOS `open`,
|
• |vim.ui.open()| opens URIs using the system default handler (macOS `open`,
|
||||||
Windows `explorer`, Linux `xdg-open`, etc.)
|
Windows `explorer`, Linux `xdg-open`, etc.)
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
CHANGED FEATURES *news-changed*
|
CHANGED FEATURES *news-changed*
|
||||||
@ -146,8 +146,9 @@ The following changes to existing APIs or features add new behavior.
|
|||||||
|
|
||||||
• |:Man| now respects 'wrapmargin'
|
• |:Man| now respects 'wrapmargin'
|
||||||
|
|
||||||
• The |gx| command now uses |vim.ui.open()| and not netrw. Continue using
|
• |gx| now uses |vim.ui.open()| and not netrw. To customize, you can redefine
|
||||||
netrw with `vim.g.use_lua_gx = false`.
|
`vim.ui.open` or remap `gx`. To continue using netrw (deprecated): >vim
|
||||||
|
:call netrw#BrowseX(expand(exists("g:netrw_gx")? g:netrw_gx : '<cfile>'), netrw#CheckIfRemote())<CR>
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
REMOVED FEATURES *news-removed*
|
REMOVED FEATURES *news-removed*
|
||||||
|
@ -98,13 +98,14 @@ g8 Print the hex values of the bytes used in the
|
|||||||
command won't move the cursor.
|
command won't move the cursor.
|
||||||
|
|
||||||
*gx*
|
*gx*
|
||||||
gx Open the current path or URL under the cursor in the
|
gx Opens the current filepath or URL (decided by
|
||||||
system's default handler with |vim.ui.open|.
|
|<cfile>|, 'isfname') at cursor using the system
|
||||||
|
default handler, by calling |vim.ui.open()|.
|
||||||
|
|
||||||
|
*v_gx*
|
||||||
|
{Visual}gx Opens the selected text using the system default
|
||||||
|
handler, by calling |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*
|
*:p* *:pr* *:print* *E749*
|
||||||
:[range]p[rint] [flags]
|
:[range]p[rint] [flags]
|
||||||
Print [range] lines (default current line).
|
Print [range] lines (default current line).
|
||||||
|
@ -576,12 +576,12 @@ M['window/showDocument'] = function(_, result, ctx, _)
|
|||||||
|
|
||||||
local ret = vim.ui.open(uri)
|
local ret = vim.ui.open(uri)
|
||||||
|
|
||||||
if ret.code ~= 0 or ret == nil 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 'No handler could be found',
|
message = ret and ret.stderr or 'No handler found',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@ -593,7 +593,7 @@ M['window/showDocument'] = function(_, result, ctx, _)
|
|||||||
local client = vim.lsp.get_client_by_id(client_id)
|
local client = vim.lsp.get_client_by_id(client_id)
|
||||||
local client_name = client and client.name or string.format('id=%d', client_id)
|
local client_name = client and client.name or string.format('id=%d', client_id)
|
||||||
if not client then
|
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
|
return vim.NIL
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -104,69 +104,54 @@ function M.input(opts, on_confirm)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Opens a path in the system's default handler.
|
--- Opens `path` with the system default handler (macOS `open`, Windows `explorer.exe`, Linux
|
||||||
--- This function utilizes `xdg-open`, `wslview`, `explorer`, or `open` commands
|
--- `xdg-open`, …), or shows a message on failure.
|
||||||
--- depending on the system to open the provided path.
|
|
||||||
---
|
---
|
||||||
--- Notifies the user if unsuccessful
|
--- Expands "~/" and environment variables in filesystem paths.
|
||||||
---
|
---
|
||||||
---@param path string Path to be opened
|
--- Examples:
|
||||||
---
|
|
||||||
---@return SystemCompleted|nil result Result of command, if an appropriate one
|
|
||||||
---could be found.
|
|
||||||
---
|
|
||||||
---@see |vim.system|
|
|
||||||
---
|
|
||||||
--- Example:
|
|
||||||
--- <pre>lua
|
--- <pre>lua
|
||||||
--- vim.ui.open("https://neovim.io/")
|
--- vim.ui.open("https://neovim.io/")
|
||||||
---
|
--- vim.ui.open("~/path/to/file")
|
||||||
--- vim.ui.open("/path/to/file")
|
--- vim.ui.open("$VIMRUNTIME")
|
||||||
--- </pre>
|
--- </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)
|
function M.open(path)
|
||||||
if not path or path == '' then
|
vim.validate{
|
||||||
vim.notify('os_open: No path provided', vim.log.levels.ERROR)
|
path={path, 'string'}
|
||||||
return nil
|
}
|
||||||
|
local is_uri = path:match('%w+:')
|
||||||
|
if not is_uri then
|
||||||
|
path = vim.fn.expand(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
local cmd
|
local cmd
|
||||||
|
|
||||||
if vim.fn.has('macunix') == 1 then
|
if vim.fn.has('mac') == 1 then
|
||||||
cmd = { 'open', path }
|
cmd = { 'open', path }
|
||||||
elseif vim.fn.has('win32') == 1 then
|
elseif vim.fn.has('win32') == 1 then
|
||||||
cmd = { 'explorer', path }
|
cmd = { 'explorer', path }
|
||||||
else
|
elseif vim.fn.executable('wslview') == 1 then
|
||||||
if vim.fn.executable('wslview') == 1 then
|
|
||||||
cmd = { 'wslview', path }
|
cmd = { 'wslview', 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
|
||||||
vim.notify(
|
vim.notify('vim.ui.open: no handler found (tried: wslview, xdg-open)', vim.log.levels.ERROR)
|
||||||
'os_open: Could not find an appropriate command to use (Is xdg-open installed?)',
|
|
||||||
vim.log.levels.ERROR
|
|
||||||
)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
local ret = vim
|
return rv
|
||||||
.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
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
@ -19,10 +19,15 @@ vim.api.nvim_create_user_command('InspectTree', function(cmd)
|
|||||||
end
|
end
|
||||||
end, { desc = 'Inspect treesitter language tree for buffer', count = true })
|
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
|
-- TODO: use vim.region() when it lands... #13896 #16843
|
||||||
vim.keymap.set({ 'n', 'x' }, 'gx', function()
|
local function get_visual_selection()
|
||||||
local uri = vim.fn.expand('<cfile>')
|
local save_a = vim.fn.getreginfo('a')
|
||||||
|
vim.cmd[[norm! "ay]]
|
||||||
vim.ui.open(uri)
|
local selection = vim.fn.getreg('a', 1)
|
||||||
end, { desc = 'Open URI under cursor with system app' })
|
vim.fn.setreg('a', save_a)
|
||||||
|
return selection
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local gx_desc = 'Opens filepath or URI under cursor with the system handler (file explorer, web browser, …)'
|
||||||
|
vim.keymap.set({ 'n' }, 'gx', function() vim.ui.open(vim.fn.expand('<cfile>')) end, { desc = gx_desc })
|
||||||
|
vim.keymap.set({ 'x' }, 'gx', function() vim.ui.open(get_visual_selection()) end, { desc = gx_desc })
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
local helpers = require('test.functional.helpers')(after_each)
|
local helpers = require('test.functional.helpers')(after_each)
|
||||||
local eq = helpers.eq
|
local eq = helpers.eq
|
||||||
|
local matches = helpers.matches
|
||||||
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 feed = helpers.feed
|
||||||
@ -11,8 +12,7 @@ describe('vim.ui', function()
|
|||||||
clear()
|
clear()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('select()', function()
|
||||||
describe('select', function()
|
|
||||||
it('can select an item', function()
|
it('can select an item', function()
|
||||||
local result = exec_lua[[
|
local result = exec_lua[[
|
||||||
local items = {
|
local items = {
|
||||||
@ -47,7 +47,7 @@ describe('vim.ui', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('input', function()
|
describe('input()', function()
|
||||||
it('can input text', function()
|
it('can input text', function()
|
||||||
local result = exec_lua[[
|
local result = exec_lua[[
|
||||||
local opts = {
|
local opts = {
|
||||||
@ -130,4 +130,18 @@ describe('vim.ui', function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('open()', function()
|
||||||
|
it('validation', function()
|
||||||
|
exec_lua[[vim.ui.open('non-existent-file')]]
|
||||||
|
matches('vim.ui.open: command failed %(%d%): { "[^"]+", "non%-existent%-file" }', eval('v:errmsg'))
|
||||||
|
|
||||||
|
exec_lua[[
|
||||||
|
vim.fn.has = function() return 0 end
|
||||||
|
vim.fn.executable = function() return 0 end
|
||||||
|
]]
|
||||||
|
exec_lua[[vim.ui.open('foo')]]
|
||||||
|
eq('vim.ui.open: no handler found (tried: wslview, xdg-open)', eval('v:errmsg'))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user