From 6752f1005d26c93a033d856a60b7b296f3e51634 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Wed, 14 Dec 2022 19:58:18 +0100 Subject: docs: naming conventions, guidelines close #21063 --- runtime/lua/vim/ui.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'runtime/lua/vim/ui.lua') diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index 8f5be15221..aaee175f3a 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -1,6 +1,7 @@ local M = {} ---- Prompts the user to pick a single item from a collection of entries +--- Prompts the user to pick from a list of items, allowing arbitrary (potentially asynchronous) +--- work until `on_choice`. --- ---@param items table Arbitrary items ---@param opts table Additional options @@ -35,7 +36,6 @@ local M = {} --- end --- end) --- - function M.select(items, opts, on_choice) vim.validate({ items = { items, 'table', false }, @@ -55,7 +55,8 @@ function M.select(items, opts, on_choice) end end ---- Prompts the user for input +--- Prompts the user for input, allowing arbitrary (potentially asynchronous) work until +--- `on_confirm`. --- ---@param opts table Additional options. See |input()| --- - prompt (string|nil) -- cgit From af6e6ccf3dee815850639ec5613dda3442caa7d6 Mon Sep 17 00:00:00 2001 From: marshmallow Date: Sun, 30 Apr 2023 15:53:02 +1000 Subject: feat(vim.ui): vim.ui.open, "gx" without netrw MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mathias Fußenegger Co-authored-by: Justin M. Keyes Co-authored-by: ii14 <59243201+ii14@users.noreply.github.com> --- runtime/lua/vim/ui.lua | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'runtime/lua/vim/ui.lua') diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index aaee175f3a..2200ee7bc3 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -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: +---
lua
+--- vim.ui.open("https://neovim.io/")
+---
+--- vim.ui.open("/path/to/file")
+--- 
+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 -- cgit From 67b2ed1004ae551c9fe1bbd29a86b5a301570800 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 2 Jul 2023 16:51:30 +0200 Subject: 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 --- runtime/lua/vim/ui.lua | 75 ++++++++++++++++++++------------------------------ 1 file changed, 30 insertions(+), 45 deletions(-) (limited to 'runtime/lua/vim/ui.lua') 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: ---
lua
 --- vim.ui.open("https://neovim.io/")
----
---- vim.ui.open("/path/to/file")
+--- vim.ui.open("~/path/to/file")
+--- vim.ui.open("$VIMRUNTIME")
 --- 
