neovim

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

039_visual_block_mode_commands_spec.lua (5467B)


      1 -- Test Visual block mode commands
      2 -- And test "U" in Visual mode, also on German sharp S.
      3 
      4 local t = require('test.testutil')
      5 local n = require('test.functional.testnvim')()
      6 
      7 local api, eq = n.api, t.eq
      8 local insert, feed = n.insert, n.feed
      9 local clear, expect = n.clear, n.expect
     10 local feed_command = n.feed_command
     11 
     12 describe('Visual block mode', function()
     13  before_each(function()
     14    clear()
     15 
     16    feed_command('set ts&vi sw&vi sts&vi noet') -- Vim compatible
     17  end)
     18 
     19  it('should shift, insert, replace and change a block', function()
     20    insert([[
     21      abcdefghijklm
     22      abcdefghijklm
     23      abcdefghijklm
     24      abcdefghijklm
     25      abcdefghijklm]])
     26 
     27    feed('gg')
     28    -- Test shift-right of a block
     29    feed('jllll<C-v>jj>wll<C-v>jlll><CR>')
     30    -- Test shift-left of a block
     31    feed('G$hhhh<C-v>kk<lt>')
     32    -- Test block-insert
     33    feed('Gkl<C-v>kkkIxyz<ESC>')
     34    -- Test block-replace
     35    feed('Gllll<C-v>kkklllrq')
     36    -- Test block-change
     37    feed('G$khhh<C-v>hhkkcmno<ESC>')
     38 
     39    expect([[
     40      axyzbcdefghijklm
     41      axyzqqqq   mno	      ghijklm
     42      axyzqqqqef mno        ghijklm
     43      axyzqqqqefgmnoklm
     44      abcdqqqqijklm]])
     45  end)
     46 
     47  -- luacheck: ignore 611 (Line contains only whitespaces)
     48  it('should insert a block using cursor keys for movement', function()
     49    insert([[
     50      aaaaaa
     51      bbbbbb
     52      cccccc
     53      dddddd
     54      
     55      xaaa
     56      bbbb
     57      cccc
     58      dddd]])
     59 
     60    feed_command('/^aa')
     61    feed('l<C-v>jjjlllI<Right><Right>  <ESC>')
     62    feed_command('/xaaa$')
     63    feed('<C-v>jjjI<lt>><Left>p<ESC>')
     64 
     65    expect([[
     66      aaa  aaa
     67      bbb  bbb
     68      ccc  ccc
     69      ddd  ddd
     70      
     71      <p>xaaa
     72      <p>bbbb
     73      <p>cccc
     74      <p>dddd]])
     75  end)
     76 
     77  it('should create a block', function()
     78    insert([[
     79      A23
     80      4567
     81      
     82      B23
     83      4567
     84      
     85      C23
     86      4567]])
     87 
     88    -- Test for Visual block was created with the last <C-v>$.
     89    feed_command('/^A23$/')
     90    feed('l<C-v>j$Aab<ESC>')
     91    -- Test for Visual block was created with the middle <C-v>$ (1).
     92    feed_command('/^B23$/')
     93    feed('l<C-v>j$hAab<ESC>')
     94    -- Test for Visual block was created with the middle <C-v>$ (2).
     95    feed_command('/^C23$/')
     96    feed('l<C-v>j$hhAab<ESC>')
     97 
     98    expect([[
     99      A23ab
    100      4567ab
    101      
    102      B23 ab
    103      4567ab
    104      
    105      C23ab
    106      456ab7]])
    107  end)
    108 
    109  -- luacheck: ignore 621 (Indentation)
    110  it('should insert and append a block when virtualedit=all', function()
    111    insert([[
    112      		line1
    113      		line2
    114      		line3
    115      .
    116    ]])
    117 
    118    -- Test for Visual block insert when virtualedit=all and utf-8 encoding.
    119    feed_command('set ve=all')
    120    feed_command('/\t\tline')
    121    feed('07l<C-v>jjIx<ESC>')
    122 
    123    expect([[
    124             x 	line1
    125             x 	line2
    126             x 	line3
    127      .
    128    ]])
    129 
    130    -- Test for Visual block append when virtualedit=all.
    131    feed('012l<C-v>jjAx<ESC>')
    132 
    133    expect([[
    134             x     x   line1
    135             x     x   line2
    136             x     x   line3
    137      .
    138    ]])
    139  end)
    140 
    141  it('should make a selected part uppercase', function()
    142    -- GUe must uppercase a whole word, also when ß changes to ẞ.
    143    feed('Gothe youtußeuu end<ESC>Ypk0wgUe<CR>')
    144    -- GUfx must uppercase until x, inclusive.
    145    feed('O- youßtußexu -<ESC>0fogUfx<CR>')
    146    -- VU must uppercase a whole line.
    147    feed('YpkVU<CR>')
    148    -- Same, when it's the last line in the buffer.
    149    feed('YPGi111<ESC>VUddP<CR>')
    150    -- Uppercase two lines.
    151    feed('Oblah di<CR>')
    152    feed('doh dut<ESC>VkUj<CR>')
    153    -- Uppercase part of two lines.
    154    feed('ddppi333<ESC>k0i222<esc>fyllvjfuUk<CR>')
    155 
    156    expect([[
    157      
    158      the YOUTUẞEUU end
    159      - yOUẞTUẞEXu -
    160      THE YOUTUẞEUU END
    161      111THE YOUTUẞEUU END
    162      BLAH DI
    163      DOH DUT
    164      222the yoUTUẞEUU END
    165      333THE YOUTUßeuu end]])
    166  end)
    167 
    168  it('should replace using Enter or NL', function()
    169    -- Visual replace using Enter or NL.
    170    feed('G3o123456789<ESC>2k05l<C-v>2jr<CR>')
    171    feed('G3o98765<ESC>2k02l<C-v>2jr<C-v><CR>')
    172    feed('G3o123456789<ESC>2k05l<C-v>2jr<CR>')
    173    feed('G3o98765<ESC>2k02l<C-v>2jr<C-v><Nul>')
    174 
    175    local expected = [[
    176      
    177      12345
    178      789
    179      12345
    180      789
    181      12345
    182      789
    183      98<CR>65
    184      98<CR>65
    185      98<CR>65
    186      12345
    187      789
    188      12345
    189      789
    190      12345
    191      789
    192      98<Nul>65
    193      98<Nul>65
    194      98<Nul>65]]
    195    expected = expected:gsub('<CR>', '\r')
    196    expected = expected:gsub('<Nul>', '\000')
    197 
    198    expect(expected)
    199  end)
    200 
    201  it('should treat cursor position correctly when virtualedit=block', function()
    202    insert([[
    203      12345
    204      789
    205      98765]])
    206 
    207    -- Test cursor position. When virtualedit=block and Visual block mode and $gj.
    208    feed_command('set ve=block')
    209    feed('G2l')
    210    feed('2k<C-v>$gj<ESC>')
    211    feed_command([[let cpos=getpos("'>")]])
    212    local cpos = api.nvim_get_var('cpos')
    213    local expected = {
    214      col = 4,
    215      off = 0,
    216    }
    217    local actual = {
    218      col = cpos[3],
    219      off = cpos[4],
    220    }
    221 
    222    eq(expected, actual)
    223  end)
    224 
    225  it('should replace spaces in front of the block with tabs', function()
    226    insert([[
    227      #define BO_ALL	    0x0001
    228      #define BO_BS	    0x0002
    229      #define BO_CRSR	    0x0004]])
    230 
    231    -- Block_insert when replacing spaces in front of the block with tabs.
    232    feed_command('set ts=8 sts=4 sw=4')
    233    feed('ggf0<C-v>2jI<TAB><ESC>')
    234 
    235    expect([[
    236      #define BO_ALL		0x0001
    237      #define BO_BS	    	0x0002
    238      #define BO_CRSR	    	0x0004]])
    239  end)
    240 end)