tabnewentered_spec.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local clear = helpers.clear
  3. local command = helpers.command
  4. local dedent = helpers.dedent
  5. local eval = helpers.eval
  6. local eq = helpers.eq
  7. local feed = helpers.feed
  8. local nvim = helpers.nvim
  9. local exec_capture = helpers.exec_capture
  10. describe('TabNewEntered', function()
  11. describe('au TabNewEntered', function()
  12. describe('with * as <afile>', function()
  13. it('matches when entering any new tab', function()
  14. clear()
  15. nvim('command', 'au! TabNewEntered * echom "tabnewentered:".tabpagenr().":".bufnr("")')
  16. eq("tabnewentered:2:2", nvim('exec', 'tabnew', true))
  17. eq("tabnewentered:3:3", nvim('exec', 'tabnew test.x2', true))
  18. end)
  19. end)
  20. describe('with FILE as <afile>', function()
  21. it('matches when opening a new tab for FILE', function()
  22. nvim('command', 'au! TabNewEntered Xtest-tabnewentered echom "tabnewentered:match"')
  23. eq('tabnewentered:4:4\ntabnewentered:match',
  24. nvim('exec', 'tabnew Xtest-tabnewentered', true))
  25. end)
  26. end)
  27. describe('with CTRL-W T', function()
  28. it('works when opening a new tab with CTRL-W T', function()
  29. clear()
  30. nvim('command', 'au! TabNewEntered * echom "entered"')
  31. nvim('command', 'tabnew test.x2')
  32. nvim('command', 'split')
  33. eq('entered', nvim('exec', 'execute "normal \\<C-W>T"', true))
  34. end)
  35. end)
  36. end)
  37. end)
  38. describe('TabEnter', function()
  39. before_each(clear)
  40. it('has correct previous tab when entering any new tab', function()
  41. command('augroup TEMP')
  42. nvim('command', 'au! TabEnter * echom "tabenter:".tabpagenr().":".tabpagenr(\'#\')')
  43. command('augroup END')
  44. eq("tabenter:2:1", nvim('exec', 'tabnew', true))
  45. eq("tabenter:3:2", nvim('exec', 'tabnew test.x2', true))
  46. command('augroup! TEMP')
  47. end)
  48. it('has correct previous tab when entering any preexisting tab', function()
  49. command('tabnew')
  50. command('tabnew')
  51. command('augroup TEMP')
  52. nvim('command', 'au! TabEnter * echom "tabenter:".tabpagenr().":".tabpagenr(\'#\')')
  53. command('augroup END')
  54. eq("tabenter:1:3", nvim('exec', 'tabnext', true))
  55. eq("tabenter:2:1", nvim('exec', 'tabnext', true))
  56. command('augroup! TEMP')
  57. end)
  58. end)
  59. describe('tabpage/previous', function()
  60. before_each(clear)
  61. local function switches_to_previous_after_new_tab_creation_at_end(characters)
  62. return function()
  63. -- Add three tabs for a total of four
  64. command('tabnew')
  65. command('tabnew')
  66. command('tabnew')
  67. -- The previous tab is now the third.
  68. eq(3, eval('tabpagenr(\'#\')'))
  69. -- Switch to the previous (third) tab
  70. feed(characters)
  71. eq(dedent([=[
  72. Tab page 1
  73. [No Name]
  74. Tab page 2
  75. [No Name]
  76. Tab page 3
  77. > [No Name]
  78. Tab page 4
  79. # [No Name]]=]),
  80. exec_capture('tabs')
  81. )
  82. -- The previous tab is now the fourth.
  83. eq(4, eval('tabpagenr(\'#\')'))
  84. end
  85. end
  86. it('switches to previous via g<Tab> after new tab creation at end',
  87. switches_to_previous_after_new_tab_creation_at_end('g<Tab>'))
  88. it('switches to previous via <C-W>g<Tab>. after new tab creation at end', switches_to_previous_after_new_tab_creation_at_end('<C-W>g<Tab>'))
  89. it('switches to previous via <C-Tab>. after new tab creation at end', switches_to_previous_after_new_tab_creation_at_end('<C-Tab>'))
  90. it('switches to previous via :tabn #<CR>. after new tab creation at end', switches_to_previous_after_new_tab_creation_at_end(':tabn #<CR>'))
  91. local function switches_to_previous_after_new_tab_creation_in_middle(characters)
  92. return function()
  93. -- Add three tabs for a total of four
  94. command('tabnew')
  95. command('tabnew')
  96. command('tabnew')
  97. -- Switch to the second tab
  98. command('tabnext 2')
  99. -- Add a new tab after the second tab
  100. command('tabnew')
  101. -- The previous tab is now the second.
  102. eq(2, eval('tabpagenr(\'#\')'))
  103. -- Switch to the previous (second) tab
  104. feed(characters)
  105. eq(dedent([=[
  106. Tab page 1
  107. [No Name]
  108. Tab page 2
  109. > [No Name]
  110. Tab page 3
  111. # [No Name]
  112. Tab page 4
  113. [No Name]
  114. Tab page 5
  115. [No Name]]=]),
  116. exec_capture('tabs')
  117. )
  118. -- The previous tab is now the third.
  119. eq(3, eval('tabpagenr(\'#\')'))
  120. end
  121. end
  122. it('switches to previous via g<Tab> after new tab creation in middle',
  123. switches_to_previous_after_new_tab_creation_in_middle('g<Tab>'))
  124. it('switches to previous via <C-W>g<Tab> after new tab creation in middle',
  125. switches_to_previous_after_new_tab_creation_in_middle('<C-W>g<Tab>'))
  126. it('switches to previous via <C-Tab> after new tab creation in middle',
  127. switches_to_previous_after_new_tab_creation_in_middle('<C-Tab>'))
  128. it('switches to previous via :tabn #<CR> after new tab creation in middle',
  129. switches_to_previous_after_new_tab_creation_in_middle(':tabn #<CR>'))
  130. local function switches_to_previous_after_switching_to_next_tab(characters)
  131. return function()
  132. -- Add three tabs for a total of four
  133. command('tabnew')
  134. command('tabnew')
  135. command('tabnew')
  136. -- Switch to the next (first) tab
  137. command('tabnext')
  138. -- The previous tab is now the fourth.
  139. eq(4, eval('tabpagenr(\'#\')'))
  140. -- Switch to the previous (fourth) tab
  141. feed(characters)
  142. eq(dedent([=[
  143. Tab page 1
  144. # [No Name]
  145. Tab page 2
  146. [No Name]
  147. Tab page 3
  148. [No Name]
  149. Tab page 4
  150. > [No Name]]=]),
  151. exec_capture('tabs')
  152. )
  153. -- The previous tab is now the first.
  154. eq(1, eval('tabpagenr(\'#\')'))
  155. end
  156. end
  157. it('switches to previous via g<Tab> after switching to next tab',
  158. switches_to_previous_after_switching_to_next_tab('g<Tab>'))
  159. it('switches to previous via <C-W>g<Tab> after switching to next tab',
  160. switches_to_previous_after_switching_to_next_tab('<C-W>g<Tab>'))
  161. it('switches to previous via <C-Tab> after switching to next tab',
  162. switches_to_previous_after_switching_to_next_tab('<C-Tab>'))
  163. it('switches to previous via :tabn #<CR> after switching to next tab',
  164. switches_to_previous_after_switching_to_next_tab(':tabn #<CR>'))
  165. local function switches_to_previous_after_switching_to_last_tab(characters)
  166. return function()
  167. -- Add three tabs for a total of four
  168. command('tabnew')
  169. command('tabnew')
  170. command('tabnew')
  171. -- Switch to the next (first) tab
  172. command('tabnext')
  173. -- Switch to the last (fourth) tab.
  174. command('tablast')
  175. -- The previous tab is now the second.
  176. eq(1, eval('tabpagenr(\'#\')'))
  177. -- Switch to the previous (second) tab
  178. feed(characters)
  179. eq(dedent([=[
  180. Tab page 1
  181. > [No Name]
  182. Tab page 2
  183. [No Name]
  184. Tab page 3
  185. [No Name]
  186. Tab page 4
  187. # [No Name]]=]),
  188. exec_capture('tabs')
  189. )
  190. -- The previous tab is now the fourth.
  191. eq(4, eval('tabpagenr(\'#\')'))
  192. end
  193. end
  194. it('switches to previous after switching to last tab',
  195. switches_to_previous_after_switching_to_last_tab('g<Tab>'))
  196. it('switches to previous after switching to last tab',
  197. switches_to_previous_after_switching_to_last_tab('<C-W>g<Tab>'))
  198. it('switches to previous after switching to last tab',
  199. switches_to_previous_after_switching_to_last_tab('<C-Tab>'))
  200. it('switches to previous after switching to last tab',
  201. switches_to_previous_after_switching_to_last_tab(':tabn #<CR>'))
  202. local function switches_to_previous_after_switching_to_previous_tab(characters)
  203. return function()
  204. -- Add three tabs for a total of four
  205. command('tabnew')
  206. command('tabnew')
  207. command('tabnew')
  208. -- Switch to the previous (third) tab
  209. command('tabprevious')
  210. -- The previous tab is now the fourth.
  211. eq(4, eval('tabpagenr(\'#\')'))
  212. -- Switch to the previous (fourth) tab
  213. feed(characters)
  214. eq(dedent([=[
  215. Tab page 1
  216. [No Name]
  217. Tab page 2
  218. [No Name]
  219. Tab page 3
  220. # [No Name]
  221. Tab page 4
  222. > [No Name]]=]),
  223. exec_capture('tabs')
  224. )
  225. -- The previous tab is now the third.
  226. eq(3, eval('tabpagenr(\'#\')'))
  227. end
  228. end
  229. it('switches to previous via g<Tab> after switching to previous tab',
  230. switches_to_previous_after_switching_to_previous_tab('g<Tab>'))
  231. it('switches to previous via <C-W>g<Tab> after switching to previous tab',
  232. switches_to_previous_after_switching_to_previous_tab('<C-W>g<Tab>'))
  233. it('switches to previous via <C-Tab> after switching to previous tab',
  234. switches_to_previous_after_switching_to_previous_tab('<C-Tab>'))
  235. it('switches to previous via :tabn #<CR> after switching to previous tab',
  236. switches_to_previous_after_switching_to_previous_tab(':tabn #<CR>'))
  237. local function switches_to_previous_after_switching_to_first_tab(characters)
  238. return function()
  239. -- Add three tabs for a total of four
  240. command('tabnew')
  241. command('tabnew')
  242. command('tabnew')
  243. -- Switch to the previous (third) tab
  244. command('tabprevious')
  245. -- Switch to the first tab
  246. command('tabfirst')
  247. -- The previous tab is now the third.
  248. eq(3, eval('tabpagenr(\'#\')'))
  249. -- Switch to the previous (third) tab
  250. feed(characters)
  251. eq(dedent([=[
  252. Tab page 1
  253. # [No Name]
  254. Tab page 2
  255. [No Name]
  256. Tab page 3
  257. > [No Name]
  258. Tab page 4
  259. [No Name]]=]),
  260. exec_capture('tabs')
  261. )
  262. -- The previous tab is now the first.
  263. eq(1, eval('tabpagenr(\'#\')'))
  264. end
  265. end
  266. it('switches to previous via g<Tab> after switching to first tab',
  267. switches_to_previous_after_switching_to_first_tab('g<Tab>'))
  268. it('switches to previous via <C-W>g<Tab> after switching to first tab',
  269. switches_to_previous_after_switching_to_first_tab('<C-W>g<Tab>'))
  270. it('switches to previous via <C-Tab> after switching to first tab',
  271. switches_to_previous_after_switching_to_first_tab('<C-Tab>'))
  272. it('switches to previous via :tabn #<CR> after switching to first tab',
  273. switches_to_previous_after_switching_to_first_tab(':tabn #<CR>'))
  274. local function switches_to_previous_after_numbered_tab_switch(characters)
  275. return function()
  276. -- Add three tabs for a total of four
  277. command('tabnew')
  278. command('tabnew')
  279. command('tabnew')
  280. -- Switch to the second tab
  281. command('tabnext 2')
  282. -- The previous tab is now the fourth.
  283. eq(4, eval('tabpagenr(\'#\')'))
  284. -- Switch to the previous (fourth) tab
  285. feed(characters)
  286. eq(dedent([=[
  287. Tab page 1
  288. [No Name]
  289. Tab page 2
  290. # [No Name]
  291. Tab page 3
  292. [No Name]
  293. Tab page 4
  294. > [No Name]]=]),
  295. exec_capture('tabs')
  296. )
  297. -- The previous tab is now the second.
  298. eq(2, eval('tabpagenr(\'#\')'))
  299. end
  300. end
  301. it('switches to previous via g<Tab> after numbered tab switch',
  302. switches_to_previous_after_numbered_tab_switch('g<Tab>'))
  303. it('switches to previous via <C-W>g<Tab> after numbered tab switch',
  304. switches_to_previous_after_numbered_tab_switch('<C-W>g<Tab>'))
  305. it('switches to previous via <C-Tab> after numbered tab switch',
  306. switches_to_previous_after_numbered_tab_switch('<C-Tab>'))
  307. it('switches to previous via :tabn #<CR> after numbered tab switch',
  308. switches_to_previous_after_numbered_tab_switch(':tabn #<CR>'))
  309. local function switches_to_previous_after_switching_to_previous(characters1, characters2)
  310. return function()
  311. -- Add three tabs for a total of four
  312. command('tabnew')
  313. command('tabnew')
  314. command('tabnew')
  315. -- Switch to the second tab
  316. command('tabnext 2')
  317. -- Switch to the previous (fourth) tab
  318. feed(characters1)
  319. -- The previous tab is now the second.
  320. eq(2, eval('tabpagenr(\'#\')'))
  321. -- Switch to the previous (second) tab
  322. feed(characters2)
  323. eq(dedent([=[
  324. Tab page 1
  325. [No Name]
  326. Tab page 2
  327. > [No Name]
  328. Tab page 3
  329. [No Name]
  330. Tab page 4
  331. # [No Name]]=]),
  332. exec_capture('tabs')
  333. )
  334. -- The previous tab is now the fourth.
  335. eq(4, eval('tabpagenr(\'#\')'))
  336. end
  337. end
  338. it('switches to previous via g<Tab> after switching to previous via g<Tab>',
  339. switches_to_previous_after_switching_to_previous('g<Tab>', 'g<Tab>'))
  340. it('switches to previous via <C-W>g<Tab> after switching to previous via g<Tab>',
  341. switches_to_previous_after_switching_to_previous('g<Tab>', '<C-W>g<Tab>'))
  342. it('switches to previous via <C-Tab> after switching to previous via g<Tab>',
  343. switches_to_previous_after_switching_to_previous('g<Tab>', '<C-Tab>'))
  344. it('switches to previous via :tabn #<CR> after switching to previous via g<Tab>',
  345. switches_to_previous_after_switching_to_previous('g<Tab>', ':tabn #<CR>'))
  346. it('switches to previous via g<Tab> after switching to previous via <C-W>g<Tab>',
  347. switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', 'g<Tab>'))
  348. it('switches to previous via <C-W>g<Tab> after switching to previous via <C-W>g<Tab>',
  349. switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', '<C-W>g<Tab>'))
  350. it('switches to previous via <C-Tab> after switching to previous via <C-W>g<Tab>',
  351. switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', '<C-Tab>'))
  352. it('switches to previous via :tabn #<CR> after switching to previous via <C-W>g<Tab>',
  353. switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', ':tabn #<CR>'))
  354. it('switches to previous via g<Tab> after switching to previous via <C-Tab>',
  355. switches_to_previous_after_switching_to_previous('<C-Tab>', 'g<Tab>'))
  356. it('switches to previous via <C-W>g<Tab> after switching to previous via <C-Tab>',
  357. switches_to_previous_after_switching_to_previous('<C-Tab>', '<C-W>g<Tab>'))
  358. it('switches to previous via <C-Tab> after switching to previous via <C-Tab>',
  359. switches_to_previous_after_switching_to_previous('<C-Tab>', '<C-Tab>'))
  360. it('switches to previous via :tabn #<CR> after switching to previous via <C-Tab>',
  361. switches_to_previous_after_switching_to_previous('<C-Tab>', ':tabn #<CR>'))
  362. it('switches to previous via g<Tab> after switching to previous via :tabn #<CR>',
  363. switches_to_previous_after_switching_to_previous(':tabn #<CR>', 'g<Tab>'))
  364. it('switches to previous via <C-W>g<Tab> after switching to previous via :tabn #<CR>',
  365. switches_to_previous_after_switching_to_previous(':tabn #<CR>', '<C-W>g<Tab>'))
  366. it('switches to previous via <C-Tab> after switching to previous via <C-Tab>',
  367. switches_to_previous_after_switching_to_previous(':tabn #<CR>', '<C-Tab>'))
  368. it('switches to previous via :tabn #<CR> after switching to previous via :tabn #<CR>',
  369. switches_to_previous_after_switching_to_previous(':tabn #<CR>', ':tabn #<CR>'))
  370. local function does_not_switch_to_previous_after_closing_current_tab(characters)
  371. return function()
  372. -- Add three tabs for a total of four
  373. command('tabnew')
  374. command('tabnew')
  375. command('tabnew')
  376. -- Close the current (fourth tab)
  377. command('wincmd c')
  378. -- The previous tab is now the "zeroth" -- there isn't one.
  379. eq(0, eval('tabpagenr(\'#\')'))
  380. -- At this point, switching to the "previous" (i.e. fourth) tab would mean
  381. -- switching to either a dangling or a null pointer.
  382. feed(characters)
  383. eq(dedent([=[
  384. Tab page 1
  385. [No Name]
  386. Tab page 2
  387. [No Name]
  388. Tab page 3
  389. > [No Name]]=]),
  390. exec_capture('tabs')
  391. )
  392. -- The previous tab is now the "zero".
  393. eq(0, eval('tabpagenr(\'#\')'))
  394. end
  395. end
  396. it('does not switch to previous via g<Tab> after closing current tab',
  397. does_not_switch_to_previous_after_closing_current_tab('g<Tab>'))
  398. it('does not switch to previous via <C-W>g<Tab> after closing current tab',
  399. does_not_switch_to_previous_after_closing_current_tab('<C-W>g<Tab>'))
  400. it('does not switch to previous via <C-Tab> after closing current tab',
  401. does_not_switch_to_previous_after_closing_current_tab('<C-Tab>'))
  402. it('does not switch to previous via :tabn #<CR> after closing current tab',
  403. does_not_switch_to_previous_after_closing_current_tab(':tabn #<CR>'))
  404. local function does_not_switch_to_previous_after_entering_operator_pending(characters)
  405. return function()
  406. -- Add three tabs for a total of four
  407. command('tabnew')
  408. command('tabnew')
  409. command('tabnew')
  410. -- The previous tab is now the third.
  411. eq(3, eval('tabpagenr(\'#\')'))
  412. -- Enter operator pending mode.
  413. feed('d')
  414. eq('no', eval('mode(1)'))
  415. -- At this point switching to the previous tab should have no effect
  416. -- other than leaving operator pending mode.
  417. feed(characters)
  418. -- Attempting to switch tabs returns us to normal mode.
  419. eq('n', eval('mode()'))
  420. -- The current tab is still the fourth.
  421. eq(4, eval('tabpagenr()'))
  422. -- The previous tab is still the third.
  423. eq(3, eval('tabpagenr(\'#\')'))
  424. end
  425. end
  426. it('does not switch to previous via g<Tab> after entering operator pending',
  427. does_not_switch_to_previous_after_entering_operator_pending('g<Tab>'))
  428. -- NOTE: When in operator pending mode, attempting to switch to previous has
  429. -- the following effect:
  430. -- - Ctrl-W exits operator pending mode
  431. -- - g<Tab> switches to the previous tab
  432. -- In other words, the effect of "<C-W>g<Tab>" is to switch to the
  433. -- previous tab even from operator pending mode, but only thanks to the
  434. -- fact that the suffix after "<C-W>" in "<C-W>g<Tab>" just happens to
  435. -- be the same as the normal mode command to switch to the previous tab.
  436. -- it('does not switch to previous via <C-W>g<Tab> after entering operator pending',
  437. -- does_not_switch_to_previous_after_entering_operator_pending('<C-W>g<Tab>'))
  438. it('does not switch to previous via <C-Tab> after entering operator pending',
  439. does_not_switch_to_previous_after_entering_operator_pending('<C-Tab>'))
  440. -- NOTE: When in operator pending mode, pressing : leaves operator pending
  441. -- mode and enters command mode, so :tabn #<CR> does in fact switch
  442. -- tabs.
  443. -- it('does not switch to previous via :tabn #<CR> after entering operator pending',
  444. -- does_not_switch_to_previous_after_entering_operator_pending(':tabn #<CR>'))
  445. local function cmdline_win_prevents_tab_switch(characters, completion_visible)
  446. return function()
  447. -- Add three tabs for a total of four
  448. command('tabnew')
  449. command('tabnew')
  450. command('tabnew')
  451. -- The previous tab is now the third.
  452. eq(3, eval('tabpagenr(\'#\')'))
  453. -- Edit : command line in command-line window
  454. feed('q:')
  455. local cmdline_win_id = eval('win_getid()')
  456. -- At this point switching to the previous tab should have no effect.
  457. feed(characters)
  458. -- Attempting to switch tabs maintains the current window.
  459. eq(cmdline_win_id, eval('win_getid()'))
  460. eq(completion_visible, eval('complete_info().pum_visible'))
  461. -- The current tab is still the fourth.
  462. eq(4, eval('tabpagenr()'))
  463. -- The previous tab is still the third.
  464. eq(3, eval('tabpagenr(\'#\')'))
  465. end
  466. end
  467. it('cmdline-win prevents tab switch via g<Tab>',
  468. cmdline_win_prevents_tab_switch('g<Tab>', 0))
  469. it('cmdline-win prevents tab switch via <C-W>g<Tab>',
  470. cmdline_win_prevents_tab_switch('<C-W>g<Tab>', 1))
  471. it('cmdline-win prevents tab switch via <C-Tab>',
  472. cmdline_win_prevents_tab_switch('<C-Tab>', 0))
  473. it('cmdline-win prevents tab switch via :tabn #<CR>',
  474. cmdline_win_prevents_tab_switch(':tabn #<CR>', 0))
  475. it(':tabs indicates correct prevtab curwin', function()
  476. -- Add three tabs for a total of four
  477. command('tabnew')
  478. command('tabnew')
  479. command('split')
  480. command('vsplit')
  481. feed('<C-w>p')
  482. command('tabnew')
  483. -- The previous tab is now the three.
  484. eq(3, eval('tabpagenr(\'#\')'))
  485. eq(dedent([=[
  486. Tab page 1
  487. [No Name]
  488. Tab page 2
  489. [No Name]
  490. Tab page 3
  491. [No Name]
  492. # [No Name]
  493. [No Name]
  494. Tab page 4
  495. > [No Name]]=]),
  496. exec_capture('tabs')
  497. )
  498. end)
  499. end)