robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// Implements extended marks for plugins. Marks sit in a MarkTree 2// datastructure which provides both efficient mark insertations/lookups 3// and adjustment to text changes. See marktree.c for more details. 4// 5// A map of pointers to the marks is used for fast lookup by mark id. 6// 7// Marks are moved by calls to extmark_splice. Some standard interfaces 8// mark_adjust and inserted_bytes already adjust marks, check if these are 9// being used before adding extmark_splice calls! 10// 11// Undo/Redo of marks is implemented by storing the call arguments to 12// extmark_splice. The list of arguments is applied in extmark_apply_undo. 13// We have to copy extmark positions when the extmarks are within a 14// deleted/changed region. 15// 16// Marks live in namespaces that allow plugins/users to segregate marks 17// from other users. 18// 19// Deleting marks only happens when explicitly calling extmark_del, deleting 20// over a range of marks will only move the marks. Deleting on a mark will 21// leave it in same position unless it is on the EOL of a line. 22// 23// Extmarks are used to implement buffer decoration. Decoration is mostly 24// regarded as an application of extmarks, however for practical reasons code 25// that deletes an extmark with decoration will call back into the decoration 26// code for redrawing the line with the deleted decoration. 27 28#include <assert.h> 29#include <stddef.h> 30 31#include "nvim/api/private/defs.h" 32#include "nvim/buffer_defs.h" 33#include "nvim/buffer_updates.h" 34#include "nvim/decoration.h" 35#include "nvim/decoration_defs.h" 36#include "nvim/extmark.h" 37#include "nvim/extmark_defs.h" 38#include "nvim/globals.h" 39#include "nvim/map_defs.h" 40#include "nvim/marktree.h" 41#include "nvim/mcursor.h" 42#include "nvim/memline.h" 43#include "nvim/memory.h" 44#include "nvim/pos_defs.h" 45#include "nvim/types_defs.h" 46#include "nvim/undo.h" 47#include "nvim/undo_defs.h" 48 49#include "extmark.c.generated.h" 50 51/// Records a raw mark's position in the open undo block, for `extmark_apply_undo()` to replay. 52/// 53/// Pass -1 for a row to skip that direction: the mark is left wherever replaying the surrounding 54/// splices puts it. 55static void extmark_push_savepos (buf_T * buf ,uint64_t mark ,int undo_row ,colnr_T undo_col , 56int redo_row ,colnr_T redo_col ) 57{ 58u_header_T * uhp = u_force_get_undo_header (buf ); 59if (uhp == NULL ) { 60return ; 61 } 62kv_push (uhp -> uh_extmark , ((ExtmarkUndoObject ){ 63 .type = kExtmarkSavePos , 64 .data .savepos = { 65 .mark = mark , 66 .undo_row = undo_row , 67 .undo_col = undo_col , 68 .redo_row = redo_row , 69 .redo_col = redo_col , 70 .invalidated = false, 71 } 72 })); 73} 74 75/// Create or update an extmark 76/// 77/// must not be used during iteration! 78void extmark_set (buf_T * buf ,uint32_t ns_id ,uint32_t * idp ,int row ,colnr_T col ,int end_row , 79colnr_T end_col ,DecorInline decor ,uint16_t decor_flags ,bool right_gravity , 80bool end_right_gravity ,bool no_undo ,bool invalidate ,Error * err ) 81{ 82uint32_t * ns = map_put_ref (uint32_t ,uint32_t )(buf -> b_extmark_ns ,ns_id ,NULL ,NULL ); 83uint32_t id = idp ?* idp :0 ; 84bool created = true;// Mark did not exist before this call. 85 86uint16_t flags = mt_flags (right_gravity ,no_undo ,invalidate ,decor .ext ) |decor_flags ; 87if (id == 0 ) { 88id = ++* ns ; 89 }else { 90MarkTreeIter itr [1 ]= {0 }; 91MTKey old_mark = marktree_lookup_ns (buf -> b_marktree ,ns_id ,id , false,itr ); 92if (old_mark .id ) { 93created = false; 94// Moved inside an open undo block: record BOTH positions, because replaying the surrounding 95// splices reproduces only the shifts they caused, not an explicit extmark_set(). 96if (!no_undo && !buf -> b_u_synced 97&& (old_mark .pos .row != row || old_mark .pos .col != col )) { 98extmark_push_savepos (buf ,mt_lookup_key (old_mark ),old_mark .pos .row ,old_mark .pos .col ,row , 99col ); 100 } 101if (mt_paired (old_mark )|| end_row > -1 ) { 102extmark_del_id (buf ,ns_id ,id ); 103 }else { 104assert (marktree_itr_valid (itr )); 105if (old_mark .pos .row == row && old_mark .pos .col == col ) { 106// not paired: we can revise in place 107if (!mt_invalid (old_mark )&& mt_decor_any (old_mark )) { 108mt_itr_rawkey (itr ).flags &= (uint16_t ) ~MT_FLAG_EXTERNAL_MASK ; 109buf_decor_remove (buf ,row ,row ,col ,mt_decor (old_mark ), true); 110 } 111mt_itr_rawkey (itr ).flags |=flags ; 112mt_itr_rawkey (itr ).decor_data = decor .data ; 113marktree_revise_meta (buf -> b_marktree ,itr ,old_mark ); 114 gotorevised ; 115 } 116marktree_del_itr (buf -> b_marktree ,itr , false); 117if (!mt_invalid (old_mark )) { 118buf_decor_remove (buf ,old_mark .pos .row ,old_mark .pos .row ,old_mark .pos .col , 119mt_decor (old_mark ), true); 120 } 121 } 122 }else { 123* ns = MAX (* ns ,id ); 124 } 125 } 126 127MTKey mark = { {row ,col },ns_id ,id ,flags ,decor .data }; 128 129marktree_put (buf -> b_marktree ,mark ,end_row ,end_col ,end_right_gravity ); 130decor_state_invalidate (buf ); 131 132// Created inside an open undo block: record the position for redo. Undo leaves the mark where the 133// splice replay puts it (it did not exist before the edit), but redo must re-apply the explicit 134// position, else a mark over text that undo deleted never comes back. 135if (created && !no_undo && !buf -> b_u_synced ) { 136extmark_push_savepos (buf ,mt_lookup_id (ns_id ,id , false),-1 ,0 ,row ,col ); 137if (end_row > -1 ) { 138extmark_push_savepos (buf ,mt_lookup_id (ns_id ,id , true),-1 ,0 ,end_row ,end_col ); 139 } 140 } 141 142revised : 143if (decor_flags || decor .ext ) { 144buf_put_decor (buf ,decor ,row ,end_row > -1 ?end_row :row ); 145decor_redraw (buf ,row ,end_row > -1 ?end_row :row ,col ,decor ); 146 } 147 148if (idp ) { 149* idp = id ; 150 } 151 152if (created ) { 153// A new multicursor extmark is a new cursor. 154mc_on_extmark_set (buf ,ns_id ,id , (pos_T ){ .lnum = row + 1 , .col = col }); 155 } 156} 157 158/// Moves a raw mark back to a recorded position. 159/// 160/// @param revive Clear MT_FLAG_INVALID, if set: the associated text exists again. 161static void extmark_setraw (buf_T * buf ,uint64_t mark ,int row ,colnr_T col ,bool revive ) 162{ 163MarkTreeIter itr [1 ]= {0 }; 164MTKey key = marktree_lookup (buf -> b_marktree ,mark ,itr ); 165bool move = key .pos .row != row || key .pos .col != col ; 166// Only an invalidated mark is revived; reviving a valid one would double-count its decor. 167bool invalid = revive && mt_invalid (key ); 168if (key .pos .row < 0 || (!move && !invalid )) { 169return ;// Mark was deleted or no change needed 170 } 171 172// Only the position before undo needs to be redrawn here, 173// as the position after undo should be marked as changed. 174if (!invalid && mt_decor_any (key )&& key .pos .row != row ) { 175decor_redraw (buf ,key .pos .row ,key .pos .row ,key .pos .col ,mt_decor (key )); 176 } 177 178int row1 = 0 ; 179int row2 = 0 ; 180MarkTreeIter altitr [1 ]= {* itr }; 181MTKey alt = marktree_get_alt (buf -> b_marktree ,key ,altitr ); 182 183if (invalid ) { 184mt_itr_rawkey (itr ).flags &= (uint16_t ) ~MT_FLAG_INVALID ; 185mt_itr_rawkey (altitr ).flags &= (uint16_t ) ~MT_FLAG_INVALID ; 186marktree_revise_meta (buf -> b_marktree ,mt_end (key ) ?altitr :itr ,mt_end (key ) ?alt :key ); 187 }else if (!mt_invalid (key )&& key .flags & MT_FLAG_DECOR_SIGNTEXT && buf -> b_signcols .autom ) { 188row1 = MIN (alt .pos .row ,MIN (key .pos .row ,row )); 189row2 = MAX (alt .pos .row ,MAX (key .pos .row ,row )); 190buf_signcols_count_range (buf ,row1 ,MIN (curbuf -> b_ml .ml_line_count - 1 ,row2 ),0 ,kTrue ); 191 } 192 193if (move ) { 194marktree_move (buf -> b_marktree ,itr ,row ,col ); 195 } 196 197if (invalid ) { 198buf_put_decor (buf ,mt_decor (key ),MIN (row ,alt .pos .row ),MAX (row ,alt .pos .row )); 199 }else if (!mt_invalid (key )&& key .flags & MT_FLAG_DECOR_SIGNTEXT && buf -> b_signcols .autom ) { 200buf_signcols_count_range (buf ,row1 ,MIN (curbuf -> b_ml .ml_line_count - 1 ,row2 ),0 ,kNone ); 201 } 202} 203 204/// Remove an extmark in "ns_id" by "id" 205/// 206/// @return false on missing id 207bool extmark_del_id (buf_T * buf ,uint32_t ns_id ,uint32_t id ) 208{ 209MarkTreeIter itr [1 ]= {0 }; 210MTKey key = marktree_lookup_ns (buf -> b_marktree ,ns_id ,id , false,itr ); 211if (key .id ) { 212extmark_del (buf ,itr ,key , false); 213 } 214 215return key .id > 0 ; 216} 217 218/// Remove a (paired) extmark "key" pointed to by "itr" 219void extmark_del (buf_T * buf ,MarkTreeIter * itr ,MTKey key ,bool restore ) 220{ 221assert (key .pos .row >=0 ); 222 223MTKey key2 = key ; 224uint64_t other = marktree_del_itr (buf -> b_marktree ,itr , false); 225if (other ) { 226key2 = marktree_lookup (buf -> b_marktree ,other ,itr ); 227assert (key2 .pos .row >=0 ); 228marktree_del_itr (buf -> b_marktree ,itr , false); 229if (restore ) { 230marktree_itr_get (buf -> b_marktree ,key .pos .row ,key .pos .col ,itr ); 231 } 232 } 233 234if (mt_decor_any (key )) { 235if (mt_invalid (key )) { 236decor_free (mt_decor (key )); 237 }else { 238if (mt_end (key )) { 239MTKey k = key ; 240key = key2 ; 241key2 = k ; 242 } 243buf_decor_remove (buf ,key .pos .row ,key2 .pos .row ,key .pos .col ,mt_decor (key ), true); 244 } 245 } 246 247decor_state_invalidate (buf ); 248 249// TODO(bfredl): delete it from current undo header, opportunistically? 250} 251 252/// Free extmarks in a ns between lines 253/// if ns = 0, it means clear all namespaces 254bool extmark_clear (buf_T * buf ,uint32_t ns_id ,int l_row ,colnr_T l_col ,int u_row ,colnr_T u_col ) 255{ 256if (!map_size (buf -> b_extmark_ns )) { 257return false; 258 } 259 260// Multicursor extmarks are about to die, let mc snapshot them. 261mc_ns_clearing (buf ,ns_id ); 262 263bool all_ns = (ns_id == 0 ); 264uint32_t * ns = NULL ; 265if (!all_ns ) { 266ns = map_ref (uint32_t ,uint32_t )(buf -> b_extmark_ns ,ns_id ,NULL ); 267if (!ns ) { 268// nothing to do 269return false; 270 } 271 } 272 273bool marks_cleared_any = false; 274bool marks_cleared_all = l_row == 0 && l_col == 0 ; 275 276MarkTreeIter itr [1 ]= {0 }; 277marktree_itr_get (buf -> b_marktree ,l_row ,l_col ,itr ); 278while (true) { 279MTKey mark = marktree_itr_current (itr ); 280if (mark .pos .row < 0 281|| mark .pos .row > u_row 282|| (mark .pos .row == u_row && mark .pos .col > u_col )) { 283if (mark .pos .row >=0 ) { 284marks_cleared_all = false; 285 } 286break ; 287 } 288if (mark .ns == ns_id || all_ns ) { 289marks_cleared_any = true; 290extmark_del (buf ,itr ,mark , true); 291 }else { 292marktree_itr_next (buf -> b_marktree ,itr ); 293 } 294 } 295 296if (marks_cleared_all ) { 297if (all_ns ) { 298map_destroy (uint32_t ,buf -> b_extmark_ns ); 299* buf -> b_extmark_ns = (Map (uint32_t ,uint32_t ))MAP_INIT ; 300 }else { 301map_del (uint32_t ,uint32_t )(buf -> b_extmark_ns ,ns_id ,NULL ); 302 } 303 } 304 305if (marks_cleared_any ) { 306decor_state_invalidate (buf ); 307// Deleting the "nvim.multicursor" namespace deletes the cursors it tracked. 308// TODO(justinmk): ideally, clearing a ns could be handled in userspace, e.g. an event? 309mc_ns_cleared (buf ,ns_id ); 310 } 311 312return marks_cleared_any ; 313} 314 315/// @return the position of marks between a range, 316/// marks found at the start or end index will be included. 317/// 318/// if upper_lnum or upper_col are negative the buffer 319/// will be searched to the start, or end 320/// amount = amount of marks to find or INT64_MAX for all 321ExtmarkInfoArray extmark_get (buf_T * buf ,uint32_t ns_id ,int l_row ,colnr_T l_col ,int u_row , 322colnr_T u_col ,int64_t amount ,ExtmarkType type_filter ,bool overlap ) 323{ 324ExtmarkInfoArray array = KV_INITIAL_VALUE ; 325MarkTreeIter itr [1 ]; 326 327if (overlap ) { 328// Find all the marks overlapping the start position 329if (!marktree_itr_get_overlap (buf -> b_marktree ,l_row ,l_col ,itr )) { 330return array ; 331 } 332 333while ((int64_t )kv_size (array )< amount ) { 334MTPair pair ; 335if (!marktree_itr_step_overlap (buf -> b_marktree ,itr ,& pair )) { 336break ; 337 } 338push_mark (& array ,ns_id ,type_filter ,pair ); 339 } 340 }else { 341// Find all the marks beginning with the start position 342marktree_itr_get_ext (buf -> b_marktree ,MTPos (l_row ,l_col ), 343itr , false, false,NULL ,NULL ); 344 } 345 346while ((int64_t )kv_size (array )< amount ) { 347MTKey mark = marktree_itr_current (itr ); 348if (mark .pos .row < 0 349|| (mark .pos .row > u_row ) 350|| (mark .pos .row == u_row && mark .pos .col > u_col )) { 351break ; 352 } 353if (!mt_end (mark )) { 354MTKey end = marktree_get_alt (buf -> b_marktree ,mark ,NULL ); 355push_mark (& array ,ns_id ,type_filter ,mtpair_from (mark ,end )); 356 } 357marktree_itr_next (buf -> b_marktree ,itr ); 358 } 359return array ; 360} 361 362static void push_mark (ExtmarkInfoArray * array ,uint32_t ns_id ,ExtmarkType type_filter ,MTPair mark ) 363{ 364if (!(ns_id == UINT32_MAX || mark .start .ns == ns_id )) { 365return ; 366 } 367if (type_filter != kExtmarkNone ) { 368if (!mt_decor_any (mark .start )) { 369return ; 370 } 371uint16_t type_flags = decor_type_flags (mt_decor (mark .start )); 372 373if (!(type_flags & type_filter )) { 374return ; 375 } 376 } 377 378kv_push (* array ,mark ); 379} 380 381/// Lookup an extmark by id 382MTPair extmark_from_id (buf_T * buf ,uint32_t ns_id ,uint32_t id ) 383{ 384MTKey mark = marktree_lookup_ns (buf -> b_marktree ,ns_id ,id , false,NULL ); 385if (!mark .id ) { 386return mtpair_from (mark ,mark );// invalid 387 } 388assert (mark .pos .row >=0 ); 389MTKey end = marktree_get_alt (buf -> b_marktree ,mark ,NULL ); 390 391return mtpair_from (mark ,end ); 392} 393 394/// Creates or updates point extmark (shifts with buffer edits) `mark` at `pos`. 395/// 396/// @param right_gravity The mark shifts with text inserted exactly at its position (e.g. "o" on the 397/// line above). 398/// @param no_undo Transient mark: undo does not restore its position. 399/// @param ui_watched Mark is drawn by the UI, which receives its position per redraw (ui-event 400/// "win_extmark"). 401void extmark_set_pos (buf_T * buf ,uint32_t ns_id ,uint32_t * mark ,pos_T pos ,bool right_gravity , 402bool no_undo ,bool ui_watched ) 403{ 404DecorInline decor = DECOR_INLINE_INIT ; 405if (ui_watched ) { 406decor .data .hl .flags = kSHUIWatched |kSHUIWatchedOverlay ; 407 } 408extmark_set (buf ,ns_id ,mark , (int )pos .lnum - 1 ,pos .col ,-1 ,0 ,decor , 409ui_watched ?MT_FLAG_DECOR_HL :0 ,right_gravity , false,no_undo , false,NULL ); 410} 411 412/// Gets the position of an extmark, shifted by buffer edits since it was set. Keeps `pos->coladd`. 413/// 414/// @return False if the mark no longer exists (`pos` untouched). 415bool extmark_get_pos (buf_T * buf ,uint32_t ns_id ,uint32_t id ,pos_T * pos ) 416{ 417MTPair mtp = extmark_from_id (buf ,ns_id ,id ); 418if (mtp .start .id == 0 ) { 419return false; 420 } 421pos -> lnum = mtp .start .pos .row + 1 ; 422pos -> col = mtp .start .pos .col ; 423return true; 424} 425 426/// free extmarks from the buffer 427void extmark_free_all (buf_T * buf ) 428{ 429MarkTreeIter itr [1 ]= {0 }; 430marktree_itr_get (buf -> b_marktree ,0 ,0 ,itr ); 431while (true) { 432MTKey mark = marktree_itr_current (itr ); 433if (mark .pos .row < 0 ) { 434break ; 435 } 436 437// don't free mark.decor twice for a paired mark. 438if (!(mt_paired (mark )&& mt_end (mark ))) { 439decor_free (mt_decor (mark )); 440 } 441 442marktree_itr_next (buf -> b_marktree ,itr ); 443 } 444 445marktree_clear (buf -> b_marktree ); 446 447buf -> b_signcols .max = 0 ; 448CLEAR_FIELD (buf -> b_signcols .count ); 449 450map_destroy (uint32_t ,buf -> b_extmark_ns ); 451* buf -> b_extmark_ns = (Map (uint32_t ,uint32_t ))MAP_INIT ; 452} 453 454/// invalidate extmarks between range and copy to undo header 455/// 456/// copying is useful when we cannot simply reverse the operation. This will do 457/// nothing on redo, enforces correct position when undo. 458void extmark_splice_delete (buf_T * buf ,int l_row ,colnr_T l_col ,int u_row ,colnr_T u_col , 459extmark_undo_vec_t * uvp ,bool only_copy ,ExtmarkOp op ) 460{ 461MarkTreeIter itr [1 ]= {0 }; 462ExtmarkUndoObject undo ; 463 464marktree_itr_get (buf -> b_marktree , (int32_t )l_row ,l_col ,itr ); 465while (true) { 466MTKey mark = marktree_itr_current (itr ); 467if (mark .pos .row < 0 || mark .pos .row > u_row ) { 468break ; 469 } 470 471bool copy = true; 472// No need to copy left gravity marks at the beginning of the range, 473// and right gravity marks at the end of the range, unless invalidated. 474if (mark .pos .row == l_row && mark .pos .col - !mt_right (mark )< l_col ) { 475copy = false; 476 }else if (mark .pos .row == u_row ) { 477if (mark .pos .col > u_col + 1 ) { 478break ; 479 }else if (mark .pos .col + mt_right (mark )> u_col ) { 480copy = false; 481 } 482 } 483 484bool invalidated = false; 485// Invalidate/delete mark 486if (!only_copy && !mt_invalid (mark )&& mt_invalidate (mark )&& !mt_end (mark )) { 487MarkTreeIter enditr [1 ]= {* itr }; 488MTPos endpos = marktree_get_altpos (buf -> b_marktree ,mark ,enditr ); 489// Invalidate unpaired marks in deleted lines and paired marks whose entire 490// range has been deleted. 491if ((!mt_paired (mark )&& mark .pos .row < u_row ) 492|| (mt_paired (mark ) 493&& (mark .pos .row > l_row || (mark .pos .row == l_row && mark .pos .col >=l_col )) 494&& (endpos .row < u_row || (endpos .row == u_row && endpos .col <=u_col )))) { 495if (mt_no_undo (mark )) { 496extmark_del (buf ,itr ,mark , true); 497continue ; 498 }else { 499copy = true; 500invalidated = true; 501mt_itr_rawkey (itr ).flags |=MT_FLAG_INVALID ; 502mt_itr_rawkey (enditr ).flags |=MT_FLAG_INVALID ; 503marktree_revise_meta (buf -> b_marktree ,itr ,mark ); 504buf_decor_remove (buf ,mark .pos .row ,endpos .row ,mark .pos .col ,mt_decor (mark ), false); 505 } 506 } 507 } 508 509// Push mark to undo header 510if (copy && (only_copy || (uvp != NULL && op == kExtmarkUndo && !mt_no_undo (mark )))) { 511ExtmarkSavePos pos = { 512 .mark = mt_lookup_key (mark ), 513 .invalidated = invalidated , 514 .undo_row = mark .pos .row , 515 .undo_col = mark .pos .col , 516 .redo_row = -1 , 517 .redo_col = -1 , 518 }; 519undo .data .savepos = pos ; 520undo .type = kExtmarkSavePos ; 521kv_push (* uvp ,undo ); 522 } 523 524marktree_itr_next (buf -> b_marktree ,itr ); 525 } 526} 527 528/// undo or redo an extmark operation 529void extmark_apply_undo (ExtmarkUndoObject undo_info ,bool undo ) 530{ 531// splice: any text operation changing position (except :move) 532if (undo_info .type == kExtmarkSplice ) { 533// Undo 534ExtmarkSplice splice = undo_info .data .splice ; 535if (undo ) { 536extmark_splice_impl (curbuf , 537splice .start_row ,splice .start_col ,splice .start_byte , 538splice .new_row ,splice .new_col ,splice .new_byte , 539splice .old_row ,splice .old_col ,splice .old_byte , 540kExtmarkNoUndo ); 541 }else { 542extmark_splice_impl (curbuf , 543splice .start_row ,splice .start_col ,splice .start_byte , 544splice .old_row ,splice .old_col ,splice .old_byte , 545splice .new_row ,splice .new_col ,splice .new_byte , 546kExtmarkNoUndo ); 547 } 548// kExtmarkSavePos 549 }else if (undo_info .type == kExtmarkSavePos ) { 550ExtmarkSavePos pos = undo_info .data .savepos ; 551if (undo && pos .undo_row >=0 ) { 552extmark_setraw (curbuf ,pos .mark ,pos .undo_row ,pos .undo_col ,pos .invalidated ); 553 }else if (!undo && pos .redo_row >=0 ) { 554// Redo an explicit extmark_set. The mark was valid when set, revive if undo invalidated it. 555extmark_setraw (curbuf ,pos .mark ,pos .redo_row ,pos .redo_col , true); 556 } 557 }else if (undo_info .type == kExtmarkMove ) { 558ExtmarkMove move = undo_info .data .move ; 559if (undo ) { 560extmark_move_region (curbuf , 561move .new_row ,move .new_col ,move .new_byte , 562move .extent_row ,move .extent_col ,move .extent_byte , 563move .start_row ,move .start_col ,move .start_byte , 564kExtmarkNoUndo ); 565 }else { 566extmark_move_region (curbuf , 567move .start_row ,move .start_col ,move .start_byte , 568move .extent_row ,move .extent_col ,move .extent_byte , 569move .new_row ,move .new_col ,move .new_byte , 570kExtmarkNoUndo ); 571 } 572 } 573} 574 575/// Adjust extmark row for inserted/deleted rows (columns stay fixed). 576void extmark_adjust (buf_T * buf ,linenr_T line1 ,linenr_T line2 ,linenr_T amount , 577linenr_T amount_after ,ExtmarkOp undo ) 578{ 579if (curbuf_splice_pending ) { 580return ; 581 } 582bcount_t start_byte = ml_find_line_or_offset (buf ,line1 ,NULL , true); 583bcount_t old_byte = 0 ; 584bcount_t new_byte = 0 ; 585int old_row ; 586int new_row ; 587if (amount == MAXLNUM ) { 588old_row = line2 - line1 + 1 ; 589// TODO(bfredl): ej kasta? 590old_byte = (bcount_t )buf -> deleted_bytes2 ; 591new_row = amount_after + old_row ; 592 }else { 593// A region is either deleted (amount == MAXLNUM) or 594// added (line2 == MAXLNUM). The only other case is :move 595// which is handled by a separate entry point extmark_move_region. 596assert (line2 == MAXLNUM ); 597old_row = 0 ; 598new_row = (int )amount ; 599 } 600if (new_row > 0 ) { 601new_byte = ml_find_line_or_offset (buf ,line1 + new_row ,NULL , true) 602- start_byte ; 603 } 604extmark_splice_impl (buf , 605 (int )line1 - 1 ,0 ,start_byte , 606old_row ,0 ,old_byte , 607new_row ,0 ,new_byte ,undo ); 608} 609 610// Adjusts extmarks after a text edit, and emits the `on_bytes` event (`:h api-buffer-updates`). 611// 612// @param buf 613// @param start_row Start row of the region to be changed 614// @param start_col Start col of the region to be changed 615// @param old_row End row of the region to be changed. 616// Encoded as an offset to start_row. 617// @param old_col End col of the region to be changed. Encodes 618// an offset from start_col if old_row = 0; otherwise, 619// encodes the end column of the old region. 620// @param old_byte Byte extent of the region to be changed. 621// @param new_row Row offset of the new region. 622// @param new_col Col offset of the new region. Encodes an offset from 623// start_col if new_row = 0; otherwise, encodes 624// the end column of the new region. 625// @param new_byte Byte extent of the new region. 626// @param undo 627void extmark_splice (buf_T * buf ,int start_row ,colnr_T start_col ,int old_row ,colnr_T old_col , 628bcount_t old_byte ,int new_row ,colnr_T new_col ,bcount_t new_byte , 629ExtmarkOp undo ) 630{ 631int offset = ml_find_line_or_offset (buf ,start_row + 1 ,NULL , true); 632 633// On empty buffers, when editing the first line, the line is buffered, 634// causing offset to be < 0. While the buffer is not actually empty, the 635// buffered line has not been flushed (and should not be) yet, so the call is 636// valid but an edge case. 637// 638// TODO(vigoux): maybe the is a better way of testing that ? 639if (offset < 0 && buf -> b_ml .ml_chunksize == NULL ) { 640offset = 0 ; 641 } 642extmark_splice_impl (buf ,start_row ,start_col ,offset + start_col , 643old_row ,old_col ,old_byte ,new_row ,new_col ,new_byte , 644undo ); 645} 646 647void extmark_splice_impl (buf_T * buf ,int start_row ,colnr_T start_col ,bcount_t start_byte , 648int old_row ,colnr_T old_col ,bcount_t old_byte ,int new_row , 649colnr_T new_col ,bcount_t new_byte ,ExtmarkOp undo ) 650{ 651buf -> deleted_bytes2 = 0 ; 652buf_updates_send_splice (buf ,start_row ,start_col ,start_byte , 653old_row ,old_col ,old_byte , 654new_row ,new_col ,new_byte ); 655 656if (old_row > 0 || old_col > 0 ) { 657// Copy and invalidate marks that would be effected by delete 658// TODO(bfredl): Be smart about marks that already have been 659// saved (important for merge!) 660int end_row = start_row + old_row ; 661int end_col = (old_row ?0 :start_col )+ old_col ; 662u_header_T * uhp = u_force_get_undo_header (buf ); 663extmark_undo_vec_t * uvp = uhp ?& uhp -> uh_extmark :NULL ; 664extmark_splice_delete (buf ,start_row ,start_col ,end_row ,end_col ,uvp , false,undo ); 665 } 666 667// Remove signs inside edited region from "b_signcols.count", add after splicing. 668if (old_row > 0 || new_row > 0 ) { 669int count = buf -> b_prev_line_count > 0 ?buf -> b_prev_line_count :buf -> b_ml .ml_line_count ; 670buf_signcols_count_range (buf ,start_row ,MIN (count - 1 ,start_row + old_row ),0 ,kTrue ); 671buf -> b_prev_line_count = 0 ; 672 } 673 674marktree_splice (buf -> b_marktree , (int32_t )start_row ,start_col , 675old_row ,old_col , 676new_row ,new_col ); 677 678if (old_row > 0 || new_row > 0 ) { 679int row2 = MIN (buf -> b_ml .ml_line_count - 1 ,start_row + new_row ); 680buf_signcols_count_range (buf ,start_row ,row2 ,0 ,kNone ); 681 } 682 683if (undo == kExtmarkUndo ) { 684u_header_T * uhp = u_force_get_undo_header (buf ); 685if (!uhp ) { 686return ; 687 } 688 689bool merged = false; 690// TODO(bfredl): this is quite rudimentary. We merge small (within line) 691// inserts with each other and small deletes with each other. Add full 692// merge algorithm later. 693if (old_row == 0 && new_row == 0 && kv_size (uhp -> uh_extmark )) { 694ExtmarkUndoObject * item = & kv_A (uhp -> uh_extmark , 695kv_size (uhp -> uh_extmark )- 1 ); 696if (item -> type == kExtmarkSplice ) { 697ExtmarkSplice * splice = & item -> data .splice ; 698if (splice -> start_row == start_row && splice -> old_row == 0 699&& splice -> new_row == 0 ) { 700if (old_col == 0 && start_col >=splice -> start_col 701&& start_col <=splice -> start_col + splice -> new_col ) { 702splice -> new_col += new_col ; 703splice -> new_byte += new_byte ; 704merged = true; 705 }else if (new_col == 0 706&& start_col == splice -> start_col + splice -> new_col ) { 707splice -> old_col += old_col ; 708splice -> old_byte += old_byte ; 709merged = true; 710 }else if (new_col == 0 711&& start_col + old_col == splice -> start_col ) { 712splice -> start_col = start_col ; 713splice -> start_byte = start_byte ; 714splice -> old_col += old_col ; 715splice -> old_byte += old_byte ; 716merged = true; 717 } 718 } 719 } 720 } 721 722if (!merged ) { 723ExtmarkSplice splice ; 724splice .start_row = start_row ; 725splice .start_col = start_col ; 726splice .start_byte = start_byte ; 727splice .old_row = old_row ; 728splice .old_col = old_col ; 729splice .old_byte = old_byte ; 730splice .new_row = new_row ; 731splice .new_col = new_col ; 732splice .new_byte = new_byte ; 733 734kv_push (uhp -> uh_extmark , 735 ((ExtmarkUndoObject ){ .type = kExtmarkSplice , 736 .data .splice = splice })); 737 } 738 } 739} 740 741void extmark_splice_cols (buf_T * buf ,int start_row ,colnr_T start_col ,colnr_T old_col , 742colnr_T new_col ,ExtmarkOp undo ) 743{ 744extmark_splice (buf ,start_row ,start_col , 7450 ,old_col ,old_col , 7460 ,new_col ,new_col ,undo ); 747} 748 749void extmark_move_region (buf_T * buf ,int start_row ,colnr_T start_col ,bcount_t start_byte , 750int extent_row ,colnr_T extent_col ,bcount_t extent_byte ,int new_row , 751colnr_T new_col ,bcount_t new_byte ,ExtmarkOp undo ) 752{ 753buf -> deleted_bytes2 = 0 ; 754// TODO(bfredl): this is not synced to the buffer state inside the callback. 755// But unless we make the undo implementation smarter, this is not ensured 756// anyway. 757buf_updates_send_splice (buf ,start_row ,start_col ,start_byte , 758extent_row ,extent_col ,extent_byte , 7590 ,0 ,0 ); 760 761int row1 = MIN (start_row ,new_row ); 762int row2 = MAX (start_row ,new_row )+ extent_row ; 763buf_signcols_count_range (buf ,row1 ,row2 ,0 ,kTrue ); 764 765marktree_move_region (buf -> b_marktree ,start_row ,start_col , 766extent_row ,extent_col , 767new_row ,new_col ); 768 769buf_signcols_count_range (buf ,row1 ,row2 ,0 ,kNone ); 770 771buf_updates_send_splice (buf ,new_row ,new_col ,new_byte , 7720 ,0 ,0 , 773extent_row ,extent_col ,extent_byte ); 774 775if (undo == kExtmarkUndo ) { 776u_header_T * uhp = u_force_get_undo_header (buf ); 777if (!uhp ) { 778return ; 779 } 780 781ExtmarkMove move ; 782move .start_row = start_row ; 783move .start_col = start_col ; 784move .start_byte = start_byte ; 785move .extent_row = extent_row ; 786move .extent_col = extent_col ; 787move .extent_byte = extent_byte ; 788move .new_row = new_row ; 789move .new_col = new_col ; 790move .new_byte = new_byte ; 791 792kv_push (uhp -> uh_extmark , 793 ((ExtmarkUndoObject ){ .type = kExtmarkMove , 794 .data .move = move })); 795 } 796}