robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Test suite for testing interactions with the incremental sync algorithms powering the LSP client 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local api =n . api 7local clear =n . clear 8local eq =t . eq 9local exec_lua =n . exec_lua 10local feed =n . feed 11 12before_each ( function () 13clear () 14exec_lua ( function () 15local sync =require ( 'vim.lsp.sync' ) 16local events ={} 17 18-- local format_line_ending = { 19-- ["unix"] = '\n', 20-- ["dos"] = '\r\n', 21-- ["mac"] = '\r', 22-- } 23 24-- local line_ending = format_line_ending[vim.api.nvim_get_option_value('fileformat', {})] 25 26--- @diagnostic disable-next-line:duplicate-set-field 27function _G . test_register ( bufnr , id , position_encoding , line_ending ) 28local prev_lines =vim . api . nvim_buf_get_lines ( bufnr , 0 , - 1 , true ) 29 30local function callback ( _ , bufnr0 , _changedtick , firstline , lastline , new_lastline ) 31if _G . test_unreg == id then 32return true 33end 34 35local curr_lines =vim . api . nvim_buf_get_lines ( bufnr0 , 0 , - 1 , true ) 36local incremental_change =sync . compute_diff ( 37prev_lines , 38curr_lines , 39firstline , 40lastline , 41new_lastline , 42position_encoding , 43line_ending 44) 45 46table . insert ( events , incremental_change ) 47prev_lines =curr_lines 48end 49local opts ={ on_lines =callback , on_detach =callback , on_reload =callback } 50vim . api . nvim_buf_attach ( bufnr , false , opts ) 51end 52 53--- @diagnostic disable-next-line:duplicate-set-field 54function _G . get_events () 55local ret_events =events 56events ={} 57return ret_events 58end 59end ) 60end ) 61 62--- @param edit_operations string[] 63local function test_edit ( 64prev_buffer , 65edit_operations , 66expected_text_changes , 67position_encoding , 68line_ending 69) 70position_encoding =position_encoding or 'utf-16' 71line_ending =line_ending or '\n' 72 73api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , prev_buffer ) 74exec_lua ( function () 75return _G . test_register ( 0 , 'test1' , position_encoding , line_ending ) 76end ) 77 78for _ , edit in ipairs ( edit_operations ) do 79feed ( edit ) 80end 81eq ( 82expected_text_changes , 83exec_lua ( function () 84return _G . get_events () 85end ) 86) 87exec_lua ( function () 88_G . test_unreg ='test1' 89end ) 90end 91 92describe ( 'incremental synchronization' , function () 93describe ( 'single line edit' , function () 94it ( 'inserting a character in an empty buffer' , function () 95local expected_text_changes ={ 96{ 97range ={ 98[ 'start' ] ={ 99character =0 , 100line =0 , 101} , 102[ 'end' ] ={ 103character =0 , 104line =0 , 105} , 106} , 107rangeLength =0 , 108text ='a' , 109} , 110} 111test_edit ( { '' } , { 'ia' } , expected_text_changes , 'utf-16' , '\n' ) 112end ) 113it ( 'inserting a character in the middle of a the first line' , function () 114local expected_text_changes ={ 115{ 116range ={ 117[ 'start' ] ={ 118character =1 , 119line =0 , 120} , 121[ 'end' ] ={ 122character =1 , 123line =0 , 124} , 125} , 126rangeLength =0 , 127text ='a' , 128} , 129} 130test_edit ( { 'ab' } , { 'lia' } , expected_text_changes , 'utf-16' , '\n' ) 131end ) 132it ( 'deleting the only character in a buffer' , function () 133local expected_text_changes ={ 134{ 135range ={ 136[ 'start' ] ={ 137character =0 , 138line =0 , 139} , 140[ 'end' ] ={ 141character =1 , 142line =0 , 143} , 144} , 145rangeLength =1 , 146text ='' , 147} , 148} 149test_edit ( { 'a' } , { 'x' } , expected_text_changes , 'utf-16' , '\n' ) 150end ) 151it ( 'deleting a character in the middle of the line' , function () 152local expected_text_changes ={ 153{ 154range ={ 155[ 'start' ] ={ 156character =1 , 157line =0 , 158} , 159[ 'end' ] ={ 160character =2 , 161line =0 , 162} , 163} , 164rangeLength =1 , 165text ='' , 166} , 167} 168test_edit ( { 'abc' } , { 'lx' } , expected_text_changes , 'utf-16' , '\n' ) 169end ) 170it ( 'replacing a character' , function () 171local expected_text_changes ={ 172{ 173range ={ 174[ 'start' ] ={ 175character =0 , 176line =0 , 177} , 178[ 'end' ] ={ 179character =1 , 180line =0 , 181} , 182} , 183rangeLength =1 , 184text ='b' , 185} , 186} 187test_edit ( { 'a' } , { 'rb' } , expected_text_changes , 'utf-16' , '\n' ) 188end ) 189it ( 'deleting the first line' , function () 190local expected_text_changes ={ 191{ 192range ={ 193[ 'start' ] ={ 194character =0 , 195line =0 , 196} , 197[ 'end' ] ={ 198character =0 , 199line =1 , 200} , 201} , 202rangeLength =6 , 203text ='' , 204} , 205} 206test_edit ( { 'hello' , 'world' } , { 'ggdd' } , expected_text_changes , 'utf-16' , '\n' ) 207end ) 208it ( 'deleting the last line' , function () 209local expected_text_changes ={ 210{ 211range ={ 212[ 'start' ] ={ 213character =0 , 214line =1 , 215} , 216[ 'end' ] ={ 217character =0 , 218line =2 , 219} , 220} , 221rangeLength =6 , 222text ='' , 223} , 224} 225test_edit ( { 'hello' , 'world' } , { '2ggdd' } , expected_text_changes , 'utf-16' , '\n' ) 226end ) 227it ( 'deleting all lines' , function () 228local expected_text_changes ={ 229{ 230range ={ 231[ 'start' ] ={ 232character =0 , 233line =0 , 234} , 235[ 'end' ] ={ 236character =5 , 237line =1 , 238} , 239} , 240rangeLength =11 , 241text ='' , 242} , 243} 244test_edit ( { 'hello' , 'world' } , { 'ggdG' } , expected_text_changes , 'utf-16' , '\n' ) 245end ) 246it ( 'deleting an empty line' , function () 247local expected_text_changes ={ 248{ 249range ={ 250[ 'start' ] ={ 251character =0 , 252line =1 , 253} , 254[ 'end' ] ={ 255character =0 , 256line =2 , 257} , 258} , 259rangeLength =1 , 260text ='' , 261} , 262} 263test_edit ( { 'hello world' , '' } , { 'jdd' } , expected_text_changes , 'utf-16' , '\n' ) 264end ) 265it ( 'adding a line' , function () 266local expected_text_changes ={ 267{ 268range ={ 269[ 'start' ] ={ 270character =11 , 271line =0 , 272} , 273[ 'end' ] ={ 274character =0 , 275line =1 , 276} , 277} , 278rangeLength =1 , 279text ='\nhello world\n' , 280} , 281} 282test_edit ( { 'hello world' } , { 'yyp' } , expected_text_changes , 'utf-16' , '\n' ) 283end ) 284it ( 'adding an empty line' , function () 285local expected_text_changes ={ 286{ 287range ={ 288[ 'start' ] ={ 289character =11 , 290line =0 , 291} , 292[ 'end' ] ={ 293character =0 , 294line =1 , 295} , 296} , 297rangeLength =1 , 298text ='\n\n' , 299} , 300} 301test_edit ( { 'hello world' } , { 'o' } , expected_text_changes , 'utf-16' , '\n' ) 302end ) 303it ( 'adding a line to an empty buffer' , function () 304local expected_text_changes ={ 305{ 306range ={ 307[ 'start' ] ={ 308character =0 , 309line =0 , 310} , 311[ 'end' ] ={ 312character =0 , 313line =1 , 314} , 315} , 316rangeLength =1 , 317text ='\n\n' , 318} , 319} 320test_edit ( { '' } , { 'o' } , expected_text_changes , 'utf-16' , '\n' ) 321end ) 322it ( 'insert a line above the current line' , function () 323local expected_text_changes ={ 324{ 325range ={ 326[ 'start' ] ={ 327character =0 , 328line =0 , 329} , 330[ 'end' ] ={ 331character =0 , 332line =0 , 333} , 334} , 335rangeLength =0 , 336text ='\n' , 337} , 338} 339test_edit ( { '' } , { 'O' } , expected_text_changes , 'utf-16' , '\n' ) 340end ) 341end ) 342describe ( 'multi line edit' , function () 343it ( 'deletion and insertion' , function () 344local expected_text_changes ={ 345-- delete "_fsda" from end of line 1 346{ 347range ={ 348[ 'start' ] ={ 349character =4 , 350line =1 , 351} , 352[ 'end' ] ={ 353character =9 , 354line =1 , 355} , 356} , 357rangeLength =5 , 358text ='' , 359} , 360-- delete "hello world\n" from line 2 361{ 362range ={ 363[ 'start' ] ={ 364character =0 , 365line =2 , 366} , 367[ 'end' ] ={ 368character =0 , 369line =3 , 370} , 371} , 372rangeLength =12 , 373text ='' , 374} , 375-- delete "1234" from beginning of line 2 376{ 377range ={ 378[ 'start' ] ={ 379character =0 , 380line =2 , 381} , 382[ 'end' ] ={ 383character =4 , 384line =2 , 385} , 386} , 387rangeLength =4 , 388text ='' , 389} , 390-- add " asdf" to end of line 1 391{ 392range ={ 393[ 'start' ] ={ 394character =4 , 395line =1 , 396} , 397[ 'end' ] ={ 398character =4 , 399line =1 , 400} , 401} , 402rangeLength =0 , 403text =' asdf' , 404} , 405-- delete " asdf\n" from line 2 406{ 407range ={ 408[ 'start' ] ={ 409character =0 , 410line =2 , 411} , 412[ 'end' ] ={ 413character =0 , 414line =3 , 415} , 416} , 417rangeLength =6 , 418text ='' , 419} , 420-- undo entire deletion 421{ 422range ={ 423[ 'start' ] ={ 424character =4 , 425line =1 , 426} , 427[ 'end' ] ={ 428character =9 , 429line =1 , 430} , 431} , 432rangeLength =5 , 433text ='_fdsa\nhello world\n1234 asdf' , 434} , 435-- redo entire deletion 436{ 437range ={ 438[ 'start' ] ={ 439character =4 , 440line =1 , 441} , 442[ 'end' ] ={ 443character =9 , 444line =3 , 445} , 446} , 447rangeLength =27 , 448text =' asdf' , 449} , 450} 451local original_lines ={ 452'\\begin{document}' , 453'test_fdsa' , 454'hello world' , 455'1234 asdf' , 456'\\end{document}' , 457} 458test_edit ( original_lines , { 'jf_vejjbhhdu<C-R>' } , expected_text_changes , 'utf-16' , '\n' ) 459end ) 460end ) 461 462describe ( 'multi-operation edits' , function () 463it ( 'mult-line substitution' , function () 464local expected_text_changes ={ 465{ 466range ={ 467[ 'end' ] ={ 468character =11 , 469line =2 , 470} , 471[ 'start' ] ={ 472character =10 , 473line =2 , 474} , 475} , 476rangeLength =1 , 477text ='' , 478} , 479{ 480range ={ 481[ 'end' ] ={ 482character =10 , 483line =2 , 484} , 485start ={ 486character =10 , 487line =2 , 488} , 489} , 490rangeLength =0 , 491text ='2' , 492} , 493{ 494range ={ 495[ 'end' ] ={ 496character =11 , 497line =3 , 498} , 499[ 'start' ] ={ 500character =10 , 501line =3 , 502} , 503} , 504rangeLength =1 , 505text ='' , 506} , 507{ 508range ={ 509[ 'end' ] ={ 510character =10 , 511line =3 , 512} , 513[ 'start' ] ={ 514character =10 , 515line =3 , 516} , 517} , 518rangeLength =0 , 519text ='3' , 520} , 521{ 522range ={ 523[ 'end' ] ={ 524character =0 , 525line =3 , 526} , 527[ 'start' ] ={ 528character =12 , 529line =2 , 530} , 531} , 532rangeLength =1 , 533text ='\n' , 534} , 535} 536local original_lines ={ 537'\\begin{document}' , 538'\\section*{1}' , 539'\\section*{1}' , 540'\\section*{1}' , 541'\\end{document}' , 542} 543test_edit ( original_lines , { '3gg$h<C-V>jg<C-A>' } , expected_text_changes , 'utf-16' , '\n' ) 544end ) 545it ( 'join and undo' , function () 546local expected_text_changes ={ 547{ 548range ={ 549[ 'start' ] ={ 550character =11 , 551line =0 , 552} , 553[ 'end' ] ={ 554character =11 , 555line =0 , 556} , 557} , 558rangeLength =0 , 559text =' test3' , 560} , 561{ 562range ={ 563[ 'start' ] ={ 564character =0 , 565line =1 , 566} , 567[ 'end' ] ={ 568character =0 , 569line =2 , 570} , 571} , 572rangeLength =6 , 573text ='' , 574} , 575{ 576range ={ 577[ 'start' ] ={ 578character =11 , 579line =0 , 580} , 581[ 'end' ] ={ 582character =17 , 583line =0 , 584} , 585} , 586rangeLength =6 , 587text ='\ntest3' , 588} , 589} 590test_edit ( { 'test1 test2' , 'test3' } , { 'J' , 'u' } , expected_text_changes , 'utf-16' , '\n' ) 591end ) 592end ) 593 594describe ( 'multi-byte edits' , function () 595it ( 'deleting a multibyte character' , function () 596local expected_text_changes ={ 597{ 598range ={ 599[ 'start' ] ={ 600character =0 , 601line =0 , 602} , 603[ 'end' ] ={ 604character =2 , 605line =0 , 606} , 607} , 608rangeLength =2 , 609text ='' , 610} , 611} 612test_edit ( { '🔥' } , { 'x' } , expected_text_changes , 'utf-16' , '\n' ) 613end ) 614it ( 'replacing a multibyte character with matching prefix' , function () 615local expected_text_changes ={ 616{ 617range ={ 618[ 'start' ] ={ 619character =0 , 620line =1 , 621} , 622[ 'end' ] ={ 623character =1 , 624line =1 , 625} , 626} , 627rangeLength =1 , 628text ='⟩' , 629} , 630} 631-- ⟨ is e29fa8, ⟩ is e29fa9 632local original_lines ={ 633'\\begin{document}' , 634'⟨' , 635'\\end{document}' , 636} 637test_edit ( original_lines , { 'jr⟩' } , expected_text_changes , 'utf-16' , '\n' ) 638end ) 639it ( 'replacing a multibyte character with matching suffix' , function () 640local expected_text_changes ={ 641{ 642range ={ 643[ 'start' ] ={ 644character =0 , 645line =1 , 646} , 647[ 'end' ] ={ 648character =1 , 649line =1 , 650} , 651} , 652rangeLength =1 , 653text ='ḟ' , 654} , 655} 656-- ฟ is e0b89f, ḟ is e1b89f 657local original_lines ={ 658'\\begin{document}' , 659'ฟ' , 660'\\end{document}' , 661} 662test_edit ( original_lines , { 'jrḟ' } , expected_text_changes , 'utf-16' , '\n' ) 663end ) 664it ( 'inserting before a multibyte character' , function () 665local expected_text_changes ={ 666{ 667range ={ 668[ 'start' ] ={ 669character =0 , 670line =1 , 671} , 672[ 'end' ] ={ 673character =0 , 674line =1 , 675} , 676} , 677rangeLength =0 , 678text =' ' , 679} , 680} 681local original_lines ={ 682'\\begin{document}' , 683'→' , 684'\\end{document}' , 685} 686test_edit ( original_lines , { 'ji ' } , expected_text_changes , 'utf-16' , '\n' ) 687end ) 688it ( 'deleting a multibyte character from a long line' , function () 689local expected_text_changes ={ 690{ 691range ={ 692[ 'start' ] ={ 693character =85 , 694line =1 , 695} , 696[ 'end' ] ={ 697character =86 , 698line =1 , 699} , 700} , 701rangeLength =1 , 702text ='' , 703} , 704} 705local original_lines ={ 706'\\begin{document}' , 707'→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→' , 708'\\end{document}' , 709} 710test_edit ( original_lines , { 'jx' } , expected_text_changes , 'utf-16' , '\n' ) 711end ) 712it ( 'deleting multiple lines containing multibyte characters' , function () 713local expected_text_changes ={ 714{ 715range ={ 716[ 'start' ] ={ 717character =0 , 718line =1 , 719} , 720[ 'end' ] ={ 721character =0 , 722line =3 , 723} , 724} , 725--utf 16 len of 🔥 is 2 726rangeLength =8 , 727text ='' , 728} , 729} 730test_edit ( 731{ 'a🔥' , 'b🔥' , 'c🔥' , 'd🔥' } , 732{ 'j2dd' } , 733expected_text_changes , 734'utf-16' , 735'\n' 736) 737end ) 738end ) 739end ) 740 741-- TODO(mjlbach): Add additional tests 742-- deleting single lone line 743-- 2 lines -> 2 line delete -> undo -> redo 744-- describe('future tests', function() 745-- -- This test is currently wrong, ask bjorn why dd on an empty line triggers on_lines 746-- it('deleting an empty line', function() 747-- local expected_text_changes = {{ }} 748-- test_edit({""}, {"ggdd"}, expected_text_changes, 'utf-16', '\n') 749-- end) 750-- end)