Skip to content

Commit 6a409e0

Browse files
committed
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.
1 parent e6ba789 commit 6a409e0

File tree

3 files changed

+82
-8
lines changed

3 files changed

+82
-8
lines changed

src/nvim/buffer_defs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ struct file_buffer {
399399

400400
varnumber_T b_last_changedtick; // b:changedtick when TextChanged was
401401
// last triggered.
402-
varnumber_T b_last_changedtick_i; // b:changedtick for TextChangedI
402+
varnumber_T b_last_changedtick_i; // b:changedtick for TextChangedI/T
403403
varnumber_T b_last_changedtick_pum; // b:changedtick for TextChangedP
404404

405405
bool b_saving; // Set to true if we are in the middle of

src/nvim/terminal.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,8 @@ bool terminal_enter(void)
775775

776776
// Tell the terminal it has focus
777777
terminal_focus(s->term, true);
778+
// Don't fire TextChangedT from changes in Normal mode.
779+
curbuf->b_last_changedtick_i = buf_get_changedtick(curbuf);
778780

779781
apply_autocmds(EVENT_TERMENTER, NULL, NULL, false, curbuf);
780782
may_trigger_modechanged();
@@ -800,6 +802,8 @@ bool terminal_enter(void)
800802

801803
// Tell the terminal it lost focus
802804
terminal_focus(s->term, false);
805+
// Don't fire TextChanged from changes in terminal mode.
806+
curbuf->b_last_changedtick = buf_get_changedtick(curbuf);
803807

804808
if (curbuf->terminal == s->term && !s->close) {
805809
terminal_check_cursor();
@@ -887,9 +891,10 @@ static int terminal_check(VimState *state)
887891

888892
// Don't let autocommands free the terminal from under our fingers.
889893
s->term->refcount++;
890-
if (must_redraw) {
891-
// TODO(seandewar): above changes will maybe change the behaviour of this more; untrollify this
894+
if (has_event(EVENT_TEXTCHANGEDT)
895+
&& curbuf->b_last_changedtick_i != buf_get_changedtick(curbuf)) {
892896
apply_autocmds(EVENT_TEXTCHANGEDT, NULL, NULL, false, curbuf);
897+
curbuf->b_last_changedtick_i = buf_get_changedtick(curbuf);
893898
}
894899
may_trigger_win_scrolled_resized();
895900
s->term->refcount--;

test/functional/autocmd/termxx_spec.lua

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local t = require('test.testutil')
22
local n = require('test.functional.testnvim')()
3-
local tt = require('test.functional.testterm')
3+
local Screen = require('test.functional.ui.screen')
44
local uv = vim.uv
55

66
local clear, command, testprg = n.clear, n.command, n.testprg
@@ -202,12 +202,81 @@ describe('autocmd TextChangedT,WinResized', function()
202202
before_each(clear)
203203

204204
it('TextChangedT works', function()
205-
command('autocmd TextChangedT * ++once let g:called = 1')
206-
tt.setup_screen()
207-
tt.feed_data('a')
205+
local screen = Screen.new(50, 7)
206+
screen:set_default_attr_ids({
207+
[1] = { bold = true },
208+
[31] = { foreground = Screen.colors.Gray100, background = Screen.colors.DarkGreen },
209+
[32] = {
210+
foreground = Screen.colors.Gray100,
211+
bold = true,
212+
background = Screen.colors.DarkGreen,
213+
},
214+
})
215+
216+
local term, term_unfocused = exec_lua(function()
217+
-- Split windows before opening terminals so TextChangedT doesn't fire an additional time due
218+
-- to the inner terminal being resized (which is usually deferred too).
219+
vim.cmd.vnew()
220+
local term_unfocused = vim.api.nvim_open_term(0, {})
221+
vim.cmd.wincmd 'p'
222+
local term = vim.api.nvim_open_term(0, {})
223+
vim.cmd.startinsert()
224+
return term, term_unfocused
225+
end)
226+
eq('t', eval('mode()'))
227+
228+
exec_lua(function()
229+
_G.n_triggered = 0
230+
vim.api.nvim_create_autocmd('TextChanged', {
231+
callback = function()
232+
_G.n_triggered = _G.n_triggered + 1
233+
end,
234+
})
235+
_G.t_triggered = 0
236+
vim.api.nvim_create_autocmd('TextChangedT', {
237+
callback = function()
238+
_G.t_triggered = _G.t_triggered + 1
239+
end,
240+
})
241+
end)
242+
243+
api.nvim_chan_send(term, 'a')
244+
retry(nil, nil, function()
245+
eq(1, exec_lua('return _G.t_triggered'))
246+
end)
247+
api.nvim_chan_send(term, 'b')
208248
retry(nil, nil, function()
209-
eq(1, api.nvim_get_var('called'))
249+
eq(2, exec_lua('return _G.t_triggered'))
210250
end)
251+
252+
-- Not triggered by changes in a non-current terminal.
253+
api.nvim_chan_send(term_unfocused, 'hello')
254+
screen:expect([[
255+
hello │ab^ |
256+
│ |*4
257+
{31:[Scratch] [-] }{32:[Scratch] [-] }|
258+
{1:-- TERMINAL --} |
259+
]])
260+
eq(2, exec_lua('return _G.t_triggered'))
261+
262+
-- Not triggered by unflushed redraws.
263+
api.nvim__redraw({ valid = false, flush = false })
264+
eq(2, exec_lua('return _G.t_triggered'))
265+
266+
-- Not triggered when not in terminal mode.
267+
command('stopinsert')
268+
eq('n', eval('mode()'))
269+
eq(2, exec_lua('return _G.t_triggered'))
270+
eq(0, exec_lua('return _G.n_triggered')) -- Nothing we did was in Normal mode yet.
271+
272+
api.nvim_chan_send(term, 'c')
273+
screen:expect([[
274+
hello │a^bc |
275+
│ |*4
276+
{31:[Scratch] [-] }{32:[Scratch] [-] }|
277+
|
278+
]])
279+
eq(1, exec_lua('return _G.n_triggered')) -- Happened in Normal mode.
211280
end)
212281

213282
it('no crash when deleting terminal buffer', function()

0 commit comments

Comments
 (0)