robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local api =n . api 6local clear =n . clear 7local eq =t . eq 8local exec_capture =n . exec_capture 9local exec_lua =n . exec_lua 10local feed =n . feed 11 12-- Reference text 13-- aa 14-- aa 15-- aa 16-- 17-- aa 18-- aa 19-- aa 20local example_lines ={ 'aa' , ' aa' , ' aa' , '' , ' aa' , ' aa' , 'aa' } 21 22local set_commentstring =function ( commentstring ) 23api . nvim_set_option_value ( 'commentstring' , commentstring , { buf =0 } ) 24end 25 26local get_lines =function ( from , to ) 27from , to =from or 0 , to or - 1 28return api . nvim_buf_get_lines ( 0 , from , to , false ) 29end 30 31local set_lines =function ( lines , from , to ) 32from , to =from or 0 , to or - 1 33api . nvim_buf_set_lines ( 0 , from , to , false , lines ) 34end 35 36local set_cursor =function ( row , col ) 37api . nvim_win_set_cursor ( 0 , { row , col } ) 38end 39 40local get_cursor =function () 41return api . nvim_win_get_cursor ( 0 ) 42end 43 44local setup_treesitter =function () 45-- NOTE: This leverages bundled Vimscript and Lua tree-sitter parsers 46api . nvim_set_option_value ( 'filetype' , 'vim' , { buf =0 } ) 47exec_lua ( 'vim.treesitter.start()' ) 48end 49 50before_each ( function () 51-- avoid options, but we still need TS parsers 52clear ( { args_rm ={ '--cmd' } , args ={ '--clean' , '--cmd' , n . runtime_set } } ) 53end ) 54 55describe ( 'commenting' , function () 56before_each ( function () 57set_lines ( example_lines ) 58set_commentstring ( '# %s' ) 59end ) 60 61describe ( 'toggle_lines()' , function () 62local toggle_lines =function ( ... ) 63exec_lua ( 'require("vim._comment").toggle_lines(...)' , ... ) 64end 65 66it ( 'works' , function () 67toggle_lines ( 3 , 5 ) 68eq ( { ' # aa' , ' #' , ' # aa' } , get_lines ( 2 , 5 )) 69 70toggle_lines ( 3 , 5 ) 71eq ( { ' aa' , '' , ' aa' } , get_lines ( 2 , 5 )) 72end ) 73 74it ( "works with different 'commentstring' options" , function () 75local validate =function ( lines_before , lines_after , lines_again ) 76set_lines ( lines_before ) 77toggle_lines ( 1 , # lines_before ) 78eq ( lines_after , get_lines ()) 79toggle_lines ( 1 , # lines_before ) 80eq ( lines_again or lines_before , get_lines ()) 81end 82 83-- Single whitespace inside comment parts (main case) 84set_commentstring ( '# %s #' ) 85-- - General case 86validate ( 87{ 'aa' , ' aa' , 'aa ' , ' aa ' } , 88{ '# aa #' , '# aa #' , '# aa #' , '# aa #' } 89) 90-- - Tabs 91validate ( 92{ 'aa' , '\taa' , 'aa\t' , '\taa\t' } , 93{ '# aa #' , '# \taa #' , '# aa\t #' , '# \taa\t #' } 94) 95-- - With indent 96validate ( { ' aa' , ' aa' } , { ' # aa #' , ' # aa #' } ) 97-- - With blank/empty lines 98validate ( 99{ ' aa' , '' , ' ' , '\t' } , 100{ ' # aa #' , ' ##' , ' ##' , ' ##' } , 101{ ' aa' , '' , '' , '' } 102) 103 104set_commentstring ( '# %s' ) 105validate ( { 'aa' , ' aa' , 'aa ' , ' aa ' } , { '# aa' , '# aa' , '# aa ' , '# aa ' } ) 106validate ( { 'aa' , '\taa' , 'aa\t' , '\taa\t' } , { '# aa' , '# \taa' , '# aa\t' , '# \taa\t' } ) 107validate ( { ' aa' , ' aa' } , { ' # aa' , ' # aa' } ) 108validate ( 109{ ' aa' , '' , ' ' , '\t' } , 110{ ' # aa' , ' #' , ' #' , ' #' } , 111{ ' aa' , '' , '' , '' } 112) 113 114set_commentstring ( '%s #' ) 115validate ( { 'aa' , ' aa' , 'aa ' , ' aa ' } , { 'aa #' , ' aa #' , 'aa #' , ' aa #' } ) 116validate ( { 'aa' , '\taa' , 'aa\t' , '\taa\t' } , { 'aa #' , '\taa #' , 'aa\t #' , '\taa\t #' } ) 117validate ( { ' aa' , ' aa' } , { ' aa #' , ' aa #' } ) 118validate ( 119{ ' aa' , '' , ' ' , '\t' } , 120{ ' aa #' , ' #' , ' #' , ' #' } , 121{ ' aa' , '' , '' , '' } 122) 123 124-- No whitespace in parts 125set_commentstring ( '#%s#' ) 126validate ( { 'aa' , ' aa' , 'aa ' , ' aa ' } , { '#aa#' , '# aa#' , '#aa #' , '# aa #' } ) 127validate ( { 'aa' , '\taa' , 'aa\t' , '\taa\t' } , { '#aa#' , '#\taa#' , '#aa\t#' , '#\taa\t#' } ) 128validate ( { ' aa' , ' aa' } , { ' #aa#' , ' # aa#' } ) 129validate ( 130{ ' aa' , '' , ' ' , '\t' } , 131{ ' #aa#' , ' ##' , ' ##' , ' ##' } , 132{ ' aa' , '' , '' , '' } 133) 134 135set_commentstring ( '#%s' ) 136validate ( { 'aa' , ' aa' , 'aa ' , ' aa ' } , { '#aa' , '# aa' , '#aa ' , '# aa ' } ) 137validate ( { 'aa' , '\taa' , 'aa\t' , '\taa\t' } , { '#aa' , '#\taa' , '#aa\t' , '#\taa\t' } ) 138validate ( { ' aa' , ' aa' } , { ' #aa' , ' # aa' } ) 139validate ( { ' aa' , '' , ' ' , '\t' } , { ' #aa' , ' #' , ' #' , ' #' } , { ' aa' , '' , '' , '' } ) 140 141set_commentstring ( '%s#' ) 142validate ( { 'aa' , ' aa' , 'aa ' , ' aa ' } , { 'aa#' , ' aa#' , 'aa #' , ' aa #' } ) 143validate ( { 'aa' , '\taa' , 'aa\t' , '\taa\t' } , { 'aa#' , '\taa#' , 'aa\t#' , '\taa\t#' } ) 144validate ( { ' aa' , ' aa' } , { ' aa#' , ' aa#' } ) 145validate ( { ' aa' , '' , ' ' , '\t' } , { ' aa#' , ' #' , ' #' , ' #' } , { ' aa' , '' , '' , '' } ) 146 147-- Extra whitespace inside comment parts 148set_commentstring ( '# %s #' ) 149validate ( 150{ 'aa' , ' aa' , 'aa ' , ' aa ' } , 151{ '# aa #' , '# aa #' , '# aa #' , '# aa #' } 152) 153validate ( 154{ 'aa' , '\taa' , 'aa\t' , '\taa\t' } , 155{ '# aa #' , '# \taa #' , '# aa\t #' , '# \taa\t #' } 156) 157validate ( { ' aa' , ' aa' } , { ' # aa #' , ' # aa #' } ) 158validate ( 159{ ' aa' , '' , ' ' , '\t' } , 160{ ' # aa #' , ' ##' , ' ##' , ' ##' } , 161{ ' aa' , '' , '' , '' } 162) 163 164set_commentstring ( '# %s' ) 165validate ( { 'aa' , ' aa' , 'aa ' , ' aa ' } , { '# aa' , '# aa' , '# aa ' , '# aa ' } ) 166validate ( { 'aa' , '\taa' , 'aa\t' , '\taa\t' } , { '# aa' , '# \taa' , '# aa\t' , '# \taa\t' } ) 167validate ( { ' aa' , ' aa' } , { ' # aa' , ' # aa' } ) 168validate ( 169{ ' aa' , '' , ' ' , '\t' } , 170{ ' # aa' , ' #' , ' #' , ' #' } , 171{ ' aa' , '' , '' , '' } 172) 173 174set_commentstring ( '%s #' ) 175validate ( { 'aa' , ' aa' , 'aa ' , ' aa ' } , { 'aa #' , ' aa #' , 'aa #' , ' aa #' } ) 176validate ( { 'aa' , '\taa' , 'aa\t' , '\taa\t' } , { 'aa #' , '\taa #' , 'aa\t #' , '\taa\t #' } ) 177validate ( { ' aa' , ' aa' } , { ' aa #' , ' aa #' } ) 178validate ( 179{ ' aa' , '' , ' ' , '\t' } , 180{ ' aa #' , ' #' , ' #' , ' #' } , 181{ ' aa' , '' , '' , '' } 182) 183 184-- Whitespace outside of comment parts 185set_commentstring ( ' # %s # ' ) 186validate ( 187{ 'aa' , ' aa' , 'aa ' , ' aa ' } , 188{ ' # aa # ' , ' # aa # ' , ' # aa # ' , ' # aa # ' } 189) 190validate ( 191{ 'aa' , '\taa' , 'aa\t' , '\taa\t' } , 192{ ' # aa # ' , ' # \taa # ' , ' # aa\t # ' , ' # \taa\t # ' } 193) 194validate ( { ' aa' , ' aa' } , { ' # aa # ' , ' # aa # ' } ) 195validate ( 196{ ' aa' , '' , ' ' , '\t' } , 197{ ' # aa # ' , ' ##' , ' ##' , ' ##' } , 198{ ' aa' , '' , '' , '' } 199) 200 201set_commentstring ( ' # %s ' ) 202validate ( 203{ 'aa' , ' aa' , 'aa ' , ' aa ' } , 204{ ' # aa ' , ' # aa ' , ' # aa ' , ' # aa ' } 205) 206validate ( 207{ 'aa' , '\taa' , 'aa\t' , '\taa\t' } , 208{ ' # aa ' , ' # \taa ' , ' # aa\t ' , ' # \taa\t ' } 209) 210validate ( { ' aa' , ' aa' } , { ' # aa ' , ' # aa ' } ) 211validate ( 212{ ' aa' , '' , ' ' , '\t' } , 213{ ' # aa ' , ' #' , ' #' , ' #' } , 214{ ' aa' , '' , '' , '' } 215) 216 217set_commentstring ( ' %s # ' ) 218validate ( 219{ 'aa' , ' aa' , 'aa ' , ' aa ' } , 220{ ' aa # ' , ' aa # ' , ' aa # ' , ' aa # ' } 221) 222validate ( 223{ 'aa' , '\taa' , 'aa\t' , '\taa\t' } , 224{ ' aa # ' , ' \taa # ' , ' aa\t # ' , ' \taa\t # ' } 225) 226validate ( { ' aa' , ' aa' } , { ' aa # ' , ' aa # ' } ) 227validate ( 228{ ' aa' , '' , ' ' , '\t' } , 229{ ' aa # ' , ' #' , ' #' , ' #' } , 230{ ' aa' , '' , '' , '' } 231) 232 233-- LaTeX 234set_commentstring ( '% %s' ) 235validate ( { 'aa' , ' aa' , 'aa ' , ' aa ' } , { '% aa' , '% aa' , '% aa ' , '% aa ' } ) 236validate ( { 'aa' , '\taa' , 'aa\t' , '\taa\t' } , { '% aa' , '% \taa' , '% aa\t' , '% \taa\t' } ) 237validate ( { ' aa' , ' aa' } , { ' % aa' , ' % aa' } ) 238validate ( 239{ ' aa' , '' , ' ' , '\t' } , 240{ ' % aa' , ' %' , ' %' , ' %' } , 241{ ' aa' , '' , '' , '' } 242) 243end ) 244 245it ( 'respects tree-sitter injections' , function () 246setup_treesitter () 247 248local lines ={ 249'set background=dark' , 250'lua << EOF' , 251'print(1)' , 252'vim.api.nvim_exec2([[' , 253' set background=light' , 254']])' , 255'EOF' , 256} 257 258-- Single line comments 259local validate =function ( line , ref_output ) 260set_lines ( lines ) 261toggle_lines ( line , line ) 262eq ( ref_output , get_lines ( line - 1 , line )[ 1 ]) 263end 264 265validate ( 1 , '"set background=dark' ) 266validate ( 2 , '"lua << EOF' ) 267validate ( 3 , '-- print(1)' ) 268validate ( 4 , '-- vim.api.nvim_exec2([[' ) 269validate ( 5 , ' "set background=light' ) 270validate ( 6 , '-- ]])' ) 271validate ( 7 , '"EOF' ) 272 273-- Multiline comments should be computed based on first line 'commentstring' 274set_lines ( lines ) 275toggle_lines ( 1 , 3 ) 276local out_lines =get_lines () 277eq ( '"set background=dark' , out_lines [ 1 ]) 278eq ( '"lua << EOF' , out_lines [ 2 ]) 279eq ( '"print(1)' , out_lines [ 3 ]) 280end ) 281 282it ( 'correctly computes indent' , function () 283toggle_lines ( 2 , 4 ) 284eq ( { ' # aa' , ' # aa' , ' #' } , get_lines ( 1 , 4 )) 285end ) 286 287it ( 'correctly detects comment/uncomment' , function () 288local validate =function ( from , to , ref_lines ) 289set_lines ( { '' , 'aa' , '# aa' , '# aa' , 'aa' , '' } ) 290toggle_lines ( from , to ) 291eq ( ref_lines , get_lines ()) 292end 293 294-- It should uncomment only if all non-blank lines are comments 295validate ( 3 , 4 , { '' , 'aa' , 'aa' , 'aa' , 'aa' , '' } ) 296validate ( 2 , 4 , { '' , '# aa' , '# # aa' , '# # aa' , 'aa' , '' } ) 297validate ( 3 , 5 , { '' , 'aa' , '# # aa' , '# # aa' , '# aa' , '' } ) 298validate ( 1 , 6 , { '#' , '# aa' , '# # aa' , '# # aa' , '# aa' , '#' } ) 299 300-- Blank lines should be ignored when making a decision 301set_lines ( { '# aa' , '' , ' ' , '\t' , '# aa' } ) 302toggle_lines ( 1 , 5 ) 303eq ( { 'aa' , '' , ' ' , '\t' , 'aa' } , get_lines ()) 304end ) 305 306it ( 'correctly matches comment parts during checking and uncommenting' , function () 307local validate =function ( from , to , ref_lines ) 308set_lines ( { '/*aa*/' , '/* aa */' , '/* aa */' } ) 309toggle_lines ( from , to ) 310eq ( ref_lines , get_lines ()) 311end 312 313-- Should first try to match 'commentstring' parts exactly with their 314-- whitespace, with fallback on trimmed parts 315set_commentstring ( '/*%s*/' ) 316validate ( 1 , 3 , { 'aa' , ' aa ' , ' aa ' } ) 317validate ( 2 , 3 , { '/*aa*/' , ' aa ' , ' aa ' } ) 318validate ( 3 , 3 , { '/*aa*/' , '/* aa */' , ' aa ' } ) 319 320set_commentstring ( '/* %s */' ) 321validate ( 1 , 3 , { 'aa' , 'aa' , ' aa ' } ) 322validate ( 2 , 3 , { '/*aa*/' , 'aa' , ' aa ' } ) 323validate ( 3 , 3 , { '/*aa*/' , '/* aa */' , ' aa ' } ) 324 325set_commentstring ( '/* %s */' ) 326validate ( 1 , 3 , { 'aa' , ' aa ' , 'aa' } ) 327validate ( 2 , 3 , { '/*aa*/' , ' aa ' , 'aa' } ) 328validate ( 3 , 3 , { '/*aa*/' , '/* aa */' , 'aa' } ) 329 330set_commentstring ( ' /*%s*/ ' ) 331validate ( 1 , 3 , { 'aa' , ' aa ' , ' aa ' } ) 332validate ( 2 , 3 , { '/*aa*/' , ' aa ' , ' aa ' } ) 333validate ( 3 , 3 , { '/*aa*/' , '/* aa */' , ' aa ' } ) 334end ) 335 336it ( 'uncomments on inconsistent indent levels' , function () 337set_lines ( { '# aa' , ' # aa' , ' # aa' } ) 338toggle_lines ( 1 , 3 ) 339eq ( { 'aa' , ' aa' , ' aa' } , get_lines ()) 340end ) 341 342it ( 'respects tabs' , function () 343api . nvim_set_option_value ( 'expandtab' , false , { buf =0 } ) 344set_lines ( { '\t\taa' , '\t\taa' } ) 345 346toggle_lines ( 1 , 2 ) 347eq ( { '\t\t# aa' , '\t\t# aa' } , get_lines ()) 348 349toggle_lines ( 1 , 2 ) 350eq ( { '\t\taa' , '\t\taa' } , get_lines ()) 351end ) 352 353it ( 'works with trailing whitespace' , function () 354-- Without right-hand side 355set_commentstring ( '# %s' ) 356set_lines ( { ' aa' , ' aa ' , ' ' } ) 357toggle_lines ( 1 , 3 ) 358eq ( { ' # aa' , ' # aa ' , ' #' } , get_lines ()) 359toggle_lines ( 1 , 3 ) 360eq ( { ' aa' , ' aa ' , '' } , get_lines ()) 361 362-- With right-hand side 363set_commentstring ( '%s #' ) 364set_lines ( { ' aa' , ' aa ' , ' ' } ) 365toggle_lines ( 1 , 3 ) 366eq ( { ' aa #' , ' aa #' , ' #' } , get_lines ()) 367toggle_lines ( 1 , 3 ) 368eq ( { ' aa' , ' aa ' , '' } , get_lines ()) 369 370-- Trailing whitespace after right side should be preserved for non-blanks 371set_commentstring ( '%s #' ) 372set_lines ( { ' aa # ' , ' aa #\t' , ' # ' , ' #\t' } ) 373toggle_lines ( 1 , 4 ) 374eq ( { ' aa ' , ' aa\t' , '' , '' } , get_lines ()) 375end ) 376end ) 377 378describe ( 'Operator' , function () 379it ( 'works in Normal mode' , function () 380set_cursor ( 2 , 2 ) 381feed ( 'gc' , 'ap' ) 382eq ( { '# aa' , '# aa' , '# aa' , '#' , ' aa' , ' aa' , 'aa' } , get_lines ()) 383-- Cursor moves to start line 384eq ( { 1 , 0 } , get_cursor ()) 385 386-- Supports `v:count` 387set_lines ( example_lines ) 388set_cursor ( 2 , 0 ) 389feed ( '2gc' , 'ap' ) 390eq ( { '# aa' , '# aa' , '# aa' , '#' , '# aa' , '# aa' , '# aa' } , get_lines ()) 391end ) 392 393it ( 'allows dot-repeat in Normal mode' , function () 394local doubly_commented ={ '# # aa' , '# # aa' , '# # aa' , '# #' , '# aa' , '# aa' , '# aa' } 395 396set_lines ( example_lines ) 397set_cursor ( 2 , 2 ) 398feed ( 'gc' , 'ap' ) 399feed ( '.' ) 400eq ( doubly_commented , get_lines ()) 401 402-- Not immediate dot-repeat 403set_lines ( example_lines ) 404set_cursor ( 2 , 2 ) 405feed ( 'gc' , 'ap' ) 406set_cursor ( 7 , 0 ) 407feed ( '.' ) 408eq ( doubly_commented , get_lines ()) 409end ) 410 411it ( 'works in Visual mode' , function () 412set_cursor ( 2 , 2 ) 413feed ( 'v' , 'ap' , 'gc' ) 414eq ( { '# aa' , '# aa' , '# aa' , '#' , ' aa' , ' aa' , 'aa' } , get_lines ()) 415 416-- Cursor moves to start line 417eq ( { 1 , 0 } , get_cursor ()) 418end ) 419 420it ( 'allows dot-repeat after initial Visual mode' , function () 421-- local example_lines = { 'aa', ' aa', ' aa', '', ' aa', ' aa', 'aa' } 422 423set_lines ( example_lines ) 424set_cursor ( 2 , 2 ) 425feed ( 'vip' , 'gc' ) 426eq ( { '# aa' , '# aa' , '# aa' , '' , ' aa' , ' aa' , 'aa' } , get_lines ()) 427eq ( { 1 , 0 } , get_cursor ()) 428 429-- Dot-repeat after first application in Visual mode applies to the paragraph at cursor (not 430-- a fixed-size region). 431feed ( '.' ) 432eq ( example_lines , get_lines ()) 433 434set_cursor ( 3 , 0 ) 435feed ( '.' ) 436eq ( { '# aa' , '# aa' , '# aa' , '' , ' aa' , ' aa' , 'aa' } , get_lines ()) 437end ) 438 439it ( "respects 'commentstring'" , function () 440set_commentstring ( '/*%s*/' ) 441set_cursor ( 2 , 2 ) 442feed ( 'gc' , 'ap' ) 443eq ( { '/*aa*/' , '/* aa*/' , '/* aa*/' , '/**/' , ' aa' , ' aa' , 'aa' } , get_lines ()) 444end ) 445 446it ( "works with empty 'commentstring'" , function () 447set_commentstring ( '' ) 448set_cursor ( 2 , 2 ) 449feed ( 'gc' , 'ap' ) 450eq ( example_lines , get_lines ()) 451eq ( [[Option 'commentstring' is empty.]] , exec_capture ( '1messages' )) 452end ) 453 454it ( 'respects tree-sitter injections' , function () 455setup_treesitter () 456 457local lines ={ 458'set background=dark' , 459'lua << EOF' , 460'print(1)' , 461'vim.api.nvim_exec2([[' , 462' set background=light' , 463']])' , 464'EOF' , 465} 466 467-- Single line comments 468local validate =function ( line , ref_output ) 469set_lines ( lines ) 470set_cursor ( line , 0 ) 471feed ( 'gc_' ) 472eq ( ref_output , get_lines ( line - 1 , line )[ 1 ]) 473end 474 475validate ( 1 , '"set background=dark' ) 476validate ( 2 , '"lua << EOF' ) 477validate ( 3 , '-- print(1)' ) 478validate ( 4 , '-- vim.api.nvim_exec2([[' ) 479validate ( 5 , ' "set background=light' ) 480validate ( 6 , '-- ]])' ) 481validate ( 7 , '"EOF' ) 482 483-- Has proper dot-repeat which recomputes 'commentstring' 484set_lines ( lines ) 485 486set_cursor ( 1 , 0 ) 487feed ( 'gc_' ) 488eq ( '"set background=dark' , get_lines ()[ 1 ]) 489 490set_cursor ( 3 , 0 ) 491feed ( '.' ) 492eq ( '-- print(1)' , get_lines ()[ 3 ]) 493 494-- Multiline comments should be computed based on cursor position 495-- which in case of Visual selection means its left part 496set_lines ( lines ) 497set_cursor ( 1 , 0 ) 498feed ( 'v2j' , 'gc' ) 499local out_lines =get_lines () 500eq ( '"set background=dark' , out_lines [ 1 ]) 501eq ( '"lua << EOF' , out_lines [ 2 ]) 502eq ( '"print(1)' , out_lines [ 3 ]) 503end ) 504 505it ( "recomputes local 'commentstring' based on cursor position" , function () 506setup_treesitter () 507local lines ={ 508' print(1)' , 509'lua << EOF' , 510' print(1)' , 511'EOF' , 512} 513set_lines ( lines ) 514 515set_cursor ( 1 , 1 ) 516feed ( 'gc_' ) 517eq ( ' "print(1)' , get_lines ()[ 1 ]) 518 519set_lines ( lines ) 520set_cursor ( 3 , 2 ) 521feed ( '.' ) 522eq ( ' -- print(1)' , get_lines ()[ 3 ]) 523end ) 524 525it ( 'preserves marks' , function () 526set_cursor ( 2 , 0 ) 527-- Set '`<' and '`>' marks 528feed ( 'VV' ) 529feed ( 'gc' , 'ip' ) 530eq ( { 2 , 0 } , api . nvim_buf_get_mark ( 0 , '<' )) 531eq ( { 2 , 2147483647 } , api . nvim_buf_get_mark ( 0 , '>' )) 532end ) 533end ) 534 535describe ( 'Current line' , function () 536it ( 'works' , function () 537set_lines ( example_lines ) 538set_cursor ( 1 , 1 ) 539feed ( 'gcc' ) 540eq ( { '# aa' , ' aa' } , get_lines ( 0 , 2 )) 541 542-- Does not comment empty line 543set_lines ( example_lines ) 544set_cursor ( 4 , 0 ) 545feed ( 'gcc' ) 546eq ( { ' aa' , '' , ' aa' } , get_lines ( 2 , 5 )) 547 548-- Supports `v:count` 549set_lines ( example_lines ) 550set_cursor ( 2 , 0 ) 551feed ( '2gcc' ) 552eq ( { 'aa' , ' # aa' , ' # aa' } , get_lines ( 0 , 3 )) 553end ) 554 555it ( 'allows dot-repeat' , function () 556set_lines ( example_lines ) 557set_cursor ( 1 , 1 ) 558feed ( 'gcc' ) 559feed ( '.' ) 560eq ( example_lines , get_lines ()) 561 562-- Not immediate dot-repeat 563set_lines ( example_lines ) 564set_cursor ( 1 , 1 ) 565feed ( 'gcc' ) 566set_cursor ( 7 , 0 ) 567feed ( '.' ) 568eq ( { '# aa' } , get_lines ( 6 , 7 )) 569end ) 570 571it ( 'respects tree-sitter injections' , function () 572setup_treesitter () 573 574local lines ={ 575'set background=dark' , 576'lua << EOF' , 577'print(1)' , 578'EOF' , 579} 580set_lines ( lines ) 581 582set_cursor ( 1 , 0 ) 583feed ( 'gcc' ) 584eq ( { '"set background=dark' , 'lua << EOF' , 'print(1)' , 'EOF' } , get_lines ()) 585 586-- Should work with dot-repeat 587set_cursor ( 3 , 0 ) 588feed ( '.' ) 589eq ( { '"set background=dark' , 'lua << EOF' , '-- print(1)' , 'EOF' } , get_lines ()) 590end ) 591 592it ( 'respects tree-sitter commentstring metadata' , function () 593exec_lua [=[ 594vim.treesitter.query.set('vim', 'highlights', [[ 595((list) @_list (#set! @_list bo.commentstring "!! %s")) 596]]) 597]=] 598setup_treesitter () 599 600local lines ={ 601'set background=dark' , 602'let mylist = [' , 603[[ \"a",]] , 604[[ \"b",]] , 605[[ \"c",]] , 606' \\]' , 607} 608set_lines ( lines ) 609 610set_cursor ( 1 , 0 ) 611feed ( 'gcc' ) 612eq ( 613{ '"set background=dark' , 'let mylist = [' , [[ \"a",]] , [[ \"b",]] , [[ \"c",]] , ' \\]' } , 614get_lines () 615) 616 617-- Should work with dot-repeat 618set_cursor ( 4 , 0 ) 619feed ( '.' ) 620eq ( { 621'"set background=dark' , 622'let mylist = [' , 623[[ \"a",]] , 624[[ !! \"b",]] , 625[[ \"c",]] , 626' \\]' , 627} , get_lines ()) 628end ) 629 630it ( 'only applies the innermost tree-sitter commentstring metadata' , function () 631exec_lua [=[ 632vim.treesitter.query.set('vim', 'highlights', [[ 633((list) @_list (#gsub! @_list "(.*)" "%1") (#set! bo.commentstring "!! %s")) 634((script_file) @_src (#set! @_src bo.commentstring "## %s")) 635]]) 636]=] 637setup_treesitter () 638 639local lines ={ 640'set background=dark' , 641'let mylist = [' , 642[[ \"a",]] , 643[[ \"b",]] , 644[[ \"c",]] , 645' \\]' , 646} 647set_lines ( lines ) 648 649set_cursor ( 1 , 0 ) 650feed ( 'gcc' ) 651eq ( { 652'## set background=dark' , 653'let mylist = [' , 654[[ \"a",]] , 655[[ \"b",]] , 656[[ \"c",]] , 657' \\]' , 658} , get_lines ()) 659 660-- Should work with dot-repeat 661set_cursor ( 4 , 0 ) 662feed ( '.' ) 663eq ( { 664'## set background=dark' , 665'let mylist = [' , 666[[ \"a",]] , 667[[ !! \"b",]] , 668[[ \"c",]] , 669' \\]' , 670} , get_lines ()) 671end ) 672 673it ( 'respects injected tree-sitter commentstring metadata' , function () 674exec_lua [=[ 675vim.treesitter.query.set('lua', 'highlights', [[ 676((string) @string (#set! @string bo.commentstring "; %s")) 677]]) 678]=] 679setup_treesitter () 680 681local lines ={ 682'set background=dark' , 683'lua << EOF' , 684'print[[' , 685'Inside string' , 686']]' , 687'EOF' , 688} 689set_lines ( lines ) 690 691set_cursor ( 1 , 0 ) 692feed ( 'gcc' ) 693eq ( { 694'"set background=dark' , 695'lua << EOF' , 696'print[[' , 697'Inside string' , 698']]' , 699'EOF' , 700} , get_lines ()) 701 702-- Should work with dot-repeat 703set_cursor ( 4 , 0 ) 704feed ( '.' ) 705eq ( { 706'"set background=dark' , 707'lua << EOF' , 708'print[[' , 709'; Inside string' , 710']]' , 711'EOF' , 712} , get_lines ()) 713 714set_cursor ( 3 , 0 ) 715feed ( '.' ) 716eq ( { 717'"set background=dark' , 718'lua << EOF' , 719'-- print[[' , 720'; Inside string' , 721']]' , 722'EOF' , 723} , get_lines ()) 724end ) 725 726it ( 'works across combined injections #30799' , function () 727exec_lua [=[ 728vim.treesitter.query.set('lua', 'injections', [[ 729((function_call 730name: (_) @_vimcmd_identifier 731arguments: (arguments 732(string 733content: _ @injection.content))) 734(#eq? @_vimcmd_identifier "vim.cmd") 735(#set! injection.language "vim") 736(#set! injection.combined)) 737]]) 738]=] 739 740api . nvim_set_option_value ( 'filetype' , 'lua' , { buf =0 } ) 741exec_lua ( 'vim.treesitter.start()' ) 742 743local lines ={ 744'vim.cmd([[" some text]])' , 745'local a = 123' , 746'vim.cmd([[" some more text]])' , 747} 748set_lines ( lines ) 749 750set_cursor ( 2 , 0 ) 751feed ( 'gcc' ) 752eq ( { 753'vim.cmd([[" some text]])' , 754'-- local a = 123' , 755'vim.cmd([[" some more text]])' , 756} , get_lines ()) 757end ) 758end ) 759 760describe ( 'Textobject' , function () 761it ( 'works' , function () 762set_lines ( { 'aa' , '# aa' , '# aa' , 'aa' } ) 763set_cursor ( 2 , 0 ) 764feed ( 'd' , 'gc' ) 765eq ( { 'aa' , 'aa' } , get_lines ()) 766end ) 767 768it ( 'allows dot-repeat' , function () 769set_lines ( { 'aa' , '# aa' , '# aa' , 'aa' , '# aa' } ) 770set_cursor ( 2 , 0 ) 771feed ( 'd' , 'gc' ) 772set_cursor ( 3 , 0 ) 773feed ( '.' ) 774eq ( { 'aa' , 'aa' } , get_lines ()) 775end ) 776 777it ( 'does nothing when not inside textobject' , function () 778-- Builtin operators 779feed ( 'd' , 'gc' ) 780eq ( example_lines , get_lines ()) 781 782-- Comment operator 783local validate_no_action =function ( line , col ) 784set_lines ( example_lines ) 785set_cursor ( line , col ) 786feed ( 'gc' , 'gc' ) 787eq ( example_lines , get_lines ()) 788end 789 790validate_no_action ( 1 , 1 ) 791validate_no_action ( 2 , 2 ) 792 793-- Doesn't work (but should) because both `[` and `]` are set to (1, 0) 794-- (instead of more reasonable (1, -1) or (0, 2147483647)). 795-- validate_no_action(1, 0) 796end ) 797 798it ( 'respects tree-sitter injections' , function () 799setup_treesitter () 800local lines ={ 801'"set background=dark' , 802'"set termguicolors' , 803'lua << EOF' , 804'-- print(1)' , 805'-- print(2)' , 806'EOF' , 807} 808set_lines ( lines ) 809 810set_cursor ( 1 , 0 ) 811feed ( 'dgc' ) 812eq ( { 'lua << EOF' , '-- print(1)' , '-- print(2)' , 'EOF' } , get_lines ()) 813 814-- Should work with dot-repeat 815set_cursor ( 2 , 0 ) 816feed ( '.' ) 817eq ( { 'lua << EOF' , 'EOF' } , get_lines ()) 818end ) 819end ) 820end )