robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 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 insert =n . insert 11local feed =n . feed 12local command =n . command 13local assert_alive =n . assert_alive 14 15-- Implements a :Replace command that works like :substitute and has multibuffer support. 16local setup_replace_cmd =[[ 17local function show_replace_preview(use_preview_win, preview_ns, preview_buf, matches) 18-- Find the width taken by the largest line number, used for padding the line numbers 19local highest_lnum = math.max(matches[#matches][1], 1) 20local highest_lnum_width = math.floor(math.log10(highest_lnum)) 21local preview_buf_line = 0 22local multibuffer = #matches > 1 23 24for _, match in ipairs(matches) do 25local buf = match[1] 26local buf_matches = match[2] 27 28if multibuffer and #buf_matches > 0 and use_preview_win then 29local bufname = vim.api.nvim_buf_get_name(buf) 30 31if bufname == "" then 32bufname = string.format("Buffer #%d", buf) 33end 34 35vim.api.nvim_buf_set_lines( 36preview_buf, 37preview_buf_line, 38preview_buf_line, 390, 40{ bufname .. ':' } 41) 42 43preview_buf_line = preview_buf_line + 1 44end 45 46for _, buf_match in ipairs(buf_matches) do 47local lnum = buf_match[1] 48local line_matches = buf_match[2] 49local prefix 50 51if use_preview_win then 52prefix = string.format( 53'|%s%d| ', 54string.rep(' ', highest_lnum_width - math.floor(math.log10(lnum))), 55lnum 56) 57 58vim.api.nvim_buf_set_lines( 59preview_buf, 60preview_buf_line, 61preview_buf_line, 620, 63{ prefix .. vim.api.nvim_buf_get_lines(buf, lnum - 1, lnum, false)[1] } 64) 65end 66 67for _, line_match in ipairs(line_matches) do 68vim.api.nvim_buf_add_highlight( 69buf, 70preview_ns, 71'Substitute', 72lnum - 1, 73line_match[1], 74line_match[2] 75) 76 77if use_preview_win then 78vim.api.nvim_buf_add_highlight( 79preview_buf, 80preview_ns, 81'Substitute', 82preview_buf_line, 83#prefix + line_match[1], 84#prefix + line_match[2] 85) 86end 87end 88 89preview_buf_line = preview_buf_line + 1 90end 91end 92 93if use_preview_win then 94return 2 95else 96return 1 97end 98end 99 100local function do_replace(opts, preview, preview_ns, preview_buf) 101local pat1 = opts.fargs[1] 102 103if not pat1 then return end 104 105local pat2 = opts.fargs[2] or '' 106local line1 = opts.line1 107local line2 = opts.line2 108local matches = {} 109 110-- Get list of valid and listed buffers 111local buffers = vim.tbl_filter( 112function(buf) 113if not (vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buflisted and buf ~= preview_buf) 114then 115return false 116end 117 118-- Check if there's at least one window using the buffer 119for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do 120if vim.api.nvim_win_get_buf(win) == buf then 121return true 122end 123end 124 125return false 126end, 127vim.api.nvim_list_bufs() 128) 129 130for _, buf in ipairs(buffers) do 131local lines = vim.api.nvim_buf_get_lines(buf, line1 - 1, line2, false) 132local buf_matches = {} 133 134for i, line in ipairs(lines) do 135local startidx, endidx = 0, 0 136local line_matches = {} 137local num = 1 138 139while startidx ~= -1 do 140local match = vim.fn.matchstrpos(line, pat1, 0, num) 141startidx, endidx = match[2], match[3] 142 143if startidx ~= -1 then 144line_matches[#line_matches+1] = { startidx, endidx } 145end 146 147num = num + 1 148end 149 150if #line_matches > 0 then 151buf_matches[#buf_matches+1] = { line1 + i - 1, line_matches } 152end 153end 154 155local new_lines = {} 156 157for _, buf_match in ipairs(buf_matches) do 158local lnum = buf_match[1] 159local line_matches = buf_match[2] 160local line = lines[lnum - line1 + 1] 161local pat_width_differences = {} 162 163-- If previewing, only replace the text in current buffer if pat2 isn't empty 164-- Otherwise, always replace the text 165if pat2 ~= '' or not preview then 166if preview then 167for _, line_match in ipairs(line_matches) do 168local startidx, endidx = unpack(line_match) 169local pat_match = line:sub(startidx + 1, endidx) 170 171pat_width_differences[#pat_width_differences+1] = 172#vim.fn.substitute(pat_match, pat1, pat2, 'g') - #pat_match 173end 174end 175 176new_lines[lnum] = vim.fn.substitute(line, pat1, pat2, 'g') 177end 178 179-- Highlight the matches if previewing 180if preview then 181local idx_offset = 0 182for i, line_match in ipairs(line_matches) do 183local startidx, endidx = unpack(line_match) 184-- Starting index of replacement text 185local repl_startidx = startidx + idx_offset 186-- Ending index of the replacement text (if pat2 isn't empty) 187local repl_endidx 188 189if pat2 ~= '' then 190repl_endidx = endidx + idx_offset + pat_width_differences[i] 191else 192repl_endidx = endidx + idx_offset 193end 194 195if pat2 ~= '' then 196idx_offset = idx_offset + pat_width_differences[i] 197end 198 199line_matches[i] = { repl_startidx, repl_endidx } 200end 201end 202end 203 204for lnum, line in pairs(new_lines) do 205vim.api.nvim_buf_set_lines(buf, lnum - 1, lnum, false, { line }) 206end 207 208matches[#matches+1] = { buf, buf_matches } 209end 210 211if preview then 212local lnum = vim.api.nvim_win_get_cursor(0)[1] 213-- Use preview window only if preview buffer is provided and range isn't just the current line 214local use_preview_win = (preview_buf ~= nil) and (line1 ~= lnum or line2 ~= lnum) 215return show_replace_preview(use_preview_win, preview_ns, preview_buf, matches) 216end 217end 218 219local function replace(opts) 220do_replace(opts, false) 221end 222 223local function replace_preview(opts, preview_ns, preview_buf) 224return do_replace(opts, true, preview_ns, preview_buf) 225end 226 227-- ":<range>Replace <pat1> <pat2>" 228-- Replaces all occurrences of <pat1> in <range> with <pat2> 229vim.api.nvim_create_user_command( 230'Replace', 231replace, 232{ nargs = '*', range = '%', addr = 'lines', 233preview = replace_preview } 234) 235]] 236 237describe ( "'inccommand' for user commands" , function () 238local screen 239 240before_each ( function () 241clear () 242screen =Screen . new ( 40 , 17 ) 243exec_lua ( setup_replace_cmd ) 244command ( 'set cmdwinheight=5' ) 245insert [[ 246text on line 1 247more text on line 2 248oh no, even more text 249will the text ever stop 250oh well 251did the text stop 252why won't it stop 253make the text stop 254]] 255end ) 256 257it ( "can preview 'nomodifiable' buffer" , function () 258exec_lua ( [[ 259vim.api.nvim_create_user_command("PreviewTest", function() end, { 260preview = function(ev) 261vim.bo.modifiable = true 262vim.api.nvim_buf_set_lines(0, 0, -1, false, {"cats"}) 263return 2 264end, 265}) 266]] ) 267command ( 'set inccommand=split' ) 268 269command ( 'set nomodifiable' ) 270eq ( false , api . nvim_get_option_value ( 'modifiable' , { buf =0 } )) 271 272feed ( ':PreviewTest' ) 273 274screen : expect ( [[ 275cats | 276{1:~ }|*8 277{3:[No Name] [+] }| 278| 279{1:~ }|*4 280{2:[Preview] }| 281:PreviewTest^ | 282]] ) 283feed ( '<Esc>' ) 284screen : expect ( [[ 285text on line 1 | 286more text on line 2 | 287oh no, even more text | 288will the text ever stop | 289oh well | 290did the text stop | 291why won't it stop | 292make the text stop | 293^ | 294{1:~ }|*7 295| 296]] ) 297 298eq ( false , api . nvim_get_option_value ( 'modifiable' , { buf =0 } )) 299end ) 300 301it ( 'works with inccommand=nosplit' , function () 302command ( 'set inccommand=nosplit' ) 303feed ( ':Replace text cats' ) 304screen : expect ( [[ 305{10:cats} on line 1 | 306more {10:cats} on line 2 | 307oh no, even more {10:cats} | 308will the {10:cats} ever stop | 309oh well | 310did the {10:cats} stop | 311why won't it stop | 312make the {10:cats} stop | 313| 314{1:~ }|*7 315:Replace text cats^ | 316]] ) 317end ) 318 319it ( 'works with inccommand=split' , function () 320command ( 'set inccommand=split' ) 321feed ( ':Replace text cats' ) 322screen : expect ( [[ 323{10:cats} on line 1 | 324more {10:cats} on line 2 | 325oh no, even more {10:cats} | 326will the {10:cats} ever stop | 327oh well | 328did the {10:cats} stop | 329why won't it stop | 330make the {10:cats} stop | 331| 332{3:[No Name] [+] }| 333|1| {10:cats} on line 1 | 334|2| more {10:cats} on line 2 | 335|3| oh no, even more {10:cats} | 336|4| will the {10:cats} ever stop | 337|6| did the {10:cats} stop | 338{2:[Preview] }| 339:Replace text cats^ | 340]] ) 341end ) 342 343it ( 'properly closes preview when inccommand=split' , function () 344command ( 'set inccommand=split' ) 345feed ( ':Replace text cats<Esc>' ) 346screen : expect ( [[ 347text on line 1 | 348more text on line 2 | 349oh no, even more text | 350will the text ever stop | 351oh well | 352did the text stop | 353why won't it stop | 354make the text stop | 355^ | 356{1:~ }|*7 357| 358]] ) 359end ) 360 361it ( 'properly executes command when inccommand=split' , function () 362command ( 'set inccommand=split' ) 363feed ( ':Replace text cats<CR>' ) 364screen : expect ( [[ 365cats on line 1 | 366more cats on line 2 | 367oh no, even more cats | 368will the cats ever stop | 369oh well | 370did the cats stop | 371why won't it stop | 372make the cats stop | 373^ | 374{1:~ }|*7 375:Replace text cats | 376]] ) 377end ) 378 379it ( 'shows preview window only when range is not current line' , function () 380command ( 'set inccommand=split' ) 381feed ( 'gg:.Replace text cats' ) 382screen : expect ( [[ 383{10:cats} on line 1 | 384more text on line 2 | 385oh no, even more text | 386will the text ever stop | 387oh well | 388did the text stop | 389why won't it stop | 390make the text stop | 391| 392{1:~ }|*7 393:.Replace text cats^ | 394]] ) 395end ) 396 397it ( 'no crash on ambiguous command #18825' , function () 398command ( 'set inccommand=split' ) 399command ( 'command Reply echo 1' ) 400feed ( ':R' ) 401assert_alive () 402feed ( 'e' ) 403assert_alive () 404end ) 405 406it ( 'no crash if preview callback changes inccommand option' , function () 407command ( 'set inccommand=nosplit' ) 408exec_lua ( [[ 409vim.api.nvim_create_user_command('Replace', function() end, { 410nargs = '*', 411preview = function() 412vim.api.nvim_set_option_value('inccommand', 'split', {}) 413return 2 414end, 415}) 416]] ) 417feed ( ':R' ) 418assert_alive () 419feed ( 'e' ) 420assert_alive () 421end ) 422 423it ( 'no crash when adding highlight after :substitute #21495' , function () 424command ( 'set inccommand=nosplit' ) 425exec_lua ( [[ 426vim.api.nvim_create_user_command("Crash", function() end, { 427preview = function(_, preview_ns, _) 428vim.cmd("%s/text/cats/g") 429vim.api.nvim_buf_add_highlight(0, preview_ns, "Search", 0, 0, -1) 430return 1 431end, 432}) 433]] ) 434feed ( ':C' ) 435screen : expect ( [[ 436{10:cats on line 1} | 437more cats on line 2 | 438oh no, even more cats | 439will the cats ever stop | 440oh well | 441did the cats stop | 442why won't it stop | 443make the cats stop | 444| 445{1:~ }|*7 446:C^ | 447]] ) 448assert_alive () 449end ) 450 451it ( 'no crash if preview callback executes undo #20036' , function () 452command ( 'set inccommand=nosplit' ) 453exec_lua ( [[ 454vim.api.nvim_create_user_command('Foo', function() end, { 455nargs = '?', 456preview = function(_, _, _) 457vim.cmd.undo() 458end, 459}) 460]] ) 461 462-- Clear undo history 463command ( 'set undolevels=-1' ) 464feed ( 'ggyyp' ) 465command ( 'set undolevels=1000' ) 466 467feed ( 'yypp:Fo' ) 468assert_alive () 469feed ( '<Esc>:Fo' ) 470assert_alive () 471end ) 472 473local function test_preview_break_undo () 474command ( 'set inccommand=nosplit' ) 475exec_lua ( [[ 476vim.api.nvim_create_user_command('Test', function() end, { 477nargs = 1, 478preview = function(opts, _, _) 479vim.cmd('norm i' .. opts.args) 480return 1 481end 482}) 483]] ) 484feed ( ':Test a.a.a.a.' ) 485screen : expect ( [[ 486text on line 1 | 487more text on line 2 | 488oh no, even more text | 489will the text ever stop | 490oh well | 491did the text stop | 492why won't it stop | 493make the text stop | 494a.a.a.a. | 495{1:~ }|*7 496:Test a.a.a.a.^ | 497]] ) 498feed ( '<C-V><Esc>u' ) 499screen : expect ( [[ 500text on line 1 | 501more text on line 2 | 502oh no, even more text | 503will the text ever stop | 504oh well | 505did the text stop | 506why won't it stop | 507make the text stop | 508a.a.a. | 509{1:~ }|*7 510:Test a.a.a.a.{18:^[}u^ | 511]] ) 512feed ( '<Esc>' ) 513screen : expect ( [[ 514text on line 1 | 515more text on line 2 | 516oh no, even more text | 517will the text ever stop | 518oh well | 519did the text stop | 520why won't it stop | 521make the text stop | 522^ | 523{1:~ }|*7 524| 525]] ) 526end 527 528describe ( 'breaking undo chain in Insert mode works properly' , function () 529it ( 'when using i_CTRL-G_u #20248' , function () 530command ( 'inoremap . .<C-G>u' ) 531test_preview_break_undo () 532end ) 533 534it ( 'when setting &l:undolevels to itself #24575' , function () 535command ( 'inoremap . .<Cmd>let &l:undolevels = &l:undolevels<CR>' ) 536test_preview_break_undo () 537end ) 538end ) 539 540it ( 'disables preview if preview buffer cannot be created #27086' , function () 541command ( 'set inccommand=split' ) 542api . nvim_buf_set_name ( 0 , '[Preview]' ) 543exec_lua ( [[ 544vim.api.nvim_create_user_command('Test', function() end, { 545nargs = '*', 546preview = function(_, _, _) 547return 2 548end 549}) 550]] ) 551eq ( 'split' , api . nvim_get_option_value ( 'inccommand' , {} )) 552feed ( ':Test' ) 553eq ( 'nosplit' , api . nvim_get_option_value ( 'inccommand' , {} )) 554end ) 555 556it ( 'does not flush intermediate cursor position at end of message grid' , function () 557exec_lua ( [[ 558vim.api.nvim_create_user_command('Test', function() end, { 559nargs = '*', 560preview = function(_, _, _) 561vim.api.nvim_buf_set_text(0, 0, 0, 1, -1, { "Preview" }) 562vim.cmd.sleep("1m") 563return 1 564end 565}) 566]] ) 567local cursor_goto =screen . _handle_grid_cursor_goto 568screen . _handle_grid_cursor_goto =function ( ... ) 569cursor_goto ( ... ) 570assert ( screen . _cursor . col < 12 ) 571end 572feed ( ':Test baz<Left><Left>arb' ) 573screen : expect ( [[ 574Preview | 575oh no, even more text | 576will the text ever stop | 577oh well | 578did the text stop | 579why won't it stop | 580make the text stop | 581| 582{1:~ }|*8 583:Test barb^az | 584]] ) 585end ) 586 587it ( 'works when CmdlineChanged calls wildtrigger() #35246' , function () 588api . nvim_buf_set_text ( 0 , 0 , 0 , 1 , - 1 , { '' } ) 589exec_lua ( [[ 590vim.api.nvim_create_user_command("Repro", function() end, { 591nargs = '+', 592preview = function(opts, ns, buf) 593vim.api.nvim_buf_set_lines(0, 0, -1, true, { opts.args }) 594return 2 595end 596}) 597]] ) 598command ( [[autocmd CmdlineChanged [:/\?] call wildtrigger()]] ) 599command ( 'set wildmode=noselect:lastused,full wildoptions=pum' ) 600feed ( ':Repro ' ) 601screen : expect ( [[ 602| 603{1:~ }|*15 604:Repro ^ | 605]] ) 606feed ( 'a' ) 607screen : expect ( [[ 608a | 609{1:~ }|*15 610:Repro a^ | 611]] ) 612feed ( 'bc' ) 613screen : expect ( [[ 614abc | 615{1:~ }|*15 616:Repro abc^ | 617]] ) 618end ) 619 620it ( 'no crash with % + preview + file completion #28851' , function () 621exec_lua ( [[ 622local function callback() end 623local function preview() 624return 0 625end 626 627vim.api.nvim_create_user_command('TestCommand', callback, { 628nargs = '?', 629complete = 'file', 630preview = preview, 631}) 632 633vim.cmd.edit('Xtestscript') 634]] ) 635feed ( ':TestCommand %' ) 636assert_alive () 637end ) 638end ) 639 640describe ( "'inccommand' with multiple buffers" , function () 641local screen 642 643before_each ( function () 644clear () 645screen =Screen . new ( 40 , 17 ) 646exec_lua ( setup_replace_cmd ) 647command ( 'set cmdwinheight=10' ) 648insert [[ 649foo bar baz 650bar baz foo 651baz foo bar 652]] 653command ( 'vsplit | enew' ) 654insert [[ 655bar baz foo 656baz foo bar 657foo bar baz 658]] 659end ) 660 661it ( 'works' , function () 662command ( 'set inccommand=nosplit' ) 663feed ( ':Replace foo bar' ) 664screen : expect ( [[ 665bar baz {10:bar} │{10:bar} bar baz | 666baz {10:bar} bar │bar baz {10:bar} | 667{10:bar} bar baz │baz {10:bar} bar | 668│ | 669{1:~ }│{1:~ }|*11 670{3:[No Name] [+] }{2:[No Name] [+] }| 671:Replace foo bar^ | 672]] ) 673feed ( '<CR>' ) 674screen : expect ( [[ 675bar baz bar │bar bar baz | 676baz bar bar │bar baz bar | 677bar bar baz │baz bar bar | 678^ │ | 679{1:~ }│{1:~ }|*11 680{3:[No Name] [+] }{2:[No Name] [+] }| 681:Replace foo bar | 682]] ) 683end ) 684 685it ( 'works with inccommand=split' , function () 686command ( 'set inccommand=split' ) 687feed ( ':Replace foo bar' ) 688screen : expect ( [[ 689bar baz {10:bar} │{10:bar} bar baz | 690baz {10:bar} bar │bar baz {10:bar} | 691{10:bar} bar baz │baz {10:bar} bar | 692│ | 693{3:[No Name] [+] }{2:[No Name] [+] }| 694Buffer #1: | 695|1| {10:bar} bar baz | 696|2| bar baz {10:bar} | 697|3| baz {10:bar} bar | 698Buffer #2: | 699|1| bar baz {10:bar} | 700|2| baz {10:bar} bar | 701|3| {10:bar} bar baz | 702| 703{1:~ }| 704{2:[Preview] }| 705:Replace foo bar^ | 706]] ) 707feed ( '<CR>' ) 708screen : expect ( [[ 709bar baz bar │bar bar baz | 710baz bar bar │bar baz bar | 711bar bar baz │baz bar bar | 712^ │ | 713{1:~ }│{1:~ }|*11 714{3:[No Name] [+] }{2:[No Name] [+] }| 715:Replace foo bar | 716]] ) 717end ) 718end )