fix(terminal): don't trigger TextChangedT for unrelated redraws

Problem: TextChangedT fires depending on whether Nvim needs to update_screen
while in terminal mode. This makes little sense as redraws can be completely
unrelated to the terminal. Also, TextChanged could be fired from changes in
terminal mode after returning to normal mode.

Solution: trigger it when b:changedtick changes, like other such events. Happens
when invalid cells are refreshed, though is no longer affected by cursor
changes. Don't fire TextChanged from changes in terminal mode after leaving.

Unlike the other TextChanged* events, I've elected to not have it be influenced
by typeahead. Plus, unlike when leaving insert mode when no TextChangedI events
are defined, I don't trigger TextChanged when returning to normal mode from
changes in terminal mode (is that a Vim bug?)

Curiously, Vim's TextChangedT is different; it's tied to its terminal cursor
redraws, which triggers pretty eagerly (but is unaffected by unrelated redraws)
- usually *twice* when data is sent to the terminal (regardless of whether it
causes any visible changes, like incomplete escape codes; wasn't true for Nvim).

Not clear to me how this event was actually intended to work, but this seems to
make the most sense to me.
This commit is contained in:
Sean Dewar
2025-08-19 15:03:40 +01:00
committed by zeertzjq
parent 5ee9e3f258
commit e4db5edb8a
3 changed files with 83 additions and 9 deletions

View File

@@ -408,7 +408,7 @@ struct file_buffer {
varnumber_T b_last_changedtick; // b:changedtick when TextChanged was
// last triggered.
varnumber_T b_last_changedtick_i; // b:changedtick for TextChangedI
varnumber_T b_last_changedtick_i; // b:changedtick for TextChangedI/T
varnumber_T b_last_changedtick_pum; // b:changedtick for TextChangedP
bool b_saving; // Set to true if we are in the middle of

View File

@@ -780,6 +780,8 @@ bool terminal_enter(void)
// Tell the terminal it has focus
terminal_focus(s->term, true);
// Don't fire TextChangedT from changes in Normal mode.
curbuf->b_last_changedtick_i = buf_get_changedtick(curbuf);
apply_autocmds(EVENT_TERMENTER, NULL, NULL, false, curbuf);
may_trigger_modechanged();
@@ -805,6 +807,8 @@ bool terminal_enter(void)
// Tell the terminal it lost focus
terminal_focus(s->term, false);
// Don't fire TextChanged from changes in terminal mode.
curbuf->b_last_changedtick = buf_get_changedtick(curbuf);
if (curbuf->terminal == s->term && !s->close) {
terminal_check_cursor();
@@ -892,9 +896,10 @@ static int terminal_check(VimState *state)
// Don't let autocommands free the terminal from under our fingers.
s->term->refcount++;
if (must_redraw) {
// TODO(seandewar): above changes will maybe change the behaviour of this more; untrollify this
if (has_event(EVENT_TEXTCHANGEDT)
&& curbuf->b_last_changedtick_i != buf_get_changedtick(curbuf)) {
apply_autocmds(EVENT_TEXTCHANGEDT, NULL, NULL, false, curbuf);
curbuf->b_last_changedtick_i = buf_get_changedtick(curbuf);
}
may_trigger_win_scrolled_resized();
s->term->refcount--;

View File

@@ -1,6 +1,6 @@
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local tt = require('test.functional.testterm')
local Screen = require('test.functional.ui.screen')
local uv = vim.uv
local clear, command, testprg = n.clear, n.command, n.testprg
@@ -202,12 +202,81 @@ describe('autocmd TextChangedT,WinResized', function()
before_each(clear)
it('TextChangedT works', function()
command('autocmd TextChangedT * ++once let g:called = 1')
tt.setup_screen()
tt.feed_data('a')
retry(nil, nil, function()
eq(1, api.nvim_get_var('called'))
local screen = Screen.new(50, 7)
screen:set_default_attr_ids({
[1] = { bold = true },
[31] = { foreground = Screen.colors.Gray100, background = Screen.colors.DarkGreen },
[32] = {
foreground = Screen.colors.Gray100,
bold = true,
background = Screen.colors.DarkGreen,
},
})
local term, term_unfocused = exec_lua(function()
-- Split windows before opening terminals so TextChangedT doesn't fire an additional time due
-- to the inner terminal being resized (which is usually deferred too).
vim.cmd.vnew()
local term_unfocused = vim.api.nvim_open_term(0, {})
vim.cmd.wincmd 'p'
local term = vim.api.nvim_open_term(0, {})
vim.cmd.startinsert()
return term, term_unfocused
end)
eq('t', eval('mode()'))
exec_lua(function()
_G.n_triggered = 0
vim.api.nvim_create_autocmd('TextChanged', {
callback = function()
_G.n_triggered = _G.n_triggered + 1
end,
})
_G.t_triggered = 0
vim.api.nvim_create_autocmd('TextChangedT', {
callback = function()
_G.t_triggered = _G.t_triggered + 1
end,
})
end)
api.nvim_chan_send(term, 'a')
retry(nil, nil, function()
eq(1, exec_lua('return _G.t_triggered'))
end)
api.nvim_chan_send(term, 'b')
retry(nil, nil, function()
eq(2, exec_lua('return _G.t_triggered'))
end)
-- Not triggered by changes in a non-current terminal.
api.nvim_chan_send(term_unfocused, 'hello')
screen:expect([[
hello │ab^ |
│ |*4
{31:[Scratch] [-] }{32:[Scratch] [-] }|
{1:-- TERMINAL --} |
]])
eq(2, exec_lua('return _G.t_triggered'))
-- Not triggered by unflushed redraws.
api.nvim__redraw({ valid = false, flush = false })
eq(2, exec_lua('return _G.t_triggered'))
-- Not triggered when not in terminal mode.
command('stopinsert')
eq('n', eval('mode()'))
eq(2, exec_lua('return _G.t_triggered'))
eq(0, exec_lua('return _G.n_triggered')) -- Nothing we did was in Normal mode yet.
api.nvim_chan_send(term, 'c')
screen:expect([[
hello │a^bc |
│ |*4
{31:[Scratch] [-] }{32:[Scratch] [-] }|
|
]])
eq(1, exec_lua('return _G.n_triggered')) -- Happened in Normal mode.
end)
it('no crash when deleting terminal buffer', function()