2021-05-28 12:45:34 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
|
|
|
|
|
|
|
local clear = helpers.clear
|
|
|
|
local command = helpers.command
|
|
|
|
local eq = helpers.eq
|
|
|
|
local neq = helpers.neq
|
|
|
|
local exec_lua = helpers.exec_lua
|
|
|
|
local matches = helpers.matches
|
2024-01-12 10:59:57 -07:00
|
|
|
local api = helpers.api
|
2021-05-28 12:45:34 -07:00
|
|
|
local source = helpers.source
|
2022-03-06 12:35:14 -07:00
|
|
|
local pcall_err = helpers.pcall_err
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
before_each(clear)
|
|
|
|
|
|
|
|
describe('autocmd api', function()
|
|
|
|
describe('nvim_create_autocmd', function()
|
2023-02-14 06:07:38 -07:00
|
|
|
it('validation', function()
|
|
|
|
eq(
|
|
|
|
"Cannot use both 'callback' and 'command'",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_create_autocmd, 'BufReadPost', {
|
2023-02-14 06:07:38 -07:00
|
|
|
pattern = '*.py,*.pyi',
|
2021-05-28 12:45:34 -07:00
|
|
|
command = "echo 'Should Have Errored",
|
2023-02-14 06:07:38 -07:00
|
|
|
callback = 'NotAllowed',
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 06:07:38 -07:00
|
|
|
eq(
|
|
|
|
"Cannot use both 'pattern' and 'buffer' for the same autocmd",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_create_autocmd, 'FileType', {
|
2023-02-14 06:07:38 -07:00
|
|
|
command = 'let g:called = g:called + 1',
|
|
|
|
buffer = 0,
|
|
|
|
pattern = '*.py',
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 06:07:38 -07:00
|
|
|
eq(
|
|
|
|
"Required: 'event'",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_create_autocmd, {}, {
|
2023-02-14 06:07:38 -07:00
|
|
|
command = 'ls',
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq("Required: 'command' or 'callback'", pcall_err(api.nvim_create_autocmd, 'FileType', {}))
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'desc': expected String, got Integer",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_create_autocmd, 'FileType', {
|
2023-02-14 06:07:38 -07:00
|
|
|
command = 'ls',
|
|
|
|
desc = 42,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'callback': expected Lua function or Vim function name, got Integer",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_create_autocmd, 'FileType', {
|
2023-02-14 06:07:38 -07:00
|
|
|
callback = 0,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 06:07:38 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'event' item: expected String, got Array",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_create_autocmd, { 'FileType', {} }, {})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-04-22 18:23:25 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'group': 0",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_create_autocmd, 'FileType', {
|
2023-04-22 18:23:25 -07:00
|
|
|
group = 0,
|
|
|
|
command = 'ls',
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-10-09 02:50:44 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq("Invalid 'event': 'foo'", pcall_err(api.nvim_create_autocmd, 'foo', { command = '' }))
|
2023-10-09 02:50:44 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'event': 'VimEnter '",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_create_autocmd, 'VimEnter ', { command = '' })
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-10-09 02:50:44 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'event': 'VimEnter foo'",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_create_autocmd, 'VimEnter foo', { command = '' })
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-10-09 02:50:44 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'event': 'BufAdd,BufDelete'",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_create_autocmd, 'BufAdd,BufDelete', { command = '' })
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('doesnt leak when you use ++once', function()
|
|
|
|
eq(
|
|
|
|
1,
|
|
|
|
exec_lua(
|
|
|
|
[[
|
|
|
|
local count = 0
|
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
vim.api.nvim_create_autocmd("FileType", {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = "*",
|
|
|
|
callback = function() count = count + 1 end,
|
|
|
|
once = true
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
vim.cmd "set filetype=txt"
|
|
|
|
vim.cmd "set filetype=python"
|
|
|
|
|
|
|
|
return count
|
|
|
|
]],
|
|
|
|
{}
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('allows passing buffer by key', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('called', 0)
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('FileType', {
|
2021-05-28 12:45:34 -07:00
|
|
|
command = 'let g:called = g:called + 1',
|
|
|
|
buffer = 0,
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 06:11:28 -07:00
|
|
|
command 'set filetype=txt'
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(1, api.nvim_get_var('called'))
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- switch to a new buffer
|
2024-01-12 06:11:28 -07:00
|
|
|
command 'new'
|
|
|
|
command 'set filetype=python'
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(1, api.nvim_get_var('called'))
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('does not allow passing invalid buffers', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local ok, msg = pcall(api.nvim_create_autocmd, 'FileType', {
|
2021-05-28 12:45:34 -07:00
|
|
|
command = 'let g:called = g:called + 1',
|
|
|
|
buffer = -1,
|
|
|
|
})
|
|
|
|
|
|
|
|
eq(false, ok)
|
|
|
|
matches('Invalid buffer id', msg)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('errors on non-functions for cb', function()
|
|
|
|
eq(
|
|
|
|
false,
|
|
|
|
pcall(
|
|
|
|
exec_lua,
|
|
|
|
[[
|
2021-05-28 12:45:34 -07:00
|
|
|
vim.api.nvim_create_autocmd("BufReadPost", {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = "*.py,*.pyi",
|
|
|
|
callback = 5,
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
]]
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('allow passing pattern and <buffer> in same pattern', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local ok = pcall(api.nvim_create_autocmd, 'BufReadPost', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '*.py,<buffer>',
|
|
|
|
command = "echo 'Should Not Error'",
|
|
|
|
})
|
|
|
|
|
|
|
|
eq(true, ok)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should handle multiple values as comma separated list', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('BufReadPost', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '*.py,*.pyi',
|
|
|
|
command = "echo 'Should Not Have Errored'",
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- We should have one autocmd for *.py and one for *.pyi
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(2, #api.nvim_get_autocmds { event = 'BufReadPost' })
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('should handle multiple values as array', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('BufReadPost', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = { '*.py', '*.pyi' },
|
|
|
|
command = "echo 'Should Not Have Errored'",
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- We should have one autocmd for *.py and one for *.pyi
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(2, #api.nvim_get_autocmds { event = 'BufReadPost' })
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('desc', function()
|
|
|
|
it('can add description to one autocmd', function()
|
2022-05-21 06:55:48 -07:00
|
|
|
local cmd = "echo 'Should Not Have Errored'"
|
|
|
|
local desc = 'Can show description'
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('BufReadPost', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '*.py',
|
2022-05-21 06:55:48 -07:00
|
|
|
command = cmd,
|
|
|
|
desc = desc,
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(desc, api.nvim_get_autocmds { event = 'BufReadPost' }[1].desc)
|
|
|
|
eq(cmd, api.nvim_get_autocmds { event = 'BufReadPost' }[1].command)
|
2022-05-21 06:55:48 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can add description to one autocmd that uses a callback', function()
|
|
|
|
local desc = 'Can show description'
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('desc', desc)
|
2022-05-21 06:55:48 -07:00
|
|
|
|
2022-06-09 06:18:56 -07:00
|
|
|
local result = exec_lua([[
|
2022-05-21 06:55:48 -07:00
|
|
|
local callback = function() print 'Should Not Have Errored' end
|
|
|
|
vim.api.nvim_create_autocmd("BufReadPost", {
|
|
|
|
pattern = "*.py",
|
|
|
|
callback = callback,
|
|
|
|
desc = vim.g.desc,
|
|
|
|
})
|
2022-06-09 06:18:56 -07:00
|
|
|
local aus = vim.api.nvim_get_autocmds({ event = 'BufReadPost' })
|
|
|
|
local first = aus[1]
|
|
|
|
return {
|
|
|
|
desc = first.desc,
|
|
|
|
cbtype = type(first.callback)
|
|
|
|
}
|
2022-05-21 06:55:48 -07:00
|
|
|
]])
|
|
|
|
|
2022-06-09 06:18:56 -07:00
|
|
|
eq({ desc = desc, cbtype = 'function' }, result)
|
2022-05-21 06:55:48 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('will not add a description unless it was provided', function()
|
|
|
|
exec_lua([[
|
|
|
|
local callback = function() print 'Should Not Have Errored' end
|
|
|
|
vim.api.nvim_create_autocmd("BufReadPost", {
|
|
|
|
pattern = "*.py",
|
|
|
|
callback = callback,
|
|
|
|
})
|
|
|
|
]])
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(nil, api.nvim_get_autocmds({ event = 'BufReadPost' })[1].desc)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can add description to multiple autocmd', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('BufReadPost', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = { '*.py', '*.pyi' },
|
|
|
|
command = "echo 'Should Not Have Errored'",
|
|
|
|
desc = 'Can show description',
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { event = 'BufReadPost' }
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(2, #aus)
|
|
|
|
eq('Can show description', aus[1].desc)
|
|
|
|
eq('Can show description', aus[2].desc)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
pending('script and verbose settings', function()
|
|
|
|
it('marks API client', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('BufReadPost', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '*.py',
|
|
|
|
command = "echo 'Should Not Have Errored'",
|
|
|
|
desc = 'Can show description',
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { event = 'BufReadPost' }
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(1, #aus, aus)
|
|
|
|
end)
|
|
|
|
end)
|
2022-03-19 17:57:58 -07:00
|
|
|
|
|
|
|
it('removes an autocommand if the callback returns true', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('some_condition', false)
|
2022-03-19 17:57:58 -07:00
|
|
|
|
|
|
|
exec_lua [[
|
|
|
|
vim.api.nvim_create_autocmd("User", {
|
|
|
|
pattern = "Test",
|
|
|
|
desc = "A test autocommand",
|
|
|
|
callback = function()
|
|
|
|
return vim.g.some_condition
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('User', { pattern = 'Test' })
|
2022-05-21 06:55:48 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds({ event = 'User', pattern = 'Test' })
|
2022-05-21 06:55:48 -07:00
|
|
|
local first = aus[1]
|
2023-02-11 01:45:11 -07:00
|
|
|
eq(true, first.id > 0)
|
2022-05-21 06:55:48 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('some_condition', true)
|
|
|
|
api.nvim_exec_autocmds('User', { pattern = 'Test' })
|
|
|
|
eq({}, api.nvim_get_autocmds({ event = 'User', pattern = 'Test' }))
|
2022-03-19 17:57:58 -07:00
|
|
|
end)
|
2022-03-19 18:16:19 -07:00
|
|
|
|
|
|
|
it('receives an args table', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local group_id = api.nvim_create_augroup('TestGroup', {})
|
2023-06-06 18:29:12 -07:00
|
|
|
-- Having an existing autocmd calling expand("<afile>") shouldn't change args #18964
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('User', {
|
2023-06-06 18:29:12 -07:00
|
|
|
group = 'TestGroup',
|
|
|
|
pattern = 'Te*',
|
|
|
|
command = 'call expand("<afile>")',
|
|
|
|
})
|
|
|
|
|
|
|
|
local autocmd_id = exec_lua [[
|
|
|
|
return vim.api.nvim_create_autocmd("User", {
|
2022-03-19 18:16:19 -07:00
|
|
|
group = "TestGroup",
|
|
|
|
pattern = "Te*",
|
|
|
|
callback = function(args)
|
|
|
|
vim.g.autocmd_args = args
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('User', { pattern = 'Test pattern' })
|
2022-03-19 18:16:19 -07:00
|
|
|
eq({
|
2023-06-06 18:29:12 -07:00
|
|
|
id = autocmd_id,
|
|
|
|
group = group_id,
|
2022-03-19 18:16:19 -07:00
|
|
|
event = 'User',
|
|
|
|
match = 'Test pattern',
|
|
|
|
file = 'Test pattern',
|
|
|
|
buf = 1,
|
2024-01-12 10:59:57 -07:00
|
|
|
}, api.nvim_get_var('autocmd_args'))
|
2022-03-19 18:16:19 -07:00
|
|
|
|
|
|
|
-- Test without a group
|
2023-06-06 18:29:12 -07:00
|
|
|
autocmd_id = exec_lua [[
|
|
|
|
return vim.api.nvim_create_autocmd("User", {
|
2022-03-19 18:16:19 -07:00
|
|
|
pattern = "*",
|
|
|
|
callback = function(args)
|
|
|
|
vim.g.autocmd_args = args
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('User', { pattern = 'some_pat' })
|
2022-03-19 18:16:19 -07:00
|
|
|
eq({
|
2023-06-06 18:29:12 -07:00
|
|
|
id = autocmd_id,
|
2022-03-19 18:16:19 -07:00
|
|
|
group = nil,
|
|
|
|
event = 'User',
|
|
|
|
match = 'some_pat',
|
|
|
|
file = 'some_pat',
|
|
|
|
buf = 1,
|
2024-01-12 10:59:57 -07:00
|
|
|
}, api.nvim_get_var('autocmd_args'))
|
2022-03-19 18:16:19 -07:00
|
|
|
end)
|
2022-05-18 08:51:26 -07:00
|
|
|
|
|
|
|
it('can receive arbitrary data', function()
|
|
|
|
local function test(data)
|
|
|
|
eq(
|
|
|
|
data,
|
|
|
|
exec_lua(
|
|
|
|
[[
|
|
|
|
local input = ...
|
|
|
|
local output
|
|
|
|
vim.api.nvim_create_autocmd("User", {
|
|
|
|
pattern = "Test",
|
|
|
|
callback = function(args)
|
|
|
|
output = args.data
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
vim.api.nvim_exec_autocmds("User", {
|
|
|
|
pattern = "Test",
|
|
|
|
data = input,
|
|
|
|
})
|
|
|
|
|
|
|
|
return output
|
|
|
|
]],
|
|
|
|
data
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-05-18 08:51:26 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
test('Hello')
|
|
|
|
test(42)
|
|
|
|
test(true)
|
|
|
|
test({ 'list' })
|
|
|
|
test({ foo = 'bar' })
|
|
|
|
end)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('nvim_get_autocmds', function()
|
2023-02-14 12:19:28 -07:00
|
|
|
it('validation', function()
|
|
|
|
eq(
|
|
|
|
"Invalid 'group': 9997999",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, {
|
2023-02-14 12:19:28 -07:00
|
|
|
group = 9997999,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'group': 'bogus'",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, {
|
2023-02-14 12:19:28 -07:00
|
|
|
group = 'bogus',
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-04-22 18:23:25 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'group': 0",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, {
|
2023-04-22 18:23:25 -07:00
|
|
|
group = 0,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'group': expected String or Integer, got Array",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, {
|
2023-02-14 12:19:28 -07:00
|
|
|
group = {},
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'buffer': expected Integer or Array, got Boolean",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, {
|
2023-02-14 12:19:28 -07:00
|
|
|
buffer = true,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'event': expected String or Array",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, {
|
2023-02-14 12:19:28 -07:00
|
|
|
event = true,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'pattern': expected String or Array, got Boolean",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, {
|
2023-02-14 12:19:28 -07:00
|
|
|
pattern = true,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 12:19:28 -07:00
|
|
|
end)
|
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
describe('events', function()
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns one autocmd when there is only one for an event', function()
|
2021-05-28 12:45:34 -07:00
|
|
|
command [[au! InsertEnter]]
|
|
|
|
command [[au InsertEnter * :echo "1"]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { event = 'InsertEnter' }
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(1, #aus)
|
|
|
|
end)
|
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns two autocmds when there are two for an event', function()
|
2021-05-28 12:45:34 -07:00
|
|
|
command [[au! InsertEnter]]
|
|
|
|
command [[au InsertEnter * :echo "1"]]
|
|
|
|
command [[au InsertEnter * :echo "2"]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { event = 'InsertEnter' }
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(2, #aus)
|
|
|
|
end)
|
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns the same thing if you use string or list', function()
|
2021-05-28 12:45:34 -07:00
|
|
|
command [[au! InsertEnter]]
|
|
|
|
command [[au InsertEnter * :echo "1"]]
|
|
|
|
command [[au InsertEnter * :echo "2"]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local string_aus = api.nvim_get_autocmds { event = 'InsertEnter' }
|
|
|
|
local array_aus = api.nvim_get_autocmds { event = { 'InsertEnter' } }
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(string_aus, array_aus)
|
|
|
|
end)
|
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns two autocmds when there are two for an event', function()
|
2021-05-28 12:45:34 -07:00
|
|
|
command [[au! InsertEnter]]
|
|
|
|
command [[au! InsertLeave]]
|
|
|
|
command [[au InsertEnter * :echo "1"]]
|
|
|
|
command [[au InsertEnter * :echo "2"]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { event = { 'InsertEnter', 'InsertLeave' } }
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(2, #aus)
|
|
|
|
end)
|
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns different IDs for different autocmds', function()
|
2021-05-28 12:45:34 -07:00
|
|
|
command [[au! InsertEnter]]
|
|
|
|
command [[au! InsertLeave]]
|
|
|
|
command [[au InsertEnter * :echo "1"]]
|
|
|
|
source [[
|
2021-05-28 12:45:34 -07:00
|
|
|
call nvim_create_autocmd("InsertLeave", #{
|
2021-05-28 12:45:34 -07:00
|
|
|
\ command: ":echo 2",
|
|
|
|
\ })
|
|
|
|
]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { event = { 'InsertEnter', 'InsertLeave' } }
|
2021-05-28 12:45:34 -07:00
|
|
|
local first = aus[1]
|
|
|
|
eq(first.id, nil)
|
|
|
|
|
|
|
|
-- TODO: Maybe don't have this number, just assert it's not nil
|
|
|
|
local second = aus[2]
|
|
|
|
neq(second.id, nil)
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_del_autocmd(second.id)
|
|
|
|
local new_aus = api.nvim_get_autocmds { event = { 'InsertEnter', 'InsertLeave' } }
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(1, #new_aus)
|
|
|
|
eq(first, new_aus[1])
|
|
|
|
end)
|
2022-03-01 01:07:41 -07:00
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns event name', function()
|
2022-03-01 01:07:41 -07:00
|
|
|
command [[au! InsertEnter]]
|
|
|
|
command [[au InsertEnter * :echo "1"]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { event = 'InsertEnter' }
|
2022-03-01 01:07:41 -07:00
|
|
|
eq({
|
|
|
|
{
|
|
|
|
buflocal = false,
|
|
|
|
command = ':echo "1"',
|
|
|
|
event = 'InsertEnter',
|
|
|
|
once = false,
|
|
|
|
pattern = '*',
|
|
|
|
},
|
|
|
|
}, aus)
|
|
|
|
end)
|
2022-03-06 12:35:14 -07:00
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('works with buffer numbers', function()
|
2022-03-06 12:35:14 -07:00
|
|
|
command [[new]]
|
|
|
|
command [[au! InsertEnter]]
|
|
|
|
command [[au InsertEnter <buffer=1> :echo "1"]]
|
|
|
|
command [[au InsertEnter <buffer=2> :echo "2"]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { event = 'InsertEnter', buffer = 0 }
|
2022-03-06 12:35:14 -07:00
|
|
|
eq({
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
2022-03-06 12:35:14 -07:00
|
|
|
buffer = 2,
|
|
|
|
buflocal = true,
|
|
|
|
command = ':echo "2"',
|
|
|
|
event = 'InsertEnter',
|
|
|
|
once = false,
|
|
|
|
pattern = '<buffer=2>',
|
2024-01-02 18:09:18 -07:00
|
|
|
},
|
2022-03-06 12:35:14 -07:00
|
|
|
}, aus)
|
2024-01-02 18:09:18 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
aus = api.nvim_get_autocmds { event = 'InsertEnter', buffer = 1 }
|
2022-03-06 12:35:14 -07:00
|
|
|
eq({
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
2022-03-06 12:35:14 -07:00
|
|
|
buffer = 1,
|
|
|
|
buflocal = true,
|
|
|
|
command = ':echo "1"',
|
|
|
|
event = 'InsertEnter',
|
|
|
|
once = false,
|
|
|
|
pattern = '<buffer=1>',
|
2024-01-02 18:09:18 -07:00
|
|
|
},
|
2022-03-06 12:35:14 -07:00
|
|
|
}, aus)
|
2024-01-02 18:09:18 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
aus = api.nvim_get_autocmds { event = 'InsertEnter', buffer = { 1, 2 } }
|
2022-03-06 12:35:14 -07:00
|
|
|
eq({
|
2024-01-02 18:09:18 -07:00
|
|
|
{
|
2022-03-06 12:35:14 -07:00
|
|
|
buffer = 1,
|
|
|
|
buflocal = true,
|
|
|
|
command = ':echo "1"',
|
|
|
|
event = 'InsertEnter',
|
|
|
|
once = false,
|
|
|
|
pattern = '<buffer=1>',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
buffer = 2,
|
|
|
|
buflocal = true,
|
|
|
|
command = ':echo "2"',
|
|
|
|
event = 'InsertEnter',
|
|
|
|
once = false,
|
|
|
|
pattern = '<buffer=2>',
|
2024-01-02 18:09:18 -07:00
|
|
|
},
|
2022-03-06 12:35:14 -07:00
|
|
|
}, aus)
|
2024-01-02 18:09:18 -07:00
|
|
|
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'buffer': expected Integer or Array, got String",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, { event = 'InsertEnter', buffer = 'foo' })
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'buffer': expected Integer, got String",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, { event = 'InsertEnter', buffer = { 'foo', 42 } })
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-03-06 12:35:14 -07:00
|
|
|
eq(
|
|
|
|
'Invalid buffer id: 42',
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, { event = 'InsertEnter', buffer = { 42 } })
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-03-06 12:35:14 -07:00
|
|
|
|
|
|
|
local bufs = {}
|
|
|
|
for _ = 1, 257 do
|
2024-01-12 10:59:57 -07:00
|
|
|
table.insert(bufs, api.nvim_create_buf(true, false))
|
2022-03-06 12:35:14 -07:00
|
|
|
end
|
|
|
|
|
2023-02-14 03:19:04 -07:00
|
|
|
eq(
|
|
|
|
'Too many buffers (maximum of 256)',
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_get_autocmds, { event = 'InsertEnter', buffer = bufs })
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-03-06 12:35:14 -07:00
|
|
|
end)
|
2022-03-07 21:15:43 -07:00
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns autocmds when group is specified by id', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local auid = api.nvim_create_augroup('nvim_test_augroup', { clear = true })
|
|
|
|
api.nvim_create_autocmd('FileType', { group = auid, command = 'echo "1"' })
|
|
|
|
api.nvim_create_autocmd('FileType', { group = auid, command = 'echo "2"' })
|
2022-03-07 21:15:43 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { group = auid }
|
2022-03-07 21:15:43 -07:00
|
|
|
eq(2, #aus)
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus2 = api.nvim_get_autocmds { group = auid, event = 'InsertEnter' }
|
2022-03-07 21:15:43 -07:00
|
|
|
eq(0, #aus2)
|
|
|
|
end)
|
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns autocmds when group is specified by name', function()
|
2022-03-07 21:15:43 -07:00
|
|
|
local auname = 'nvim_test_augroup'
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_augroup(auname, { clear = true })
|
|
|
|
api.nvim_create_autocmd('FileType', { group = auname, command = 'echo "1"' })
|
|
|
|
api.nvim_create_autocmd('FileType', { group = auname, command = 'echo "2"' })
|
2022-03-07 21:15:43 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { group = auname }
|
2022-03-07 21:15:43 -07:00
|
|
|
eq(2, #aus)
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus2 = api.nvim_get_autocmds { group = auname, event = 'InsertEnter' }
|
2022-03-07 21:15:43 -07:00
|
|
|
eq(0, #aus2)
|
|
|
|
end)
|
2022-03-30 08:59:36 -07:00
|
|
|
|
|
|
|
it('should respect nested', function()
|
|
|
|
local bufs = exec_lua [[
|
|
|
|
local count = 0
|
|
|
|
vim.api.nvim_create_autocmd("BufNew", {
|
|
|
|
once = false,
|
|
|
|
nested = true,
|
|
|
|
callback = function()
|
|
|
|
count = count + 1
|
|
|
|
if count > 5 then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
vim.cmd(string.format("new README_%s.md", count))
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
|
|
|
vim.cmd "new First.md"
|
|
|
|
|
|
|
|
return vim.api.nvim_list_bufs()
|
|
|
|
]]
|
|
|
|
|
|
|
|
-- 1 for the first buffer
|
|
|
|
-- 2 for First.md
|
|
|
|
-- 3-7 for the 5 we make in the autocmd
|
|
|
|
eq({ 1, 2, 3, 4, 5, 6, 7 }, bufs)
|
|
|
|
end)
|
2022-06-09 06:18:56 -07:00
|
|
|
|
|
|
|
it('can retrieve a callback from an autocmd', function()
|
|
|
|
local content = 'I Am A Callback'
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('content', content)
|
2022-06-09 06:18:56 -07:00
|
|
|
|
|
|
|
local result = exec_lua([[
|
|
|
|
local cb = function() return vim.g.content end
|
|
|
|
vim.api.nvim_create_autocmd("User", {
|
|
|
|
pattern = "TestTrigger",
|
|
|
|
desc = "A test autocommand with a callback",
|
|
|
|
callback = cb,
|
|
|
|
})
|
|
|
|
local aus = vim.api.nvim_get_autocmds({ event = 'User', pattern = 'TestTrigger'})
|
|
|
|
local first = aus[1]
|
|
|
|
return {
|
|
|
|
cb = {
|
|
|
|
type = type(first.callback),
|
|
|
|
can_retrieve = first.callback() == vim.g.content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]])
|
|
|
|
|
|
|
|
eq('function', result.cb.type)
|
|
|
|
eq(true, result.cb.can_retrieve)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it(
|
|
|
|
'will return an empty string as the command for an autocmd that uses a callback',
|
|
|
|
function()
|
|
|
|
local result = exec_lua([[
|
|
|
|
local callback = function() print 'I Am A Callback' end
|
|
|
|
vim.api.nvim_create_autocmd("BufWritePost", {
|
|
|
|
pattern = "*.py",
|
|
|
|
callback = callback,
|
|
|
|
})
|
|
|
|
local aus = vim.api.nvim_get_autocmds({ event = 'BufWritePost' })
|
|
|
|
local first = aus[1]
|
|
|
|
return {
|
|
|
|
command = first.command,
|
|
|
|
cbtype = type(first.callback)
|
|
|
|
}
|
|
|
|
]])
|
|
|
|
|
|
|
|
eq({ command = '', cbtype = 'function' }, result)
|
|
|
|
end
|
|
|
|
)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('groups', function()
|
|
|
|
before_each(function()
|
|
|
|
command [[au! InsertEnter]]
|
|
|
|
|
|
|
|
command [[au InsertEnter * :echo "No Group"]]
|
|
|
|
|
|
|
|
command [[augroup GroupOne]]
|
|
|
|
command [[ au InsertEnter * :echo "GroupOne:1"]]
|
|
|
|
command [[augroup END]]
|
|
|
|
|
|
|
|
command [[augroup GroupTwo]]
|
|
|
|
command [[ au InsertEnter * :echo "GroupTwo:2"]]
|
|
|
|
command [[ au InsertEnter * :echo "GroupTwo:3"]]
|
|
|
|
command [[augroup END]]
|
|
|
|
end)
|
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns all groups if no group is specified', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { event = 'InsertEnter' }
|
2021-05-28 12:45:34 -07:00
|
|
|
if #aus ~= 4 then
|
|
|
|
eq({}, aus)
|
|
|
|
end
|
|
|
|
|
|
|
|
eq(4, #aus)
|
|
|
|
end)
|
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns only the group specified', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds {
|
2021-05-28 12:45:34 -07:00
|
|
|
event = 'InsertEnter',
|
|
|
|
group = 'GroupOne',
|
|
|
|
}
|
|
|
|
|
|
|
|
eq(1, #aus)
|
|
|
|
eq([[:echo "GroupOne:1"]], aus[1].command)
|
2022-05-05 08:00:47 -07:00
|
|
|
eq('GroupOne', aus[1].group_name)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns only the group specified, multiple values', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds {
|
2021-05-28 12:45:34 -07:00
|
|
|
event = 'InsertEnter',
|
|
|
|
group = 'GroupTwo',
|
|
|
|
}
|
|
|
|
|
|
|
|
eq(2, #aus)
|
|
|
|
eq([[:echo "GroupTwo:2"]], aus[1].command)
|
2022-05-05 08:00:47 -07:00
|
|
|
eq('GroupTwo', aus[1].group_name)
|
2021-05-28 12:45:34 -07:00
|
|
|
eq([[:echo "GroupTwo:3"]], aus[2].command)
|
2022-05-05 08:00:47 -07:00
|
|
|
eq('GroupTwo', aus[2].group_name)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('groups: 2', function()
|
2022-03-07 21:15:43 -07:00
|
|
|
it('raises error for undefined augroup name', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local success, code = unpack(api.nvim_exec_lua(
|
2021-05-28 12:45:34 -07:00
|
|
|
[[
|
|
|
|
return {pcall(function()
|
2021-05-28 12:45:34 -07:00
|
|
|
vim.api.nvim_create_autocmd("FileType", {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = "*",
|
|
|
|
group = "NotDefined",
|
|
|
|
command = "echo 'hello'",
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
end)}
|
|
|
|
]],
|
|
|
|
{}
|
|
|
|
))
|
|
|
|
|
|
|
|
eq(false, success)
|
2023-02-14 12:19:28 -07:00
|
|
|
matches("Invalid 'group': 'NotDefined'", code)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
2022-03-07 21:15:43 -07:00
|
|
|
|
|
|
|
it('raises error for undefined augroup id', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local success, code = unpack(api.nvim_exec_lua(
|
2022-03-07 21:15:43 -07:00
|
|
|
[[
|
|
|
|
return {pcall(function()
|
|
|
|
-- Make sure the augroup is deleted
|
|
|
|
vim.api.nvim_del_augroup_by_id(1)
|
|
|
|
|
|
|
|
vim.api.nvim_create_autocmd("FileType", {
|
|
|
|
pattern = "*",
|
|
|
|
group = 1,
|
|
|
|
command = "echo 'hello'",
|
|
|
|
})
|
|
|
|
end)}
|
|
|
|
]],
|
|
|
|
{}
|
|
|
|
))
|
|
|
|
|
|
|
|
eq(false, success)
|
2023-02-14 12:19:28 -07:00
|
|
|
matches("Invalid 'group': 1", code)
|
2022-03-07 21:15:43 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('raises error for invalid group type', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local success, code = unpack(api.nvim_exec_lua(
|
2022-03-07 21:15:43 -07:00
|
|
|
[[
|
|
|
|
return {pcall(function()
|
|
|
|
vim.api.nvim_create_autocmd("FileType", {
|
|
|
|
pattern = "*",
|
|
|
|
group = true,
|
|
|
|
command = "echo 'hello'",
|
|
|
|
})
|
|
|
|
end)}
|
|
|
|
]],
|
|
|
|
{}
|
|
|
|
))
|
|
|
|
|
|
|
|
eq(false, success)
|
2023-02-14 12:19:28 -07:00
|
|
|
matches("Invalid 'group': expected String or Integer, got Boolean", code)
|
2022-03-07 21:15:43 -07:00
|
|
|
end)
|
2023-01-12 08:25:44 -07:00
|
|
|
|
|
|
|
it('raises error for invalid pattern array', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local success, code = unpack(api.nvim_exec_lua(
|
2023-01-12 08:25:44 -07:00
|
|
|
[[
|
|
|
|
return {pcall(function()
|
|
|
|
vim.api.nvim_create_autocmd("FileType", {
|
|
|
|
pattern = {{}},
|
|
|
|
command = "echo 'hello'",
|
|
|
|
})
|
|
|
|
end)}
|
|
|
|
]],
|
|
|
|
{}
|
|
|
|
))
|
|
|
|
|
|
|
|
eq(false, success)
|
2023-02-14 06:07:38 -07:00
|
|
|
matches("Invalid 'pattern' item: expected String, got Array", code)
|
2023-01-12 08:25:44 -07:00
|
|
|
end)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('patterns', function()
|
|
|
|
before_each(function()
|
|
|
|
command [[au! InsertEnter]]
|
|
|
|
|
|
|
|
command [[au InsertEnter * :echo "No Group"]]
|
|
|
|
command [[au InsertEnter *.one :echo "GroupOne:1"]]
|
|
|
|
command [[au InsertEnter *.two :echo "GroupTwo:2"]]
|
|
|
|
command [[au InsertEnter *.two :echo "GroupTwo:3"]]
|
|
|
|
command [[au InsertEnter <buffer> :echo "Buffer"]]
|
|
|
|
end)
|
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns for literal match', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds {
|
2021-05-28 12:45:34 -07:00
|
|
|
event = 'InsertEnter',
|
|
|
|
pattern = '*',
|
|
|
|
}
|
|
|
|
|
|
|
|
eq(1, #aus)
|
|
|
|
eq([[:echo "No Group"]], aus[1].command)
|
|
|
|
end)
|
|
|
|
|
2023-02-14 06:07:38 -07:00
|
|
|
it('returns for multiple matches', function()
|
2021-05-28 12:45:34 -07:00
|
|
|
-- vim.api.nvim_get_autocmds
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds {
|
2021-05-28 12:45:34 -07:00
|
|
|
event = 'InsertEnter',
|
|
|
|
pattern = { '*.one', '*.two' },
|
|
|
|
}
|
|
|
|
|
|
|
|
eq(3, #aus)
|
|
|
|
eq([[:echo "GroupOne:1"]], aus[1].command)
|
|
|
|
eq([[:echo "GroupTwo:2"]], aus[2].command)
|
|
|
|
eq([[:echo "GroupTwo:3"]], aus[3].command)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should work for buffer autocmds', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local normalized_aus = api.nvim_get_autocmds {
|
2021-05-28 12:45:34 -07:00
|
|
|
event = 'InsertEnter',
|
|
|
|
pattern = '<buffer=1>',
|
|
|
|
}
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local raw_aus = api.nvim_get_autocmds {
|
2021-05-28 12:45:34 -07:00
|
|
|
event = 'InsertEnter',
|
|
|
|
pattern = '<buffer>',
|
|
|
|
}
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local zero_aus = api.nvim_get_autocmds {
|
2021-05-28 12:45:34 -07:00
|
|
|
event = 'InsertEnter',
|
|
|
|
pattern = '<buffer=0>',
|
|
|
|
}
|
|
|
|
|
|
|
|
eq(normalized_aus, raw_aus)
|
|
|
|
eq(normalized_aus, zero_aus)
|
|
|
|
eq([[:echo "Buffer"]], normalized_aus[1].command)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2022-03-31 10:13:22 -07:00
|
|
|
describe('nvim_exec_autocmds', function()
|
2023-02-14 06:07:38 -07:00
|
|
|
it('validation', function()
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'group': 9997999",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_exec_autocmds, 'FileType', {
|
2023-02-14 06:07:38 -07:00
|
|
|
group = 9997999,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'group': 'bogus'",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_exec_autocmds, 'FileType', {
|
2023-02-14 06:07:38 -07:00
|
|
|
group = 'bogus',
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 12:19:28 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'group': expected String or Integer, got Array",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_exec_autocmds, 'FileType', {
|
2023-02-14 06:07:38 -07:00
|
|
|
group = {},
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-04-22 18:23:25 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'group': 0",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_exec_autocmds, 'FileType', {
|
2023-04-22 18:23:25 -07:00
|
|
|
group = 0,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-08-02 04:00:13 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'buffer': expected Buffer, got Array",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_exec_autocmds, 'FileType', {
|
2023-02-14 06:07:38 -07:00
|
|
|
buffer = {},
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 06:07:38 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'event' item: expected String, got Array",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_exec_autocmds, { 'FileType', {} }, {})
|
2023-02-14 06:07:38 -07:00
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
it('can trigger builtin autocmds', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('autocmd_executed', false)
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('BufReadPost', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '*',
|
|
|
|
command = 'let g:autocmd_executed = v:true',
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(false, api.nvim_get_var('autocmd_executed'))
|
|
|
|
api.nvim_exec_autocmds('BufReadPost', {})
|
|
|
|
eq(true, api.nvim_get_var('autocmd_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
2022-06-25 10:50:06 -07:00
|
|
|
it('can trigger multiple patterns', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('autocmd_executed', 0)
|
2022-06-25 10:50:06 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('BufReadPost', {
|
2022-06-25 10:50:06 -07:00
|
|
|
pattern = '*',
|
|
|
|
command = 'let g:autocmd_executed += 1',
|
|
|
|
})
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('BufReadPost', { pattern = { '*.lua', '*.vim' } })
|
|
|
|
eq(2, api.nvim_get_var('autocmd_executed'))
|
2022-06-25 10:50:06 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('BufReadPre', {
|
2022-06-25 10:50:06 -07:00
|
|
|
pattern = { 'bar', 'foo' },
|
|
|
|
command = 'let g:autocmd_executed += 10',
|
|
|
|
})
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('BufReadPre', { pattern = { 'foo', 'bar', 'baz', 'frederick' } })
|
|
|
|
eq(22, api.nvim_get_var('autocmd_executed'))
|
2022-06-25 10:50:06 -07:00
|
|
|
end)
|
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
it('can pass the buffer', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('buffer_executed', -1)
|
|
|
|
eq(-1, api.nvim_get_var('buffer_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('BufLeave', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '*',
|
|
|
|
command = 'let g:buffer_executed = +expand("<abuf>")',
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- Doesn't execute for other non-matching events
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('CursorHold', { buffer = 1 })
|
|
|
|
eq(-1, api.nvim_get_var('buffer_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('BufLeave', { buffer = 1 })
|
|
|
|
eq(1, api.nvim_get_var('buffer_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can pass the filename, pattern match', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('filename_executed', 'none')
|
|
|
|
eq('none', api.nvim_get_var('filename_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('BufEnter', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '*.py',
|
|
|
|
command = 'let g:filename_executed = expand("<afile>")',
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- Doesn't execute for other non-matching events
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('CursorHold', { buffer = 1 })
|
|
|
|
eq('none', api.nvim_get_var('filename_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 06:11:28 -07:00
|
|
|
command('edit __init__.py')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('__init__.py', api.nvim_get_var('filename_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('cannot pass buf and fname', function()
|
2022-03-31 10:13:22 -07:00
|
|
|
local ok = pcall(
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds,
|
2022-03-31 10:13:22 -07:00
|
|
|
'BufReadPre',
|
|
|
|
{ pattern = 'literally_cannot_error.rs', buffer = 1 }
|
|
|
|
)
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(false, ok)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can pass the filename, exact match', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('filename_executed', 'none')
|
|
|
|
eq('none', api.nvim_get_var('filename_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 06:11:28 -07:00
|
|
|
command('edit other_file.txt')
|
|
|
|
command('edit __init__.py')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('none', api.nvim_get_var('filename_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('CursorHoldI', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '__init__.py',
|
|
|
|
command = 'let g:filename_executed = expand("<afile>")',
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- Doesn't execute for other non-matching events
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('CursorHoldI', { buffer = 1 })
|
|
|
|
eq('none', api.nvim_get_var('filename_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('CursorHoldI', { buffer = api.nvim_get_current_buf() })
|
|
|
|
eq('__init__.py', api.nvim_get_var('filename_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- Reset filename
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('filename_executed', 'none')
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('CursorHoldI', { pattern = '__init__.py' })
|
|
|
|
eq('__init__.py', api.nvim_get_var('filename_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with user autocmds', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('matched', 'none')
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('User', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = 'TestCommand',
|
|
|
|
command = 'let g:matched = "matched"',
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_exec_autocmds('User', { pattern = 'OtherCommand' })
|
|
|
|
eq('none', api.nvim_get_var('matched'))
|
|
|
|
api.nvim_exec_autocmds('User', { pattern = 'TestCommand' })
|
|
|
|
eq('matched', api.nvim_get_var('matched'))
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
2022-03-07 21:15:43 -07:00
|
|
|
|
|
|
|
it('can pass group by id', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('group_executed', false)
|
2022-03-07 21:15:43 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local auid = api.nvim_create_augroup('nvim_test_augroup', { clear = true })
|
|
|
|
api.nvim_create_autocmd('FileType', {
|
2022-03-07 21:15:43 -07:00
|
|
|
group = auid,
|
|
|
|
command = 'let g:group_executed = v:true',
|
|
|
|
})
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(false, api.nvim_get_var('group_executed'))
|
|
|
|
api.nvim_exec_autocmds('FileType', { group = auid })
|
|
|
|
eq(true, api.nvim_get_var('group_executed'))
|
2022-03-07 21:15:43 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can pass group by name', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('group_executed', false)
|
2022-03-07 21:15:43 -07:00
|
|
|
|
|
|
|
local auname = 'nvim_test_augroup'
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_augroup(auname, { clear = true })
|
|
|
|
api.nvim_create_autocmd('FileType', {
|
2022-03-07 21:15:43 -07:00
|
|
|
group = auname,
|
|
|
|
command = 'let g:group_executed = v:true',
|
|
|
|
})
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(false, api.nvim_get_var('group_executed'))
|
|
|
|
api.nvim_exec_autocmds('FileType', { group = auname })
|
|
|
|
eq(true, api.nvim_get_var('group_executed'))
|
2022-03-07 21:15:43 -07:00
|
|
|
end)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('nvim_create_augroup', function()
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('executed', 0)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
local make_counting_autocmd = function(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
|
|
|
|
local resulting = {
|
|
|
|
pattern = '*',
|
|
|
|
command = 'let g:executed = g:executed + 1',
|
|
|
|
}
|
|
|
|
|
|
|
|
resulting.group = opts.group
|
|
|
|
resulting.once = opts.once
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_autocmd('FileType', resulting)
|
2021-05-28 12:45:34 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local set_ft = function(ft)
|
|
|
|
ft = ft or 'txt'
|
|
|
|
source(string.format('set filetype=%s', ft))
|
|
|
|
end
|
|
|
|
|
|
|
|
local get_executed_count = function()
|
2024-01-12 10:59:57 -07:00
|
|
|
return api.nvim_get_var('executed')
|
2021-05-28 12:45:34 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
it('can be added in a group', function()
|
|
|
|
local augroup = 'TestGroup'
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_augroup(augroup, { clear = true })
|
2021-05-28 12:45:34 -07:00
|
|
|
make_counting_autocmd { group = augroup }
|
|
|
|
|
|
|
|
set_ft('txt')
|
|
|
|
set_ft('python')
|
|
|
|
|
2022-04-25 20:35:05 -07:00
|
|
|
eq(2, get_executed_count())
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('works getting called multiple times', function()
|
|
|
|
make_counting_autocmd()
|
|
|
|
set_ft()
|
|
|
|
set_ft()
|
|
|
|
set_ft()
|
|
|
|
|
2022-04-25 20:35:05 -07:00
|
|
|
eq(3, get_executed_count())
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('handles ++once', function()
|
|
|
|
make_counting_autocmd { once = true }
|
|
|
|
set_ft('txt')
|
|
|
|
set_ft('help')
|
|
|
|
set_ft('txt')
|
|
|
|
set_ft('help')
|
|
|
|
|
2022-04-25 20:35:05 -07:00
|
|
|
eq(1, get_executed_count())
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('errors on unexpected keys', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
local success, code = pcall(api.nvim_create_autocmd, 'FileType', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '*',
|
|
|
|
not_a_valid_key = 'NotDefined',
|
|
|
|
})
|
|
|
|
|
|
|
|
eq(false, success)
|
|
|
|
matches('not_a_valid_key', code)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can execute simple callback', function()
|
|
|
|
exec_lua(
|
|
|
|
[[
|
|
|
|
vim.g.executed = false
|
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
vim.api.nvim_create_autocmd("FileType", {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = "*",
|
|
|
|
callback = function() vim.g.executed = true end,
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
]],
|
|
|
|
{}
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(
|
|
|
|
true,
|
|
|
|
exec_lua(
|
|
|
|
[[
|
|
|
|
vim.cmd "set filetype=txt"
|
|
|
|
return vim.g.executed
|
|
|
|
]],
|
|
|
|
{}
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('calls multiple lua callbacks for the same autocmd execution', function()
|
|
|
|
eq(
|
|
|
|
4,
|
|
|
|
exec_lua(
|
|
|
|
[[
|
|
|
|
local count = 0
|
|
|
|
local counter = function()
|
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
vim.api.nvim_create_autocmd("FileType", {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = "*",
|
|
|
|
callback = counter,
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
vim.api.nvim_create_autocmd("FileType", {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = "*",
|
|
|
|
callback = counter,
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
vim.cmd "set filetype=txt"
|
|
|
|
vim.cmd "set filetype=txt"
|
|
|
|
|
|
|
|
return count
|
|
|
|
]],
|
|
|
|
{}
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('properly releases functions with ++once', function()
|
|
|
|
exec_lua([[
|
|
|
|
WeakTable = setmetatable({}, { __mode = "k" })
|
|
|
|
|
|
|
|
OnceCount = 0
|
|
|
|
|
|
|
|
MyVal = {}
|
|
|
|
WeakTable[MyVal] = true
|
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
vim.api.nvim_create_autocmd("FileType", {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = "*",
|
|
|
|
callback = function()
|
|
|
|
OnceCount = OnceCount + 1
|
|
|
|
MyVal = {}
|
|
|
|
end,
|
|
|
|
once = true
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
]])
|
|
|
|
|
|
|
|
command [[set filetype=txt]]
|
|
|
|
eq(1, exec_lua([[return OnceCount]], {}))
|
|
|
|
|
|
|
|
exec_lua([[collectgarbage()]], {})
|
|
|
|
|
|
|
|
command [[set filetype=txt]]
|
|
|
|
eq(1, exec_lua([[return OnceCount]], {}))
|
|
|
|
|
|
|
|
eq(
|
|
|
|
0,
|
|
|
|
exec_lua([[
|
|
|
|
local count = 0
|
|
|
|
for _ in pairs(WeakTable) do
|
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
return count
|
|
|
|
]]),
|
|
|
|
'Should have no keys remaining'
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('groups can be cleared', function()
|
|
|
|
local augroup = 'TestGroup'
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_augroup(augroup, { clear = true })
|
|
|
|
api.nvim_create_autocmd('FileType', {
|
2021-05-28 12:45:34 -07:00
|
|
|
group = augroup,
|
|
|
|
command = 'let g:executed = g:executed + 1',
|
|
|
|
})
|
|
|
|
|
|
|
|
set_ft('txt')
|
|
|
|
set_ft('txt')
|
|
|
|
eq(2, get_executed_count(), 'should only count twice')
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_augroup(augroup, { clear = true })
|
|
|
|
eq({}, api.nvim_get_autocmds { group = augroup })
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
set_ft('txt')
|
|
|
|
set_ft('txt')
|
|
|
|
eq(2, get_executed_count(), 'No additional counts')
|
|
|
|
end)
|
|
|
|
|
2022-03-31 01:59:14 -07:00
|
|
|
it('can delete non-existent groups with pcall', function()
|
|
|
|
eq(false, exec_lua [[return pcall(vim.api.nvim_del_augroup_by_name, 'noexist')]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('Vim:E367: No such group: "noexist"', pcall_err(api.nvim_del_augroup_by_name, 'noexist'))
|
2022-03-31 01:59:14 -07:00
|
|
|
|
|
|
|
eq(false, exec_lua [[return pcall(vim.api.nvim_del_augroup_by_id, -12342)]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('Vim:E367: No such group: "--Deleted--"', pcall_err(api.nvim_del_augroup_by_id, -12312))
|
2023-04-22 18:23:25 -07:00
|
|
|
|
|
|
|
eq(false, exec_lua [[return pcall(vim.api.nvim_del_augroup_by_id, 0)]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('Vim:E367: No such group: "[NULL]"', pcall_err(api.nvim_del_augroup_by_id, 0))
|
2023-04-22 18:23:25 -07:00
|
|
|
|
|
|
|
eq(false, exec_lua [[return pcall(vim.api.nvim_del_augroup_by_id, 12342)]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('Vim:E367: No such group: "[NULL]"', pcall_err(api.nvim_del_augroup_by_id, 12312))
|
2022-03-31 01:59:14 -07:00
|
|
|
end)
|
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
it('groups work with once', function()
|
|
|
|
local augroup = 'TestGroup'
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_augroup(augroup, { clear = true })
|
2021-05-28 12:45:34 -07:00
|
|
|
make_counting_autocmd { group = augroup, once = true }
|
|
|
|
|
|
|
|
set_ft('txt')
|
|
|
|
set_ft('python')
|
|
|
|
|
2022-04-25 20:35:05 -07:00
|
|
|
eq(1, get_executed_count())
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('autocmds can be registered multiple times.', function()
|
|
|
|
local augroup = 'TestGroup'
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_augroup(augroup, { clear = true })
|
2021-05-28 12:45:34 -07:00
|
|
|
make_counting_autocmd { group = augroup, once = false }
|
|
|
|
make_counting_autocmd { group = augroup, once = false }
|
|
|
|
make_counting_autocmd { group = augroup, once = false }
|
|
|
|
|
|
|
|
set_ft('txt')
|
|
|
|
set_ft('python')
|
|
|
|
|
2022-04-25 20:35:05 -07:00
|
|
|
eq(3 * 2, get_executed_count())
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can be deleted', function()
|
|
|
|
local augroup = 'WillBeDeleted'
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_augroup(augroup, { clear = true })
|
|
|
|
api.nvim_create_autocmd({ 'FileType' }, {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '*',
|
|
|
|
command = "echo 'does not matter'",
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- Clears the augroup from before, which erases the autocmd
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_augroup(augroup, { clear = true })
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local result = #api.nvim_get_autocmds { group = augroup }
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
eq(0, result)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('can be used for buffer local autocmds', function()
|
|
|
|
local augroup = 'WillBeDeleted'
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('value_set', false)
|
2021-05-28 12:45:34 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_create_augroup(augroup, { clear = true })
|
|
|
|
api.nvim_create_autocmd('FileType', {
|
2021-05-28 12:45:34 -07:00
|
|
|
pattern = '<buffer>',
|
|
|
|
command = 'let g:value_set = v:true',
|
2021-05-28 12:45:34 -07:00
|
|
|
})
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
command 'new'
|
|
|
|
command 'set filetype=python'
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(false, api.nvim_get_var('value_set'))
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('can accept vimscript functions', function()
|
|
|
|
source [[
|
|
|
|
let g:vimscript_executed = 0
|
|
|
|
|
|
|
|
function! MyVimscriptFunction() abort
|
|
|
|
let g:vimscript_executed = g:vimscript_executed + 1
|
|
|
|
endfunction
|
|
|
|
|
2021-05-28 12:45:34 -07:00
|
|
|
call nvim_create_autocmd("FileType", #{
|
2021-05-28 12:45:34 -07:00
|
|
|
\ pattern: ["python", "javascript"],
|
|
|
|
\ callback: "MyVimscriptFunction",
|
|
|
|
\ })
|
|
|
|
|
|
|
|
set filetype=txt
|
|
|
|
set filetype=python
|
|
|
|
set filetype=txt
|
|
|
|
set filetype=javascript
|
|
|
|
set filetype=txt
|
|
|
|
]]
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(2, api.nvim_get_var('vimscript_executed'))
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('augroup!', function()
|
|
|
|
it('legacy: should clear and not return any autocmds for delete groups', function()
|
|
|
|
command('augroup TEMP_A')
|
|
|
|
command(' autocmd! BufReadPost *.py :echo "Hello"')
|
|
|
|
command('augroup END')
|
|
|
|
|
|
|
|
command('augroup! TEMP_A')
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(false, pcall(api.nvim_get_autocmds, { group = 'TEMP_A' }))
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- For some reason, augroup! doesn't clear the autocmds themselves, which is just wild
|
|
|
|
-- but we managed to keep this behavior.
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(1, #api.nvim_get_autocmds { event = 'BufReadPost' })
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('legacy: remove augroups that have no autocmds', function()
|
|
|
|
command('augroup TEMP_AB')
|
|
|
|
command('augroup END')
|
|
|
|
|
|
|
|
command('augroup! TEMP_AB')
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(false, pcall(api.nvim_get_autocmds, { group = 'TEMP_AB' }))
|
|
|
|
eq(0, #api.nvim_get_autocmds { event = 'BufReadPost' })
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('legacy: multiple remove and add augroup', function()
|
|
|
|
command('augroup TEMP_ABC')
|
|
|
|
command(' au!')
|
|
|
|
command(' autocmd BufReadPost *.py echo "Hello"')
|
|
|
|
command('augroup END')
|
|
|
|
|
|
|
|
command('augroup! TEMP_ABC')
|
|
|
|
|
|
|
|
-- Should still have one autocmd :'(
|
2024-01-12 10:59:57 -07:00
|
|
|
local aus = api.nvim_get_autocmds { event = 'BufReadPost' }
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(1, #aus, aus)
|
|
|
|
|
|
|
|
command('augroup TEMP_ABC')
|
|
|
|
command(' au!')
|
|
|
|
command(' autocmd BufReadPost *.py echo "Hello"')
|
|
|
|
command('augroup END')
|
|
|
|
|
|
|
|
-- Should now have two autocmds :'(
|
2024-01-12 10:59:57 -07:00
|
|
|
aus = api.nvim_get_autocmds { event = 'BufReadPost' }
|
2021-05-28 12:45:34 -07:00
|
|
|
eq(2, #aus, aus)
|
|
|
|
|
|
|
|
command('augroup! TEMP_ABC')
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(false, pcall(api.nvim_get_autocmds, { group = 'TEMP_ABC' }))
|
|
|
|
eq(2, #api.nvim_get_autocmds { event = 'BufReadPost' })
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('api: should clear and not return any autocmds for delete groups by id', function()
|
|
|
|
command('augroup TEMP_ABCD')
|
|
|
|
command('autocmd! BufReadPost *.py :echo "Hello"')
|
|
|
|
command('augroup END')
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local augroup_id = api.nvim_create_augroup('TEMP_ABCD', { clear = false })
|
|
|
|
api.nvim_del_augroup_by_id(augroup_id)
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- For good reason, we kill all the autocmds from del_augroup,
|
|
|
|
-- so now this works as expected
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(false, pcall(api.nvim_get_autocmds, { group = 'TEMP_ABCD' }))
|
|
|
|
eq(0, #api.nvim_get_autocmds { event = 'BufReadPost' })
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('api: should clear and not return any autocmds for delete groups by name', function()
|
|
|
|
command('augroup TEMP_ABCDE')
|
|
|
|
command('autocmd! BufReadPost *.py :echo "Hello"')
|
|
|
|
command('augroup END')
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_del_augroup_by_name('TEMP_ABCDE')
|
2021-05-28 12:45:34 -07:00
|
|
|
|
|
|
|
-- For good reason, we kill all the autocmds from del_augroup,
|
|
|
|
-- so now this works as expected
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(false, pcall(api.nvim_get_autocmds, { group = 'TEMP_ABCDE' }))
|
|
|
|
eq(0, #api.nvim_get_autocmds { event = 'BufReadPost' })
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|
|
|
|
end)
|
2022-03-12 13:12:02 -07:00
|
|
|
|
2022-03-31 10:13:22 -07:00
|
|
|
describe('nvim_clear_autocmds', function()
|
2023-02-14 06:07:38 -07:00
|
|
|
it('validation', function()
|
|
|
|
eq(
|
|
|
|
"Cannot use both 'pattern' and 'buffer'",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_clear_autocmds, {
|
2023-02-14 06:07:38 -07:00
|
|
|
pattern = '*',
|
|
|
|
buffer = 42,
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-02-14 06:07:38 -07:00
|
|
|
eq(
|
|
|
|
"Invalid 'event' item: expected String, got Array",
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(api.nvim_clear_autocmds, {
|
2023-02-14 06:07:38 -07:00
|
|
|
event = { 'FileType', {} },
|
|
|
|
})
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq("Invalid 'group': 0", pcall_err(api.nvim_clear_autocmds, { group = 0 }))
|
2023-02-14 06:07:38 -07:00
|
|
|
end)
|
|
|
|
|
2022-03-12 13:12:02 -07:00
|
|
|
it('should clear based on event + pattern', function()
|
|
|
|
command('autocmd InsertEnter *.py :echo "Python can be cool sometimes"')
|
|
|
|
command('autocmd InsertEnter *.txt :echo "Text Files Are Cool"')
|
|
|
|
|
|
|
|
local search = { event = 'InsertEnter', pattern = '*.txt' }
|
2024-01-12 10:59:57 -07:00
|
|
|
local before_delete = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(1, #before_delete)
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local before_delete_all = api.nvim_get_autocmds { event = search.event }
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(2, #before_delete_all)
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_clear_autocmds(search)
|
|
|
|
local after_delete = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(0, #after_delete)
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local after_delete_all = api.nvim_get_autocmds { event = search.event }
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(1, #after_delete_all)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should clear based on event', function()
|
|
|
|
command('autocmd InsertEnter *.py :echo "Python can be cool sometimes"')
|
|
|
|
command('autocmd InsertEnter *.txt :echo "Text Files Are Cool"')
|
|
|
|
|
|
|
|
local search = { event = 'InsertEnter' }
|
2024-01-12 10:59:57 -07:00
|
|
|
local before_delete = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(2, #before_delete)
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_clear_autocmds(search)
|
|
|
|
local after_delete = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(0, #after_delete)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should clear based on pattern', function()
|
|
|
|
command('autocmd InsertEnter *.TestPat1 :echo "Enter 1"')
|
|
|
|
command('autocmd InsertLeave *.TestPat1 :echo "Leave 1"')
|
|
|
|
command('autocmd InsertEnter *.TestPat2 :echo "Enter 2"')
|
|
|
|
command('autocmd InsertLeave *.TestPat2 :echo "Leave 2"')
|
|
|
|
|
|
|
|
local search = { pattern = '*.TestPat1' }
|
2024-01-12 10:59:57 -07:00
|
|
|
local before_delete = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(2, #before_delete)
|
2024-01-12 05:44:54 -07:00
|
|
|
local before_delete_events =
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_get_autocmds { event = { 'InsertEnter', 'InsertLeave' } }
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(4, #before_delete_events)
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_clear_autocmds(search)
|
|
|
|
local after_delete = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(0, #after_delete)
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
local after_delete_events = api.nvim_get_autocmds { event = { 'InsertEnter', 'InsertLeave' } }
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(2, #after_delete_events)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should allow clearing by buffer', function()
|
|
|
|
command('autocmd! InsertEnter')
|
|
|
|
command('autocmd InsertEnter <buffer> :echo "Enter Buffer"')
|
|
|
|
command('autocmd InsertEnter *.TestPat1 :echo "Enter Pattern"')
|
|
|
|
|
|
|
|
local search = { event = 'InsertEnter' }
|
2024-01-12 10:59:57 -07:00
|
|
|
local before_delete = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(2, #before_delete)
|
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_clear_autocmds { buffer = 0 }
|
|
|
|
local after_delete = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(1, #after_delete)
|
|
|
|
eq('*.TestPat1', after_delete[1].pattern)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should allow clearing by buffer and group', function()
|
|
|
|
command('augroup TestNvimClearAutocmds')
|
|
|
|
command(' au!')
|
|
|
|
command(' autocmd InsertEnter <buffer> :echo "Enter Buffer"')
|
|
|
|
command(' autocmd InsertEnter *.TestPat1 :echo "Enter Pattern"')
|
|
|
|
command('augroup END')
|
|
|
|
|
|
|
|
local search = { event = 'InsertEnter', group = 'TestNvimClearAutocmds' }
|
2024-01-12 10:59:57 -07:00
|
|
|
local before_delete = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(2, #before_delete)
|
|
|
|
|
|
|
|
-- Doesn't clear without passing group.
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_clear_autocmds { buffer = 0 }
|
|
|
|
local without_group = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(2, #without_group)
|
|
|
|
|
2023-10-29 01:02:32 -07:00
|
|
|
-- Doesn't clear with passing group.
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_clear_autocmds { buffer = 0, group = search.group }
|
|
|
|
local with_group = api.nvim_get_autocmds(search)
|
2022-03-12 13:12:02 -07:00
|
|
|
eq(1, #with_group)
|
|
|
|
end)
|
|
|
|
end)
|
2021-05-28 12:45:34 -07:00
|
|
|
end)
|