robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. Keyesfixfix(cmdatom): repeat Visual <Cmd>; CmdFrame + stage #41355f6bf814378

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