Skip to content

Commit e33024a

Browse files
resolrittersmjonas
authored andcommitted
fix(lua): make it possible to cancel vim.wait() with Ctrl-C (neovim#19217)
1 parent f668e4c commit e33024a

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/nvim/lua/executor.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,9 @@ static int nlua_wait(lua_State *lstate)
416416
LOOP_PROCESS_EVENTS_UNTIL(&main_loop,
417417
loop_events,
418418
(int)timeout,
419-
is_function ? nlua_wait_condition(lstate,
420-
&pcall_status,
421-
&callback_result) : false || got_int);
419+
got_int || (is_function ? nlua_wait_condition(lstate,
420+
&pcall_status,
421+
&callback_result) : false));
422422

423423
// Stop dummy timer
424424
time_watcher_stop(tw);

test/functional/lua/vim_spec.lua

+35
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,41 @@ describe('lua stdlib', function()
24932493

24942494
eq(false, pcall_result)
24952495
end)
2496+
2497+
describe('returns -2 when interrupted', function()
2498+
before_each(function()
2499+
local channel = meths.get_api_info()[1]
2500+
meths.set_var('channel', channel)
2501+
end)
2502+
2503+
it('without callback', function()
2504+
exec_lua([[
2505+
function _G.Wait()
2506+
vim.rpcnotify(vim.g.channel, 'ready')
2507+
local _, interrupted = vim.wait(4000)
2508+
vim.rpcnotify(vim.g.channel, 'wait', interrupted)
2509+
end
2510+
]])
2511+
feed(':lua _G.Wait()<CR>')
2512+
eq({'notification', 'ready', {}}, next_msg(500))
2513+
feed('<C-C>')
2514+
eq({'notification', 'wait', {-2}}, next_msg(500))
2515+
end)
2516+
2517+
it('with callback', function()
2518+
exec_lua([[
2519+
function _G.Wait()
2520+
vim.rpcnotify(vim.g.channel, 'ready')
2521+
local _, interrupted = vim.wait(4000, function() end)
2522+
vim.rpcnotify(vim.g.channel, 'wait', interrupted)
2523+
end
2524+
]])
2525+
feed(':lua _G.Wait()<CR>')
2526+
eq({'notification', 'ready', {}}, next_msg(500))
2527+
feed('<C-C>')
2528+
eq({'notification', 'wait', {-2}}, next_msg(500))
2529+
end)
2530+
end)
24962531
end)
24972532

24982533
it('vim.notify_once', function()

0 commit comments

Comments
 (0)