2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
2017-11-22 14:35:20 -07:00
|
|
|
local Screen = require('test.functional.ui.screen')
|
|
|
|
|
2024-04-20 08:44:13 -07:00
|
|
|
local clear = n.clear
|
|
|
|
local command = n.command
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
2024-04-20 08:44:13 -07:00
|
|
|
local expect = n.expect
|
|
|
|
local eval = n.eval
|
|
|
|
local next_msg = n.next_msg
|
|
|
|
local feed = n.feed
|
|
|
|
local api = n.api
|
2017-11-22 14:35:20 -07:00
|
|
|
|
|
|
|
describe('cmdline autocommands', function()
|
|
|
|
local channel
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
2024-02-12 06:50:39 -07:00
|
|
|
channel = api.nvim_get_chan_info(0).id
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('channel', channel)
|
2017-11-22 14:35:20 -07:00
|
|
|
command("autocmd CmdlineEnter * call rpcnotify(g:channel, 'CmdlineEnter', v:event)")
|
|
|
|
command("autocmd CmdlineLeave * call rpcnotify(g:channel, 'CmdlineLeave', v:event)")
|
|
|
|
command("autocmd CmdWinEnter * call rpcnotify(g:channel, 'CmdWinEnter', v:event)")
|
|
|
|
command("autocmd CmdWinLeave * call rpcnotify(g:channel, 'CmdWinLeave', v:event)")
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('works', function()
|
|
|
|
feed(':')
|
|
|
|
eq({ 'notification', 'CmdlineEnter', { { cmdtype = ':', cmdlevel = 1 } } }, next_msg())
|
|
|
|
feed('redraw<cr>')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineLeave', { { cmdtype = ':', cmdlevel = 1, abort = false } } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-11-22 14:35:20 -07:00
|
|
|
|
|
|
|
feed(':')
|
|
|
|
eq({ 'notification', 'CmdlineEnter', { { cmdtype = ':', cmdlevel = 1 } } }, next_msg())
|
|
|
|
|
|
|
|
-- note: feed('bork<c-c>') might not consume 'bork'
|
2019-11-26 05:15:14 -07:00
|
|
|
-- due to out-of-band interrupt handling
|
2017-11-22 14:35:20 -07:00
|
|
|
feed('bork<esc>')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineLeave', { { cmdtype = ':', cmdlevel = 1, abort = true } } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-11-22 14:35:20 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can abort cmdline', function()
|
|
|
|
command('autocmd CmdlineLeave * let v:event.abort= len(getcmdline())>15')
|
|
|
|
feed(":put! ='ok'<cr>")
|
|
|
|
expect([[
|
|
|
|
ok
|
|
|
|
]])
|
|
|
|
|
|
|
|
feed(":put! ='blah blah'<cr>")
|
|
|
|
expect([[
|
|
|
|
ok
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('handles errors correctly', function()
|
|
|
|
clear()
|
|
|
|
local screen = Screen.new(72, 8)
|
|
|
|
command("autocmd CmdlineEnter * echoerr 'FAIL'")
|
|
|
|
command("autocmd CmdlineLeave * echoerr 'very error'")
|
2018-12-12 03:01:46 -07:00
|
|
|
|
2017-11-22 14:35:20 -07:00
|
|
|
feed(':')
|
|
|
|
screen:expect([[
|
2018-03-31 02:12:27 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{1:~ }|*3
|
2024-08-14 04:42:05 -07:00
|
|
|
{3: }|
|
2017-11-22 14:35:20 -07:00
|
|
|
: |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineEnter Autocommands for "*": Vim(echoerr):FAIL} |
|
2017-11-22 14:35:20 -07:00
|
|
|
:^ |
|
|
|
|
]])
|
2018-12-12 03:01:46 -07:00
|
|
|
|
2017-11-22 14:35:20 -07:00
|
|
|
feed("put ='lorem ipsum'<cr>")
|
|
|
|
screen:expect([[
|
2018-03-31 02:12:27 -07:00
|
|
|
|
|
2024-08-14 04:42:05 -07:00
|
|
|
{3: }|
|
2017-11-22 14:35:20 -07:00
|
|
|
: |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineEnter Autocommands for "*": Vim(echoerr):FAIL} |
|
2017-11-22 14:35:20 -07:00
|
|
|
:put ='lorem ipsum' |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineLeave Autocommands for "*": Vim(echoerr):very error} |
|
2017-11-22 14:35:20 -07:00
|
|
|
|
|
2024-08-14 04:42:05 -07:00
|
|
|
{6:Press ENTER or type command to continue}^ |
|
2017-11-22 14:35:20 -07:00
|
|
|
]])
|
|
|
|
|
2018-12-12 03:01:46 -07:00
|
|
|
-- cmdline was still executed
|
2017-11-22 14:35:20 -07:00
|
|
|
feed('<cr>')
|
|
|
|
screen:expect([[
|
|
|
|
|
|
|
|
|
^lorem ipsum |
|
2023-12-09 05:42:00 -07:00
|
|
|
{1:~ }|*5
|
2017-11-22 14:35:20 -07:00
|
|
|
|
|
|
|
|
]])
|
2018-12-12 03:01:46 -07:00
|
|
|
|
|
|
|
command("autocmd CmdlineChanged * echoerr 'change erreor'")
|
|
|
|
|
|
|
|
-- history recall still works
|
|
|
|
feed(':<c-p>')
|
|
|
|
screen:expect([[
|
|
|
|
|
|
|
|
|
lorem ipsum |
|
2024-08-14 04:42:05 -07:00
|
|
|
{3: }|
|
2018-12-12 03:01:46 -07:00
|
|
|
: |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineEnter Autocommands for "*": Vim(echoerr):FAIL} |
|
2018-12-12 03:01:46 -07:00
|
|
|
:put ='lorem ipsum' |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineChanged Autocommands for "*": Vim(echoerr):change erreor} |
|
2018-12-12 03:01:46 -07:00
|
|
|
:put ='lorem ipsum'^ |
|
|
|
|
]])
|
|
|
|
|
|
|
|
feed('<left>')
|
|
|
|
screen:expect([[
|
|
|
|
|
|
|
|
|
lorem ipsum |
|
2024-08-14 04:42:05 -07:00
|
|
|
{3: }|
|
2018-12-12 03:01:46 -07:00
|
|
|
: |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineEnter Autocommands for "*": Vim(echoerr):FAIL} |
|
2018-12-12 03:01:46 -07:00
|
|
|
:put ='lorem ipsum' |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineChanged Autocommands for "*": Vim(echoerr):change erreor} |
|
2018-12-12 03:01:46 -07:00
|
|
|
:put ='lorem ipsum^' |
|
|
|
|
]])
|
|
|
|
|
|
|
|
-- edit still works
|
|
|
|
feed('.')
|
|
|
|
screen:expect([[
|
2024-08-14 04:42:05 -07:00
|
|
|
{3: }|
|
2018-12-12 03:01:46 -07:00
|
|
|
: |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineEnter Autocommands for "*": Vim(echoerr):FAIL} |
|
2018-12-12 03:01:46 -07:00
|
|
|
:put ='lorem ipsum' |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineChanged Autocommands for "*": Vim(echoerr):change erreor} |
|
2018-12-12 03:01:46 -07:00
|
|
|
:put ='lorem ipsum.' |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineChanged Autocommands for "*": Vim(echoerr):change erreor} |
|
2018-12-12 03:01:46 -07:00
|
|
|
:put ='lorem ipsum.^' |
|
|
|
|
]])
|
|
|
|
|
|
|
|
feed('<cr>')
|
|
|
|
screen:expect([[
|
|
|
|
:put ='lorem ipsum' |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineChanged Autocommands for "*": Vim(echoerr):change erreor} |
|
2018-12-12 03:01:46 -07:00
|
|
|
:put ='lorem ipsum.' |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineChanged Autocommands for "*": Vim(echoerr):change erreor} |
|
2018-12-12 03:01:46 -07:00
|
|
|
:put ='lorem ipsum.' |
|
2024-08-14 04:42:05 -07:00
|
|
|
{9:CmdlineLeave Autocommands for "*": Vim(echoerr):very error} |
|
2018-12-12 03:01:46 -07:00
|
|
|
|
|
2024-08-14 04:42:05 -07:00
|
|
|
{6:Press ENTER or type command to continue}^ |
|
2018-12-12 03:01:46 -07:00
|
|
|
]])
|
|
|
|
|
|
|
|
-- cmdline was still executed
|
|
|
|
feed('<cr>')
|
|
|
|
screen:expect([[
|
|
|
|
|
|
|
|
|
lorem ipsum |
|
|
|
|
^lorem ipsum. |
|
2023-12-09 05:42:00 -07:00
|
|
|
{1:~ }|*4
|
2018-12-12 03:01:46 -07:00
|
|
|
|
|
|
|
|
]])
|
2017-11-22 14:35:20 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with nested cmdline', function()
|
|
|
|
feed(':')
|
|
|
|
eq({ 'notification', 'CmdlineEnter', { { cmdtype = ':', cmdlevel = 1 } } }, next_msg())
|
|
|
|
feed('<c-r>=')
|
|
|
|
eq({ 'notification', 'CmdlineEnter', { { cmdtype = '=', cmdlevel = 2 } } }, next_msg())
|
|
|
|
feed('<c-f>')
|
|
|
|
eq({ 'notification', 'CmdWinEnter', { {} } }, next_msg())
|
|
|
|
feed(':')
|
|
|
|
eq({ 'notification', 'CmdlineEnter', { { cmdtype = ':', cmdlevel = 3 } } }, next_msg())
|
|
|
|
feed('<c-c>')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineLeave', { { cmdtype = ':', cmdlevel = 3, abort = true } } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-11-22 14:35:20 -07:00
|
|
|
feed('<c-c>')
|
|
|
|
eq({ 'notification', 'CmdWinLeave', { {} } }, next_msg())
|
|
|
|
feed('1+2<cr>')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineLeave', { { cmdtype = '=', cmdlevel = 2, abort = false } } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-11-22 14:35:20 -07:00
|
|
|
end)
|
2018-12-12 03:01:46 -07:00
|
|
|
|
2022-12-06 17:57:29 -07:00
|
|
|
it('no crash with recursive use of v:event #19484', function()
|
|
|
|
command('autocmd CmdlineEnter * normal :')
|
|
|
|
feed(':')
|
|
|
|
eq({ 'notification', 'CmdlineEnter', { { cmdtype = ':', cmdlevel = 1 } } }, next_msg())
|
|
|
|
feed('<CR>')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineLeave', { { cmdtype = ':', cmdlevel = 1, abort = false } } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-12-06 17:57:29 -07:00
|
|
|
end)
|
|
|
|
|
2018-12-12 03:01:46 -07:00
|
|
|
it('supports CmdlineChanged', function()
|
|
|
|
command(
|
|
|
|
"autocmd CmdlineChanged * call rpcnotify(g:channel, 'CmdlineChanged', v:event, getcmdline())"
|
|
|
|
)
|
|
|
|
feed(':')
|
|
|
|
eq({ 'notification', 'CmdlineEnter', { { cmdtype = ':', cmdlevel = 1 } } }, next_msg())
|
|
|
|
feed('l')
|
|
|
|
eq({ 'notification', 'CmdlineChanged', { { cmdtype = ':', cmdlevel = 1 }, 'l' } }, next_msg())
|
|
|
|
feed('e')
|
|
|
|
eq({ 'notification', 'CmdlineChanged', { { cmdtype = ':', cmdlevel = 1 }, 'le' } }, next_msg())
|
|
|
|
feed('t')
|
|
|
|
eq({ 'notification', 'CmdlineChanged', { { cmdtype = ':', cmdlevel = 1 }, 'let' } }, next_msg())
|
|
|
|
feed('<space>')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineChanged', { { cmdtype = ':', cmdlevel = 1 }, 'let ' } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-12-12 03:01:46 -07:00
|
|
|
feed('x')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineChanged', { { cmdtype = ':', cmdlevel = 1 }, 'let x' } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-12-12 03:01:46 -07:00
|
|
|
feed('<space>')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineChanged', { { cmdtype = ':', cmdlevel = 1 }, 'let x ' } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-12-12 03:01:46 -07:00
|
|
|
feed('=')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineChanged', { { cmdtype = ':', cmdlevel = 1 }, 'let x =' } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-12-12 03:01:46 -07:00
|
|
|
feed('<space>')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineChanged', { { cmdtype = ':', cmdlevel = 1 }, 'let x = ' } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-12-12 03:01:46 -07:00
|
|
|
feed('<c-r>=')
|
|
|
|
eq({ 'notification', 'CmdlineEnter', { { cmdtype = '=', cmdlevel = 2 } } }, next_msg())
|
|
|
|
feed('1')
|
|
|
|
eq({ 'notification', 'CmdlineChanged', { { cmdtype = '=', cmdlevel = 2 }, '1' } }, next_msg())
|
|
|
|
feed('+')
|
|
|
|
eq({ 'notification', 'CmdlineChanged', { { cmdtype = '=', cmdlevel = 2 }, '1+' } }, next_msg())
|
|
|
|
feed('1')
|
|
|
|
eq({ 'notification', 'CmdlineChanged', { { cmdtype = '=', cmdlevel = 2 }, '1+1' } }, next_msg())
|
|
|
|
feed('<cr>')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineLeave', { { cmdtype = '=', cmdlevel = 2, abort = false } } },
|
|
|
|
next_msg()
|
|
|
|
)
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineChanged', { { cmdtype = ':', cmdlevel = 1 }, 'let x = 2' } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-12-12 03:01:46 -07:00
|
|
|
feed('<cr>')
|
|
|
|
eq(
|
|
|
|
{ 'notification', 'CmdlineLeave', { { cmdtype = ':', cmdlevel = 1, abort = false } } },
|
|
|
|
next_msg()
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2018-12-12 03:01:46 -07:00
|
|
|
eq(2, eval('x'))
|
|
|
|
end)
|
2017-11-22 14:35:20 -07:00
|
|
|
end)
|