]> granicus.if.org Git - vim/commitdiff
patch 8.1.1326: no test for listener with partial v8.1.1326
authorBram Moolenaar <Bram@vim.org>
Sun, 12 May 2019 11:53:50 +0000 (13:53 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 12 May 2019 11:53:50 +0000 (13:53 +0200)
Problem:    No test for listener with partial.
Solution:   Add a test.  Add example to help.

runtime/doc/eval.txt
src/testdir/test_listener.vim
src/version.c

index 0b71c44dabf3721b2e6dae0de661c8e44d066a6f..c1123ab6fa1d0b58edba179af73b24453fcfef64 100644 (file)
@@ -6323,7 +6323,8 @@ listener_add({callback} [, {buf}])                        *listener_add()*
                Returns a unique ID that can be passed to |listener_remove()|.
 
                The {callback} is invoked with a list of items that indicate a
-               change.  Each list item is a dictionary with these entries:
+               change.  The list cannot be changed.  Each list item is a
+               dictionary with these entries:
                    lnum        the first line number of the change
                    end         the first line below the change
                    added       number of lines added; negative if lines were
@@ -6349,7 +6350,21 @@ listener_add({callback} [, {buf}])                       *listener_add()*
                    added       zero
                    col         first column with a change or one
 
-               The {callback} is invoked just before the screen is updated.
+               The entries are in the order the changes was made, thus the
+               most recent change is at the end.  One has to go through the
+               list from end to start to compute the line numbers in the
+               current state of the text.
+
+               When using the same function for multiple buffers, you can
+               pass the buffer to that function using a |Partial|.
+               Example: >
+                   func Listener(bufnr, changes)
+                     " ...
+                   endfunc
+                   let bufnr = ...
+                   call listener_add(function('Listener', [bufnr]), bufnr)
+
+<              The {callback} is invoked just before the screen is updated.
                To trigger this in a script use the `:redraw` command.
 
                The {callback} is not invoked when the buffer is first loaded.
@@ -10984,10 +10999,10 @@ expressions |expr-lambda|.
 
 Example: >
   function Something(key, value = 10)
-     echo a:key .. ": " .. value
+     echo a:key .. ": " .. a:value
   endfunction
   call Something('empty')      "empty: 10"
-  call Something('key, 20)     "key: 20"
+  call Something('key', 20)    "key: 20"
 
 The argument default expressions are evaluated at the time of the function
 call, not definition.  Thus it is possible to use an expression which is
index 87183e5b78fe06d0549dec8cb55debea555be78b..d0e4366a20f157c9a2b8d112ad304ab29a2d1ef2 100644 (file)
@@ -1,77 +1,86 @@
 " tests for listener_add() and listener_remove()
 
-func StoreList(l)
-  let g:list = a:l
+func s:StoreList(l)
+  let s:list = a:l
 endfunc
 
-func AnotherStoreList(l)
-  let g:list2 = a:l
+func s:AnotherStoreList(l)
+  let s:list2 = a:l
 endfunc
 
-func EvilStoreList(l)
-  let g:list3 = a:l
+func s:EvilStoreList(l)
+  let s:list3 = a:l
   call assert_fails("call add(a:l, 'myitem')", "E742:")
 endfunc
 
 func Test_listening()
   new
   call setline(1, ['one', 'two'])
-  let id = listener_add({l -> StoreList(l)})
+  let id = listener_add({l -> s:StoreList(l)})
   call setline(1, 'one one')
   redraw
-  call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], g:list)
+  call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list)
 
   " Two listeners, both get called.
-  let id2 = listener_add({l -> AnotherStoreList(l)})
-  let g:list = []
-  let g:list2 = []
+  let id2 = listener_add({l -> s:AnotherStoreList(l)})
+  let s:list = []
+  let s:list2 = []
   exe "normal $asome\<Esc>"
   redraw
-  call assert_equal([{'lnum': 1, 'end': 2, 'col': 8, 'added': 0}], g:list)
-  call assert_equal([{'lnum': 1, 'end': 2, 'col': 8, 'added': 0}], g:list2)
+  call assert_equal([{'lnum': 1, 'end': 2, 'col': 8, 'added': 0}], s:list)
+  call assert_equal([{'lnum': 1, 'end': 2, 'col': 8, 'added': 0}], s:list2)
 
   call listener_remove(id2)
-  let g:list = []
-  let g:list2 = []
+  let s:list = []
+  let s:list2 = []
   call setline(3, 'three')
   redraw
-  call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1}], g:list)
-  call assert_equal([], g:list2)
+  call assert_equal([{'lnum': 3, 'end': 3, 'col': 1, 'added': 1}], s:list)
+  call assert_equal([], s:list2)
 
   " the "o" command first adds an empty line and then changes it
-  let g:list = []
+  let s:list = []
   exe "normal Gofour\<Esc>"
   redraw
   call assert_equal([{'lnum': 4, 'end': 4, 'col': 1, 'added': 1},
-       \ {'lnum': 4, 'end': 5, 'col': 1, 'added': 0}], g:list)
+       \ {'lnum': 4, 'end': 5, 'col': 1, 'added': 0}], s:list)
 
-  let g:list = []
+  " Remove last listener
+  let s:list = []
   call listener_remove(id)
   call setline(1, 'asdfasdf')
   redraw
-  call assert_equal([], g:list)
+  call assert_equal([], s:list)
 
   " Trying to change the list fails
-  let id = listener_add({l -> EvilStoreList(l)})
-  let g:list3 = []
+  let id = listener_add({l -> s:EvilStoreList(l)})
+  let s:list3 = []
   call setline(1, 'asdfasdf')
   redraw
-  call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], g:list3)
+  call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list3)
 
+  call listener_remove(id)
   bwipe!
 endfunc
 
+func s:StoreBufList(buf, l)
+  let s:bufnr = a:buf
+  let s:list = a:l
+endfunc
+
 func Test_listening_other_buf()
   new
   call setline(1, ['one', 'two'])
   let bufnr = bufnr('')
   normal ww
-  let id = listener_add({l -> StoreList(l)}, bufnr)
-  let g:list = []
+  let id = listener_add(function('s:StoreBufList', [bufnr]), bufnr)
+  let s:list = []
   call setbufline(bufnr, 1, 'hello')
   redraw
-  call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], g:list)
+  call assert_equal(bufnr, s:bufnr)
+  call assert_equal([{'lnum': 1, 'end': 2, 'col': 1, 'added': 0}], s:list)
 
+  call listener_remove(id)
   exe "buf " .. bufnr
   bwipe!
 endfunc
index 24f4bea8db9613675a7bc3f6a1948e1cd2fbe28c..a957025eeb6774c18a53e66541a4acbb5d7789d3 100644 (file)
@@ -767,6 +767,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1326,
 /**/
     1325,
 /**/