neovim

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

094_visual_mode_operators_spec.lua (8263B)


      1 -- Test for Visual mode and operators.
      2 --
      3 -- Tests for the two kinds of operations: Those executed with Visual mode
      4 -- followed by an operator and those executed via Operator-pending mode. Also
      5 -- part of the test are mappings, counts, and repetition with the . command.
      6 
      7 local n = require('test.functional.testnvim')()
      8 
      9 local feed, insert, source = n.feed, n.insert, n.source
     10 local clear, feed_command, expect = n.clear, n.feed_command, n.expect
     11 
     12 -- Vim script user functions needed for some of the test cases.
     13 local function source_user_functions()
     14  source([[
     15    function MoveToCap()
     16      call search('\u', 'W')
     17    endfunction
     18    function SelectInCaps()
     19      let [line1, col1] = searchpos('\u', 'bcnW')
     20      let [line2, col2] = searchpos('.\u', 'nW')
     21      call setpos("'<", [0, line1, col1, 0])
     22      call setpos("'>", [0, line2, col2, 0])
     23      normal! gv
     24    endfunction
     25  ]])
     26 end
     27 
     28 local function put_abc()
     29  source([[
     30    $put ='a'
     31    $put ='b'
     32    $put ='c']])
     33 end
     34 
     35 local function put_aaabbbccc()
     36  source([[
     37    $put ='aaa'
     38    $put ='bbb'
     39    $put ='ccc']])
     40 end
     41 
     42 local function define_select_mode_maps()
     43  source([[
     44    snoremap <lt>End> <End>
     45    snoremap <lt>Down> <Down>
     46    snoremap <lt>Del> <Del>]])
     47 end
     48 
     49 describe('Visual mode and operator', function()
     50  before_each(function()
     51    clear()
     52    source_user_functions()
     53  end)
     54 
     55  it('simple change in Visual mode', function()
     56    insert([[
     57      apple banana cherry
     58 
     59      line 1 line 1
     60      line 2 line 2
     61      line 3 line 3
     62      line 4 line 4
     63      line 5 line 5
     64      line 6 line 6
     65 
     66      xxxxxxxxxxxxx
     67      xxxxxxxxxxxxx
     68      xxxxxxxxxxxxx
     69      xxxxxxxxxxxxx]])
     70 
     71    -- Exercise characterwise Visual mode plus operator, with count and repeat.
     72    feed_command('/^apple')
     73    feed('lvld.l3vd.')
     74 
     75    -- Same in linewise Visual mode.
     76    feed_command('/^line 1')
     77    feed('Vcnewline<esc>j.j2Vd.')
     78 
     79    -- Same in blockwise Visual mode.
     80    feed_command('/^xxxx')
     81    feed('<c-v>jlc  <esc>l.l2<c-v>c----<esc>l.')
     82 
     83    -- Assert buffer contents.
     84    expect([[
     85      a y
     86 
     87      newline
     88      newline
     89 
     90          --------x
     91          --------x
     92      xxxx--------x
     93      xxxx--------x]])
     94  end)
     95 
     96  it('Visual mode mapping', function()
     97    insert([[
     98      KiwiRaspberryDateWatermelonPeach
     99      JambuRambutanBananaTangerineMango]])
    100 
    101    -- Set up Visual mode mappings.
    102    feed_command('vnoremap W /\\u/s-1<CR>')
    103    feed_command('vnoremap iW :<C-U>call SelectInCaps()<CR>')
    104 
    105    -- Do a simple change using the simple vmap, also with count and repeat.
    106    feed_command('/^Kiwi')
    107    feed('vWcNo<esc>l.fD2vd.')
    108 
    109    -- Same, using the vmap that maps to an Ex command.
    110    feed_command('/^Jambu')
    111    feed('llviWc-<esc>l.l2vdl.')
    112 
    113    -- Assert buffer contents.
    114    expect([[
    115      NoNoberryach
    116      --ago]])
    117  end)
    118 
    119  it('Operator-pending mode mapping', function()
    120    insert([[
    121      PineappleQuinceLoganberryOrangeGrapefruitKiwiZ
    122      JuniperDurianZ
    123      LemonNectarineZ]])
    124 
    125    -- Set up Operator-pending mode mappings.
    126    feed_command('onoremap W /\\u/<CR>')
    127    feed_command('onoremap <Leader>W :<C-U>call MoveToCap()<CR>')
    128    feed_command('onoremap iW :<C-U>call SelectInCaps()<CR>')
    129 
    130    -- Do a simple change using the simple omap, also with count and repeat.
    131    feed_command('/^Pineapple')
    132    feed('cW-<esc>l.l2.l.')
    133 
    134    -- Same, using the omap that maps to an Ex command to move the cursor.
    135    feed_command('/^Juniper')
    136    feed('g?\\WfD.')
    137 
    138    -- Same, using the omap that uses Ex and Visual mode (custom text object).
    139    feed_command('/^Lemon')
    140    feed('yiWPlciWNew<esc>fr.')
    141 
    142    -- Assert buffer contents.
    143    expect([[
    144      ----Z
    145      WhavcreQhevnaZ
    146      LemonNewNewZ]])
    147  end)
    148 
    149  -- Vim patch 7.3.879 addressed a bug where typing ":" (the start of an Ex
    150  -- command) in Operator-pending mode couldn't be aborted with Escape, the
    151  -- change operation implied by the operator was always executed.
    152  it('patch 7.3.879', function()
    153    insert([[
    154      zzzz
    155      zzzz]])
    156 
    157    -- Start a change operation consisting of operator plus Ex command, like
    158    -- "dV:..." etc., then either
    159    -- - complete the operation by pressing Enter: as a result the buffer is
    160    --   changed, taking into account the v/V/<c-v> modifier given; or
    161    -- - abort the operation by pressing Escape: no change to the buffer is
    162    --   carried out.
    163    feed_command('/^zzzz')
    164    feed([[dV:<cr>dv:<cr>:set noma | let v:errmsg = ''<cr>]])
    165    feed([[d:<cr>:set ma | put = v:errmsg =~# '^E21' ? 'ok' : 'failed'<cr>]])
    166    feed([[dv:<esc>dV:<esc>:set noma | let v:errmsg = ''<cr>]])
    167    feed([[d:<esc>:set ma | put = v:errmsg =~# '^E21' ? 'failed' : 'ok'<cr>]])
    168 
    169    -- Assert buffer contents.
    170    expect([[
    171      zzz
    172      ok
    173      ok]])
    174  end)
    175 
    176  describe('characterwise visual mode:', function()
    177    it('replace last line', function()
    178      source([[
    179        $put ='a'
    180        let @" = 'x']])
    181      feed('v$p')
    182 
    183      expect([[
    184 
    185        x]])
    186    end)
    187 
    188    it('delete middle line', function()
    189      put_abc()
    190      feed('kkv$d')
    191 
    192      expect([[
    193 
    194        b
    195        c]])
    196    end)
    197 
    198    it('delete middle two line', function()
    199      put_abc()
    200      feed('kkvj$d')
    201 
    202      expect([[
    203 
    204        c]])
    205    end)
    206 
    207    it('delete last line', function()
    208      put_abc()
    209      feed('v$d')
    210 
    211      expect([[
    212 
    213        a
    214        b
    215        ]])
    216    end)
    217 
    218    it('delete last two line', function()
    219      put_abc()
    220      feed('kvj$d')
    221 
    222      expect([[
    223 
    224        a
    225        ]])
    226    end)
    227  end)
    228 
    229  describe('characterwise select mode:', function()
    230    before_each(function()
    231      define_select_mode_maps()
    232    end)
    233 
    234    it('delete middle line', function()
    235      put_abc()
    236      feed('kkgh<End><Del>')
    237 
    238      expect([[
    239 
    240        b
    241        c]])
    242    end)
    243 
    244    it('delete middle two line', function()
    245      put_abc()
    246      feed('kkgh<Down><End><Del>')
    247 
    248      expect([[
    249 
    250        c]])
    251    end)
    252 
    253    it('delete last line', function()
    254      put_abc()
    255      feed('gh<End><Del>')
    256 
    257      expect([[
    258 
    259        a
    260        b
    261        ]])
    262    end)
    263 
    264    it('delete last two line', function()
    265      put_abc()
    266      feed('kgh<Down><End><Del>')
    267 
    268      expect([[
    269 
    270        a
    271        ]])
    272    end)
    273  end)
    274 
    275  describe('linewise select mode:', function()
    276    before_each(function()
    277      define_select_mode_maps()
    278    end)
    279 
    280    it('delete middle line', function()
    281      put_abc()
    282      feed(' kkgH<Del> ')
    283 
    284      expect([[
    285 
    286        b
    287        c]])
    288    end)
    289 
    290    it('delete middle two line', function()
    291      put_abc()
    292      feed('kkgH<Down><Del>')
    293 
    294      expect([[
    295 
    296        c]])
    297    end)
    298 
    299    it('delete last line', function()
    300      put_abc()
    301      feed('gH<Del>')
    302 
    303      expect([[
    304 
    305        a
    306        b]])
    307    end)
    308 
    309    it('delete last two line', function()
    310      put_abc()
    311      feed('kgH<Down><Del>')
    312 
    313      expect([[
    314 
    315        a]])
    316    end)
    317  end)
    318 
    319  describe('v_p:', function()
    320    it('replace last character with line register at middle line', function()
    321      put_aaabbbccc()
    322      feed_command('-2yank')
    323      feed('k$vp')
    324 
    325      expect([[
    326 
    327        aaa
    328        bb
    329        aaa
    330 
    331        ccc]])
    332    end)
    333 
    334    it('replace last character with line register at middle line selecting newline', function()
    335      put_aaabbbccc()
    336      feed_command('-2yank')
    337      feed('k$v$p')
    338 
    339      expect([[
    340 
    341        aaa
    342        bb
    343        aaa
    344        ccc]])
    345    end)
    346 
    347    it('replace last character with line register at last line', function()
    348      put_aaabbbccc()
    349      feed_command('-2yank')
    350      feed('$vp')
    351 
    352      expect([[
    353 
    354        aaa
    355        bbb
    356        cc
    357        aaa
    358        ]])
    359    end)
    360 
    361    it('replace last character with line register at last line selecting newline', function()
    362      put_aaabbbccc()
    363      feed_command('-2yank')
    364      feed('$v$p')
    365 
    366      expect([[
    367 
    368        aaa
    369        bbb
    370        cc
    371        aaa
    372        ]])
    373    end)
    374  end)
    375 
    376  -- luacheck: ignore 613 (Trailing whitespace in a string)
    377  it('gv in exclusive select mode after operation', function()
    378    source([[
    379      $put ='zzz '
    380      $put ='äà '
    381      set selection=exclusive]])
    382    feed('kv3lyjv3lpgvcxxx<Esc>')
    383 
    384    expect([[
    385 
    386      zzz 
    387      xxx ]])
    388  end)
    389 
    390  it('gv in exclusive select mode without operation', function()
    391    source([[
    392      $put ='zzz '
    393      set selection=exclusive]])
    394    feed('0v3l<Esc>gvcxxx<Esc>')
    395 
    396    expect([[
    397 
    398      xxx ]])
    399  end)
    400 end)