robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#include <inttypes.h> 2#include <lauxlib.h> 3#include <stdbool.h> 4#include <stddef.h> 5 6#include "klib/kvec.h" 7#include "nvim/api/buffer.h" 8#include "nvim/api/private/defs.h" 9#include "nvim/api/private/helpers.h" 10#include "nvim/assert_defs.h" 11#include "nvim/buffer.h" 12#include "nvim/buffer_defs.h" 13#include "nvim/buffer_updates.h" 14#include "nvim/globals.h" 15#include "nvim/log.h" 16#include "nvim/lua/executor.h" 17#include "nvim/memline.h" 18#include "nvim/memory.h" 19#include "nvim/memory_defs.h" 20#include "nvim/msgpack_rpc/channel.h" 21#include "nvim/pos_defs.h" 22#include "nvim/types_defs.h" 23 24#include "buffer_updates.c.generated.h" // IWYU pragma: keep 25 26// Register a channel. Return True if the channel was added, or already added. 27// Return False if the channel couldn't be added because the buffer is 28// unloaded. 29bool buf_updates_register (buf_T * buf ,uint64_t channel_id ,BufUpdateCallbacks cb ,bool send_buffer ) 30{ 31// must fail if the buffer isn't loaded 32if (buf -> b_ml .ml_mfp == NULL ) { 33return false; 34 } 35 36if (channel_id == LUA_INTERNAL_CALL ) { 37kv_push (buf -> update_callbacks ,cb ); 38if (cb .utf_sizes ) { 39buf -> update_need_codepoints = true; 40 } 41return true; 42 } 43 44// count how many channels are currently watching the buffer 45size_t size = kv_size (buf -> update_channels ); 46for (size_t i = 0 ;i < size ;i ++ ) { 47if (kv_A (buf -> update_channels ,i )== channel_id ) { 48// buffer is already registered ... nothing to do 49return true; 50 } 51 } 52 53// append the channelid to the list 54kv_push (buf -> update_channels ,channel_id ); 55 56if (send_buffer ) { 57MAXSIZE_TEMP_ARRAY (args ,6 ); 58 59// the first argument is always the buffer handle 60ADD_C (args ,BUFFER_OBJ (buf -> handle )); 61ADD_C (args ,INTEGER_OBJ (buf_get_changedtick (buf ))); 62// the first line that changed (zero-indexed) 63ADD_C (args ,INTEGER_OBJ (0 )); 64// the last line that was changed 65ADD_C (args ,INTEGER_OBJ (-1 )); 66 67// collect buffer contents 68 69STATIC_ASSERT (SIZE_MAX >=MAXLNUM ,"size_t smaller than MAXLNUM" ); 70size_t line_count = (size_t )buf -> b_ml .ml_line_count ; 71 72Array linedata = ARRAY_DICT_INIT ; 73Arena arena = ARENA_EMPTY ; 74if (line_count > 0 ) { 75linedata = arena_array (& arena ,line_count ); 76buf_collect_lines (buf ,line_count ,1 ,0 , true,& linedata ,NULL ,& arena ); 77 } 78 79ADD_C (args ,ARRAY_OBJ (linedata )); 80ADD_C (args ,BOOLEAN_OBJ (false)); 81 82rpc_send_event (channel_id ,"nvim_buf_lines_event" ,args ); 83arena_mem_free (arena_finish (& arena )); 84 }else { 85buf_updates_changedtick_single (buf ,channel_id ); 86 } 87 88return true; 89} 90 91bool buf_updates_active (buf_T * buf ) 92FUNC_ATTR_PURE 93{ 94return kv_size (buf -> update_channels )|| kv_size (buf -> update_callbacks ); 95} 96 97void buf_updates_send_end (buf_T * buf ,uint64_t channelid ) 98{ 99MAXSIZE_TEMP_ARRAY (args ,1 ); 100ADD_C (args ,BUFFER_OBJ (buf -> handle )); 101rpc_send_event (channelid ,"nvim_buf_detach_event" ,args ); 102} 103 104void buf_updates_unregister (buf_T * buf ,uint64_t channelid ) 105{ 106size_t size = kv_size (buf -> update_channels ); 107if (!size ) { 108return ; 109 } 110 111// go through list backwards and remove the channel id each time it appears 112// (it should never appear more than once) 113size_t j = 0 ; 114size_t found = 0 ; 115for (size_t i = 0 ;i < size ;i ++ ) { 116if (kv_A (buf -> update_channels ,i )== channelid ) { 117found ++ ; 118 }else { 119// copy item backwards into prior slot if needed 120if (i != j ) { 121kv_A (buf -> update_channels ,j )= kv_A (buf -> update_channels ,i ); 122 } 123j ++ ; 124 } 125 } 126 127if (found ) { 128// remove X items from the end of the array 129buf -> update_channels .size -= found ; 130 131// make a new copy of the active array without the channelid in it 132buf_updates_send_end (buf ,channelid ); 133 134if (found == size ) { 135kv_destroy (buf -> update_channels ); 136kv_init (buf -> update_channels ); 137 } 138 } 139} 140 141void buf_free_callbacks (buf_T * buf ) 142{ 143kv_destroy (buf -> update_channels ); 144for (size_t i = 0 ;i < kv_size (buf -> update_callbacks );i ++ ) { 145buffer_update_callbacks_free (kv_A (buf -> update_callbacks ,i )); 146 } 147kv_destroy (buf -> update_callbacks ); 148} 149 150void buf_updates_unload (buf_T * buf ,bool can_reload ) 151{ 152size_t size = kv_size (buf -> update_channels ); 153if (size ) { 154for (size_t i = 0 ;i < size ;i ++ ) { 155buf_updates_send_end (buf ,kv_A (buf -> update_channels ,i )); 156 } 157kv_destroy (buf -> update_channels ); 158kv_init (buf -> update_channels ); 159 } 160 161size_t j = 0 ; 162for (size_t i = 0 ;i < kv_size (buf -> update_callbacks );i ++ ) { 163BufUpdateCallbacks cb = kv_A (buf -> update_callbacks ,i ); 164LuaRef thecb = LUA_NOREF ; 165 166bool keep = false; 167if (can_reload && cb .on_reload != LUA_NOREF ) { 168keep = true; 169thecb = cb .on_reload ; 170 }else if (cb .on_detach != LUA_NOREF ) { 171thecb = cb .on_detach ; 172 } 173 174if (thecb != LUA_NOREF ) { 175MAXSIZE_TEMP_ARRAY (args ,1 ); 176 177// the first argument is always the buffer handle 178ADD_C (args ,BUFFER_OBJ (buf -> handle )); 179 180TEXTLOCK_WRAP ({ 181nlua_call_ref (thecb ,keep ?"reload" :"detach" ,args , false,NULL ,NULL ); 182 }); 183 } 184 185if (keep ) { 186kv_A (buf -> update_callbacks ,j ++ )= kv_A (buf -> update_callbacks ,i ); 187 }else { 188buffer_update_callbacks_free (cb ); 189 } 190 } 191kv_size (buf -> update_callbacks )= j ; 192if (kv_size (buf -> update_callbacks )== 0 ) { 193kv_destroy (buf -> update_callbacks ); 194kv_init (buf -> update_callbacks ); 195 } 196} 197 198void buf_updates_send_changes (buf_T * buf ,linenr_T firstline ,int64_t num_added , 199int64_t num_removed ) 200{ 201size_t deleted_codepoints ,deleted_codeunits ; 202size_t deleted_bytes = ml_flush_deleted_bytes (buf ,& deleted_codepoints , 203& deleted_codeunits ); 204 205if (!buf_updates_active (buf )) { 206return ; 207 } 208 209// Don't send b:changedtick during 'inccommand' preview if "buf" is the current buffer. 210bool send_tick = !(cmdpreview && buf == curbuf ); 211 212// if one the channels doesn't work, put its ID here so we can remove it later 213uint64_t badchannelid = 0 ; 214 215Arena arena = ARENA_EMPTY ; 216Array linedata = ARRAY_DICT_INIT ; 217if (num_added > 0 && kv_size (buf -> update_channels )) { 218STATIC_ASSERT (SIZE_MAX >=MAXLNUM ,"size_t smaller than MAXLNUM" ); 219linedata = arena_array (& arena , (size_t )num_added ); 220buf_collect_lines (buf , (size_t )num_added ,firstline ,0 , true,& linedata , 221NULL ,& arena ); 222 } 223 224// notify each of the active channels 225for (size_t i = 0 ;i < kv_size (buf -> update_channels );i ++ ) { 226uint64_t channelid = kv_A (buf -> update_channels ,i ); 227 228// send through the changes now channel contents now 229MAXSIZE_TEMP_ARRAY (args ,6 ); 230 231// the first argument is always the buffer handle 232ADD_C (args ,BUFFER_OBJ (buf -> handle )); 233 234// next argument is b:changedtick 235ADD_C (args ,send_tick ?INTEGER_OBJ (buf_get_changedtick (buf )) :NIL ); 236 237// the first line that changed (zero-indexed) 238ADD_C (args ,INTEGER_OBJ (firstline - 1 )); 239 240// the last line that was changed 241ADD_C (args ,INTEGER_OBJ (firstline - 1 + num_removed )); 242 243// linedata of lines being swapped in 244ADD_C (args ,ARRAY_OBJ (linedata )); 245ADD_C (args ,BOOLEAN_OBJ (false)); 246if (!rpc_send_event (channelid ,"nvim_buf_lines_event" ,args )) { 247// We can't unregister the channel while we're iterating over the 248// update_channels array, so we remember its ID to unregister it at 249// the end. 250badchannelid = channelid ; 251 } 252 } 253 254// We can only ever remove one dead channel at a time. This is OK because the 255// change notifications are so frequent that many dead channels will be 256// cleared up quickly. 257if (badchannelid != 0 ) { 258ELOG ("Disabling buffer updates for dead channel %" PRIu64 ,badchannelid ); 259buf_updates_unregister (buf ,badchannelid ); 260 } 261 262// callbacks don't use linedata 263arena_mem_free (arena_finish (& arena )); 264 265// notify each of the active callbacks 266size_t j = 0 ; 267for (size_t i = 0 ;i < kv_size (buf -> update_callbacks );i ++ ) { 268BufUpdateCallbacks cb = kv_A (buf -> update_callbacks ,i ); 269bool keep = true; 270if (cb .on_lines != LUA_NOREF && (cb .preview || !cmdpreview )) { 271MAXSIZE_TEMP_ARRAY (args ,8 );// 6 or 8 used 272 273// the first argument is always the buffer handle 274ADD_C (args ,BUFFER_OBJ (buf -> handle )); 275 276// next argument is b:changedtick 277ADD_C (args ,send_tick ?INTEGER_OBJ (buf_get_changedtick (buf )) :NIL ); 278 279// the first line that changed (zero-indexed) 280ADD_C (args ,INTEGER_OBJ (firstline - 1 )); 281 282// the last line that was changed 283ADD_C (args ,INTEGER_OBJ (firstline - 1 + num_removed )); 284 285// the last line in the updated range 286ADD_C (args ,INTEGER_OBJ (firstline - 1 + num_added )); 287 288// byte count of previous contents 289ADD_C (args ,INTEGER_OBJ ((Integer )deleted_bytes )); 290if (cb .utf_sizes ) { 291ADD_C (args ,INTEGER_OBJ ((Integer )deleted_codepoints )); 292ADD_C (args ,INTEGER_OBJ ((Integer )deleted_codeunits )); 293 } 294 295Object res ; 296TEXTLOCK_WRAP ({ 297res = nlua_call_ref (cb .on_lines ,"lines" ,args ,kRetNilBool ,NULL ,NULL ); 298 }); 299 300if (LUARET_TRUTHY (res )) { 301buffer_update_callbacks_free (cb ); 302keep = false; 303 } 304 } 305if (keep ) { 306kv_A (buf -> update_callbacks ,j ++ )= kv_A (buf -> update_callbacks ,i ); 307 } 308 } 309kv_size (buf -> update_callbacks )= j ; 310} 311 312void buf_updates_send_splice (buf_T * buf ,int start_row ,colnr_T start_col ,bcount_t start_byte , 313int old_row ,colnr_T old_col ,bcount_t old_byte ,int new_row , 314colnr_T new_col ,bcount_t new_byte ) 315{ 316if (!buf_updates_active (buf ) 317|| (old_byte == 0 && new_byte == 0 )) { 318return ; 319 } 320 321// notify each of the active callbacks 322size_t j = 0 ; 323for (size_t i = 0 ;i < kv_size (buf -> update_callbacks );i ++ ) { 324BufUpdateCallbacks cb = kv_A (buf -> update_callbacks ,i ); 325bool keep = true; 326if (cb .on_bytes != LUA_NOREF && (cb .preview || !cmdpreview )) { 327MAXSIZE_TEMP_ARRAY (args ,11 ); 328 329// the first argument is always the buffer handle 330ADD_C (args ,BUFFER_OBJ (buf -> handle )); 331 332// next argument is b:changedtick 333ADD_C (args ,INTEGER_OBJ (buf_get_changedtick (buf ))); 334 335ADD_C (args ,INTEGER_OBJ (start_row )); 336ADD_C (args ,INTEGER_OBJ (start_col )); 337ADD_C (args ,INTEGER_OBJ (start_byte )); 338ADD_C (args ,INTEGER_OBJ (old_row )); 339ADD_C (args ,INTEGER_OBJ (old_col )); 340ADD_C (args ,INTEGER_OBJ (old_byte )); 341ADD_C (args ,INTEGER_OBJ (new_row )); 342ADD_C (args ,INTEGER_OBJ (new_col )); 343ADD_C (args ,INTEGER_OBJ (new_byte )); 344 345Object res ; 346TEXTLOCK_WRAP ({ 347res = nlua_call_ref (cb .on_bytes ,"bytes" ,args ,kRetNilBool ,NULL ,NULL ); 348 }); 349 350if (LUARET_TRUTHY (res )) { 351buffer_update_callbacks_free (cb ); 352keep = false; 353 } 354 } 355if (keep ) { 356kv_A (buf -> update_callbacks ,j ++ )= kv_A (buf -> update_callbacks ,i ); 357 } 358 } 359kv_size (buf -> update_callbacks )= j ; 360} 361void buf_updates_changedtick (buf_T * buf ) 362{ 363// notify each of the active channels 364for (size_t i = 0 ;i < kv_size (buf -> update_channels );i ++ ) { 365uint64_t channel_id = kv_A (buf -> update_channels ,i ); 366buf_updates_changedtick_single (buf ,channel_id ); 367 } 368size_t j = 0 ; 369for (size_t i = 0 ;i < kv_size (buf -> update_callbacks );i ++ ) { 370BufUpdateCallbacks cb = kv_A (buf -> update_callbacks ,i ); 371bool keep = true; 372if (cb .on_changedtick != LUA_NOREF ) { 373MAXSIZE_TEMP_ARRAY (args ,2 ); 374 375// the first argument is always the buffer handle 376ADD_C (args ,BUFFER_OBJ (buf -> handle )); 377 378// next argument is b:changedtick 379ADD_C (args ,INTEGER_OBJ (buf_get_changedtick (buf ))); 380 381Object res ; 382TEXTLOCK_WRAP ({ 383res = nlua_call_ref (cb .on_changedtick ,"changedtick" ,args ,kRetNilBool ,NULL ,NULL ); 384 }); 385 386if (LUARET_TRUTHY (res )) { 387buffer_update_callbacks_free (cb ); 388keep = false; 389 } 390 } 391if (keep ) { 392kv_A (buf -> update_callbacks ,j ++ )= kv_A (buf -> update_callbacks ,i ); 393 } 394 } 395kv_size (buf -> update_callbacks )= j ; 396} 397 398void buf_updates_changedtick_single (buf_T * buf ,uint64_t channel_id ) 399{ 400MAXSIZE_TEMP_ARRAY (args ,2 ); 401 402// the first argument is always the buffer handle 403ADD_C (args ,BUFFER_OBJ (buf -> handle )); 404 405// next argument is b:changedtick 406ADD_C (args ,INTEGER_OBJ (buf_get_changedtick (buf ))); 407 408// don't try and clean up dead channels here 409rpc_send_event (channel_id ,"nvim_buf_changedtick_event" ,args ); 410} 411 412void buffer_update_callbacks_free (BufUpdateCallbacks cb ) 413{ 414api_free_luaref (cb .on_lines ); 415api_free_luaref (cb .on_bytes ); 416api_free_luaref (cb .on_changedtick ); 417api_free_luaref (cb .on_reload ); 418api_free_luaref (cb .on_detach ); 419}