robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
15.1 KiB526 linesraw
1-- Insert-mode tests.
2
3local t = require('test.testutil')
4local n = require('test.functional.testnvim')()
5local Screen = require('test.functional.ui.screen')
6
7local describe, it, before_each = t.describe, t.it, t.before_each
8local clear, feed, insert = n.clear, n.feed, n.insert
9local expect = n.expect
10local command = n.command
11local eq = t.eq
12local eval = n.eval
13local curbuf_contents = n.curbuf_contents
14local api = n.api
15
16describe('insert-mode', function()
17  before_each(function()
18    clear()
19  end)
20
21  it('indents only once after "!" keys #12894', function()
22    command('let counter = []')
23    command('set indentexpr=len(add(counter,0))')
24    feed('i<C-F>x')
25    eq(' x', curbuf_contents())
26  end)
27
28  it('indent works properly with autocompletion enabled #35381', function()
29    command('set autoindent cindent autocomplete')
30    feed('ivoid func(void) {<CR>')
31    expect('void func(void) {\n\t')
32    feed('}')
33    expect('void func(void) {\n}')
34  end)
35
36  it('CTRL-@', function()
37    -- Inserts last-inserted text, leaves insert-mode.
38    insert('hello')
39    feed('i<C-@>x')
40    expect('hellhello')
41
42    -- C-Space is the same as C-@.
43    -- CTRL-SPC inserts last-inserted text, leaves insert-mode.
44    feed('i<C-Space>x')
45    expect('hellhellhello')
46
47    -- CTRL-A inserts last inserted text
48    feed('i<C-A>x')
49    expect('hellhellhellhelloxo')
50  end)
51
52  it('InsertCharPre is not triggered for stuffed text (redo/dot-repeat) #25296', function()
53    n.exec_lua([[
54      _G.n = 0
55      vim.api.nvim_create_autocmd('InsertCharPre', {
56        callback = function()
57          _G.n = _G.n + 1
58          if vim.v.char == '(' then
59            vim.v.char = '()'
60          end
61        end,
62      })
63    ]])
64    local function calls()
65      return n.exec_lua('return _G.n')
66    end
67    api.nvim_buf_set_lines(0, 0, -1, true, { 'a', 'b', 'c', 'd' })
68    feed('gg0i(<Esc>')
69    eq(1, calls())
70
71    -- Dot-repeat ("redo") inserts literally (no InsertCharPre).
72    feed('j.')
73    expect([[
74      ()a
75      ()b
76      c
77      d]])
78    eq(1, calls())
79
80    -- i_CTRL-R, i_CTRL-A inserts as stuffed text (no InsertCharPre).
81    n.fn.setreg('a', '(')
82    feed('ji<C-R>a<Esc>')
83    feed('ji<C-A><Esc>')
84    expect([[
85      ()a
86      ()b
87      (c
88      (d]])
89    eq(1, calls())
90
91    -- Macro keys are typeahead, not stuffed, thus trigger InsertCharPre.
92    n.fn.setreg('q', 'A(\27')
93    feed('gg@q')
94    expect([[
95      ()a()
96      ()b
97      (c
98      (d]])
99    eq(2, calls())
100  end)
101
102  describe('Ctrl-R', function()
103    it('works', function()
104      command("let @@ = 'test'")
105      feed('i<C-r>"')
106      expect('test')
107    end)
108
109    it('works with multi-byte text', function()
110      command("let @@ = 'påskägg'")
111      feed('i<C-r>"')
112      expect('påskägg')
113    end)
114
115    it('double quote is removed after hit-enter prompt #22609', function()
116      local screen = Screen.new(50, 6)
117      feed('i<C-R>')
118      screen:expect([[
119        {18:^"}                                                 |
120        {1:~                                                 }|*4
121        {5:-- INSERT --}                                      |
122      ]])
123      feed("=function('add')")
124      screen:expect([[
125        {18:"}                                                 |
126        {1:~                                                 }|*4
127        ={25:function}{16:(}{26:'add'}{16:)}^                                  |
128      ]])
129      feed('<CR>')
130      screen:expect([[
131        {18:"}                                                 |
132        {1:~                                                 }|
133        {3:                                                  }|
134        ={25:function}{16:(}{26:'add'}{16:)}                                  |
135        {9:E729: Using a Funcref as a String}                 |
136        {6:Press ENTER or type command to continue}^           |
137      ]])
138      feed('<CR>')
139      screen:expect([[
140        ^                                                  |
141        {1:~                                                 }|*4
142        {5:-- INSERT --}                                      |
143      ]])
144    end)
145
146    it('inserts named/clipboard registers literally', function()
147      local screen = Screen.new(50, 6)
148      -- regular text without special character command
149      command('let @a = "test"')
150      feed('i<C-R>a<ESC>')
151      screen:expect([[
152        tes^t                                              |
153        {1:~                                                 }|*4
154                                                          |
155      ]])
156
157      -- text with backspace character gets written literally by default
158      command('let @a = "test\\<C-H>"')
159      feed('cc<C-R>a<ESC>')
160      screen:expect([[
161        test{18:^^H}                                            |
162        {1:~                                                 }|*4
163                                                          |
164      ]])
165
166      -- =@reg<CR> can be used to get effect of keypress
167      command('let @a = "test\\<C-H>"')
168      feed('cc<C-R>=@a<CR><ESC>')
169      screen:expect([[
170        te^s                                               |
171        {1:~                                                 }|*4
172                                                          |
173      ]])
174    end)
175  end)
176
177  describe('Ctrl-O', function()
178    it('enters command mode for one command', function()
179      feed('ihello world<C-o>')
180      feed(':let ctrlo = "test"<CR>')
181      feed('iii')
182      expect('hello worldiii')
183      eq(1, eval('ctrlo ==# "test"'))
184    end)
185
186    it('re-enters insert mode at the end of the line when running startinsert', function()
187      -- #6962
188      feed('ihello world<C-o>')
189      feed(':startinsert<CR>')
190      feed('iii')
191      expect('hello worldiii')
192    end)
193
194    it('re-enters insert mode at the beginning of the line when running startinsert', function()
195      insert('hello world')
196      feed('0<C-o>')
197      feed(':startinsert<CR>')
198      feed('aaa')
199      expect('aaahello world')
200    end)
201
202    it('re-enters insert mode in the middle of the line when running startinsert', function()
203      insert('hello world')
204      feed('bi<C-o>')
205      feed(':startinsert<CR>')
206      feed('ooo')
207      expect('hello oooworld')
208    end)
209  end)
210
211  describe('Ctrl-V', function()
212    it('supports entering the decimal value of a character', function()
213      feed('i<C-V>076<C-V>167')
214      expect('L§')
215    end)
216
217    it('supports entering the octal value of a character with "o"', function()
218      feed('i<C-V>o114<C-V>o247<Esc>')
219      expect('L§')
220    end)
221
222    it('supports entering the octal value of a character with "O"', function()
223      feed('i<C-V>O114<C-V>O247<Esc>')
224      expect('L§')
225    end)
226
227    it('supports entering the hexadecimal value of a character with "x"', function()
228      feed('i<C-V>x4c<C-V>xA7<Esc>')
229      expect('L§')
230    end)
231
232    it('supports entering the hexadecimal value of a character with "X"', function()
233      feed('i<C-V>X4c<C-V>XA7<Esc>')
234      expect('L§')
235    end)
236
237    it('supports entering the hexadecimal value of a character with "u"', function()
238      feed('i<C-V>u25ba<C-V>u25C7<Esc>')
239      expect('►◇')
240    end)
241
242    it('supports entering the hexadecimal value of a character with "U"', function()
243      feed('i<C-V>U0001f600<C-V>U0001F601<Esc>')
244      expect('😀😁')
245    end)
246
247    it('entering character by value is interrupted by invalid character', function()
248      feed('i<C-V>76c<C-V>76<C-F2><C-V>u3c0j<C-V>u3c0<M-F3><C-V>U1f600j<C-V>U1f600<D-F4><Esc>')
249      expect('LcL<C-F2>πjπ<M-F3>😀j😀<D-F4>')
250    end)
251
252    it('shows o, O, u, U, x, X, and digits with modifiers', function()
253      feed('i<C-V><M-o><C-V><D-o><C-V><M-O><C-V><D-O><Esc>')
254      expect('<M-o><D-o><M-O><D-O>')
255      feed('cc<C-V><M-u><C-V><D-u><C-V><M-U><C-V><D-U><Esc>')
256      expect('<M-u><D-u><M-U><D-U>')
257      feed('cc<C-V><M-x><C-V><D-x><C-V><M-X><C-V><D-X><Esc>')
258      expect('<M-x><D-x><M-X><D-X>')
259      feed('cc<C-V><M-1><C-V><D-2><C-V><M-7><C-V><D-8><Esc>')
260      expect('<M-1><D-2><M-7><D-8>')
261    end)
262  end)
263
264  it('Ctrl-Shift-V supports entering unsimplified key notations', function()
265    feed('i<C-S-V><C-J><C-S-V><C-@><C-S-V><C-[><C-S-V><C-S-M><C-S-V><M-C-I><C-S-V><C-D-J><Esc>')
266    expect('<C-J><C-@><C-[><C-S-M><M-C-I><C-D-J>')
267  end)
268
269  it('multi-char mapping updates screen properly #25626', function()
270    local screen = Screen.new(60, 6)
271    command('vnew')
272    insert('foo\nfoo\nfoo')
273    command('wincmd w')
274    command('set timeoutlen=10000')
275    command('inoremap jk <Esc>')
276    feed('i<CR>βββ<Left><Left>j')
277    screen:expect {
278      grid = [[
279      foo                           │                             |
280      foo                           │β^jβ                          |
281      foo                           │{1:~                            }|
282      {1:~                             }│{1:~                            }|
283      {2:[No Name] [+]                  }{3:[No Name] [+]                }|
284      {5:-- INSERT --}                                                |
285    ]],
286    }
287    feed('k')
288    screen:expect {
289      grid = [[
290      foo                           │                             |
291      foo                           │^βββ                          |
292      foo                           │{1:~                            }|
293      {1:~                             }│{1:~                            }|
294      {2:[No Name] [+]                  }{3:[No Name] [+]                }|
295                                                                  |
296    ]],
297    }
298  end)
299
300  describe('backspace', function()
301    local function set_lines(line_b, line_e, ...)
302      api.nvim_buf_set_lines(0, line_b, line_e, true, { ... })
303    end
304    local function s(count)
305      return (' '):rep(count)
306    end
307
308    local function test_cols(expected_cols)
309      local cols = { { n.fn.col('.'), n.fn.virtcol('.') } }
310      for _ = 2, #expected_cols do
311        feed('<BS>')
312        table.insert(cols, { n.fn.col('.'), n.fn.virtcol('.') })
313      end
314      eq(expected_cols, cols)
315    end
316
317    it('works with tabs and spaces', function()
318      local _ = Screen.new(30, 2)
319      command('setl ts=4 sw=4')
320      set_lines(0, 1, '\t' .. s(4) .. '\t' .. s(9) .. '\t a')
321      feed('$i')
322      test_cols({
323        { 18, 26 },
324        { 17, 25 },
325        { 15, 21 },
326        { 11, 17 },
327        { 7, 13 },
328        { 6, 9 },
329        { 2, 5 },
330        { 1, 1 },
331      })
332    end)
333
334    it('works with varsofttabstop', function()
335      local _ = Screen.new(30, 2)
336      command('setl vsts=6,2,5,3')
337      set_lines(0, 1, 'a\t' .. s(4) .. '\t a')
338      feed('$i')
339      test_cols({
340        { 9, 18 },
341        { 8, 17 },
342        { 8, 14 },
343        { 3, 9 },
344        { 7, 7 },
345        { 2, 2 },
346        { 1, 1 },
347      })
348    end)
349
350    it('works with tab as ^I', function()
351      local _ = Screen.new(30, 2)
352      command('set list listchars=space:.')
353      command('setl ts=4 sw=4')
354      set_lines(0, 1, '\t' .. s(4) .. '\t' .. s(9) .. '\t a')
355      feed('$i')
356      test_cols({
357        { 18, 21 },
358        { 15, 17 },
359        { 11, 13 },
360        { 7, 9 },
361        { 4, 5 },
362        { 1, 1 },
363      })
364    end)
365
366    it('works in replace mode', function()
367      local _ = Screen.new(50, 2)
368      command('setl ts=8 sw=8 sts=8')
369      set_lines(0, 1, '\t' .. s(4) .. '\t' .. s(9) .. '\t a')
370      feed('$R')
371      test_cols({
372        { 18, 34 },
373        { 17, 33 },
374        { 15, 25 },
375        { 7, 17 },
376        { 2, 9 },
377        { 1, 8 }, -- last screen cell of first tab is at vcol 8
378      })
379    end)
380
381    it('works with breakindent', function()
382      local _ = Screen.new(17, 4)
383      command('setl ts=4 sw=4 bri briopt=min:5')
384      set_lines(0, 1, '\t' .. s(4) .. '\t' .. s(9) .. '\t a')
385      feed('$i')
386      test_cols({
387        { 18, 50 },
388        { 17, 49 },
389        { 15, 33 },
390        { 11, 17 },
391        { 7, 13 },
392        { 6, 9 },
393        { 2, 5 },
394        { 1, 1 },
395      })
396    end)
397
398    it('works with inline virtual text', function()
399      local _ = Screen.new(50, 2)
400      command('setl ts=4 sw=4')
401      set_lines(0, 1, '\t' .. s(4) .. '\t' .. s(9) .. '\t a')
402      local ns = api.nvim_create_namespace('')
403      local vt_opts = { virt_text = { { 'text' } }, virt_text_pos = 'inline' }
404      api.nvim_buf_set_extmark(0, ns, 0, 2, vt_opts)
405      feed('$i')
406      test_cols({
407        { 18, 30 },
408        { 17, 29 },
409        { 15, 25 },
410        { 11, 21 },
411        { 7, 17 },
412        { 6, 13 },
413        { 2, 9 },
414        { 1, 5 },
415      })
416    end)
417
418    it("works with 'revins'", function()
419      local _ = Screen.new(30, 3)
420      command('setl ts=4 sw=4 revins')
421      set_lines(0, 1, ('a'):rep(16), s(3) .. '\t' .. s(4) .. '\t a')
422      feed('j$i')
423      test_cols({
424        { 11, 14 },
425        { 10, 13 },
426        { 9, 9 },
427        { 5, 5 },
428        { 1, 1 },
429        { 1, 1 }, -- backspace on empty line does nothing
430      })
431      eq(2, api.nvim_win_get_cursor(0)[1])
432    end)
433  end)
434
435  it('backspace after replacing multibyte chars', function()
436    local screen = Screen.new(30, 3)
437    api.nvim_buf_set_lines(0, 0, -1, true, { 'test ȧ̟̜̝̅̚m̆̉̐̐̇̈ å' })
438    feed('^Rabcdefghi')
439    screen:expect([[
440      abcdefghi^                     |
441      {1:~                             }|
442      {5:-- REPLACE --}                 |
443    ]])
444
445    feed('<bs>')
446    screen:expect([[
447      abcdefgh^å                     |
448      {1:~                             }|
449      {5:-- REPLACE --}                 |
450    ]])
451
452    feed('<bs>')
453    screen:expect([[
454      abcdefg^ å                     |
455      {1:~                             }|
456      {5:-- REPLACE --}                 |
457    ]])
458
459    feed('<bs>')
460    screen:expect([[
461      abcdef^m̆̉̐̐̇̈ å                     |
462      {1:~                             }|
463      {5:-- REPLACE --}                 |
464    ]])
465
466    feed('<bs>')
467    screen:expect([[
468      abcde^ȧ̟̜̝̅̚m̆̉̐̐̇̈ å                     |
469      {1:~                             }|
470      {5:-- REPLACE --}                 |
471    ]])
472
473    feed('<bs>')
474    screen:expect([[
475      abcd^ ȧ̟̜̝̅̚m̆̉̐̐̇̈ å                     |
476      {1:~                             }|
477      {5:-- REPLACE --}                 |
478    ]])
479
480    feed('<esc>')
481
482    api.nvim_buf_set_lines(0, 0, -1, true, { 'wow 🧑‍🌾🏳️‍⚧️x' })
483    feed('^Rabcd')
484
485    screen:expect([[
486      abcd^🧑‍🌾🏳️‍⚧️x                     |
487      {1:~                             }|
488      {5:-- REPLACE --}                 |
489    ]])
490
491    feed('e')
492    screen:expect([[
493      abcde^🏳️‍⚧️x                      |
494      {1:~                             }|
495      {5:-- REPLACE --}                 |
496    ]])
497
498    feed('f')
499    screen:expect([[
500      abcdef^x                       |
501      {1:~                             }|
502      {5:-- REPLACE --}                 |
503    ]])
504
505    feed('<bs>')
506    screen:expect([[
507      abcde^🏳️‍⚧️x                      |
508      {1:~                             }|
509      {5:-- REPLACE --}                 |
510    ]])
511
512    feed('<bs>')
513    screen:expect([[
514      abcd^🧑‍🌾🏳️‍⚧️x                     |
515      {1:~                             }|
516      {5:-- REPLACE --}                 |
517    ]])
518
519    feed('<bs>')
520    screen:expect([[
521      abc^ 🧑‍🌾🏳️‍⚧️x                     |
522      {1:~                             }|
523      {5:-- REPLACE --}                 |
524    ]])
525  end)
526end)