neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

tabclose_spec.lua (4763B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 
      4 local clear, eq = n.clear, t.eq
      5 local command = n.command
      6 local eval = n.eval
      7 local exec = n.exec
      8 local exec_capture = n.exec_capture
      9 
     10 describe('TabClosed', function()
     11  before_each(clear)
     12 
     13  describe('au TabClosed', function()
     14    describe('with * as <afile>', function()
     15      it('matches when closing any tab', function()
     16        command(
     17          'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()'
     18        )
     19        repeat
     20          command('tabnew')
     21        until eval('tabpagenr()') == 6 -- current tab is now 6
     22        eq('tabclosed:6:6:5', exec_capture('tabclose')) -- close last 6, current tab is now 5
     23        eq('tabclosed:5:5:4', exec_capture('close')) -- close last window on tab, closes tab
     24        eq('tabclosed:2:2:3', exec_capture('2tabclose')) -- close tab 2, current tab is now 3
     25        eq('tabclosed:1:1:2\ntabclosed:1:1:1', exec_capture('tabonly')) -- close tabs 1 and 2
     26      end)
     27 
     28      it('is triggered when closing a window via bdelete from another tab', function()
     29        command(
     30          'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()'
     31        )
     32        command('1tabedit Xtestfile')
     33        command('1tabedit Xtestfile')
     34        command('normal! 1gt')
     35        eq({ 1, 3 }, eval('[tabpagenr(), tabpagenr("$")]'))
     36        eq('tabclosed:2:2:1\ntabclosed:2:2:1', exec_capture('bdelete Xtestfile'))
     37        eq({ 1, 1 }, eval('[tabpagenr(), tabpagenr("$")]'))
     38      end)
     39 
     40      it('is triggered when closing a window via bdelete from current tab', function()
     41        command(
     42          'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()'
     43        )
     44        command('file Xtestfile1')
     45        command('1tabedit Xtestfile2')
     46        command('1tabedit Xtestfile2')
     47 
     48        -- Only one tab is closed, and the alternate file is used for the other.
     49        eq({ 2, 3 }, eval('[tabpagenr(), tabpagenr("$")]'))
     50        eq('tabclosed:2:2:2', exec_capture('bdelete Xtestfile2'))
     51        eq('Xtestfile1', eval('bufname("")'))
     52      end)
     53 
     54      it('triggers after tab page is properly freed', function()
     55        exec([[
     56          let s:tp = nvim_get_current_tabpage()
     57          let g:buf = bufnr()
     58 
     59          setlocal bufhidden=wipe
     60          tabnew
     61          au TabClosed * ++once let g:tp_valid = nvim_tabpage_is_valid(s:tp)
     62                             \| let g:curbuf = bufnr()
     63                             \| let g:abuf = expand('<abuf>')
     64 
     65          call nvim_buf_delete(g:buf, #{force: 1})
     66        ]])
     67        eq(false, eval('g:tp_valid'))
     68        eq(false, eval('nvim_buf_is_valid(g:buf)'))
     69        eq(eval('g:curbuf'), tonumber(eval('g:abuf'))) -- Falls back to curbuf.
     70 
     71        exec([[
     72          tabnew
     73          let g:buf = bufnr()
     74          let s:win = win_getid()
     75 
     76          tabfirst
     77          au TabClosed * ++once let g:abuf = expand('<abuf>')
     78 
     79          call nvim_win_close(s:win, 1)
     80        ]])
     81        eq(true, eval('nvim_buf_is_valid(g:buf)'))
     82        eq(eval('g:buf'), tonumber(eval('g:abuf')))
     83 
     84        exec([[
     85          tabnew
     86          let s:win = win_getid()
     87 
     88          tabfirst
     89          let g:buf = nvim_create_buf(1, 1)
     90          au BufHidden * ++once call nvim_win_set_buf(s:win, g:buf)
     91          au TabClosed * ++once let g:abuf = expand('<abuf>')
     92 
     93          call nvim_win_close(s:win, 1)
     94        ]])
     95        -- BufHidden switched buffers at the last moment; TabClosed's <abuf> should show that.
     96        eq(eval('g:buf'), tonumber(eval('g:abuf')))
     97      end)
     98    end)
     99 
    100    describe('with NR as <afile>', function()
    101      it('matches when closing a tab whose index is NR', function()
    102        command(
    103          'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()'
    104        )
    105        command('au! TabClosed 2 echom "tabclosed:match"')
    106        repeat
    107          command('tabnew')
    108        until eval('tabpagenr()') == 7 -- current tab is now 7
    109        -- sanity check, we shouldn't match on tabs with numbers other than 2
    110        eq('tabclosed:7:7:6', exec_capture('tabclose'))
    111        -- close tab page 2, current tab is now 5
    112        eq('tabclosed:2:2:5\ntabclosed:match', exec_capture('2tabclose'))
    113      end)
    114    end)
    115 
    116    describe('with close', function()
    117      it('is triggered', function()
    118        command(
    119          'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()'
    120        )
    121        command('tabedit Xtestfile')
    122        eq({ 2, 2 }, eval('[tabpagenr(), tabpagenr("$")]'))
    123        eq('tabclosed:2:2:1', exec_capture('close'))
    124        eq({ 1, 1 }, eval('[tabpagenr(), tabpagenr("$")]'))
    125      end)
    126    end)
    127  end)
    128 end)