neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

commit 6a409e05071735c4b1c0272d51d8871b58384eee
parent e6ba78919c223bd8871825edaebfa7dc92a62bf6
Author: Sean Dewar <6256228+seandewar@users.noreply.github.com>
Date:   Tue, 19 Aug 2025 15:03:40 +0100

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.

Diffstat:
Msrc/nvim/buffer_defs.h | 2+-
Msrc/nvim/terminal.c | 9+++++++--
Mtest/functional/autocmd/termxx_spec.lua | 79++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
3 files changed, 82 insertions(+), 8 deletions(-)

diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h @@ -399,7 +399,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 diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c @@ -775,6 +775,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(); @@ -800,6 +802,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(); @@ -887,9 +891,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--; diff --git a/test/functional/autocmd/termxx_spec.lua b/test/functional/autocmd/termxx_spec.lua @@ -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') + 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(1, api.nvim_get_var('called')) + 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()