2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
2024-04-15 04:33:09 -07:00
|
|
|
local ok = t.ok
|
2024-04-20 08:44:13 -07:00
|
|
|
local exec_lua = n.exec_lua
|
|
|
|
local clear = n.clear
|
|
|
|
local feed = n.feed
|
|
|
|
local eval = n.eval
|
2024-04-08 02:03:20 -07:00
|
|
|
local is_ci = t.is_ci
|
|
|
|
local is_os = t.is_os
|
2024-04-20 08:44:13 -07:00
|
|
|
local poke_eventloop = n.poke_eventloop
|
2021-09-27 12:57:28 -07:00
|
|
|
|
|
|
|
describe('vim.ui', function()
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
end)
|
|
|
|
|
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
2023-07-02 07:51:30 -07:00
|
|
|
describe('select()', function()
|
2021-09-27 12:57:28 -07:00
|
|
|
it('can select an item', function()
|
|
|
|
local result = exec_lua [[
|
|
|
|
local items = {
|
|
|
|
{ name = 'Item 1' },
|
|
|
|
{ name = 'Item 2' },
|
|
|
|
}
|
|
|
|
local opts = {
|
2021-09-27 16:12:03 -07:00
|
|
|
format_item = function(entry)
|
2021-09-27 12:57:28 -07:00
|
|
|
return entry.name
|
|
|
|
end
|
|
|
|
}
|
|
|
|
local selected
|
|
|
|
local cb = function(item)
|
|
|
|
selected = item
|
|
|
|
end
|
|
|
|
-- inputlist would require input and block the test;
|
|
|
|
local choices
|
|
|
|
vim.fn.inputlist = function(x)
|
|
|
|
choices = x
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
vim.ui.select(items, opts, cb)
|
|
|
|
vim.wait(100, function() return selected ~= nil end)
|
|
|
|
return {selected, choices}
|
|
|
|
]]
|
|
|
|
eq({ name = 'Item 1' }, result[1])
|
|
|
|
eq({
|
|
|
|
'Select one of:',
|
|
|
|
'1: Item 1',
|
|
|
|
'2: Item 2',
|
|
|
|
}, result[2])
|
|
|
|
end)
|
|
|
|
end)
|
2021-11-07 08:13:53 -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
2023-07-02 07:51:30 -07:00
|
|
|
describe('input()', function()
|
2021-11-07 08:13:53 -07:00
|
|
|
it('can input text', function()
|
|
|
|
local result = exec_lua [[
|
|
|
|
local opts = {
|
|
|
|
prompt = 'Input: ',
|
|
|
|
}
|
|
|
|
local input
|
|
|
|
local cb = function(item)
|
|
|
|
input = item
|
|
|
|
end
|
|
|
|
-- input would require input and block the test;
|
|
|
|
local prompt
|
|
|
|
vim.fn.input = function(opts)
|
|
|
|
prompt = opts.prompt
|
|
|
|
return "Inputted text"
|
|
|
|
end
|
|
|
|
vim.ui.input(opts, cb)
|
|
|
|
vim.wait(100, function() return input ~= nil end)
|
|
|
|
return {input, prompt}
|
|
|
|
]]
|
|
|
|
eq('Inputted text', result[1])
|
|
|
|
eq('Input: ', result[2])
|
|
|
|
end)
|
2022-06-28 02:53:15 -07:00
|
|
|
|
|
|
|
it('can input text on nil opt', function()
|
|
|
|
feed(':lua vim.ui.input(nil, function(input) result = input end)<cr>')
|
|
|
|
eq('', eval('v:errmsg'))
|
|
|
|
feed('Inputted text<cr>')
|
|
|
|
eq('Inputted text', exec_lua('return result'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can input text on {} opt', function()
|
|
|
|
feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
|
|
|
|
eq('', eval('v:errmsg'))
|
|
|
|
feed('abcdefg<cr>')
|
|
|
|
eq('abcdefg', exec_lua('return result'))
|
|
|
|
end)
|
2022-11-07 17:15:15 -07:00
|
|
|
|
|
|
|
it('can input empty text #18144', function()
|
|
|
|
feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
|
|
|
|
feed('<cr>')
|
|
|
|
eq('', exec_lua('return result'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can input empty text with cancelreturn opt #18144', function()
|
|
|
|
feed(':lua vim.ui.input({ cancelreturn = "CANCEL" }, function(input) result = input end)<cr>')
|
|
|
|
feed('<cr>')
|
|
|
|
eq('', exec_lua('return result'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can return nil when aborted with ESC #18144', function()
|
|
|
|
feed(':lua result = "on_confirm not called"<cr>')
|
|
|
|
feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
|
|
|
|
feed('Inputted Text<esc>')
|
|
|
|
-- Note: When `result == nil`, exec_lua('returns result') returns vim.NIL
|
|
|
|
eq(true, exec_lua('return (nil == result)'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can return opts.cacelreturn when aborted with ESC with cancelreturn opt #18144', function()
|
|
|
|
feed(':lua result = "on_confirm not called"<cr>')
|
|
|
|
feed(':lua vim.ui.input({ cancelreturn = "CANCEL" }, function(input) result = input end)<cr>')
|
|
|
|
feed('Inputted Text<esc>')
|
|
|
|
eq('CANCEL', exec_lua('return result'))
|
|
|
|
end)
|
|
|
|
|
fix: vim.ui.input always calls callback #21006
Followup to #20883
Related: #18144
This patch changes the behavior of the default `vim.ui.input` when the user
aborts with `<C-c>`. Currently, it produces an error message + stack and causes
`on_confirm` to not be called. With this patch, `<C-c>` will cause `on_confirm`
to be called with `nil`, the same behavior as when the user aborts with `<Esc>`.
I can think of three good reasons why the behavior should be this way:
1. Easier for the user to understand** It's not intuitive for there to be two
ways to abort an input dialog that have _different_ outcomes. As a user,
I would expect any action that cancels the input to leave me in the same
state. As a plugin author, I see no value in having two possible outcomes for
aborting the input. I have to handle both cases, but I can't think of
a situation where I would want to treat one differently than the other.
2. Provides an API that can be overridden by other implementations** The current
contract of "throw an error upon `<C-c>`" cannot be replicated by async
implementations of `vim.ui.input`. If the callsite wants to handle the case
of the user hitting `<C-c>` they need to use `pcall(vim.ui.input, ...)`,
however an async implementation will instantly return and so there will be no
way for it to produce the same error-throwing behavior when the user inputs
`<C-c>`. This makes it impossible to be fully API-compatible with the
built-in `vim.ui.input`.
3. Provides a useful guarantee to the callsite** As a plugin author, I want the
guarantee that `on_confirm` will _always_ be called (only catastrophic errors
should prevent this). If I am in the middle of some async thread of logic,
I need some way to resume that logic after handing off control to
`vim.ui.input`. The only way to handle the `<C-c>` case is with `pcall`,
which as already mentioned, breaks down if you're using an alternative
implementation.
2022-11-12 07:57:35 -07:00
|
|
|
it('can return nil when interrupted with Ctrl-C #18144', function()
|
2022-11-07 17:15:15 -07:00
|
|
|
feed(':lua result = "on_confirm not called"<cr>')
|
|
|
|
feed(':lua vim.ui.input({}, function(input) result = input end)<cr>')
|
2022-11-12 17:16:06 -07:00
|
|
|
poke_eventloop() -- This is needed because Ctrl-C flushes input
|
2022-11-07 17:15:15 -07:00
|
|
|
feed('Inputted Text<c-c>')
|
fix: vim.ui.input always calls callback #21006
Followup to #20883
Related: #18144
This patch changes the behavior of the default `vim.ui.input` when the user
aborts with `<C-c>`. Currently, it produces an error message + stack and causes
`on_confirm` to not be called. With this patch, `<C-c>` will cause `on_confirm`
to be called with `nil`, the same behavior as when the user aborts with `<Esc>`.
I can think of three good reasons why the behavior should be this way:
1. Easier for the user to understand** It's not intuitive for there to be two
ways to abort an input dialog that have _different_ outcomes. As a user,
I would expect any action that cancels the input to leave me in the same
state. As a plugin author, I see no value in having two possible outcomes for
aborting the input. I have to handle both cases, but I can't think of
a situation where I would want to treat one differently than the other.
2. Provides an API that can be overridden by other implementations** The current
contract of "throw an error upon `<C-c>`" cannot be replicated by async
implementations of `vim.ui.input`. If the callsite wants to handle the case
of the user hitting `<C-c>` they need to use `pcall(vim.ui.input, ...)`,
however an async implementation will instantly return and so there will be no
way for it to produce the same error-throwing behavior when the user inputs
`<C-c>`. This makes it impossible to be fully API-compatible with the
built-in `vim.ui.input`.
3. Provides a useful guarantee to the callsite** As a plugin author, I want the
guarantee that `on_confirm` will _always_ be called (only catastrophic errors
should prevent this). If I am in the middle of some async thread of logic,
I need some way to resume that logic after handing off control to
`vim.ui.input`. The only way to handle the `<C-c>` case is with `pcall`,
which as already mentioned, breaks down if you're using an alternative
implementation.
2022-11-12 07:57:35 -07:00
|
|
|
eq(true, exec_lua('return (nil == result)'))
|
2022-11-07 17:15:15 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it(
|
|
|
|
'can return the identical object when an arbitrary opts.cancelreturn object is given',
|
|
|
|
function()
|
|
|
|
feed(':lua fn = function() return 42 end<CR>')
|
|
|
|
eq(42, exec_lua('return fn()'))
|
|
|
|
feed(':lua vim.ui.input({ cancelreturn = fn }, function(input) result = input end)<cr>')
|
|
|
|
feed('cancel<esc>')
|
|
|
|
eq(true, exec_lua('return (result == fn)'))
|
|
|
|
eq(42, exec_lua('return result()'))
|
|
|
|
end
|
|
|
|
)
|
2021-11-07 08:13:53 -07:00
|
|
|
end)
|
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
2023-07-02 07:51:30 -07:00
|
|
|
|
|
|
|
describe('open()', function()
|
|
|
|
it('validation', function()
|
2023-07-21 04:34:38 -07:00
|
|
|
if is_os('win') or not is_ci('github') then
|
2024-04-15 04:33:09 -07:00
|
|
|
exec_lua [[vim.system = function() return { wait=function() return { code=3 } end } end]]
|
2023-07-21 04:34:38 -07:00
|
|
|
end
|
|
|
|
if not is_os('bsd') then
|
2024-04-15 04:33:09 -07:00
|
|
|
local rv =
|
2024-05-03 03:20:03 -07:00
|
|
|
exec_lua [[local cmd = vim.ui.open('non-existent-file'); return cmd:wait(100).code]]
|
2024-04-15 04:33:09 -07:00
|
|
|
ok(type(rv) == 'number' and rv ~= 0, 'nonzero exit code', rv)
|
2023-07-04 14:33:23 -07:00
|
|
|
end
|
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
2023-07-02 07:51:30 -07:00
|
|
|
|
|
|
|
exec_lua [[
|
|
|
|
vim.fn.has = function() return 0 end
|
|
|
|
vim.fn.executable = function() return 0 end
|
|
|
|
]]
|
2023-07-04 14:33:23 -07:00
|
|
|
eq(
|
2024-10-18 02:22:15 -07:00
|
|
|
'vim.ui.open: no handler found (tried: wslview, explorer.exe, xdg-open, lemonade)',
|
2023-07-04 14:33:23 -07:00
|
|
|
exec_lua [[local _, err = vim.ui.open('foo') ; return err]]
|
|
|
|
)
|
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
2023-07-02 07:51:30 -07:00
|
|
|
end)
|
2024-09-16 02:44:56 -07:00
|
|
|
|
|
|
|
it('opt.cmd #29490', function()
|
|
|
|
t.matches(
|
|
|
|
'ENOENT: no such file or directory',
|
|
|
|
t.pcall_err(exec_lua, function()
|
|
|
|
vim.ui.open('foo', { cmd = { 'non-existent-tool' } })
|
|
|
|
end)
|
|
|
|
)
|
|
|
|
|
|
|
|
eq(
|
|
|
|
{
|
|
|
|
code = 0,
|
|
|
|
signal = 0,
|
|
|
|
stderr = '',
|
|
|
|
stdout = 'arg1=arg1;arg2=https://example.com;',
|
|
|
|
},
|
|
|
|
exec_lua(function(cmd_)
|
|
|
|
local cmd, err = vim.ui.open('https://example.com', { cmd = cmd_ })
|
|
|
|
assert(cmd and not err)
|
|
|
|
return cmd:wait()
|
|
|
|
end, { n.testprg('printargs-test'), 'arg1' })
|
|
|
|
)
|
|
|
|
end)
|
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
2023-07-02 07:51:30 -07:00
|
|
|
end)
|
2021-09-27 12:57:28 -07:00
|
|
|
end)
|