mirror of
https://github.com/neovim/neovim.git
synced 2026-07-14 05:10:36 +00:00
Merge pull request #16752 from gpanders/lua-user-commands
feat(api): implement nvim_{add,del}_user_command
This commit is contained in:
@@ -6,8 +6,14 @@ local command = helpers.command
|
||||
local curbufmeths = helpers.curbufmeths
|
||||
local eq = helpers.eq
|
||||
local meths = helpers.meths
|
||||
local bufmeths = helpers.bufmeths
|
||||
local matches = helpers.matches
|
||||
local source = helpers.source
|
||||
local pcall_err = helpers.pcall_err
|
||||
local exec_lua = helpers.exec_lua
|
||||
local assert_alive = helpers.assert_alive
|
||||
local feed = helpers.feed
|
||||
local funcs = helpers.funcs
|
||||
|
||||
describe('nvim_get_commands', function()
|
||||
local cmd_dict = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='echo "Hello World"', name='Hello', nargs='1', range=NIL, register=false, script_id=0, }
|
||||
@@ -78,3 +84,118 @@ describe('nvim_get_commands', function()
|
||||
eq({Cmd2=cmd2, Cmd3=cmd3, Cmd4=cmd4, Finger=cmd1, TestCmd=cmd0}, meths.get_commands({builtin=false}))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('nvim_add_user_command', function()
|
||||
before_each(clear)
|
||||
|
||||
it('works with strings', function()
|
||||
meths.add_user_command('SomeCommand', 'let g:command_fired = <args>', {nargs = 1})
|
||||
meths.command('SomeCommand 42')
|
||||
eq(42, meths.eval('g:command_fired'))
|
||||
end)
|
||||
|
||||
it('works with Lua functions', function()
|
||||
exec_lua [[
|
||||
result = {}
|
||||
vim.api.nvim_add_user_command('CommandWithLuaCallback', function(opts)
|
||||
result = opts
|
||||
end, {
|
||||
nargs = "*",
|
||||
bang = true,
|
||||
count = 2,
|
||||
})
|
||||
]]
|
||||
|
||||
eq({
|
||||
args = "hello",
|
||||
bang = false,
|
||||
line1 = 1,
|
||||
line2 = 1,
|
||||
mods = "",
|
||||
range = 0,
|
||||
count = 2,
|
||||
reg = "",
|
||||
}, exec_lua [[
|
||||
vim.api.nvim_command('CommandWithLuaCallback hello')
|
||||
return result
|
||||
]])
|
||||
|
||||
eq({
|
||||
args = "",
|
||||
bang = true,
|
||||
line1 = 10,
|
||||
line2 = 10,
|
||||
mods = "botright",
|
||||
range = 1,
|
||||
count = 10,
|
||||
reg = "",
|
||||
}, exec_lua [[
|
||||
vim.api.nvim_command('botright 10CommandWithLuaCallback!')
|
||||
return result
|
||||
]])
|
||||
|
||||
eq({
|
||||
args = "",
|
||||
bang = false,
|
||||
line1 = 1,
|
||||
line2 = 42,
|
||||
mods = "",
|
||||
range = 1,
|
||||
count = 42,
|
||||
reg = "",
|
||||
}, exec_lua [[
|
||||
vim.api.nvim_command('CommandWithLuaCallback 42')
|
||||
return result
|
||||
]])
|
||||
end)
|
||||
|
||||
it('can define buffer-local commands', function()
|
||||
local bufnr = meths.create_buf(false, false)
|
||||
bufmeths.add_user_command(bufnr, "Hello", "", {})
|
||||
matches("Not an editor command: Hello", pcall_err(meths.command, "Hello"))
|
||||
meths.set_current_buf(bufnr)
|
||||
meths.command("Hello")
|
||||
assert_alive()
|
||||
end)
|
||||
|
||||
it('can use a Lua complete function', function()
|
||||
exec_lua [[
|
||||
vim.api.nvim_add_user_command('Test', '', {
|
||||
nargs = "*",
|
||||
complete = function(arg, cmdline, pos)
|
||||
local options = {"aaa", "bbb", "ccc"}
|
||||
local t = {}
|
||||
for _, v in ipairs(options) do
|
||||
if string.find(v, "^" .. arg) then
|
||||
table.insert(t, v)
|
||||
end
|
||||
end
|
||||
return t
|
||||
end,
|
||||
})
|
||||
]]
|
||||
|
||||
feed(':Test a<Tab>')
|
||||
eq('Test aaa', funcs.getcmdline())
|
||||
feed('<C-U>Test b<Tab>')
|
||||
eq('Test bbb', funcs.getcmdline())
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('nvim_del_user_command', function()
|
||||
before_each(clear)
|
||||
|
||||
it('can delete global commands', function()
|
||||
meths.add_user_command('Hello', 'echo "Hi"', {})
|
||||
meths.command('Hello')
|
||||
meths.del_user_command('Hello')
|
||||
matches("Not an editor command: Hello", pcall_err(meths.command, "Hello"))
|
||||
end)
|
||||
|
||||
it('can delete buffer-local commands', function()
|
||||
bufmeths.add_user_command(0, 'Hello', 'echo "Hi"', {})
|
||||
meths.command('Hello')
|
||||
bufmeths.del_user_command(0, 'Hello')
|
||||
matches("Not an editor command: Hello", pcall_err(meths.command, "Hello"))
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -8,6 +8,7 @@ local clear = helpers.clear
|
||||
local eval = helpers.eval
|
||||
local NIL = helpers.NIL
|
||||
local eq = helpers.eq
|
||||
local exec_lua = helpers.exec_lua
|
||||
|
||||
before_each(clear)
|
||||
|
||||
@@ -111,6 +112,12 @@ describe('luaeval(vim.api.…)', function()
|
||||
eq(7, eval([[type(luaeval('vim.api.nvim__id(nil)'))]]))
|
||||
|
||||
eq({foo=1, bar={42, {{baz=true}, 5}}}, funcs.luaeval('vim.api.nvim__id({foo=1, bar={42, {{baz=true}, 5}}})'))
|
||||
|
||||
eq(true, funcs.luaeval('vim.api.nvim__id(vim.api.nvim__id)(true)'))
|
||||
eq(42, exec_lua [[
|
||||
local f = vim.api.nvim__id({42, vim.api.nvim__id})
|
||||
return f[2](f[1])
|
||||
]])
|
||||
end)
|
||||
|
||||
it('correctly converts container objects with type_idx to API objects', function()
|
||||
@@ -159,12 +166,8 @@ describe('luaeval(vim.api.…)', function()
|
||||
|
||||
it('errors out correctly when working with API', function()
|
||||
-- Conversion errors
|
||||
eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua type',
|
||||
remove_trace(exc_exec([[call luaeval("vim.api.nvim__id(vim.api.nvim__id)")]])))
|
||||
eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua table',
|
||||
remove_trace(exc_exec([[call luaeval("vim.api.nvim__id({1, foo=42})")]])))
|
||||
eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua type',
|
||||
remove_trace(exc_exec([[call luaeval("vim.api.nvim__id({42, vim.api.nvim__id})")]])))
|
||||
-- Errors in number of arguments
|
||||
eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected 1 argument',
|
||||
remove_trace(exc_exec([[call luaeval("vim.api.nvim__id()")]])))
|
||||
|
||||
Reference in New Issue
Block a user