123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776 |
- local t = require('test.testutil')
- local n = require('test.functional.testnvim')()
- local NIL = vim.NIL
- local clear = n.clear
- local command = n.command
- local eq = t.eq
- local api = n.api
- local matches = t.matches
- local source = n.source
- local pcall_err = t.pcall_err
- local exec_lua = n.exec_lua
- local assert_alive = n.assert_alive
- local feed = n.feed
- local fn = n.fn
- 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',
- preview = false,
- range = NIL,
- register = false,
- keepscript = false,
- script_id = 0,
- }
- local cmd_dict2 = {
- addr = NIL,
- bang = false,
- bar = false,
- complete = NIL,
- complete_arg = NIL,
- count = NIL,
- definition = 'pwd',
- name = 'Pwd',
- nargs = '?',
- preview = false,
- range = NIL,
- register = false,
- keepscript = false,
- script_id = 0,
- }
- before_each(clear)
- it('gets empty list if no commands were defined', function()
- eq({}, api.nvim_get_commands({ builtin = false }))
- end)
- it('validation', function()
- eq('builtin=true not implemented', pcall_err(api.nvim_get_commands, { builtin = true }))
- eq("Invalid key: 'foo'", pcall_err(api.nvim_get_commands, { foo = 'blah' }))
- end)
- it('gets global user-defined commands', function()
- -- Define a command.
- command('command -nargs=1 Hello echo "Hello World"')
- eq({ Hello = cmd_dict }, api.nvim_get_commands({ builtin = false }))
- -- Define another command.
- command('command -nargs=? Pwd pwd')
- eq({ Hello = cmd_dict, Pwd = cmd_dict2 }, api.nvim_get_commands({ builtin = false }))
- -- Delete a command.
- command('delcommand Pwd')
- eq({ Hello = cmd_dict }, api.nvim_get_commands({ builtin = false }))
- end)
- it('gets buffer-local user-defined commands', function()
- -- Define a buffer-local command.
- command('command -buffer -nargs=1 Hello echo "Hello World"')
- eq({ Hello = cmd_dict }, api.nvim_buf_get_commands(0, { builtin = false }))
- -- Define another buffer-local command.
- command('command -buffer -nargs=? Pwd pwd')
- eq({ Hello = cmd_dict, Pwd = cmd_dict2 }, api.nvim_buf_get_commands(0, { builtin = false }))
- -- Delete a command.
- command('delcommand Pwd')
- eq({ Hello = cmd_dict }, api.nvim_buf_get_commands(0, { builtin = false }))
- -- {builtin=true} always returns empty for buffer-local case.
- eq({}, api.nvim_buf_get_commands(0, { builtin = true }))
- end)
- it('gets various command attributes', function()
- local cmd0 = {
- addr = 'arguments',
- bang = false,
- bar = false,
- complete = 'dir',
- complete_arg = NIL,
- count = '10',
- definition = 'pwd <args>',
- name = 'TestCmd',
- nargs = '1',
- preview = false,
- range = '10',
- register = false,
- keepscript = false,
- script_id = 0,
- }
- local cmd1 = {
- addr = NIL,
- bang = false,
- bar = false,
- complete = 'custom',
- complete_arg = 'ListUsers',
- count = NIL,
- definition = '!finger <args>',
- name = 'Finger',
- nargs = '+',
- preview = false,
- range = NIL,
- register = false,
- keepscript = false,
- script_id = 1,
- }
- local cmd2 = {
- addr = NIL,
- bang = true,
- bar = false,
- complete = NIL,
- complete_arg = NIL,
- count = NIL,
- definition = 'call \128\253R2_foo(<q-args>)',
- name = 'Cmd2',
- nargs = '*',
- preview = false,
- range = NIL,
- register = false,
- keepscript = false,
- script_id = 2,
- }
- local cmd3 = {
- addr = NIL,
- bang = false,
- bar = true,
- complete = NIL,
- complete_arg = NIL,
- count = NIL,
- definition = 'call \128\253R3_ohyeah()',
- name = 'Cmd3',
- nargs = '0',
- preview = false,
- range = NIL,
- register = false,
- keepscript = false,
- script_id = 3,
- }
- local cmd4 = {
- addr = NIL,
- bang = false,
- bar = false,
- complete = NIL,
- complete_arg = NIL,
- count = NIL,
- definition = 'call \128\253R4_just_great()',
- name = 'Cmd4',
- nargs = '0',
- preview = false,
- range = NIL,
- register = true,
- keepscript = false,
- script_id = 4,
- }
- source([[
- let s:foo = 1
- command -complete=custom,ListUsers -nargs=+ Finger !finger <args>
- ]])
- eq({ Finger = cmd1 }, api.nvim_get_commands({ builtin = false }))
- command('command -nargs=1 -complete=dir -addr=arguments -count=10 TestCmd pwd <args>')
- eq({ Finger = cmd1, TestCmd = cmd0 }, api.nvim_get_commands({ builtin = false }))
- source([[
- function! s:foo() abort
- endfunction
- command -bang -nargs=* Cmd2 call <SID>foo(<q-args>)
- ]])
- source([[
- function! s:ohyeah() abort
- endfunction
- command -bar -nargs=0 Cmd3 call <SID>ohyeah()
- ]])
- source([[
- function! s:just_great() abort
- endfunction
- command -register Cmd4 call <SID>just_great()
- ]])
- -- TODO(justinmk): Order is stable but undefined. Sort before return?
- eq(
- { Cmd2 = cmd2, Cmd3 = cmd3, Cmd4 = cmd4, Finger = cmd1, TestCmd = cmd0 },
- api.nvim_get_commands({ builtin = false })
- )
- end)
- end)
- describe('nvim_create_user_command', function()
- before_each(clear)
- it('works with strings', function()
- api.nvim_create_user_command('SomeCommand', 'let g:command_fired = <args>', { nargs = 1 })
- command('SomeCommand 42')
- eq(42, api.nvim_eval('g:command_fired'))
- end)
- it('works with Lua functions', function()
- exec_lua [[
- result = {}
- vim.api.nvim_create_user_command('CommandWithLuaCallback', function(opts)
- result = opts
- end, {
- nargs = "*",
- bang = true,
- count = 2,
- })
- ]]
- eq(
- {
- name = 'CommandWithLuaCallback',
- args = [[this\ is a\ test]],
- fargs = { 'this ', 'is', 'a test' },
- bang = false,
- line1 = 1,
- line2 = 1,
- mods = '',
- smods = {
- browse = false,
- confirm = false,
- emsg_silent = false,
- hide = false,
- horizontal = false,
- keepalt = false,
- keepjumps = false,
- keepmarks = false,
- keeppatterns = false,
- lockmarks = false,
- noautocmd = false,
- noswapfile = false,
- sandbox = false,
- silent = false,
- split = '',
- tab = -1,
- unsilent = false,
- verbose = -1,
- vertical = false,
- },
- range = 0,
- count = 2,
- reg = '',
- },
- exec_lua [=[
- vim.api.nvim_command([[CommandWithLuaCallback this\ is a\ test]])
- return result
- ]=]
- )
- eq(
- {
- name = 'CommandWithLuaCallback',
- args = [[this includes\ a backslash: \\]],
- fargs = { 'this', 'includes a', 'backslash:', '\\' },
- bang = false,
- line1 = 1,
- line2 = 1,
- mods = '',
- smods = {
- browse = false,
- confirm = false,
- emsg_silent = false,
- hide = false,
- horizontal = false,
- keepalt = false,
- keepjumps = false,
- keepmarks = false,
- keeppatterns = false,
- lockmarks = false,
- noautocmd = false,
- noswapfile = false,
- sandbox = false,
- silent = false,
- split = '',
- tab = -1,
- unsilent = false,
- verbose = -1,
- vertical = false,
- },
- range = 0,
- count = 2,
- reg = '',
- },
- exec_lua [=[
- vim.api.nvim_command([[CommandWithLuaCallback this includes\ a backslash: \\]])
- return result
- ]=]
- )
- eq(
- {
- name = 'CommandWithLuaCallback',
- args = 'a\\b',
- fargs = { 'a\\b' },
- bang = false,
- line1 = 1,
- line2 = 1,
- mods = '',
- smods = {
- browse = false,
- confirm = false,
- emsg_silent = false,
- hide = false,
- horizontal = false,
- keepalt = false,
- keepjumps = false,
- keepmarks = false,
- keeppatterns = false,
- lockmarks = false,
- noautocmd = false,
- noswapfile = false,
- sandbox = false,
- silent = false,
- split = '',
- tab = -1,
- unsilent = false,
- verbose = -1,
- vertical = false,
- },
- range = 0,
- count = 2,
- reg = '',
- },
- exec_lua [=[
- vim.api.nvim_command('CommandWithLuaCallback a\\b')
- return result
- ]=]
- )
- eq(
- {
- name = 'CommandWithLuaCallback',
- args = 'h\tey ',
- fargs = { [[h]], [[ey]] },
- bang = true,
- line1 = 10,
- line2 = 10,
- mods = 'confirm unsilent botright horizontal',
- smods = {
- browse = false,
- confirm = true,
- emsg_silent = false,
- hide = false,
- horizontal = true,
- keepalt = false,
- keepjumps = false,
- keepmarks = false,
- keeppatterns = false,
- lockmarks = false,
- noautocmd = false,
- noswapfile = false,
- sandbox = false,
- silent = false,
- split = 'botright',
- tab = -1,
- unsilent = true,
- verbose = -1,
- vertical = false,
- },
- range = 1,
- count = 10,
- reg = '',
- },
- exec_lua [=[
- vim.api.nvim_command('unsilent horizontal botright confirm 10CommandWithLuaCallback! h\tey ')
- return result
- ]=]
- )
- eq(
- {
- name = 'CommandWithLuaCallback',
- args = 'h',
- fargs = { 'h' },
- bang = false,
- line1 = 1,
- line2 = 42,
- mods = '',
- smods = {
- browse = false,
- confirm = false,
- emsg_silent = false,
- hide = false,
- horizontal = false,
- keepalt = false,
- keepjumps = false,
- keepmarks = false,
- keeppatterns = false,
- lockmarks = false,
- noautocmd = false,
- noswapfile = false,
- sandbox = false,
- silent = false,
- split = '',
- tab = -1,
- unsilent = false,
- verbose = -1,
- vertical = false,
- },
- range = 1,
- count = 42,
- reg = '',
- },
- exec_lua [[
- vim.api.nvim_command('CommandWithLuaCallback 42 h')
- return result
- ]]
- )
- eq(
- {
- name = 'CommandWithLuaCallback',
- args = '',
- fargs = {}, -- fargs works without args
- bang = false,
- line1 = 1,
- line2 = 1,
- mods = '',
- smods = {
- browse = false,
- confirm = false,
- emsg_silent = false,
- hide = false,
- horizontal = false,
- keepalt = false,
- keepjumps = false,
- keepmarks = false,
- keeppatterns = false,
- lockmarks = false,
- noautocmd = false,
- noswapfile = false,
- sandbox = false,
- silent = false,
- split = '',
- tab = -1,
- unsilent = false,
- verbose = -1,
- vertical = false,
- },
- range = 0,
- count = 2,
- reg = '',
- },
- exec_lua [[
- vim.api.nvim_command('CommandWithLuaCallback')
- return result
- ]]
- )
- -- f-args doesn't split when command nargs is 1 or "?"
- exec_lua [[
- result = {}
- vim.api.nvim_create_user_command('CommandWithOneOrNoArg', function(opts)
- result = opts
- end, {
- nargs = "?",
- bang = true,
- count = 2,
- })
- ]]
- eq(
- {
- name = 'CommandWithOneOrNoArg',
- args = "hello I'm one argument",
- fargs = { "hello I'm one argument" }, -- Doesn't split args
- bang = false,
- line1 = 1,
- line2 = 1,
- mods = '',
- smods = {
- browse = false,
- confirm = false,
- emsg_silent = false,
- hide = false,
- horizontal = false,
- keepalt = false,
- keepjumps = false,
- keepmarks = false,
- keeppatterns = false,
- lockmarks = false,
- noautocmd = false,
- noswapfile = false,
- sandbox = false,
- silent = false,
- split = '',
- tab = -1,
- unsilent = false,
- verbose = -1,
- vertical = false,
- },
- range = 0,
- count = 2,
- reg = '',
- },
- exec_lua [[
- vim.api.nvim_command('CommandWithOneOrNoArg hello I\'m one argument')
- return result
- ]]
- )
- -- f-args is an empty table if no args were passed
- eq(
- {
- name = 'CommandWithOneOrNoArg',
- args = '',
- fargs = {},
- bang = false,
- line1 = 1,
- line2 = 1,
- mods = '',
- smods = {
- browse = false,
- confirm = false,
- emsg_silent = false,
- hide = false,
- horizontal = false,
- keepalt = false,
- keepjumps = false,
- keepmarks = false,
- keeppatterns = false,
- lockmarks = false,
- noautocmd = false,
- noswapfile = false,
- sandbox = false,
- silent = false,
- split = '',
- tab = -1,
- unsilent = false,
- verbose = -1,
- vertical = false,
- },
- range = 0,
- count = 2,
- reg = '',
- },
- exec_lua [[
- vim.api.nvim_command('CommandWithOneOrNoArg')
- return result
- ]]
- )
- -- f-args is an empty table when the command nargs=0
- exec_lua [[
- result = {}
- vim.api.nvim_create_user_command('CommandWithNoArgs', function(opts)
- result = opts
- end, {
- nargs = 0,
- bang = true,
- count = 2,
- register = true,
- })
- ]]
- eq(
- {
- name = 'CommandWithNoArgs',
- args = '',
- fargs = {},
- bang = false,
- line1 = 1,
- line2 = 1,
- mods = '',
- smods = {
- browse = false,
- confirm = false,
- emsg_silent = false,
- hide = false,
- horizontal = false,
- keepalt = false,
- keepjumps = false,
- keepmarks = false,
- keeppatterns = false,
- lockmarks = false,
- noautocmd = false,
- noswapfile = false,
- sandbox = false,
- silent = false,
- split = '',
- tab = -1,
- unsilent = false,
- verbose = -1,
- vertical = false,
- },
- range = 0,
- count = 2,
- reg = '',
- },
- exec_lua [[
- vim.cmd('CommandWithNoArgs')
- return result
- ]]
- )
- -- register can be specified
- eq(
- {
- name = 'CommandWithNoArgs',
- args = '',
- fargs = {},
- bang = false,
- line1 = 1,
- line2 = 1,
- mods = '',
- smods = {
- browse = false,
- confirm = false,
- emsg_silent = false,
- hide = false,
- horizontal = false,
- keepalt = false,
- keepjumps = false,
- keepmarks = false,
- keeppatterns = false,
- lockmarks = false,
- noautocmd = false,
- noswapfile = false,
- sandbox = false,
- silent = false,
- split = '',
- tab = -1,
- unsilent = false,
- verbose = -1,
- vertical = false,
- },
- range = 0,
- count = 2,
- reg = '+',
- },
- exec_lua [[
- vim.cmd('CommandWithNoArgs +')
- return result
- ]]
- )
- end)
- it('can define buffer-local commands', function()
- local bufnr = api.nvim_create_buf(false, false)
- api.nvim_buf_create_user_command(bufnr, 'Hello', '', {})
- matches('Not an editor command: Hello', pcall_err(command, 'Hello'))
- api.nvim_set_current_buf(bufnr)
- command('Hello')
- assert_alive()
- end)
- it('can use a Lua complete function', function()
- exec_lua [[
- vim.api.nvim_create_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', fn.getcmdline())
- feed('<C-U>Test b<Tab>')
- eq('Test bbb', fn.getcmdline())
- end)
- it('does not allow invalid command names', function()
- eq(
- "Invalid command name (must start with uppercase): 'test'",
- pcall_err(
- exec_lua,
- [[
- vim.api.nvim_create_user_command('test', 'echo "hi"', {})
- ]]
- )
- )
- eq(
- "Invalid command name: 't@'",
- pcall_err(
- exec_lua,
- [[
- vim.api.nvim_create_user_command('t@', 'echo "hi"', {})
- ]]
- )
- )
- eq(
- "Invalid command name: 'T@st'",
- pcall_err(
- exec_lua,
- [[
- vim.api.nvim_create_user_command('T@st', 'echo "hi"', {})
- ]]
- )
- )
- eq(
- "Invalid command name: 'Test!'",
- pcall_err(
- exec_lua,
- [[
- vim.api.nvim_create_user_command('Test!', 'echo "hi"', {})
- ]]
- )
- )
- eq(
- "Invalid command name: '💩'",
- pcall_err(
- exec_lua,
- [[
- vim.api.nvim_create_user_command('💩', 'echo "hi"', {})
- ]]
- )
- )
- end)
- it('smods can be used with nvim_cmd', function()
- exec_lua [[
- vim.api.nvim_create_user_command('MyEcho', function(opts)
- vim.api.nvim_cmd({ cmd = 'echo', args = { '&verbose' }, mods = opts.smods }, {})
- end, {})
- ]]
- eq('3', api.nvim_cmd({ cmd = 'MyEcho', mods = { verbose = 3 } }, { output = true }))
- eq(1, #api.nvim_list_tabpages())
- exec_lua [[
- vim.api.nvim_create_user_command('MySplit', function(opts)
- vim.api.nvim_cmd({ cmd = 'split', mods = opts.smods }, {})
- end, {})
- ]]
- api.nvim_cmd({ cmd = 'MySplit' }, {})
- eq(1, #api.nvim_list_tabpages())
- eq(2, #api.nvim_list_wins())
- api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 1 } }, {})
- eq(2, #api.nvim_list_tabpages())
- eq(2, fn.tabpagenr())
- api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 1 } }, {})
- eq(3, #api.nvim_list_tabpages())
- eq(2, fn.tabpagenr())
- api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 3 } }, {})
- eq(4, #api.nvim_list_tabpages())
- eq(4, fn.tabpagenr())
- api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 0 } }, {})
- eq(5, #api.nvim_list_tabpages())
- eq(1, fn.tabpagenr())
- end)
- end)
- describe('nvim_del_user_command', function()
- before_each(clear)
- it('can delete global commands', function()
- api.nvim_create_user_command('Hello', 'echo "Hi"', {})
- command('Hello')
- api.nvim_del_user_command('Hello')
- matches('Not an editor command: Hello', pcall_err(command, 'Hello'))
- end)
- it('can delete buffer-local commands', function()
- api.nvim_buf_create_user_command(0, 'Hello', 'echo "Hi"', {})
- command('Hello')
- api.nvim_buf_del_user_command(0, 'Hello')
- matches('Not an editor command: Hello', pcall_err(command, 'Hello'))
- end)
- end)
|