+--- +---@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 -- cgit From e644e7ce0b36dd5e75770f3faa0a84f15e2561e8 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 4 Jul 2023 23:33:23 +0200 Subject: fix(vim.ui.open): return (don't show) error message Problem: Showing an error via vim.notify() makes it awkward for callers such as lsp/handlers.lua to avoid showing redundant errors. Solution: Return the message instead of showing it. Let the caller decide whether and when to show the message. --- runtime/lua/vim/ui.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'runtime/lua/vim/ui.lua') diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index 3ffa329f74..fd06611da2 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -105,7 +105,7 @@ function M.input(opts, on_confirm) end --- Opens `path` with the system default handler (macOS `open`, Windows `explorer.exe`, Linux ---- `xdg-open`, …), or shows a message on failure. +--- `xdg-open`, …), or returns (but does not show) an error message on failure. --- --- Expands "~/" and environment variables in filesystem paths. --- @@ -118,13 +118,14 @@ end --- ---@param path string Path or URL to open --- ----@return SystemCompleted|nil result Command result, or nil if not found. +---@return SystemCompleted|nil # Command result, or nil if not found. +---@return string|nil # Error message on failure --- ---@see |vim.system()| function M.open(path) - vim.validate{ - path={path, 'string'} - } + vim.validate({ + path = { path, 'string' }, + }) local is_uri = path:match('%w+:') if not is_uri then path = vim.fn.expand(path) @@ -141,17 +142,16 @@ function M.open(path) elseif vim.fn.executable('xdg-open') == 1 then cmd = { 'xdg-open', path } else - vim.notify('vim.ui.open: no handler found (tried: wslview, xdg-open)', vim.log.levels.ERROR) - return nil + return nil, 'vim.ui.open: no handler found (tried: wslview, xdg-open)' end - local rv = vim.system(cmd, { text = true, detach = true, }):wait() + 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) + return rv, msg end - return rv + return rv, nil end return M -- cgit From 519b9929e94c94965b73ac4b04aedb03fd2708ca Mon Sep 17 00:00:00 2001 From: marshmallow Date: Wed, 19 Jul 2023 10:06:20 +1000 Subject: fix(ui.open): some URLs fail on Windows Problem: On Windows, `explorer.exe` fails to open some URLs, for example: :lua vim.ui.open('https://devdocs.io/#q=lua%20lua_call') https://github.com/neovim/neovim/pull/23401#issuecomment-1641015704 Solution: Use rundll32 instead. --- runtime/lua/vim/ui.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/ui.lua') diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index fd06611da2..87b52787a0 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -136,7 +136,11 @@ function M.open(path) if vim.fn.has('mac') == 1 then cmd = { 'open', path } elseif vim.fn.has('win32') == 1 then - cmd = { 'explorer', path } + if vim.fn.executable('rundll32') == 1 then + cmd = { 'rundll32', 'url.dll,FileProtocolHandler', path } + else + return nil, 'vim.ui.open: rundll32 not found' + end elseif vim.fn.executable('wslview') == 1 then cmd = { 'wslview', path } elseif vim.fn.executable('xdg-open') == 1 then -- cgit From 80d1333b7317460c562a982ac21f900d9fbd89f6 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Mon, 4 Sep 2023 12:03:03 +0100 Subject: refactor(vim.system): factor out on_exit handling --- runtime/lua/vim/ui.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/ui.lua') diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index 87b52787a0..cd90886489 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -118,7 +118,7 @@ end --- ---@param path string Path or URL to open --- ----@return SystemCompleted|nil # Command result, or nil if not found. +---@return vim.SystemCompleted|nil # Command result, or nil if not found. ---@return string|nil # Error message on failure --- ---@see |vim.system()| -- cgit From 2e92065686f62851318150a315591c30b8306a4b Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 14 Sep 2023 08:23:01 -0500 Subject: docs: replace
 with ``` (#25136)

---
 runtime/lua/vim/ui.lua | 54 ++++++++++++++++++++++++++------------------------
 1 file changed, 28 insertions(+), 26 deletions(-)

(limited to 'runtime/lua/vim/ui.lua')

diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua
index cd90886489..b6ddf337ce 100644
--- a/runtime/lua/vim/ui.lua
+++ b/runtime/lua/vim/ui.lua
@@ -3,6 +3,23 @@ local M = {}
 --- Prompts the user to pick from a list of items, allowing arbitrary (potentially asynchronous)
 --- work until `on_choice`.
 ---
+--- Example:
+---
+--- ```lua
+--- vim.ui.select({ 'tabs', 'spaces' }, {
+---     prompt = 'Select tabs or spaces:',
+---     format_item = function(item)
+---         return "I'd like to choose " .. item
+---     end,
+--- }, function(choice)
+---     if choice == 'spaces' then
+---         vim.o.expandtab = true
+---     else
+---         vim.o.expandtab = false
+---     end
+--- end)
+--- ```
+---
 ---@param items table Arbitrary items
 ---@param opts table Additional options
 ---     - prompt (string|nil)
@@ -19,23 +36,6 @@ local M = {}
 ---               Called once the user made a choice.
 ---               `idx` is the 1-based index of `item` within `items`.
 ---               `nil` if the user aborted the dialog.
----
----
---- Example:
---- 
lua
---- vim.ui.select({ 'tabs', 'spaces' }, {
----     prompt = 'Select tabs or spaces:',
----     format_item = function(item)
----         return "I'd like to choose " .. item
----     end,
---- }, function(choice)
----     if choice == 'spaces' then
----         vim.o.expandtab = true
----     else
----         vim.o.expandtab = false
----     end
---- end)
---- 
function M.select(items, opts, on_choice) vim.validate({ items = { items, 'table', false }, @@ -58,6 +58,14 @@ end --- Prompts the user for input, allowing arbitrary (potentially asynchronous) work until --- `on_confirm`. --- +--- Example: +--- +--- ```lua +--- vim.ui.input({ prompt = 'Enter value for shiftwidth: ' }, function(input) +--- vim.o.shiftwidth = tonumber(input) +--- end) +--- ``` +--- ---@param opts table Additional options. See |input()| --- - prompt (string|nil) --- Text of the prompt @@ -77,13 +85,6 @@ end --- `input` is what the user typed (it might be --- an empty string if nothing was entered), or --- `nil` if the user aborted the dialog. ---- ---- Example: ----
lua
---- vim.ui.input({ prompt = 'Enter value for shiftwidth: ' }, function(input)
----     vim.o.shiftwidth = tonumber(input)
---- end)
---- 
function M.input(opts, on_confirm) vim.validate({ on_confirm = { on_confirm, 'function', false }, @@ -110,11 +111,12 @@ end --- Expands "~/" and environment variables in filesystem paths. --- --- Examples: ----
lua
+---
+--- ```lua
 --- vim.ui.open("https://neovim.io/")
 --- vim.ui.open("~/path/to/file")
 --- vim.ui.open("$VIMRUNTIME")
---- 
+--- ``` --- ---@param path string Path or URL to open --- -- cgit