fix(events): trigger VimResume on next UI request (#24426)

This commit is contained in:
zeertzjq 2023-07-23 07:16:41 +08:00 committed by GitHub
parent ad95b36985
commit a8cfdf43bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 94 additions and 33 deletions

View File

@ -215,6 +215,8 @@ void nvim_ui_attach(uint64_t channel_id, Integer width, Integer height, Dictiona
pmap_put(uint64_t)(&connected_uis, channel_id, ui);
ui_attach_impl(ui, channel_id);
may_trigger_vim_suspend_resume(false);
}
/// @deprecated
@ -237,6 +239,10 @@ void nvim_ui_set_focus(uint64_t channel_id, Boolean gained, Error *error)
return;
}
if (gained) {
may_trigger_vim_suspend_resume(false);
}
do_autocmd_focusgained((bool)gained);
}

View File

@ -305,6 +305,7 @@ void nvim_feedkeys(String keys, String mode, Boolean escape_ks)
Integer nvim_input(String keys)
FUNC_API_SINCE(1) FUNC_API_FAST
{
may_trigger_vim_suspend_resume(false);
return (Integer)input_enqueue(keys);
}
@ -334,6 +335,8 @@ void nvim_input_mouse(String button, String action, String modifier, Integer gri
Integer col, Error *err)
FUNC_API_SINCE(6) FUNC_API_FAST
{
may_trigger_vim_suspend_resume(false);
if (button.data == NULL || action.data == NULL) {
goto error;
}

View File

@ -46,6 +46,7 @@
#include "nvim/search.h"
#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/types.h"
#include "nvim/ui.h"
#include "nvim/ui_compositor.h"
#include "nvim/vim.h"
@ -2521,6 +2522,30 @@ static bool arg_autocmd_flag_get(bool *flag, char **cmd_ptr, char *pattern, int
return false;
}
/// When kFalse: VimSuspend should be triggered next.
/// When kTrue: VimResume should be triggerd next.
/// When kNone: Currently triggering VimSuspend or VimResume.
static TriState pending_vimresume = kFalse;
static void vimresume_event(void **argv)
{
apply_autocmds(EVENT_VIMRESUME, NULL, NULL, false, NULL);
pending_vimresume = kFalse;
}
/// Trigger VimSuspend or VimResume autocommand.
void may_trigger_vim_suspend_resume(bool suspend)
{
if (suspend && pending_vimresume == kFalse) {
pending_vimresume = kNone;
apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, false, NULL);
pending_vimresume = kTrue;
} else if (!suspend && pending_vimresume == kTrue) {
pending_vimresume = kNone;
multiqueue_put(main_loop.events, vimresume_event, 0);
}
}
// UI Enter
void do_autocmd_uienter(uint64_t chanid, bool attached)
{

View File

@ -4874,12 +4874,9 @@ static void ex_stop(exarg_T *eap)
if (!eap->forceit) {
autowrite_all();
}
apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, false, NULL);
may_trigger_vim_suspend_resume(true);
ui_call_suspend();
ui_flush();
apply_autocmds(EVENT_VIMRESUME, NULL, NULL, false, NULL);
}
/// ":exit", ":xit" and ":wq": Write file and quit the current window.

View File

@ -1,8 +1,11 @@
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local clear = helpers.clear
local command = helpers.command
local eq = helpers.eq
local eval = helpers.eval
local exec = helpers.exec
local feed = helpers.feed
local meths = helpers.meths
local request = helpers.request
local pcall_err = helpers.pcall_err
@ -76,3 +79,59 @@ it('autocmds UIEnter/UILeave', function()
'UILeave',
}, eval('g:evs'))
end)
it('autocmds VimSuspend/VimResume #22041', function()
clear()
local screen = Screen.new()
screen:attach()
exec([[
let g:ev = []
autocmd VimResume * :call add(g:ev, 'r')
autocmd VimSuspend * :call add(g:ev, 's')
]])
eq(false, screen.suspended)
feed('<C-Z>')
screen:expect(function() eq (true, screen.suspended) end)
eq({ 's' }, eval('g:ev'))
screen.suspended = false
feed('<Ignore>')
eq({ 's', 'r' }, eval('g:ev'))
command('suspend')
screen:expect(function() eq (true, screen.suspended) end)
eq({ 's', 'r', 's' }, eval('g:ev'))
screen.suspended = false
meths.input_mouse('move', '', '', 0, 0, 0)
eq({ 's', 'r', 's', 'r' }, eval('g:ev'))
feed('<C-Z>')
screen:expect(function() eq (true, screen.suspended) end)
meths.ui_set_focus(false)
eq({ 's', 'r', 's', 'r', 's' }, eval('g:ev'))
screen.suspended = false
meths.ui_set_focus(true)
eq({ 's', 'r', 's', 'r', 's', 'r' }, eval('g:ev'))
command('suspend')
screen:expect(function() eq (true, screen.suspended) end)
screen:detach()
eq({ 's', 'r', 's', 'r', 's', 'r', 's' }, eval('g:ev'))
screen.suspended = false
screen:attach()
eq({ 's', 'r', 's', 'r', 's', 'r', 's', 'r' }, eval('g:ev'))
eq(false, screen.suspended)
feed('<C-Z><C-Z><C-Z><C-Z>')
screen:expect(function() eq (true, screen.suspended) end)
eq({ 's', 'r', 's', 'r', 's', 'r', 's', 'r', 's' }, eval('g:ev'))
screen.suspended = false
feed('<C-Z><C-Z><C-Z><C-Z>')
eq({ 's', 'r', 's', 'r', 's', 'r', 's', 'r', 's', 'r', 's' }, eval('g:ev'))
screen:expect(function() eq (true, screen.suspended) end)
screen.suspended = false
feed('i<C-R>=g:ev<CR>')
eq({ 's', 'r', 's', 'r', 's', 'r', 's', 'r', 's', 'r', 's', 'r' }, eval('g:ev'))
eq({ 's', 'r', 's', 'r', 's', 'r', 's', 'r', 's', 'r', 's', 'r', '' },
meths.buf_get_lines(0, 0, -1, true))
end)

View File

@ -4,7 +4,6 @@ local spawn, set_session, clear = helpers.spawn, helpers.set_session, helpers.cl
local feed, command = helpers.feed, helpers.command
local insert = helpers.insert
local eq = helpers.eq
local eval = helpers.eval
local funcs, meths = helpers.funcs, helpers.meths
describe('screen', function()
@ -64,34 +63,6 @@ local function screen_tests(linegrid)
} )
end)
describe(':suspend', function()
it('is forwarded to the UI', function()
local function check()
eq(true, screen.suspended)
end
command('let g:ev = []')
command('autocmd VimResume * :call add(g:ev, "r")')
command('autocmd VimSuspend * :call add(g:ev, "s")')
eq(false, screen.suspended)
command('suspend')
eq({ 's', 'r' }, eval('g:ev'))
screen:expect(check)
screen.suspended = false
feed('<c-z>')
eq({ 's', 'r', 's', 'r' }, eval('g:ev'))
screen:expect(check)
screen.suspended = false
command('suspend')
eq({ 's', 'r', 's', 'r', 's', 'r' }, eval('g:ev'))
end)
end)
describe('bell/visual bell', function()
it('is forwarded to the UI', function()
feed('<left>')