diff src/testdir/test_listener.vim @ 16666:978bcd70883d v8.1.1335

patch 8.1.1335: listener callback is called after inserting text commit https://github.com/vim/vim/commit/dda4144d39a9d685b8dda830978e7410bd372c40 Author: Bram Moolenaar <Bram@vim.org> Date: Thu May 16 22:11:47 2019 +0200 patch 8.1.1335: listener callback is called after inserting text Problem: Listener callback is called after inserting text. Solution: Flush the changes before inserting or deleting a line. Store changes per buffer.
author Bram Moolenaar <Bram@vim.org>
date Thu, 16 May 2019 22:15:06 +0200
parents 04c2614af21c
children 0cc3f459237b
line wrap: on
line diff
--- a/src/testdir/test_listener.vim
+++ b/src/testdir/test_listener.vim
@@ -1,6 +1,8 @@
 " tests for listener_add() and listener_remove()
 
-func s:StoreList(l)
+func s:StoreList(s, l)
+  let s:start = a:s
+  let s:text = getline(a:s)
   let s:list = a:l
 endfunc
 
@@ -17,7 +19,7 @@ func Test_listening()
   new
   call setline(1, ['one', 'two'])
   let s:list = []
-  let id = listener_add({b, s, e, a, l -> s:StoreList(l)})
+  let id = listener_add({b, s, e, a, l -> s:StoreList(s, l)})
   call setline(1, 'one one')
   call listener_flush()
   call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list)
@@ -66,8 +68,10 @@ func Test_listening()
   " an insert just above a previous change that was the last one gets merged
   call setline(1, ['one one', 'two'])
   call listener_flush()
+  let s:list = []
   call setline(2, 'something')
   call append(1, 'two two')
+  call assert_equal([], s:list)
   call listener_flush()
   call assert_equal([{'lnum': 2, 'end': 3, 'col': 1, 'added': 1}], s:list)
 
@@ -77,8 +81,32 @@ func Test_listening()
   call setline(2, 'something')
   call append(0, 'two two')
   call assert_equal([{'lnum': 2, 'end': 3, 'col': 1, 'added': 0}], s:list)
+  call assert_equal('something', s:text)
   call listener_flush()
   call assert_equal([{'lnum': 1, 'end': 1, 'col': 1, 'added': 1}], s:list)
+  call assert_equal('two two', s:text)
+
+  " a delete at a previous change that was the last one gets merged
+  call setline(1, ['one one', 'two'])
+  call listener_flush()
+  let s:list = []
+  call setline(2, 'something')
+  2del
+  call assert_equal([], s:list)
+  call listener_flush()
+  call assert_equal([{'lnum': 2, 'end': 3, 'col': 1, 'added': -1}], s:list)
+
+  " a delete above a previous change causes a flush
+  call setline(1, ['one one', 'two'])
+  call listener_flush()
+  call setline(2, 'another')
+  1del
+  call assert_equal([{'lnum': 2, 'end': 3, 'col': 1, 'added': 0}], s:list)
+  call assert_equal(2, s:start)
+  call assert_equal('another', s:text)
+  call listener_flush()
+  call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': -1}], s:list)
+  call assert_equal('another', s:text)
 
   " the "o" command first adds an empty line and then changes it
   %del