Skip to content

Commit

Permalink
patch 9.0.2075: TextChangedI may not always trigger
Browse files Browse the repository at this point in the history
Problem:  TextChangedI may not always trigger
Solution: trigger it in more cases: for insert/
          append/change operations, and when
          opening a new line,

fixes: #13367
closes: #13375

Signed-off-by: Christian Brabandt <cb@256bit.org>
Signed-off-by: Evgeni Chasnovski <evgeni.chasnovski@gmail.com>
  • Loading branch information
chrisbra committed Oct 27, 2023
1 parent daef8c7 commit 4bca489
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
3 changes: 0 additions & 3 deletions src/edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ edit(
return FALSE;
}
ins_compl_clear(); // clear stuff for CTRL-X mode
// Reset Changedtick_i, so that TextChangedI will only be triggered for stuff
// from insert mode
curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);

/*
* Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
Expand Down
6 changes: 6 additions & 0 deletions src/normal.c
Original file line number Diff line number Diff line change
Expand Up @@ -6292,6 +6292,8 @@ n_opencmd(cmdarg_T *cap)
(void)hasFolding(curwin->w_cursor.lnum,
NULL, &curwin->w_cursor.lnum);
#endif
// trigger TextChangedI for the 'o/O' command
curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
if (u_save((linenr_T)(curwin->w_cursor.lnum -
(cap->cmdchar == 'O' ? 1 : 0)),
(linenr_T)(curwin->w_cursor.lnum +
Expand Down Expand Up @@ -7083,6 +7085,10 @@ invoke_edit(
// Always reset "restart_edit", this is not a restarted edit.
restart_edit = 0;

// Reset Changedtick_i, so that TextChangedI will only be triggered for stuff
// from insert mode, for 'o/O' this has already been done in n_opencmd
if (cap->cmdchar != 'O' && cap->cmdchar != 'o')
curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
if (edit(cmd, startln, cap->count1))
cap->retval |= CA_COMMAND_BUSY;

Expand Down
6 changes: 6 additions & 0 deletions src/ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -4134,6 +4134,9 @@ do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
// before.
restore_lbr(lbr_saved);
#endif
// trigger TextChangedI
curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);

if (op_change(oap)) // will call edit()
cap->retval |= CA_COMMAND_BUSY;
if (restart_edit == 0)
Expand Down Expand Up @@ -4244,6 +4247,9 @@ do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
// before.
restore_lbr(lbr_saved);
#endif
// trigger TextChangedI
curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);

op_insert(oap, cap->count1);
#ifdef FEAT_LINEBREAK
// Reset linebreak, so that formatting works correctly.
Expand Down
32 changes: 25 additions & 7 deletions src/testdir/test_autocmd.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2566,28 +2566,27 @@ func Test_ChangedP()
call cursor(3, 1)
let g:autocmd = ''
call feedkeys("o\<esc>", 'tnix')
" `TextChangedI` triggers only if text is actually changed in Insert mode
call assert_equal('', g:autocmd)
call assert_equal('I', g:autocmd)

let g:autocmd = ''
call feedkeys("Sf", 'tnix')
call assert_equal('I', g:autocmd)
call assert_equal('II', g:autocmd)

let g:autocmd = ''
call feedkeys("Sf\<C-N>", 'tnix')
call assert_equal('IP', g:autocmd)
call assert_equal('IIP', g:autocmd)

let g:autocmd = ''
call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
call assert_equal('IPP', g:autocmd)
call assert_equal('IIPP', g:autocmd)

let g:autocmd = ''
call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
call assert_equal('IPPP', g:autocmd)
call assert_equal('IIPPP', g:autocmd)

let g:autocmd = ''
call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
call assert_equal('IPPPP', g:autocmd)
call assert_equal('IIPPPP', g:autocmd)

call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
" TODO: how should it handle completeopt=noinsert,noselect?
Expand Down Expand Up @@ -3621,6 +3620,25 @@ func Test_Changed_ChangedI()
call feedkeys("ibar\<esc>", 'tnix')
call assert_equal('', g:autocmd_n)

" If change is a mix of Normal and Insert modes, TextChangedI should trigger
func s:validate_mixed_textchangedi(keys)
call feedkeys("ifoo\<esc>", 'tnix')
let g:autocmd_i = ''
let g:autocmd_n = ''
call feedkeys(a:keys, 'tnix')
call assert_notequal('', g:autocmd_i)
call assert_equal('', g:autocmd_n)
endfunc

call s:validate_mixed_textchangedi("o\<esc>")
call s:validate_mixed_textchangedi("O\<esc>")
call s:validate_mixed_textchangedi("ciw\<esc>")
call s:validate_mixed_textchangedi("cc\<esc>")
call s:validate_mixed_textchangedi("C\<esc>")
call s:validate_mixed_textchangedi("s\<esc>")
call s:validate_mixed_textchangedi("S\<esc>")


" CleanUp
call test_override("char_avail", 0)
au! TextChanged <buffer>
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2075,
/**/
2074,
/**/
Expand Down

0 comments on commit 4bca489

Please sign in to comment.