Merge pull request #23288 from MunifTanjim/issue-22263

fix(normal): fix repeated trigger modechanged for scheduled callback
This commit is contained in:
zeertzjq 2023-04-27 00:23:39 +08:00 committed by GitHub
commit efae71819a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 12 deletions

View File

@ -956,10 +956,12 @@ normal_end:
set_reg_var(get_default_register_name()); set_reg_var(get_default_register_name());
} }
// Reset finish_op, in case it was set
s->c = finish_op; s->c = finish_op;
finish_op = false; if (s->oa.op_type == OP_NOP) {
may_trigger_modechanged(); // Reset finish_op, in case it was set
finish_op = false;
may_trigger_modechanged();
}
// Redraw the cursor with another shape, if we were in Operator-pending // Redraw the cursor with another shape, if we were in Operator-pending
// mode or did a replace command. // mode or did a replace command.
if (s->c || s->ca.cmdchar == 'r' if (s->c || s->ca.cmdchar == 'r'
@ -1729,9 +1731,9 @@ void prep_redo_num2(int regname, long num1, int cmd1, int cmd2, long num2, int c
} }
} }
/// check for operator active and clear it /// Check for operator active and clear it.
/// ///
/// @return true if operator was active /// Beep and return true if an operator was active.
static bool checkclearop(oparg_T *oap) static bool checkclearop(oparg_T *oap)
{ {
if (oap->op_type == OP_NOP) { if (oap->op_type == OP_NOP) {
@ -1743,7 +1745,7 @@ static bool checkclearop(oparg_T *oap)
/// Check for operator or Visual active. Clear active operator. /// Check for operator or Visual active. Clear active operator.
/// ///
/// @return true if operator or Visual was active. /// Beep and return true if an operator or Visual was active.
static bool checkclearopq(oparg_T *oap) static bool checkclearopq(oparg_T *oap)
{ {
if (oap->op_type == OP_NOP && !VIsual_active) { if (oap->op_type == OP_NOP && !VIsual_active) {

View File

@ -1,17 +1,19 @@
local helpers = require('test.functional.helpers')(after_each) local helpers = require('test.functional.helpers')(after_each)
local clear, eval, eq = helpers.clear, helpers.eval, helpers.eq local clear, eval, eq = helpers.clear, helpers.eval, helpers.eq
local feed, command = helpers.feed, helpers.command local feed, command = helpers.feed, helpers.command
local exec_lua = helpers.exec_lua
describe('ModeChanged', function() describe('ModeChanged', function()
before_each(function() before_each(function()
clear() clear()
command('let g:count = 0')
command('au ModeChanged * let g:event = copy(v:event)')
command('au ModeChanged * let g:count += 1')
end) end)
it('picks up terminal mode changes', function() it('picks up terminal mode changes', function()
command("term") command('let g:count = 0')
command('au ModeChanged * let g:event = copy(v:event)')
command('au ModeChanged * let g:count += 1')
command('term')
feed('i') feed('i')
eq({ eq({
old_mode = 'nt', old_mode = 'nt',
@ -28,4 +30,35 @@ describe('ModeChanged', function()
-- v:event is cleared after the autocommand is done -- v:event is cleared after the autocommand is done
eq({}, eval('v:event')) eq({}, eval('v:event'))
end) end)
it('does not repeatedly trigger for scheduled callback', function()
exec_lua([[
vim.g.s_count = 0
vim.g.s_mode = ""
vim.g.t_count = 0
vim.g.t_mode = ""
vim.api.nvim_create_autocmd("ModeChanged", {
callback = function()
vim.g.s_count = vim.g.s_count + 1
vim.g.s_mode = vim.api.nvim_get_mode().mode
vim.schedule(function()
vim.g.t_count = vim.g.t_count + 1
vim.g.t_mode = vim.api.nvim_get_mode().mode
end)
end,
})
]])
feed('d')
eq(1, eval('g:s_count'))
eq('no', eval('g:s_mode'))
eq(1, eval('g:t_count'))
eq('no', eval('g:t_mode'))
feed('<Esc>')
eq(2, eval('g:s_count'))
eq('n', eval('g:s_mode'))
eq(2, eval('g:t_count'))
eq('n', eval('g:t_mode'))
end)
end) end)

View File

@ -3453,7 +3453,7 @@ endfunc
" Test for ModeChanged pattern " Test for ModeChanged pattern
func Test_mode_changes() func Test_mode_changes()
let g:index = 0 let g:index = 0
let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'n', 'V', 'v', 's', 'n'] let g:mode_seq = ['n', 'i', 'n', 'v', 'V', 'i', 'ix', 'i', 'ic', 'i', 'n', 'no', 'noV', 'n', 'V', 'v', 's', 'n']
func! TestMode() func! TestMode()
call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode")) call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode")) call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
@ -3464,7 +3464,7 @@ func Test_mode_changes()
au ModeChanged * :call TestMode() au ModeChanged * :call TestMode()
let g:n_to_any = 0 let g:n_to_any = 0
au ModeChanged n:* let g:n_to_any += 1 au ModeChanged n:* let g:n_to_any += 1
call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdG", 'tnix') call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdV\<MouseMove>G", 'tnix')
let g:V_to_v = 0 let g:V_to_v = 0
au ModeChanged V:v let g:V_to_v += 1 au ModeChanged V:v let g:V_to_v += 1