tabnewentered_spec.lua (21422B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local clear = n.clear 5 local command = n.command 6 local dedent = t.dedent 7 local eval = n.eval 8 local eq = t.eq 9 local feed = n.feed 10 local api = n.api 11 local exec_capture = n.exec_capture 12 13 describe('TabNewEntered', function() 14 describe('au TabNewEntered', function() 15 describe('with * as <afile>', function() 16 it('matches when entering any new tab', function() 17 clear() 18 command('au! TabNewEntered * echom "tabnewentered:".tabpagenr().":".bufnr("")') 19 eq('tabnewentered:2:2', api.nvim_exec('tabnew', true)) 20 eq('tabnewentered:3:3', api.nvim_exec('tabnew test.x2', true)) 21 end) 22 end) 23 describe('with FILE as <afile>', function() 24 it('matches when opening a new tab for FILE', function() 25 clear() 26 command('au! TabNewEntered Xtest-tabnewentered echom "tabnewentered:match"') 27 eq('tabnewentered:match', api.nvim_exec('tabnew Xtest-tabnewentered', true)) 28 end) 29 end) 30 describe('with CTRL-W T', function() 31 it('works when opening a new tab with CTRL-W T', function() 32 clear() 33 command('au! TabNewEntered * echom "entered"') 34 command('tabnew test.x2') 35 command('split') 36 eq('entered', api.nvim_exec('execute "normal \\<C-W>T"', true)) 37 end) 38 end) 39 describe('with tab split #4334', function() 40 it('works when create a tab by using tab split command', function() 41 clear() 42 command('au! TabNewEntered * let b:entered = "entered"') 43 command('tab split') 44 eq('entered', api.nvim_exec('echo b:entered', true)) 45 end) 46 end) 47 end) 48 end) 49 50 describe('TabEnter', function() 51 before_each(clear) 52 it('has correct previous tab when entering any new tab', function() 53 command('augroup TEMP') 54 command('au! TabEnter * echom "tabenter:".tabpagenr().":".tabpagenr(\'#\')') 55 command('augroup END') 56 eq('tabenter:2:1', api.nvim_exec('tabnew', true)) 57 eq('tabenter:3:2', api.nvim_exec('tabnew test.x2', true)) 58 command('augroup! TEMP') 59 end) 60 it('has correct previous tab when entering any preexisting tab', function() 61 command('tabnew') 62 command('tabnew') 63 command('augroup TEMP') 64 command('au! TabEnter * echom "tabenter:".tabpagenr().":".tabpagenr(\'#\')') 65 command('augroup END') 66 eq('tabenter:1:3', api.nvim_exec('tabnext', true)) 67 eq('tabenter:2:1', api.nvim_exec('tabnext', true)) 68 command('augroup! TEMP') 69 end) 70 end) 71 72 describe('tabpage/previous', function() 73 before_each(clear) 74 local function switches_to_previous_after_new_tab_creation_at_end(characters) 75 return function() 76 -- Add three tabs for a total of four 77 command('tabnew') 78 command('tabnew') 79 command('tabnew') 80 81 -- The previous tab is now the third. 82 eq(3, eval("tabpagenr('#')")) 83 84 -- Switch to the previous (third) tab 85 feed(characters) 86 87 eq( 88 dedent([=[ 89 Tab page 1 90 [No Name] 91 Tab page 2 92 [No Name] 93 Tab page 3 94 > [No Name] 95 Tab page 4 96 # [No Name]]=]), 97 exec_capture('tabs') 98 ) 99 100 -- The previous tab is now the fourth. 101 eq(4, eval("tabpagenr('#')")) 102 end 103 end 104 it( 105 'switches to previous via g<Tab> after new tab creation at end', 106 switches_to_previous_after_new_tab_creation_at_end('g<Tab>') 107 ) 108 it( 109 'switches to previous via <C-W>g<Tab>. after new tab creation at end', 110 switches_to_previous_after_new_tab_creation_at_end('<C-W>g<Tab>') 111 ) 112 it( 113 'switches to previous via <C-Tab>. after new tab creation at end', 114 switches_to_previous_after_new_tab_creation_at_end('<C-Tab>') 115 ) 116 it( 117 'switches to previous via :tabn #<CR>. after new tab creation at end', 118 switches_to_previous_after_new_tab_creation_at_end(':tabn #<CR>') 119 ) 120 121 local function switches_to_previous_after_new_tab_creation_in_middle(characters) 122 return function() 123 -- Add three tabs for a total of four 124 command('tabnew') 125 command('tabnew') 126 command('tabnew') 127 -- Switch to the second tab 128 command('tabnext 2') 129 -- Add a new tab after the second tab 130 command('tabnew') 131 132 -- The previous tab is now the second. 133 eq(2, eval("tabpagenr('#')")) 134 135 -- Switch to the previous (second) tab 136 feed(characters) 137 eq( 138 dedent([=[ 139 Tab page 1 140 [No Name] 141 Tab page 2 142 > [No Name] 143 Tab page 3 144 # [No Name] 145 Tab page 4 146 [No Name] 147 Tab page 5 148 [No Name]]=]), 149 exec_capture('tabs') 150 ) 151 152 -- The previous tab is now the third. 153 eq(3, eval("tabpagenr('#')")) 154 end 155 end 156 it( 157 'switches to previous via g<Tab> after new tab creation in middle', 158 switches_to_previous_after_new_tab_creation_in_middle('g<Tab>') 159 ) 160 it( 161 'switches to previous via <C-W>g<Tab> after new tab creation in middle', 162 switches_to_previous_after_new_tab_creation_in_middle('<C-W>g<Tab>') 163 ) 164 it( 165 'switches to previous via <C-Tab> after new tab creation in middle', 166 switches_to_previous_after_new_tab_creation_in_middle('<C-Tab>') 167 ) 168 it( 169 'switches to previous via :tabn #<CR> after new tab creation in middle', 170 switches_to_previous_after_new_tab_creation_in_middle(':tabn #<CR>') 171 ) 172 173 local function switches_to_previous_after_switching_to_next_tab(characters) 174 return function() 175 -- Add three tabs for a total of four 176 command('tabnew') 177 command('tabnew') 178 command('tabnew') 179 -- Switch to the next (first) tab 180 command('tabnext') 181 182 -- The previous tab is now the fourth. 183 eq(4, eval("tabpagenr('#')")) 184 185 -- Switch to the previous (fourth) tab 186 feed(characters) 187 188 eq( 189 dedent([=[ 190 Tab page 1 191 # [No Name] 192 Tab page 2 193 [No Name] 194 Tab page 3 195 [No Name] 196 Tab page 4 197 > [No Name]]=]), 198 exec_capture('tabs') 199 ) 200 201 -- The previous tab is now the first. 202 eq(1, eval("tabpagenr('#')")) 203 end 204 end 205 it( 206 'switches to previous via g<Tab> after switching to next tab', 207 switches_to_previous_after_switching_to_next_tab('g<Tab>') 208 ) 209 it( 210 'switches to previous via <C-W>g<Tab> after switching to next tab', 211 switches_to_previous_after_switching_to_next_tab('<C-W>g<Tab>') 212 ) 213 it( 214 'switches to previous via <C-Tab> after switching to next tab', 215 switches_to_previous_after_switching_to_next_tab('<C-Tab>') 216 ) 217 it( 218 'switches to previous via :tabn #<CR> after switching to next tab', 219 switches_to_previous_after_switching_to_next_tab(':tabn #<CR>') 220 ) 221 222 local function switches_to_previous_after_switching_to_last_tab(characters) 223 return function() 224 -- Add three tabs for a total of four 225 command('tabnew') 226 command('tabnew') 227 command('tabnew') 228 -- Switch to the next (first) tab 229 command('tabnext') 230 -- Switch to the last (fourth) tab. 231 command('tablast') 232 233 -- The previous tab is now the second. 234 eq(1, eval("tabpagenr('#')")) 235 236 -- Switch to the previous (second) tab 237 feed(characters) 238 239 eq( 240 dedent([=[ 241 Tab page 1 242 > [No Name] 243 Tab page 2 244 [No Name] 245 Tab page 3 246 [No Name] 247 Tab page 4 248 # [No Name]]=]), 249 exec_capture('tabs') 250 ) 251 252 -- The previous tab is now the fourth. 253 eq(4, eval("tabpagenr('#')")) 254 end 255 end 256 it( 257 'switches to previous after switching to last tab', 258 switches_to_previous_after_switching_to_last_tab('g<Tab>') 259 ) 260 it( 261 'switches to previous after switching to last tab', 262 switches_to_previous_after_switching_to_last_tab('<C-W>g<Tab>') 263 ) 264 it( 265 'switches to previous after switching to last tab', 266 switches_to_previous_after_switching_to_last_tab('<C-Tab>') 267 ) 268 it( 269 'switches to previous after switching to last tab', 270 switches_to_previous_after_switching_to_last_tab(':tabn #<CR>') 271 ) 272 273 local function switches_to_previous_after_switching_to_previous_tab(characters) 274 return function() 275 -- Add three tabs for a total of four 276 command('tabnew') 277 command('tabnew') 278 command('tabnew') 279 -- Switch to the previous (third) tab 280 command('tabprevious') 281 282 -- The previous tab is now the fourth. 283 eq(4, eval("tabpagenr('#')")) 284 285 -- Switch to the previous (fourth) tab 286 feed(characters) 287 288 eq( 289 dedent([=[ 290 Tab page 1 291 [No Name] 292 Tab page 2 293 [No Name] 294 Tab page 3 295 # [No Name] 296 Tab page 4 297 > [No Name]]=]), 298 exec_capture('tabs') 299 ) 300 301 -- The previous tab is now the third. 302 eq(3, eval("tabpagenr('#')")) 303 end 304 end 305 it( 306 'switches to previous via g<Tab> after switching to previous tab', 307 switches_to_previous_after_switching_to_previous_tab('g<Tab>') 308 ) 309 it( 310 'switches to previous via <C-W>g<Tab> after switching to previous tab', 311 switches_to_previous_after_switching_to_previous_tab('<C-W>g<Tab>') 312 ) 313 it( 314 'switches to previous via <C-Tab> after switching to previous tab', 315 switches_to_previous_after_switching_to_previous_tab('<C-Tab>') 316 ) 317 it( 318 'switches to previous via :tabn #<CR> after switching to previous tab', 319 switches_to_previous_after_switching_to_previous_tab(':tabn #<CR>') 320 ) 321 322 local function switches_to_previous_after_switching_to_first_tab(characters) 323 return function() 324 -- Add three tabs for a total of four 325 command('tabnew') 326 command('tabnew') 327 command('tabnew') 328 -- Switch to the previous (third) tab 329 command('tabprevious') 330 -- Switch to the first tab 331 command('tabfirst') 332 333 -- The previous tab is now the third. 334 eq(3, eval("tabpagenr('#')")) 335 336 -- Switch to the previous (third) tab 337 feed(characters) 338 339 eq( 340 dedent([=[ 341 Tab page 1 342 # [No Name] 343 Tab page 2 344 [No Name] 345 Tab page 3 346 > [No Name] 347 Tab page 4 348 [No Name]]=]), 349 exec_capture('tabs') 350 ) 351 352 -- The previous tab is now the first. 353 eq(1, eval("tabpagenr('#')")) 354 end 355 end 356 it( 357 'switches to previous via g<Tab> after switching to first tab', 358 switches_to_previous_after_switching_to_first_tab('g<Tab>') 359 ) 360 it( 361 'switches to previous via <C-W>g<Tab> after switching to first tab', 362 switches_to_previous_after_switching_to_first_tab('<C-W>g<Tab>') 363 ) 364 it( 365 'switches to previous via <C-Tab> after switching to first tab', 366 switches_to_previous_after_switching_to_first_tab('<C-Tab>') 367 ) 368 it( 369 'switches to previous via :tabn #<CR> after switching to first tab', 370 switches_to_previous_after_switching_to_first_tab(':tabn #<CR>') 371 ) 372 373 local function switches_to_previous_after_numbered_tab_switch(characters) 374 return function() 375 -- Add three tabs for a total of four 376 command('tabnew') 377 command('tabnew') 378 command('tabnew') 379 -- Switch to the second tab 380 command('tabnext 2') 381 382 -- The previous tab is now the fourth. 383 eq(4, eval("tabpagenr('#')")) 384 385 -- Switch to the previous (fourth) tab 386 feed(characters) 387 388 eq( 389 dedent([=[ 390 Tab page 1 391 [No Name] 392 Tab page 2 393 # [No Name] 394 Tab page 3 395 [No Name] 396 Tab page 4 397 > [No Name]]=]), 398 exec_capture('tabs') 399 ) 400 401 -- The previous tab is now the second. 402 eq(2, eval("tabpagenr('#')")) 403 end 404 end 405 it( 406 'switches to previous via g<Tab> after numbered tab switch', 407 switches_to_previous_after_numbered_tab_switch('g<Tab>') 408 ) 409 it( 410 'switches to previous via <C-W>g<Tab> after numbered tab switch', 411 switches_to_previous_after_numbered_tab_switch('<C-W>g<Tab>') 412 ) 413 it( 414 'switches to previous via <C-Tab> after numbered tab switch', 415 switches_to_previous_after_numbered_tab_switch('<C-Tab>') 416 ) 417 it( 418 'switches to previous via :tabn #<CR> after numbered tab switch', 419 switches_to_previous_after_numbered_tab_switch(':tabn #<CR>') 420 ) 421 422 local function switches_to_previous_after_switching_to_previous(characters1, characters2) 423 return function() 424 -- Add three tabs for a total of four 425 command('tabnew') 426 command('tabnew') 427 command('tabnew') 428 -- Switch to the second tab 429 command('tabnext 2') 430 -- Switch to the previous (fourth) tab 431 feed(characters1) 432 433 -- The previous tab is now the second. 434 eq(2, eval("tabpagenr('#')")) 435 436 -- Switch to the previous (second) tab 437 feed(characters2) 438 439 eq( 440 dedent([=[ 441 Tab page 1 442 [No Name] 443 Tab page 2 444 > [No Name] 445 Tab page 3 446 [No Name] 447 Tab page 4 448 # [No Name]]=]), 449 exec_capture('tabs') 450 ) 451 452 -- The previous tab is now the fourth. 453 eq(4, eval("tabpagenr('#')")) 454 end 455 end 456 it( 457 'switches to previous via g<Tab> after switching to previous via g<Tab>', 458 switches_to_previous_after_switching_to_previous('g<Tab>', 'g<Tab>') 459 ) 460 it( 461 'switches to previous via <C-W>g<Tab> after switching to previous via g<Tab>', 462 switches_to_previous_after_switching_to_previous('g<Tab>', '<C-W>g<Tab>') 463 ) 464 it( 465 'switches to previous via <C-Tab> after switching to previous via g<Tab>', 466 switches_to_previous_after_switching_to_previous('g<Tab>', '<C-Tab>') 467 ) 468 it( 469 'switches to previous via :tabn #<CR> after switching to previous via g<Tab>', 470 switches_to_previous_after_switching_to_previous('g<Tab>', ':tabn #<CR>') 471 ) 472 it( 473 'switches to previous via g<Tab> after switching to previous via <C-W>g<Tab>', 474 switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', 'g<Tab>') 475 ) 476 it( 477 'switches to previous via <C-W>g<Tab> after switching to previous via <C-W>g<Tab>', 478 switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', '<C-W>g<Tab>') 479 ) 480 it( 481 'switches to previous via <C-Tab> after switching to previous via <C-W>g<Tab>', 482 switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', '<C-Tab>') 483 ) 484 it( 485 'switches to previous via :tabn #<CR> after switching to previous via <C-W>g<Tab>', 486 switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', ':tabn #<CR>') 487 ) 488 it( 489 'switches to previous via g<Tab> after switching to previous via <C-Tab>', 490 switches_to_previous_after_switching_to_previous('<C-Tab>', 'g<Tab>') 491 ) 492 it( 493 'switches to previous via <C-W>g<Tab> after switching to previous via <C-Tab>', 494 switches_to_previous_after_switching_to_previous('<C-Tab>', '<C-W>g<Tab>') 495 ) 496 it( 497 'switches to previous via <C-Tab> after switching to previous via <C-Tab>', 498 switches_to_previous_after_switching_to_previous('<C-Tab>', '<C-Tab>') 499 ) 500 it( 501 'switches to previous via :tabn #<CR> after switching to previous via <C-Tab>', 502 switches_to_previous_after_switching_to_previous('<C-Tab>', ':tabn #<CR>') 503 ) 504 it( 505 'switches to previous via g<Tab> after switching to previous via :tabn #<CR>', 506 switches_to_previous_after_switching_to_previous(':tabn #<CR>', 'g<Tab>') 507 ) 508 it( 509 'switches to previous via <C-W>g<Tab> after switching to previous via :tabn #<CR>', 510 switches_to_previous_after_switching_to_previous(':tabn #<CR>', '<C-W>g<Tab>') 511 ) 512 it( 513 'switches to previous via <C-Tab> after switching to previous via <C-Tab>', 514 switches_to_previous_after_switching_to_previous(':tabn #<CR>', '<C-Tab>') 515 ) 516 it( 517 'switches to previous via :tabn #<CR> after switching to previous via :tabn #<CR>', 518 switches_to_previous_after_switching_to_previous(':tabn #<CR>', ':tabn #<CR>') 519 ) 520 521 local function does_not_switch_to_previous_after_closing_current_tab(characters) 522 return function() 523 -- Add three tabs for a total of four 524 command('tabnew') 525 command('tabnew') 526 command('tabnew') 527 -- Close the current (fourth tab) 528 command('wincmd c') 529 530 -- The previous tab is now the "zeroth" -- there isn't one. 531 eq(0, eval("tabpagenr('#')")) 532 533 -- At this point, switching to the "previous" (i.e. fourth) tab would mean 534 -- switching to either a dangling or a null pointer. 535 feed(characters) 536 537 eq( 538 dedent([=[ 539 Tab page 1 540 [No Name] 541 Tab page 2 542 [No Name] 543 Tab page 3 544 > [No Name]]=]), 545 exec_capture('tabs') 546 ) 547 548 -- The previous tab is now the "zero". 549 eq(0, eval("tabpagenr('#')")) 550 end 551 end 552 it( 553 'does not switch to previous via g<Tab> after closing current tab', 554 does_not_switch_to_previous_after_closing_current_tab('g<Tab>') 555 ) 556 it( 557 'does not switch to previous via <C-W>g<Tab> after closing current tab', 558 does_not_switch_to_previous_after_closing_current_tab('<C-W>g<Tab>') 559 ) 560 it( 561 'does not switch to previous via <C-Tab> after closing current tab', 562 does_not_switch_to_previous_after_closing_current_tab('<C-Tab>') 563 ) 564 it( 565 'does not switch to previous via :tabn #<CR> after closing current tab', 566 does_not_switch_to_previous_after_closing_current_tab(':tabn #<CR>') 567 ) 568 569 local function does_not_switch_to_previous_after_entering_operator_pending(characters) 570 return function() 571 -- Add three tabs for a total of four 572 command('tabnew') 573 command('tabnew') 574 command('tabnew') 575 576 -- The previous tab is now the third. 577 eq(3, eval("tabpagenr('#')")) 578 579 -- Enter operator pending mode. 580 feed('d') 581 eq('no', eval('mode(1)')) 582 583 -- At this point switching to the previous tab should have no effect 584 -- other than leaving operator pending mode. 585 feed(characters) 586 587 -- Attempting to switch tabs returns us to normal mode. 588 eq('n', eval('mode()')) 589 590 -- The current tab is still the fourth. 591 eq(4, eval('tabpagenr()')) 592 593 -- The previous tab is still the third. 594 eq(3, eval("tabpagenr('#')")) 595 end 596 end 597 it( 598 'does not switch to previous via g<Tab> after entering operator pending', 599 does_not_switch_to_previous_after_entering_operator_pending('g<Tab>') 600 ) 601 -- NOTE: When in operator pending mode, attempting to switch to previous has 602 -- the following effect: 603 -- - Ctrl-W exits operator pending mode 604 -- - g<Tab> switches to the previous tab 605 -- In other words, the effect of "<C-W>g<Tab>" is to switch to the 606 -- previous tab even from operator pending mode, but only thanks to the 607 -- fact that the suffix after "<C-W>" in "<C-W>g<Tab>" just happens to 608 -- be the same as the normal mode command to switch to the previous tab. 609 -- it('does not switch to previous via <C-W>g<Tab> after entering operator pending', 610 -- does_not_switch_to_previous_after_entering_operator_pending('<C-W>g<Tab>')) 611 it( 612 'does not switch to previous via <C-Tab> after entering operator pending', 613 does_not_switch_to_previous_after_entering_operator_pending('<C-Tab>') 614 ) 615 -- NOTE: When in operator pending mode, pressing : leaves operator pending 616 -- mode and enters command mode, so :tabn #<CR> does in fact switch 617 -- tabs. 618 -- it('does not switch to previous via :tabn #<CR> after entering operator pending', 619 -- does_not_switch_to_previous_after_entering_operator_pending(':tabn #<CR>')) 620 621 local function cmdline_win_prevents_tab_switch(characters, completion_visible) 622 return function() 623 -- Add three tabs for a total of four 624 command('tabnew') 625 command('tabnew') 626 command('tabnew') 627 628 -- The previous tab is now the third. 629 eq(3, eval("tabpagenr('#')")) 630 631 -- Edit : command line in command-line window 632 feed('q:') 633 634 local cmdline_win_id = eval('win_getid()') 635 636 -- At this point switching to the previous tab should have no effect. 637 feed(characters) 638 639 -- Attempting to switch tabs maintains the current window. 640 eq(cmdline_win_id, eval('win_getid()')) 641 eq(completion_visible, eval('complete_info().pum_visible')) 642 643 -- The current tab is still the fourth. 644 eq(4, eval('tabpagenr()')) 645 646 -- The previous tab is still the third. 647 eq(3, eval("tabpagenr('#')")) 648 end 649 end 650 it('cmdline-win prevents tab switch via g<Tab>', cmdline_win_prevents_tab_switch('g<Tab>', 0)) 651 it( 652 'cmdline-win prevents tab switch via <C-W>g<Tab>', 653 cmdline_win_prevents_tab_switch('<C-W>g<Tab>', 1) 654 ) 655 it('cmdline-win prevents tab switch via <C-Tab>', cmdline_win_prevents_tab_switch('<C-Tab>', 0)) 656 it( 657 'cmdline-win prevents tab switch via :tabn #<CR>', 658 cmdline_win_prevents_tab_switch(':tabn #<CR>', 0) 659 ) 660 661 it(':tabs indicates correct prevtab curwin', function() 662 -- Add three tabs for a total of four 663 command('tabnew') 664 command('tabnew') 665 command('split') 666 command('vsplit') 667 feed('<C-w>p') 668 command('tabnew') 669 670 -- The previous tab is now the three. 671 eq(3, eval("tabpagenr('#')")) 672 673 eq( 674 dedent([=[ 675 Tab page 1 676 [No Name] 677 Tab page 2 678 [No Name] 679 Tab page 3 680 [No Name] 681 # [No Name] 682 [No Name] 683 Tab page 4 684 > [No Name]]=]), 685 exec_capture('tabs') 686 ) 687 end) 688 end)