WinClosed: sort auevents.lua; improve tests

- test: reduce verbosity, condense redundancy, improve readability
- auevents.lua: keep events sorted by name. ref afd1d412fa
This commit is contained in:
Justin M. Keyes
2020-01-13 00:19:20 -08:00
parent 2b8e66c6ce
commit 156c25e498
5 changed files with 105 additions and 101 deletions

View File

@@ -576,8 +576,7 @@ ColorSchemePre Before loading a color scheme. |:colorscheme|
CompleteChanged *CompleteChanged* CompleteChanged *CompleteChanged*
After each time the Insert mode completion After each time the Insert mode completion
menu changed. Not fired on popup menu hide, menu changed. Not fired on popup menu hide,
use |CompleteDone| for that. Never triggered use |CompleteDone| for that.
recursively.
Sets these |v:event| keys: Sets these |v:event| keys:
completed_item See |complete-items|. completed_item See |complete-items|.
@@ -588,7 +587,8 @@ CompleteChanged *CompleteChanged*
size total nr of items size total nr of items
scrollbar TRUE if visible scrollbar TRUE if visible
It is not allowed to change the text |textlock|. Non-recursive (event cannot trigger itself).
Cannot change the text. |textlock|
The size and position of the popup are also The size and position of the popup are also
available by calling |pum_getpos()|. available by calling |pum_getpos()|.
@@ -661,7 +661,7 @@ DirChanged After the |current-directory| was changed.
Sets these |v:event| keys: Sets these |v:event| keys:
cwd: current working directory cwd: current working directory
scope: "global", "tab", "window" scope: "global", "tab", "window"
Recursion is ignored. Non-recursive (event cannot trigger itself).
*FileAppendCmd* *FileAppendCmd*
FileAppendCmd Before appending to a file. Should do the FileAppendCmd Before appending to a file. Should do the
appending to the file. Use the '[ and '] appending to the file. Use the '[ and ']
@@ -697,6 +697,7 @@ ExitPre When using `:quit`, `:wq` in a way it makes
cancelled if there is a modified buffer that cancelled if there is a modified buffer that
isn't automatically saved, use |VimLeavePre| isn't automatically saved, use |VimLeavePre|
for really exiting. for really exiting.
See also |QuitPre|, |WinClosed|.
*FileChangedShell* *FileChangedShell*
FileChangedShell When Vim notices that the modification time of FileChangedShell When Vim notices that the modification time of
a file has changed since editing started. a file has changed since editing started.
@@ -841,7 +842,7 @@ TextYankPost Just after a |yank| or |deleting| command, but not
and |']| marks can be used to calculate the and |']| marks can be used to calculate the
precise region of the operation. precise region of the operation.
Recursion is ignored. Non-recursive (event cannot trigger itself).
Cannot change the text. |textlock| Cannot change the text. |textlock|
*InsertEnter* *InsertEnter*
InsertEnter Just before starting Insert mode. Also for InsertEnter Just before starting Insert mode. Also for
@@ -908,8 +909,8 @@ OptionSet After setting an option (except during
always use |:noautocmd| to prevent triggering always use |:noautocmd| to prevent triggering
OptionSet. OptionSet.
Recursion is ignored, thus |:set| in the Non-recursive: |:set| in the autocommand does
autocommand does not trigger OptionSet again. not trigger OptionSet again.
*QuickFixCmdPre* *QuickFixCmdPre*
QuickFixCmdPre Before a quickfix command is run (|:make|, QuickFixCmdPre Before a quickfix command is run (|:make|,
@@ -941,7 +942,7 @@ QuitPre When using `:quit`, `:wq` or `:qall`, before
or quits Vim. Can be used to close any or quits Vim. Can be used to close any
non-essential window if the current window is non-essential window if the current window is
the last ordinary window. the last ordinary window.
Also see |ExitPre|. See also |ExitPre|, ||WinClosed|.
*RemoteReply* *RemoteReply*
RemoteReply When a reply from a Vim that functions as RemoteReply When a reply from a Vim that functions as
server was received |server2client()|. The server was received |server2client()|. The
@@ -1042,8 +1043,8 @@ TabNew When creating a new tab page. |tab-page|
TabNewEntered After entering a new tab page. |tab-page| TabNewEntered After entering a new tab page. |tab-page|
After BufEnter. After BufEnter.
*TabClosed* *TabClosed*
TabClosed After closing a tab page. <afile> can be used TabClosed After closing a tab page. <afile> expands to
for the tab page number. the tab page number.
*TermOpen* *TermOpen*
TermOpen When a |terminal| job is starting. Can be TermOpen When a |terminal| job is starting. Can be
used to configure the terminal buffer. used to configure the terminal buffer.
@@ -1133,7 +1134,10 @@ VimResume After Nvim resumes from |suspend| state.
*VimSuspend* *VimSuspend*
VimSuspend Before Nvim enters |suspend| state. VimSuspend Before Nvim enters |suspend| state.
*WinClosed* *WinClosed*
WinClosed After closing a window. WinClosed After closing a window. <afile> expands to the
|window-ID|.
Non-recursive (event cannot trigger itself).
See also |ExitPre|, |QuitPre|.
*WinEnter* *WinEnter*
WinEnter After entering another window. Not done for WinEnter After entering another window. Not done for
the first window, when Vim has just started. the first window, when Vim has just started.

View File

@@ -21,12 +21,12 @@ return {
'BufWritePre', -- before writing a buffer 'BufWritePre', -- before writing a buffer
'ChanInfo', -- info was received about channel 'ChanInfo', -- info was received about channel
'ChanOpen', -- channel was opened 'ChanOpen', -- channel was opened
'CmdlineChanged', -- command line was modified
'CmdlineEnter', -- after entering cmdline mode
'CmdlineLeave', -- before leaving cmdline mode
'CmdUndefined', -- command undefined 'CmdUndefined', -- command undefined
'CmdWinEnter', -- after entering the cmdline window 'CmdWinEnter', -- after entering the cmdline window
'CmdWinLeave', -- before leaving the cmdline window 'CmdWinLeave', -- before leaving the cmdline window
'CmdlineChanged', -- command line was modified
'CmdlineEnter', -- after entering cmdline mode
'CmdlineLeave', -- before leaving cmdline mode
'ColorScheme', -- after loading a colorscheme 'ColorScheme', -- after loading a colorscheme
'ColorSchemePre', -- before loading a colorscheme 'ColorSchemePre', -- before loading a colorscheme
'CompleteChanged', -- after popup menu changed 'CompleteChanged', -- after popup menu changed
@@ -76,8 +76,8 @@ return {
'ShellFilterPost', -- after ":1,2!cmd", ":w !cmd", ":r !cmd". 'ShellFilterPost', -- after ":1,2!cmd", ":w !cmd", ":r !cmd".
'Signal', -- after nvim process received a signal 'Signal', -- after nvim process received a signal
'SourceCmd', -- sourcing a Vim script using command 'SourceCmd', -- sourcing a Vim script using command
'SourcePre', -- before sourcing a Vim script
'SourcePost', -- after sourcing a Vim script 'SourcePost', -- after sourcing a Vim script
'SourcePre', -- before sourcing a Vim script
'SpellFileMissing', -- spell file missing 'SpellFileMissing', -- spell file missing
'StdinReadPost', -- after reading from stdin 'StdinReadPost', -- after reading from stdin
'StdinReadPre', -- before reading from stdin 'StdinReadPre', -- before reading from stdin
@@ -107,10 +107,10 @@ return {
'VimResized', -- after Vim window was resized 'VimResized', -- after Vim window was resized
'VimResume', -- after Nvim is resumed 'VimResume', -- after Nvim is resumed
'VimSuspend', -- before Nvim is suspended 'VimSuspend', -- before Nvim is suspended
'WinClosed', -- after closing a window
'WinEnter', -- after entering a window 'WinEnter', -- after entering a window
'WinLeave', -- before leaving a window 'WinLeave', -- before leaving a window
'WinNew', -- when entering a new window 'WinNew', -- when entering a new window
'WinClosed', -- after closing a window
}, },
aliases = { aliases = {
BufCreate = 'BufAdd', BufCreate = 'BufAdd',

View File

@@ -2695,8 +2695,7 @@ static void do_autocmd_winclosed(win_T *win)
return; return;
} }
recursive = true; recursive = true;
char_u winid[NUMBUFLEN];
char_u winid[10];
vim_snprintf((char *)winid, sizeof(winid), "%i", win->handle); vim_snprintf((char *)winid, sizeof(winid), "%i", win->handle);
apply_autocmds(EVENT_WINCLOSED, winid, winid, false, win->w_buffer); apply_autocmds(EVENT_WINCLOSED, winid, winid, false, win->w_buffer);
recursive = false; recursive = false;

View File

@@ -1,8 +1,8 @@
local helpers = require('test.functional.helpers')(after_each) local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen') local Screen = require('test.functional.ui.screen')
local assert_visible = helpers.assert_visible
local dedent = helpers.dedent local dedent = helpers.dedent
local neq = helpers.neq
local eq = helpers.eq local eq = helpers.eq
local eval = helpers.eval local eval = helpers.eval
local feed = helpers.feed local feed = helpers.feed
@@ -19,98 +19,86 @@ local source = helpers.source
describe('autocmd', function() describe('autocmd', function()
before_each(clear) before_each(clear)
it(':tabnew triggers events in the correct order', function() it(':tabnew, :split, :close events order, <afile>', function()
local expected = { local expected = {
'WinLeave', {'WinLeave', ''},
'TabLeave', {'TabLeave', ''},
'WinEnter', {'WinEnter', ''},
'TabNew', {'TabNew', 'testfile1'}, -- :tabnew
'TabEnter', {'TabEnter', ''},
'BufLeave', {'BufLeave', ''},
'BufEnter' {'BufEnter', 'testfile1'}, -- :split
{'WinLeave', 'testfile1'},
{'WinEnter', 'testfile1'},
{'WinLeave', 'testfile1'},
{'WinClosed', '1002'}, -- :close, WinClosed <afile> = window-id
{'WinEnter', 'testfile1'},
{'WinLeave', 'testfile1'}, -- :bdelete
{'WinEnter', 'testfile1'},
{'BufLeave', 'testfile1'},
{'BufEnter', 'testfile2'},
{'WinClosed', '1000'},
} }
command('let g:foo = []') command('let g:evs = []')
command('autocmd BufEnter * :call add(g:foo, "BufEnter")') command('autocmd BufEnter * :call add(g:evs, ["BufEnter", expand("<afile>")])')
command('autocmd BufLeave * :call add(g:foo, "BufLeave")') command('autocmd BufLeave * :call add(g:evs, ["BufLeave", expand("<afile>")])')
command('autocmd TabEnter * :call add(g:foo, "TabEnter")') command('autocmd TabEnter * :call add(g:evs, ["TabEnter", expand("<afile>")])')
command('autocmd TabLeave * :call add(g:foo, "TabLeave")') command('autocmd TabLeave * :call add(g:evs, ["TabLeave", expand("<afile>")])')
command('autocmd TabNew * :call add(g:foo, "TabNew")') command('autocmd TabNew * :call add(g:evs, ["TabNew", expand("<afile>")])')
command('autocmd WinEnter * :call add(g:foo, "WinEnter")') command('autocmd WinEnter * :call add(g:evs, ["WinEnter", expand("<afile>")])')
command('autocmd WinLeave * :call add(g:foo, "WinLeave")') command('autocmd WinLeave * :call add(g:evs, ["WinLeave", expand("<afile>")])')
command('tabnew') command('autocmd WinClosed * :call add(g:evs, ["WinClosed", expand("<afile>")])')
assert.same(expected, eval('g:foo')) command('tabnew testfile1')
end) command('split')
it(':close triggers WinClosed event', function()
command('let g:triggered = 0')
command('new')
command('autocmd WinClosed <buffer> :let g:triggered+=1')
eq(0, eval('g:triggered'))
command('close') command('close')
eq(1, eval('g:triggered')) command('new testfile2')
command('bdelete 1')
eq(expected, eval('g:evs'))
end) end)
it('WinClosed event exposes window id as <afile>', function() it('WinClosed is non-recursive', function()
command('new')
local id = meths.get_current_win().id
helpers.nvim('command', 'au! WinClosed * echom "winclosed:".expand("<afile>").":".expand("<amatch>").":".win_getid()')
eq(string.format("winclosed:%s:%s:%s", id, id, id), helpers.nvim('exec', 'close', true))
end)
it(':bdelete triggers WinClosed event', function()
command('let g:triggered = 0') command('let g:triggered = 0')
command('autocmd WinClosed <buffer> :let g:triggered+=1') command('autocmd WinClosed * :let g:triggered+=1 | :bdelete 2')
local first_buffer = eval("bufnr('%')") command('new testfile2')
command('new') command('new testfile3')
command('bdelete ' .. first_buffer )
-- All 3 buffers are visible.
assert_visible(1, true)
assert_visible(2, true)
assert_visible(3, true)
-- Trigger WinClosed, which also deletes buffer/window 2.
command('bdelete 1')
-- Buffers 1 and 2 were closed but WinClosed was triggered only once.
eq(1, eval('g:triggered')) eq(1, eval('g:triggered'))
assert_visible(1, false)
assert_visible(2, false)
assert_visible(3, true)
end) end)
it(':close triggers WinClosed event in another tab', function() it('WinClosed from a different tabpage', function()
command('let g:triggered = 0') command('let g:evs = []')
local current_buffer = eval("bufnr('%')") command('edit tesfile1')
command('autocmd WinClosed <buffer> :let g:triggered+=1') command('autocmd WinClosed <buffer> :call add(g:evs, ["WinClosed", expand("<abuf>")])')
command('tabnew') local buf1 = eval("bufnr('%')")
command('bdelete ' .. current_buffer)
eq(1, eval('g:triggered'))
end)
it('WinClosed events are not recursive in different window', function()
command('let g:triggered = 0')
local first_buffer = eval("bufnr('%')")
command('autocmd WinClosed <buffer> :let g:triggered+=1')
command('new') command('new')
local second_buffer = eval("bufnr('%')") local buf2 = eval("bufnr('%')")
command('autocmd WinClosed <buffer> :bdelete ' .. first_buffer) command('autocmd WinClosed <buffer> :call add(g:evs, ["WinClosed", expand("<abuf>")])'
command('new') -- Attempt recursion.
neq(-1, funcs.bufwinnr(first_buffer)) ..' | bdelete '..buf2)
command('bdelete ' .. second_buffer ) command('tabedit testfile2')
eq(0, eval('g:triggered')) command('tabedit testfile3')
command('bdelete '..buf2)
-- first event was triggered, second wasn't -- Non-recursive: only triggered once.
eq(-1, funcs.bufwinnr(first_buffer)) eq({
end) {'WinClosed', '2'},
}, eval('g:evs'))
it('WinClosed events are not recursive in the same window', function() command('bdelete '..buf1)
command('let g:triggered = 0') eq({
command('new') {'WinClosed', '2'},
local second_buffer = eval("bufnr('%')") {'WinClosed', '1'},
command('autocmd WinClosed <buffer> :let g:triggered+=1 | bdelete ' .. second_buffer) }, eval('g:evs'))
neq(-1, funcs.bufwinnr(second_buffer))
eq(0, eval('g:triggered'))
command('bdelete ' .. second_buffer )
eq(-1, funcs.bufwinnr(second_buffer))
eq(1, eval('g:triggered'))
end)
it('WinClosed events are not recursive in different tab', function()
command('let g:triggered = 0')
command('new')
local second_buffer = eval("bufnr('%')")
command('autocmd WinClosed <buffer> :let g:triggered+=1 | bdelete ' .. second_buffer)
command('tabnew')
command('bdelete ' .. second_buffer )
eq(1, eval('g:triggered'))
end) end)
it('v:vim_did_enter is 1 after VimEnter', function() it('v:vim_did_enter is 1 after VimEnter', function()

View File

@@ -597,6 +597,19 @@ function module.assert_alive()
assert(2 == module.eval('1+1'), 'crash? request failed') assert(2 == module.eval('1+1'), 'crash? request failed')
end end
-- Asserts that buffer is loaded and visible in the current tabpage.
function module.assert_visible(bufnr, visible)
assert(type(visible) == 'boolean')
eq(visible, module.bufmeths.is_loaded(bufnr))
if visible then
assert(-1 ~= module.funcs.bufwinnr(bufnr),
'expected buffer to be visible in current tabpage: '..tostring(bufnr))
else
assert(-1 == module.funcs.bufwinnr(bufnr),
'expected buffer NOT visible in current tabpage: '..tostring(bufnr))
end
end
local function do_rmdir(path) local function do_rmdir(path)
local mode, errmsg, errcode = lfs.attributes(path, 'mode') local mode, errmsg, errcode = lfs.attributes(path, 'mode')
if mode == nil then if mode == nil then