robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
84.9 KiB1975 linesraw
1local t = require('test.testutil')
2local describe, it, before_each, finally = t.describe, t.it, t.before_each, t.finally
3local pcall_err = t.pcall_err
4local n = require('test.functional.testnvim')()
5local Screen = require('test.functional.ui.screen')
6
7local assert_alive = n.assert_alive
8local clear, feed = n.clear, n.feed
9local eval, eq, ok = n.eval, t.eq, t.ok
10local source, expect = n.source, n.expect
11local fn = n.fn
12local command = n.command
13local api = n.api
14local poke_eventloop = n.poke_eventloop
15local exec_lua = n.exec_lua
16
17describe('completion', function()
18  local screen
19
20  before_each(function()
21    clear()
22    source([[
23      set completeopt-=noselect
24      " Avoid tags completion (if running test locally).
25      set complete-=t
26    ]])
27    screen = Screen.new(60, 8)
28    screen:add_extra_attr_ids {
29      [100] = { foreground = Screen.colors.Gray0, background = Screen.colors.Yellow },
30      [101] = { background = Screen.colors.Gray0 },
31      [102] = { foreground = Screen.colors.SeaGreen },
32    }
33  end)
34
35  it('ctrl-x_ctrl-f completes Windows drive letter', function()
36    t.skip(not t.is_os('win'), 'N/A for non-Windows')
37    feed('iblablaC:/W<C-x><C-f>')
38    screen:expect {
39      any = [[C:\Windows\]],
40    }
41  end)
42
43  describe('v:completed_item', function()
44    it('is empty dict until completion', function()
45      eq({}, eval('v:completed_item'))
46    end)
47    it('is empty dict if the candidate is not inserted', function()
48      feed('ifoo<ESC>o<C-x><C-n>')
49      screen:expect([[
50        foo                                                         |
51        foo^                                                         |
52        {1:~                                                           }|*5
53        {5:-- Keyword Local completion (^N^P) The only match}           |
54      ]])
55      feed('<C-e>')
56      screen:expect([[
57        foo                                                         |
58        ^                                                            |
59        {1:~                                                           }|*5
60        {5:-- INSERT --}                                                |
61      ]])
62      feed('<ESC>')
63      eq({}, eval('v:completed_item'))
64    end)
65    it('returns expected dict in normal completion', function()
66      feed('ifoo<ESC>o<C-x><C-n>')
67      eq('foo', eval('getline(2)'))
68      eq({
69        word = 'foo',
70        abbr = '',
71        menu = '',
72        info = '',
73        kind = '',
74        user_data = '',
75        abbr_hlgroup = '',
76        kind_hlgroup = '',
77      }, eval('v:completed_item'))
78    end)
79    it('is readonly', function()
80      screen:try_resize(80, 8)
81      feed('ifoo<ESC>o<C-x><C-n><ESC>')
82      t.matches('E46%: ', t.pcall_err(command, 'let v:completed_item.word = "bar"'))
83      t.matches('E46%: ', t.pcall_err(command, 'let v:completed_item.abbr = "bar"'))
84      t.matches('E46%: ', t.pcall_err(command, 'let v:completed_item.menu = "bar"'))
85      t.matches('E46%: ', t.pcall_err(command, 'let v:completed_item.info = "bar"'))
86      t.matches('E46%: ', t.pcall_err(command, 'let v:completed_item.kind = "bar"'))
87      t.matches('E46%: ', t.pcall_err(command, 'let v:completed_item.user_data = "bar"'))
88    end)
89    it('returns expected dict in omni completion', function()
90      source([[
91      function! TestOmni(findstart, base) abort
92        return a:findstart ? 0 : [{'word': 'foo', 'abbr': 'bar',
93        \ 'menu': 'baz', 'info': 'foobar', 'kind': 'foobaz'},
94        \ {'word': 'word', 'abbr': 'abbr', 'menu': 'menu',
95        \  'info': 'info', 'kind': 'kind'}]
96      endfunction
97      setlocal omnifunc=TestOmni
98      ]])
99      feed('i<C-x><C-o>')
100      eq('foo', eval('getline(1)'))
101      screen:expect([[
102        foo^                                                         |
103        {12:bar  foobaz baz  }{1:                                           }|
104        {4:abbr kind   menu }{1:                                           }|
105        {1:~                                                           }|*4
106        {5:-- Omni completion (^O^N^P) }{6:match 1 of 2}                    |
107      ]])
108      eq({
109        word = 'foo',
110        abbr = 'bar',
111        menu = 'baz',
112        info = 'foobar',
113        kind = 'foobaz',
114        abbr_hlgroup = '',
115        kind_hlgroup = '',
116        user_data = '',
117      }, eval('v:completed_item'))
118    end)
119  end)
120
121  describe('completeopt', function()
122    before_each(function()
123      source([[
124      function! TestComplete() abort
125        call complete(1, ['foo'])
126        return ''
127      endfunction
128      ]])
129    end)
130
131    it('inserts the first candidate if default', function()
132      command('set completeopt+=menuone')
133      feed('ifoo<ESC>o')
134      screen:expect([[
135        foo                                                         |
136        ^                                                            |
137        {1:~                                                           }|*5
138        {5:-- INSERT --}                                                |
139      ]])
140      feed('<C-x>')
141      -- the ^X prompt, only test this once
142      screen:expect([[
143        foo                                                         |
144        ^                                                            |
145        {1:~                                                           }|*5
146        {5:-- ^X mode (^]^D^E^F^I^K^L^N^O^P^Rs^U^V^Y)}                  |
147      ]])
148      feed('<C-n>')
149      screen:expect([[
150        foo                                                         |
151        foo^                                                         |
152        {12:foo            }{1:                                             }|
153        {1:~                                                           }|*4
154        {5:-- Keyword Local completion (^N^P) The only match}           |
155      ]])
156      feed('bar<ESC>')
157      eq('foobar', eval('getline(2)'))
158      feed('o<C-r>=TestComplete()<CR>')
159      screen:expect([[
160        foo                                                         |
161        foobar                                                      |
162        foo^                                                         |
163        {12:foo            }{1:                                             }|
164        {1:~                                                           }|*3
165        {5:-- INSERT --}                                                |
166      ]])
167      eq('foo', eval('getline(3)'))
168    end)
169    it('selects the first candidate if noinsert', function()
170      command('set completeopt+=menuone,noinsert')
171      feed('ifoo<ESC>o<C-x><C-n>')
172      screen:expect([[
173        foo                                                         |
174        ^                                                            |
175        {12:foo            }{1:                                             }|
176        {1:~                                                           }|*4
177        {5:-- Keyword Local completion (^N^P) The only match}           |
178      ]])
179      feed('<C-y>')
180      screen:expect([[
181        foo                                                         |
182        foo^                                                         |
183        {1:~                                                           }|*5
184        {5:-- INSERT --}                                                |
185      ]])
186      feed('<ESC>')
187      eq('foo', eval('getline(2)'))
188      feed('o<C-r>=TestComplete()<CR>')
189      screen:expect([[
190        foo                                                         |*2
191        ^                                                            |
192        {12:foo            }{1:                                             }|
193        {1:~                                                           }|*3
194        {5:-- INSERT --}                                                |
195      ]])
196      feed('<C-y><ESC>')
197      eq('foo', eval('getline(3)'))
198    end)
199    it('does not insert the first candidate if noselect', function()
200      command('set completeopt+=menuone,noselect')
201      feed('ifoo<ESC>o<C-x><C-n>')
202      screen:expect([[
203        foo                                                         |
204        ^                                                            |
205        {4:foo            }{1:                                             }|
206        {1:~                                                           }|*4
207        {5:-- Keyword Local completion (^N^P) }{19:Back at original}         |
208      ]])
209      feed('b')
210      screen:expect([[
211        foo                                                         |
212        b^                                                           |
213        {1:~                                                           }|*5
214        {5:-- Keyword Local completion (^N^P) }{19:Back at original}         |
215      ]])
216      feed('ar<ESC>')
217      eq('bar', eval('getline(2)'))
218      feed('o<C-r>=TestComplete()<CR>')
219      screen:expect([[
220        foo                                                         |
221        bar                                                         |
222        ^                                                            |
223        {4:foo            }{1:                                             }|
224        {1:~                                                           }|*3
225        {5:-- INSERT --}                                                |
226      ]])
227      feed('bar<ESC>')
228      eq('bar', eval('getline(3)'))
229    end)
230    it('does not select/insert the first candidate if noselect and noinsert', function()
231      command('set completeopt+=menuone,noselect,noinsert')
232      feed('ifoo<ESC>o<C-x><C-n>')
233      screen:expect([[
234        foo                                                         |
235        ^                                                            |
236        {4:foo            }{1:                                             }|
237        {1:~                                                           }|*4
238        {5:-- Keyword Local completion (^N^P) }{19:Back at original}         |
239      ]])
240      feed('<ESC>')
241      screen:expect([[
242        foo                                                         |
243        ^                                                            |
244        {1:~                                                           }|*5
245                                                                    |
246      ]])
247      eq('', eval('getline(2)'))
248      feed('o<C-r>=TestComplete()<CR>')
249      screen:expect([[
250        foo                                                         |
251                                                                    |
252        ^                                                            |
253        {4:foo            }{1:                                             }|
254        {1:~                                                           }|*3
255        {5:-- INSERT --}                                                |
256      ]])
257      feed('<ESC>')
258      screen:expect([[
259        foo                                                         |
260                                                                    |
261        ^                                                            |
262        {1:~                                                           }|*4
263                                                                    |
264      ]])
265      eq('', eval('getline(3)'))
266    end)
267    it('does not change modified state if noinsert', function()
268      command('set completeopt+=menuone,noinsert')
269      command('setlocal nomodified')
270      feed('i<C-r>=TestComplete()<CR><ESC>')
271      eq(0, eval('&l:modified'))
272    end)
273    it('does not change modified state if noselect', function()
274      command('set completeopt+=menuone,noselect')
275      command('setlocal nomodified')
276      feed('i<C-r>=TestComplete()<CR><ESC>')
277      eq(0, eval('&l:modified'))
278    end)
279
280    describe('"preselect"', function()
281      it('respects "preselect" of complete items', function()
282        source([[
283          function! TestComplete() abort
284            call complete(1, g:comp_items)
285            return ''
286          endfunction
287        ]])
288        local function complete(items)
289          api.nvim_set_var('comp_items', items)
290          feed('<Esc>S<C-r>=TestComplete()<CR>')
291        end
292        local items = {
293          { word = 'aaa' },
294          { word = 'bbb' },
295          { word = 'ccc', preselect = true },
296          { word = 'ddd' },
297        }
298        command('set completeopt=menuone,preselect')
299        complete(items)
300        screen:expect([[
301          ccc^                                                         |
302          {4:aaa            }{1:                                             }|
303          {4:bbb            }{1:                                             }|
304          {12:ccc            }{1:                                             }|
305          {4:ddd            }{1:                                             }|
306          {1:~                                                           }|*2
307          {5:-- INSERT --}                                                |
308        ]])
309
310        -- scrolls pum when preselected item is far down
311        command('set pumheight=5')
312        local many = {} ---@type table[]
313        for i = 0, 19 do
314          many[i + 1] = { word = 'item' .. i }
315        end
316        many[16].preselect = true ---@type boolean
317        complete(many)
318        screen:expect([[
319          item15^                                                      |
320          {4:item13         }{12: }{1:                                            }|
321          {4:item14         }{12: }{1:                                            }|
322          {12:item15          }{1:                                            }|
323          {4:item16         }{101: }{1:                                            }|
324          {4:item17         }{12: }{1:                                            }|
325          {1:~                                                           }|
326          {5:-- INSERT --}                                                |
327        ]])
328
329        -- preselected item is selected but not inserted with "noselect"
330        command('set completeopt=menuone,noselect,preselect pumheight=0')
331        complete(items)
332        screen:expect([[
333          ^                                                            |
334          {4:aaa            }{1:                                             }|
335          {4:bbb            }{1:                                             }|
336          {12:ccc            }{1:                                             }|
337          {4:ddd            }{1:                                             }|
338          {1:~                                                           }|*2
339          {5:-- INSERT --}                                                |
340        ]])
341
342        -- selects the first preselected item that still matches
343        complete({
344          { word = 'aaa', preselect = true },
345          { word = 'abb' },
346          { word = 'abc', preselect = true },
347        })
348        feed('ab<C-Y>')
349        eq('abc', api.nvim_get_current_line())
350
351        -- without "preselect" in 'completeopt' the attribute is ignored
352        command('set completeopt-=preselect')
353        complete(items)
354        feed('<C-N><C-Y>')
355        eq('aaa', api.nvim_get_current_line())
356      end)
357
358      it('does not insert text if noinsert', function()
359        source([[
360          function! TestComplete() abort
361            call complete(1, [{'word': 'aaa', 'preselect': v:true}, {'word': 'bbb'}])
362            return ''
363          endfunction
364        ]])
365        command('set completeopt=menuone,noinsert,preselect')
366        feed('i<C-r>=TestComplete()<CR>')
367        eq('', api.nvim_get_current_line())
368      end)
369    end)
370  end)
371
372  describe('completeopt+=noinsert does not add blank undo items', function()
373    before_each(function()
374      source([[
375      function! TestComplete() abort
376        call complete(1, ['foo', 'bar'])
377        return ''
378      endfunction
379      ]])
380      command('set completeopt+=noselect,noinsert')
381      command('inoremap <right> <c-r>=TestComplete()<cr>')
382    end)
383
384    local tests = {
385      ['<up>, <down>, <cr>'] = { '<down><cr>', '<up><cr>' },
386      ['<c-n>, <c-p>, <c-y>'] = { '<c-n><c-y>', '<c-p><c-y>' },
387    }
388
389    for name, seq in pairs(tests) do
390      it('using ' .. name, function()
391        feed('iaaa<esc>')
392        feed('A<right>' .. seq[1] .. '<esc>')
393        feed('A<right><esc>A<right><esc>')
394        feed('A<cr>bbb<esc>')
395        feed('A<right>' .. seq[2] .. '<esc>')
396        feed('A<right><esc>A<right><esc>')
397        feed('A<cr>ccc<esc>')
398        feed('A<right>' .. seq[1] .. '<esc>')
399        feed('A<right><esc>A<right><esc>')
400
401        local expected = {
402          { 'foo', 'bar', 'foo' },
403          { 'foo', 'bar', 'ccc' },
404          { 'foo', 'bar' },
405          { 'foo', 'bbb' },
406          { 'foo' },
407          { 'aaa' },
408          { '' },
409        }
410
411        for i = 1, #expected do
412          if i > 1 then
413            feed('u')
414          end
415          eq(expected[i], eval('getline(1, "$")'))
416        end
417
418        for i = #expected, 1, -1 do
419          if i < #expected then
420            feed('<c-r>')
421          end
422          eq(expected[i], eval('getline(1, "$")'))
423        end
424      end)
425    end
426  end)
427
428  describe('with refresh:always and noselect', function()
429    before_each(function()
430      source([[
431        function! TestCompletion(findstart, base) abort
432          if a:findstart
433            let line = getline('.')
434            let start = col('.') - 1
435            while start > 0 && line[start - 1] =~ '\a'
436              let start -= 1
437            endwhile
438            return start
439          else
440            let ret = []
441            for m in split("January February March April May June July August September October November December")
442              if m =~ a:base  " match by regex
443                call add(ret, m)
444              endif
445            endfor
446            return {'words':ret, 'refresh':'always'}
447          endif
448        endfunction
449
450        set completeopt=menuone,noselect
451        set completefunc=TestCompletion
452      ]])
453    end)
454
455    it('completes on each input char', function()
456      feed('i<C-x><C-u>')
457      screen:expect([[
458        ^                                                            |
459        {4:January        }{101: }{1:                                            }|
460        {4:February       }{101: }{1:                                            }|
461        {4:March          }{101: }{1:                                            }|
462        {4:April          }{12: }{1:                                            }|
463        {4:May            }{12: }{1:                                            }|
464        {4:June           }{12: }{1:                                            }|
465        {5:-- User defined completion (^U^N^P) }{19:Back at original}        |
466      ]])
467      feed('u')
468      screen:expect([[
469        u^                                                           |
470        {4:January        }{1:                                             }|
471        {4:February       }{1:                                             }|
472        {4:June           }{1:                                             }|
473        {4:July           }{1:                                             }|
474        {4:August         }{1:                                             }|
475        {1:~                                                           }|
476        {5:-- User defined completion (^U^N^P) }{19:Back at original}        |
477      ]])
478      feed('g')
479      screen:expect([[
480        ug^                                                          |
481        {4:August         }{1:                                             }|
482        {1:~                                                           }|*5
483        {5:-- User defined completion (^U^N^P) }{19:Back at original}        |
484      ]])
485      feed('<Down>')
486      screen:expect([[
487        ug^                                                          |
488        {12:August         }{1:                                             }|
489        {1:~                                                           }|*5
490        {5:-- User defined completion (^U^N^P) The only match}          |
491      ]])
492      feed('<C-y>')
493      screen:expect([[
494        August^                                                      |
495        {1:~                                                           }|*6
496        {5:-- INSERT --}                                                |
497      ]])
498      expect('August')
499    end)
500
501    it('repeats correctly after backspace #2674', function()
502      feed('o<C-x><C-u>Ja')
503      screen:expect([[
504                                                                    |
505        Ja^                                                          |
506        {4:January        }{1:                                             }|
507        {1:~                                                           }|*4
508        {5:-- User defined completion (^U^N^P) }{19:Back at original}        |
509      ]])
510      feed('<BS>')
511      screen:expect([[
512                                                                    |
513        J^                                                           |
514        {4:January        }{1:                                             }|
515        {4:June           }{1:                                             }|
516        {4:July           }{1:                                             }|
517        {1:~                                                           }|*2
518        {5:-- User defined completion (^U^N^P) }{19:Back at original}        |
519      ]])
520      feed('<C-n>')
521      screen:expect([[
522                                                                    |
523        January^                                                     |
524        {12:January        }{1:                                             }|
525        {4:June           }{1:                                             }|
526        {4:July           }{1:                                             }|
527        {1:~                                                           }|*2
528        {5:-- User defined completion (^U^N^P) }{6:match 1 of 3}            |
529      ]])
530      feed('<C-n>')
531      screen:expect([[
532                                                                    |
533        June^                                                        |
534        {4:January        }{1:                                             }|
535        {12:June           }{1:                                             }|
536        {4:July           }{1:                                             }|
537        {1:~                                                           }|*2
538        {5:-- User defined completion (^U^N^P) }{6:match 2 of 3}            |
539      ]])
540      feed('<Esc>')
541      screen:expect([[
542                                                                    |
543        Jun^e                                                        |
544        {1:~                                                           }|*5
545                                                                    |
546      ]])
547      feed('.')
548      screen:expect([[
549                                                                    |
550        June                                                        |
551        Jun^e                                                        |
552        {1:~                                                           }|*4
553                                                                    |
554      ]])
555      expect([[
556
557        June
558        June]])
559    end)
560
561    it('Enter inserts newline at original text after adding leader', function()
562      feed('iJ<C-x><C-u>')
563      poke_eventloop()
564      feed('u')
565      poke_eventloop()
566      feed('<CR>')
567      expect([[
568        Ju
569        ]])
570      feed('J<C-x><C-u>')
571      poke_eventloop()
572      feed('<CR>')
573      expect([[
574        Ju
575        J
576        ]])
577    end)
578  end)
579
580  describe('with noselect but not refresh:always', function()
581    before_each(function()
582      source([[
583        function! TestCompletion(findstart, base) abort
584          if a:findstart
585            let line = getline('.')
586            let start = col('.') - 1
587            while start > 0 && line[start - 1] =~ '\a'
588              let start -= 1
589            endwhile
590            return start
591          else
592            let ret = []
593            for m in split("January February March April May June July August September October November December")
594              if m =~ a:base  " match by regex
595                call add(ret, m)
596              endif
597            endfor
598            return {'words':ret}
599          endif
600        endfunction
601
602        set completeopt=menuone,noselect
603        set completefunc=TestCompletion
604      ]])
605    end)
606
607    it('Enter inserts newline at original text after adding leader', function()
608      feed('iJ<C-x><C-u>')
609      poke_eventloop()
610      feed('u')
611      poke_eventloop()
612      feed('<CR>')
613      expect([[
614        Ju
615        ]])
616      feed('<Esc>')
617      poke_eventloop()
618      -- The behavior should be the same when completion has been interrupted,
619      -- which can happen interactively if the completion function is slow.
620      feed('ggVGSJ<C-x><C-u>u<CR>')
621      expect([[
622        Ju
623        ]])
624    end)
625  end)
626
627  describe('with a lot of items', function()
628    before_each(function()
629      source([[
630      function! TestComplete() abort
631        call complete(1, map(range(0,100), "string(v:val)"))
632        return ''
633      endfunction
634      ]])
635      command('set completeopt=menuone,noselect')
636    end)
637
638    it('works', function()
639      feed('i<C-r>=TestComplete()<CR>')
640      screen:expect([[
641        ^                                                            |
642        {4:0              }{101: }{1:                                            }|
643        {4:1              }{12: }{1:                                            }|
644        {4:2              }{12: }{1:                                            }|
645        {4:3              }{12: }{1:                                            }|
646        {4:4              }{12: }{1:                                            }|
647        {4:5              }{12: }{1:                                            }|
648        {5:-- INSERT --}                                                |
649      ]])
650      feed('7')
651      screen:expect([[
652        7^                                                           |
653        {4:7              }{101: }{1:                                            }|
654        {4:70             }{101: }{1:                                            }|
655        {4:71             }{101: }{1:                                            }|
656        {4:72             }{12: }{1:                                            }|
657        {4:73             }{12: }{1:                                            }|
658        {4:74             }{12: }{1:                                            }|
659        {5:-- INSERT --}                                                |
660      ]])
661      feed('<c-n>')
662      screen:expect([[
663        7^                                                           |
664        {12:7              }{101: }{1:                                            }|
665        {4:70             }{101: }{1:                                            }|
666        {4:71             }{101: }{1:                                            }|
667        {4:72             }{12: }{1:                                            }|
668        {4:73             }{12: }{1:                                            }|
669        {4:74             }{12: }{1:                                            }|
670        {5:-- INSERT --}                                                |
671      ]])
672      feed('<c-n>')
673      screen:expect([[
674        70^                                                          |
675        {4:7              }{101: }{1:                                            }|
676        {12:70             }{101: }{1:                                            }|
677        {4:71             }{101: }{1:                                            }|
678        {4:72             }{12: }{1:                                            }|
679        {4:73             }{12: }{1:                                            }|
680        {4:74             }{12: }{1:                                            }|
681        {5:-- INSERT --}                                                |
682      ]])
683    end)
684
685    it('can be navigated with <PageDown>, <PageUp>', function()
686      feed('i<C-r>=TestComplete()<CR>')
687      screen:expect([[
688        ^                                                            |
689        {4:0              }{101: }{1:                                            }|
690        {4:1              }{12: }{1:                                            }|
691        {4:2              }{12: }{1:                                            }|
692        {4:3              }{12: }{1:                                            }|
693        {4:4              }{12: }{1:                                            }|
694        {4:5              }{12: }{1:                                            }|
695        {5:-- INSERT --}                                                |
696      ]])
697      feed('<PageDown>')
698      screen:expect([[
699        ^                                                            |
700        {4:0              }{101: }{1:                                            }|
701        {4:1              }{12: }{1:                                            }|
702        {4:2              }{12: }{1:                                            }|
703        {12:3               }{1:                                            }|
704        {4:4              }{12: }{1:                                            }|
705        {4:5              }{12: }{1:                                            }|
706        {5:-- INSERT --}                                                |
707      ]])
708      feed('<PageDown>')
709      screen:expect([[
710        ^                                                            |
711        {4:5              }{101: }{1:                                            }|
712        {4:6              }{12: }{1:                                            }|
713        {12:7               }{1:                                            }|
714        {4:8              }{12: }{1:                                            }|
715        {4:9              }{12: }{1:                                            }|
716        {4:10             }{12: }{1:                                            }|
717        {5:-- INSERT --}                                                |
718      ]])
719      feed('<Down>')
720      screen:expect([[
721        ^                                                            |
722        {4:5              }{101: }{1:                                            }|
723        {4:6              }{12: }{1:                                            }|
724        {4:7              }{12: }{1:                                            }|
725        {12:8               }{1:                                            }|
726        {4:9              }{12: }{1:                                            }|
727        {4:10             }{12: }{1:                                            }|
728        {5:-- INSERT --}                                                |
729      ]])
730      feed('<PageUp>')
731      screen:expect([[
732        ^                                                            |
733        {4:2              }{101: }{1:                                            }|
734        {4:3              }{12: }{1:                                            }|
735        {12:4               }{1:                                            }|
736        {4:5              }{12: }{1:                                            }|
737        {4:6              }{12: }{1:                                            }|
738        {4:7              }{12: }{1:                                            }|
739        {5:-- INSERT --}                                                |
740      ]])
741      feed('<PageUp>') -- stop on first item
742      screen:expect([[
743        ^                                                            |
744        {12:0              }{101: }{1:                                            }|
745        {4:1              }{12: }{1:                                            }|
746        {4:2              }{12: }{1:                                            }|
747        {4:3              }{12: }{1:                                            }|
748        {4:4              }{12: }{1:                                            }|
749        {4:5              }{12: }{1:                                            }|
750        {5:-- INSERT --}                                                |
751      ]])
752      feed('<PageUp>') -- when on first item, unselect
753      screen:expect([[
754        ^                                                            |
755        {4:0              }{101: }{1:                                            }|
756        {4:1              }{12: }{1:                                            }|
757        {4:2              }{12: }{1:                                            }|
758        {4:3              }{12: }{1:                                            }|
759        {4:4              }{12: }{1:                                            }|
760        {4:5              }{12: }{1:                                            }|
761        {5:-- INSERT --}                                                |
762      ]])
763      feed('<PageUp>') -- when unselected, select last item
764      screen:expect([[
765        ^                                                            |
766        {4:95             }{12: }{1:                                            }|
767        {4:96             }{12: }{1:                                            }|
768        {4:97             }{12: }{1:                                            }|
769        {4:98             }{12: }{1:                                            }|
770        {4:99             }{12: }{1:                                            }|
771        {12:100            }{101: }{1:                                            }|
772        {5:-- INSERT --}                                                |
773      ]])
774      feed('<PageUp>')
775      screen:expect([[
776        ^                                                            |
777        {4:94             }{12: }{1:                                            }|
778        {4:95             }{12: }{1:                                            }|
779        {12:96              }{1:                                            }|
780        {4:97             }{12: }{1:                                            }|
781        {4:98             }{12: }{1:                                            }|
782        {4:99             }{101: }{1:                                            }|
783        {5:-- INSERT --}                                                |
784      ]])
785      feed('<cr>')
786      screen:expect([[
787        96^                                                          |
788        {1:~                                                           }|*6
789        {5:-- INSERT --}                                                |
790      ]])
791    end)
792  end)
793
794  it('does not indent until an item is selected #8345', function()
795    -- Indents on "ind", unindents on "unind".
796    source([[
797      function! TestIndent()
798        let line = getline(v:lnum)
799        if (line =~ '^\s*ind')
800          return indent(v:lnum-1) + shiftwidth()
801        elseif (line =~ '^\s*unind')
802          return indent(v:lnum-1) - shiftwidth()
803        else
804          return indent(v:lnum-1)
805        endif
806      endfunction
807      set indentexpr=TestIndent()
808      set indentkeys=o,O,!^F,=ind,=unind
809      set completeopt+=menuone
810    ]])
811
812    -- Give some words to complete.
813    feed('iinc uninc indent unindent<CR>')
814
815    -- Does not indent when "ind" is typed.
816    feed('in<C-X><C-N>')
817    -- Completion list is generated incorrectly if we send everything at once
818    -- via nvim_input().  So poke_eventloop() before sending <BS>. #8480
819    poke_eventloop()
820    feed('<BS>d')
821
822    screen:expect([[
823      inc uninc indent unindent                                   |
824      ind^                                                         |
825      {12:indent         }{1:                                             }|
826      {1:~                                                           }|*4
827      {5:-- Keyword Local completion (^N^P) }{6:match 1 of 2}             |
828    ]])
829
830    -- Indents when the item is selected
831    feed('<C-Y>')
832    screen:expect([[
833      inc uninc indent unindent                                   |
834              indent^                                              |
835      {1:~                                                           }|*5
836      {5:-- INSERT --}                                                |
837    ]])
838    -- Indents when completion is exited using ESC.
839    feed('<CR>in<C-N><BS>d<Esc>')
840    screen:expect([[
841      inc uninc indent unindent                                   |
842              indent                                              |
843                      in^d                                         |
844      {1:~                                                           }|*4
845                                                                  |
846    ]])
847    -- Works for unindenting too.
848    feed('ounin<C-X><C-N>')
849    poke_eventloop()
850    feed('<BS>d')
851    screen:expect([[
852      inc uninc indent unindent                                   |
853              indent                                              |
854                      ind                                         |
855                      unind^                                       |
856      {1:~              }{12: unindent       }{1:                             }|
857      {1:~                                                           }|*2
858      {5:-- Keyword Local completion (^N^P) }{6:match 1 of 2}             |
859    ]])
860    -- Works when going back and forth.
861    feed('<BS>c')
862    screen:expect([[
863      inc uninc indent unindent                                   |
864              indent                                              |
865                      ind                                         |
866                      uninc^                                       |
867      {1:~              }{12: uninc          }{1:                             }|
868      {1:~                                                           }|*2
869      {5:-- Keyword Local completion (^N^P) }{6:match 1 of 2}             |
870    ]])
871    feed('<BS>d')
872    screen:expect([[
873      inc uninc indent unindent                                   |
874              indent                                              |
875                      ind                                         |
876                      unind^                                       |
877      {1:~              }{12: unindent       }{1:                             }|
878      {1:~                                                           }|*2
879      {5:-- Keyword Local completion (^N^P) }{6:match 1 of 2}             |
880    ]])
881    feed('<C-N><C-N><C-Y><Esc>')
882    screen:expect([[
883      inc uninc indent unindent                                   |
884              indent                                              |
885                      ind                                         |
886              uninden^t                                            |
887      {1:~                                                           }|*3
888                                                                  |
889    ]])
890  end)
891
892  it('disables folding during completion', function()
893    command('set foldmethod=indent')
894    feed('i<Tab>foo<CR><Tab>bar<Esc>gg')
895    screen:expect([[
896              ^foo                                                 |
897              bar                                                 |
898      {1:~                                                           }|*5
899                                                                  |
900    ]])
901    feed('A<C-x><C-l>')
902    screen:expect([[
903              foo^                                                 |
904              bar                                                 |
905      {1:~                                                           }|*5
906      {5:-- Whole line completion (^L^N^P) }{9:Pattern not found}         |
907    ]])
908    eq(-1, eval('foldclosed(1)'))
909  end)
910
911  it('popupmenu is not interrupted by events', function()
912    command('set complete=.')
913
914    feed('ifoobar fooegg<cr>f<c-p>')
915    screen:expect([[
916      foobar fooegg                                               |
917      fooegg^                                                      |
918      {4:foobar         }{1:                                             }|
919      {12:fooegg         }{1:                                             }|
920      {1:~                                                           }|*3
921      {5:-- Keyword completion (^N^P) }{6:match 1 of 2}                   |
922    ]])
923
924    assert_alive()
925    -- popupmenu still visible
926    screen:expect {
927      grid = [[
928      foobar fooegg                                               |
929      fooegg^                                                      |
930      {4:foobar         }{1:                                             }|
931      {12:fooegg         }{1:                                             }|
932      {1:~                                                           }|*3
933      {5:-- Keyword completion (^N^P) }{6:match 1 of 2}                   |
934    ]],
935      unchanged = true,
936    }
937
938    feed('<c-p>')
939    -- Didn't restart completion: old matches still used
940    screen:expect([[
941      foobar fooegg                                               |
942      foobar^                                                      |
943      {12:foobar         }{1:                                             }|
944      {4:fooegg         }{1:                                             }|
945      {1:~                                                           }|*3
946      {5:-- Keyword completion (^N^P) }{6:match 2 of 2}                   |
947    ]])
948  end)
949
950  describe('lua completion', function()
951    it('expands when there is only one match', function()
952      feed(':lua CURRENT_TESTING_VAR = 1<CR>')
953      feed(':lua CURRENT_TESTING_<TAB>')
954      screen:expect {
955        grid = [[
956                                                                    |
957        {1:~                                                           }|*6
958        :lua CURRENT_TESTING_VAR^                                    |
959      ]],
960      }
961    end)
962
963    it('expands when there is only one match', function()
964      feed(':lua CURRENT_TESTING_FOO = 1<CR>')
965      feed(':lua CURRENT_TESTING_BAR = 1<CR>')
966      feed(':lua CURRENT_TESTING_<TAB>')
967      screen:expect {
968        grid = [[
969                                                                    |
970        {1:~                                                           }|*5
971        {100:CURRENT_TESTING_BAR}{3:  CURRENT_TESTING_FOO                    }|
972        :lua CURRENT_TESTING_BAR^                                    |
973      ]],
974        unchanged = true,
975      }
976    end)
977
978    it('prefix is not included in completion for cmdline mode', function()
979      feed(':lua math.a<Tab>')
980      screen:expect([[
981                                                                    |
982        {1:~                                                           }|*5
983        {100:abs}{3:  acos  asin  atan  atan2                                }|
984        :lua math.abs^                                               |
985      ]])
986      feed('<Tab>')
987      screen:expect([[
988                                                                    |
989        {1:~                                                           }|*5
990        {3:abs  }{100:acos}{3:  asin  atan  atan2                                }|
991        :lua math.acos^                                              |
992      ]])
993    end)
994
995    it('prefix is not included in completion for i_CTRL-X_CTRL-V #19623', function()
996      feed('ilua math.a<C-X><C-V>')
997      screen:expect([[
998        lua math.abs^                                                |
999        {1:~       }{12: abs            }{1:                                    }|
1000        {1:~       }{4: acos           }{1:                                    }|
1001        {1:~       }{4: asin           }{1:                                    }|
1002        {1:~       }{4: atan           }{1:                                    }|
1003        {1:~       }{4: atan2          }{1:                                    }|
1004        {1:~                                                           }|
1005        {5:-- Command-line completion (^V^N^P) }{6:match 1 of 5}            |
1006      ]])
1007      feed('<C-V>')
1008      screen:expect([[
1009        lua math.acos^                                               |
1010        {1:~       }{4: abs            }{1:                                    }|
1011        {1:~       }{12: acos           }{1:                                    }|
1012        {1:~       }{4: asin           }{1:                                    }|
1013        {1:~       }{4: atan           }{1:                                    }|
1014        {1:~       }{4: atan2          }{1:                                    }|
1015        {1:~                                                           }|
1016        {5:-- Command-line completion (^V^N^P) }{6:match 2 of 5}            |
1017      ]])
1018    end)
1019
1020    it('works when cursor is in the middle of cmdline #29586', function()
1021      feed(':lua math.a(); 1<Left><Left><Left><Left><Left><Tab>')
1022      screen:expect([[
1023                                                                    |
1024        {1:~                                                           }|*5
1025        {100:abs}{3:  acos  asin  atan  atan2                                }|
1026        :lua math.abs^(); 1                                          |
1027      ]])
1028    end)
1029
1030    it('provides completion from `getcompletion()`', function()
1031      eq({ 'vim' }, fn.getcompletion('vi', 'lua'))
1032      eq({ 'api' }, fn.getcompletion('vim.ap', 'lua'))
1033      eq({ 'tbl_filter' }, fn.getcompletion('vim.tbl_fil', 'lua'))
1034      eq({ 'vim' }, fn.getcompletion('print(vi', 'lua'))
1035      eq({ 'abs', 'acos', 'asin', 'atan', 'atan2' }, fn.getcompletion('math.a', 'lua'))
1036      eq({ 'abs', 'acos', 'asin', 'atan', 'atan2' }, fn.getcompletion('lua math.a', 'cmdline'))
1037      -- fuzzy completion is not supported, so the result should be the same
1038      command('set wildoptions+=fuzzy')
1039      eq({ 'vim' }, fn.getcompletion('vi', 'lua'))
1040    end)
1041
1042    it('completes _defer_require() modules', function()
1043      -- vim.lsp.c<tab> -> vim.lsp.completion
1044      ok(vim.tbl_contains(fn.getcompletion('lua vim.lsp.c', 'cmdline'), 'completion'))
1045      -- vim.lsp.completion.g<tab> -> vim.lsp.completion.get
1046      ok(vim.tbl_contains(fn.getcompletion('lua vim.lsp.completion.g', 'cmdline'), 'get'))
1047    end)
1048  end)
1049
1050  it('cmdline completion supports various string options', function()
1051    eq('auto', fn.getcompletion('set foldcolumn=', 'cmdline')[2])
1052    eq({ 'nosplit', 'split' }, fn.getcompletion('set inccommand=', 'cmdline'))
1053    eq({ 'ver:3,hor:6', 'hor:', 'ver:' }, fn.getcompletion('set mousescroll=', 'cmdline'))
1054    eq('BS', fn.getcompletion('set termpastefilter=', 'cmdline')[2])
1055    eq('SpecialKey', fn.getcompletion('set winhighlight=', 'cmdline')[1])
1056    eq('SpecialKey', fn.getcompletion('set winhighlight=NonText:', 'cmdline')[1])
1057  end)
1058
1059  it('cmdline completion for -complete does not contain spaces', function()
1060    for _, str in ipairs(fn.getcompletion('command -complete=', 'cmdline')) do
1061      ok(not str:find(' '), 'string without spaces', str)
1062    end
1063  end)
1064
1065  it('cmdline completion for :restart', function()
1066    eq('qall', fn.getcompletion('restart +qa', 'cmdline')[1])
1067    eq('edit', fn.getcompletion('restart +qall ed', 'cmdline')[1])
1068    eq('edit', fn.getcompletion('restart ed', 'cmdline')[1])
1069  end)
1070
1071  it('cmdline completion for :*cd family shows dirs only', function()
1072    local dir = 'Xtest-cd-compl/'
1073    local subdir1 = dir .. 'subdir1/'
1074    local subdir2 = dir .. 'subdir2/'
1075    finally(function()
1076      n.rmdir(dir)
1077    end)
1078    fn.mkdir(subdir1, 'p')
1079    fn.mkdir(subdir2, 'p')
1080    fn.writefile({ '' }, dir .. 'file')
1081    for _, cmd in ipairs({ 'cd', 'lcd', 'tcd', 'bcd' }) do
1082      eq({ subdir1, subdir2 }, fn.getcompletion(cmd .. ' ' .. dir, 'cmdline'))
1083    end
1084  end)
1085
1086  describe('from the commandline window', function()
1087    it('is cleared after CTRL-C', function()
1088      feed('q:')
1089      feed('ifoo faa fee f')
1090      screen:expect([[
1091                                                                    |
1092        {2:[No Name]                                                   }|
1093        {1::}foo faa fee f^                                              |
1094        {1:~                                                           }|*3
1095        {3:[Command Line]                                              }|
1096        {5:-- INSERT --}                                                |
1097      ]])
1098      feed('<c-x><c-n>')
1099      screen:expect([[
1100                                                                    |
1101        {2:[No Name]                                                   }|
1102        {1::}foo faa fee foo^                                            |
1103        {1:~           }{12: foo            }{1:                                }|
1104        {1:~           }{4: faa            }{1:                                }|
1105        {1:~           }{4: fee            }{1:                                }|
1106        {3:[Command Line]                                              }|
1107        {5:-- Keyword Local completion (^N^P) }{6:match 1 of 3}             |
1108      ]])
1109      feed('<c-c>')
1110      -- Non-blocking cmdwin (#40312): <C-C> closes the cmdwin and drops back
1111      -- into ":" cmdline mode pre-filled with the line under the cursor.
1112      screen:expect([[
1113                                                                    |
1114        {1:~                                                           }|*6
1115        :foo faa fee foo^                                            |
1116      ]])
1117    end)
1118  end)
1119
1120  describe('with numeric items', function()
1121    before_each(function()
1122      source([[
1123        function! TestComplete() abort
1124          call complete(1, g:_complist)
1125          return ''
1126        endfunction
1127      ]])
1128      api.nvim_set_option_value('completeopt', 'menuone,noselect', {})
1129      api.nvim_set_var('_complist', {
1130        {
1131          word = 0,
1132          abbr = 1,
1133          menu = 2,
1134          kind = 3,
1135          info = 4,
1136          icase = 5,
1137          dup = 6,
1138          empty = 7,
1139        },
1140      })
1141    end)
1142
1143    it('shows correct variant as word', function()
1144      feed('i<C-r>=TestComplete()<CR>')
1145      screen:expect([[
1146        ^                                                            |
1147        {4:1 3 2          }{1:                                             }|
1148        {1:~                                                           }|*5
1149        {5:-- INSERT --}                                                |
1150      ]])
1151    end)
1152  end)
1153
1154  it("'ignorecase' 'infercase' CTRL-X CTRL-N #6451", function()
1155    command('set ignorecase infercase')
1156    command('edit runtime/doc/credits.txt')
1157    feed('oX<C-X><C-N>')
1158    screen:expect {
1159      grid = [[
1160      *credits.txt*          Nvim                                 |
1161      Xvi^                                                         |
1162      {12:Xvi            }{101: }                                            |
1163      {4:Xvim           }{101: }                                            |
1164      {4:X11            }{12: }     NVIM REFERENCE MANUAL                  |
1165      {4:Xnull          }{12: }                                            |
1166      {4:Xoxomoon       }{12: }                                            |
1167      {5:-- Keyword Local completion (^N^P) }{6:match 1 of 10}            |
1168    ]],
1169    }
1170  end)
1171
1172  it('CompleteChanged autocommand', function()
1173    api.nvim_buf_set_lines(0, 0, 1, false, { 'foo', 'bar', 'foobar', '' })
1174    source([[
1175      set complete=. completeopt=noinsert,noselect,menuone
1176      function! OnPumChange()
1177        let g:event = copy(v:event)
1178        let g:item = get(v:event, 'completed_item', {})
1179        let g:word = get(g:item, 'word', v:null)
1180      endfunction
1181      autocmd! CompleteChanged * :call OnPumChange()
1182      call cursor(4, 1)
1183    ]])
1184
1185    -- v:event.size should be set with ext_popupmenu #20646
1186    screen:set_option('ext_popupmenu', true)
1187    feed('Sf<C-N>')
1188    screen:expect({
1189      grid = [[
1190      foo                                                         |
1191      bar                                                         |
1192      foobar                                                      |
1193      f^                                                           |
1194      {1:~                                                           }|*3
1195      {5:-- Keyword completion (^N^P) }{19:Back at original}               |
1196    ]],
1197      popupmenu = {
1198        anchor = { 1, 3, 0 },
1199        items = { { 'foo', '', '', '' }, { 'foobar', '', '', '' } },
1200        pos = -1,
1201      },
1202    })
1203    eq(
1204      { completed_item = {}, width = 0, height = 2, size = 2, col = 0, row = 4, scrollbar = false },
1205      eval('g:event')
1206    )
1207    feed('oob')
1208    screen:expect({
1209      grid = [[
1210      foo                                                         |
1211      bar                                                         |
1212      foobar                                                      |
1213      foob^                                                        |
1214      {1:~                                                           }|*3
1215      {5:-- Keyword completion (^N^P) }{19:Back at original}               |
1216    ]],
1217      popupmenu = {
1218        anchor = { 1, 3, 0 },
1219        items = { { 'foobar', '', '', '' } },
1220        pos = -1,
1221      },
1222    })
1223    eq(
1224      { completed_item = {}, width = 0, height = 1, size = 1, col = 0, row = 4, scrollbar = false },
1225      eval('g:event')
1226    )
1227    feed('<Esc>')
1228    screen:set_option('ext_popupmenu', false)
1229
1230    feed('Sf<C-N>')
1231    screen:expect([[
1232      foo                                                         |
1233      bar                                                         |
1234      foobar                                                      |
1235      f^                                                           |
1236      {4:foo            }{1:                                             }|
1237      {4:foobar         }{1:                                             }|
1238      {1:~                                                           }|
1239      {5:-- Keyword completion (^N^P) }{19:Back at original}               |
1240    ]])
1241    eq(
1242      { completed_item = {}, width = 15, height = 2, size = 2, col = 0, row = 4, scrollbar = false },
1243      eval('g:event')
1244    )
1245    feed('<C-N>')
1246    screen:expect([[
1247      foo                                                         |
1248      bar                                                         |
1249      foobar                                                      |
1250      foo^                                                         |
1251      {12:foo            }{1:                                             }|
1252      {4:foobar         }{1:                                             }|
1253      {1:~                                                           }|
1254      {5:-- Keyword completion (^N^P) }{6:match 1 of 2}                   |
1255    ]])
1256    eq('foo', eval('g:word'))
1257    feed('<C-N>')
1258    screen:expect([[
1259      foo                                                         |
1260      bar                                                         |
1261      foobar                                                      |
1262      foobar^                                                      |
1263      {4:foo            }{1:                                             }|
1264      {12:foobar         }{1:                                             }|
1265      {1:~                                                           }|
1266      {5:-- Keyword completion (^N^P) }{6:match 2 of 2}                   |
1267    ]])
1268    eq('foobar', eval('g:word'))
1269    feed('<up>')
1270    screen:expect([[
1271      foo                                                         |
1272      bar                                                         |
1273      foobar                                                      |
1274      foobar^                                                      |
1275      {12:foo            }{1:                                             }|
1276      {4:foobar         }{1:                                             }|
1277      {1:~                                                           }|
1278      {5:-- Keyword completion (^N^P) }{6:match 1 of 2}                   |
1279    ]])
1280    eq('foo', eval('g:word'))
1281    feed('<down>')
1282    screen:expect([[
1283      foo                                                         |
1284      bar                                                         |
1285      foobar                                                      |
1286      foobar^                                                      |
1287      {4:foo            }{1:                                             }|
1288      {12:foobar         }{1:                                             }|
1289      {1:~                                                           }|
1290      {5:-- Keyword completion (^N^P) }{6:match 2 of 2}                   |
1291    ]])
1292    eq('foobar', eval('g:word'))
1293    feed('<esc>')
1294  end)
1295
1296  it('is stopped by :stopinsert from timer #12976', function()
1297    screen:try_resize(32, 14)
1298    command([[call setline(1, ['hello', 'hullo', 'heeee', ''])]])
1299    feed('Gah<c-x><c-n>')
1300    screen:expect([[
1301      hello                           |
1302      hullo                           |
1303      heeee                           |
1304      hello^                           |
1305      {12:hello          }{1:                 }|
1306      {4:hullo          }{1:                 }|
1307      {4:heeee          }{1:                 }|
1308      {1:~                               }|*6
1309      {5:-- }{6:match 1 of 3}                 |
1310    ]])
1311    command([[call timer_start(100, { -> execute('stopinsert') })]])
1312    vim.uv.sleep(200)
1313    feed('k') -- cursor should move up in Normal mode
1314    screen:expect([[
1315      hello                           |
1316      hullo                           |
1317      heee^e                           |
1318      hello                           |
1319      {1:~                               }|*9
1320                                      |
1321    ]])
1322  end)
1323
1324  -- oldtest: Test_complete_included_file_name()
1325  it('shows included files by relative path', function()
1326    t.mkdir('Xincl')
1327    finally(function()
1328      n.rmdir('Xincl')
1329    end)
1330    t.mkdir('Xincl/sub')
1331    t.write_file('Xincl/sub/inc.vim', 'let included_word = 1\n')
1332    t.write_file('Xincl/main.vim', 'source ./sub/inc.vim\n\n')
1333    n.exec([[
1334      edit Xincl/main.vim
1335      setlocal include=^\\s*source\\s\\+ complete=i completeopt=menuone,noselect
1336    ]])
1337    feed('Goincluded_<C-N>')
1338    screen:expect([[
1339      source ./sub/inc.vim                                        |
1340                                                                  |
1341      included_^                                                   |
1342      {4:included_word Xincl/sub/inc.vim }{1:                            }|
1343      {1:~                                                           }|*3
1344      {5:-- Keyword completion (^N^P) }{19:Back at original}               |
1345    ]])
1346    feed('<Esc>')
1347  end)
1348
1349  -- oldtest: Test_complete_changed_complete_info()
1350  it('no crash calling complete_info() in CompleteChanged', function()
1351    source([[
1352      set completeopt=menuone
1353      autocmd CompleteChanged * call complete_info(['items'])
1354      call feedkeys("iii\<cr>\<c-p>")
1355    ]])
1356    screen:expect([[
1357      ii                                                          |
1358      ii^                                                          |
1359      {12:ii             }{1:                                             }|
1360      {1:~                                                           }|*4
1361      {5:-- Keyword completion (^N^P) The only match}                 |
1362    ]])
1363    assert_alive()
1364  end)
1365
1366  it('no crash if text changed by first call to complete function #17489', function()
1367    source([[
1368      func Complete(findstart, base) abort
1369        if a:findstart
1370          let col = col('.')
1371          call complete_add('#')
1372          return col - 1
1373        else
1374          return []
1375        endif
1376      endfunc
1377
1378      set completeopt=longest
1379      set completefunc=Complete
1380    ]])
1381    feed('ifoo#<C-X><C-U>')
1382    assert_alive()
1383  end)
1384
1385  it('no crash using i_CTRL-X_CTRL-V to complete non-existent colorscheme', function()
1386    feed('icolorscheme NOSUCHCOLORSCHEME<C-X><C-V>')
1387    expect('colorscheme NOSUCHCOLORSCHEME')
1388    assert_alive()
1389  end)
1390
1391  it('complete with f flag #25598', function()
1392    screen:try_resize(20, 9)
1393    command('set complete+=f | edit foo | edit bar |edit foa |edit .hidden')
1394    feed('i<C-n>')
1395    screen:expect {
1396      grid = [[
1397      foo^                 |
1398      {12:foo            }{1:     }|
1399      {4:bar            }{1:     }|
1400      {4:foa            }{1:     }|
1401      {4:.hidden        }{1:     }|
1402      {1:~                   }|*3
1403      {5:-- }{6:match 1 of 4}     |
1404    ]],
1405    }
1406    feed('<Esc>ccf<C-n>')
1407    screen:expect {
1408      grid = [[
1409      foo^                 |
1410      {12:foo            }{1:     }|
1411      {4:foa            }{1:     }|
1412      {1:~                   }|*5
1413      {5:-- }{6:match 1 of 2}     |
1414    ]],
1415    }
1416  end)
1417
1418  it('restores extmarks if original text is restored #23653', function()
1419    screen:try_resize(screen._width, 4)
1420    command([[
1421      call setline(1, ['aaaa'])
1422      let ns_id = nvim_create_namespace('extmark')
1423      let mark_id = nvim_buf_set_extmark(0, ns_id, 0, 0, { 'end_col':2, 'hl_group':'Error' })
1424      let mark = nvim_buf_get_extmark_by_id(0, ns_id, mark_id, { 'details':1 })
1425      inoremap <C-x> <C-r>=Complete()<CR>
1426      function Complete() abort
1427        call complete(1, [{ 'word': 'aaaaa' }])
1428        return ''
1429      endfunction
1430    ]])
1431    feed('A<C-X><C-E><Esc>')
1432    eq(eval('mark'), eval("nvim_buf_get_extmark_by_id(0, ns_id, mark_id, { 'details':1 })"))
1433    feed('A<C-N>')
1434    eq(eval('mark'), eval("nvim_buf_get_extmark_by_id(0, ns_id, mark_id, { 'details':1 })"))
1435    feed('<Esc>0Yppia<Esc>ggI<C-N>')
1436    screen:expect([[
1437      aaaa{9:^aa}aa                                                    |
1438      {12:aaaa           }                                             |
1439      {4:aaaaa          }                                             |
1440      {5:-- Keyword completion (^N^P) }{6:match 1 of 2}                   |
1441    ]])
1442    feed('<C-N><C-N><Esc>')
1443    eq(eval('mark'), eval("nvim_buf_get_extmark_by_id(0, ns_id, mark_id, { 'details':1 })"))
1444    feed('A<C-N>')
1445    eq(eval('mark'), eval("nvim_buf_get_extmark_by_id(0, ns_id, mark_id, { 'details':1 })"))
1446    feed('<C-N>')
1447    screen:expect([[
1448      aaaaa^                                                       |
1449      {4:aaaa           }                                             |
1450      {12:aaaaa          }                                             |
1451      {5:-- Keyword completion (^N^P) }{6:match 2 of 2}                   |
1452    ]])
1453    feed('<C-E>')
1454    screen:expect([[
1455      {9:aa}aa^                                                        |
1456      aaaa                                                        |
1457      aaaaa                                                       |
1458      {5:-- INSERT --}                                                |
1459    ]])
1460    -- Also when completion leader is changed #31384
1461    feed('<Esc>hi<C-N><C-P>a')
1462    screen:expect({
1463      grid = [[
1464        {9:aa}a^aa                                                       |
1465        {4:aaaa           }                                             |
1466        {4:aaaaa          }                                             |
1467        {5:-- Keyword completion (^N^P) }{19:Back at original}               |
1468      ]],
1469    })
1470    -- But still grows with end_right_gravity #31437
1471    command(
1472      "call nvim_buf_set_extmark(0, ns_id, 1, 0, { 'end_col':2, 'hl_group':'Error', 'end_right_gravity': 1 })"
1473    )
1474    feed('<Esc>ji<C-N>a')
1475    screen:expect({
1476      grid = [[
1477        {9:aa}aaa                                                       |
1478        {9:aaa}^aa                                                       |
1479        aaaaa                                                       |
1480        {5:-- INSERT --}                                                |
1481      ]],
1482    })
1483  end)
1484
1485  describe('nvim__complete_set', function()
1486    it("fails when 'completeopt' does not include popup", function()
1487      exec_lua([[
1488        function _G.omni_test(findstart, base)
1489          if findstart == 1 then
1490            return vim.fn.col('.') - 1
1491          end
1492          return { { word = 'one' } }
1493        end
1494        vim.api.nvim_create_autocmd('CompleteChanged', {
1495          callback = function()
1496            local ok, err = pcall(vim.api.nvim__complete_set, 0, { info = '1info' })
1497            if not ok then
1498              vim.g.err_msg = err
1499            end
1500          end,
1501        })
1502        vim.opt.completeopt = 'menu,menuone'
1503        vim.opt.omnifunc = 'v:lua.omni_test'
1504      ]])
1505      feed('S<C-X><C-O>')
1506      eq('completeopt option does not include popup', api.nvim_get_var('err_msg'))
1507    end)
1508  end)
1509
1510  it([[does not include buffer from non-focusable window for 'complete' "w"]], function()
1511    local buf = api.nvim_create_buf(false, true)
1512    local cfg = { focusable = false, relative = 'win', bufpos = { 1, 0 }, width = 1, height = 1 }
1513    local win = api.nvim_open_win(buf, false, cfg)
1514    api.nvim_buf_set_lines(buf, 0, -1, false, { 'foo' })
1515    feed('i<C-N>')
1516    screen:expect([[
1517      ^                                                            |
1518      {4:f}{1:                                                           }|
1519      {1:~                                                           }|*5
1520      {5:-- Keyword completion (^N^P) }{9:Pattern not found}              |
1521    ]])
1522    api.nvim_win_set_config(win, { focusable = true })
1523    feed('<Esc>i<C-N>')
1524    screen:expect([[
1525      foo^                                                         |
1526      {4:f}{1:                                                           }|
1527      {1:~                                                           }|*5
1528      {5:-- Keyword completion (^N^P) The only match}                 |
1529    ]])
1530  end)
1531
1532  -- oldtest: Test_shortmess()
1533  it('shortmess+=c turns off completion messages', function()
1534    command('set ruler')
1535    command([[call setline(1, ['hello', 'hullo', 'heee'])]])
1536    feed('Goh<C-N>')
1537    screen:expect([[
1538      hello                                                       |
1539      hullo                                                       |
1540      heee                                                        |
1541      hello^                                                       |
1542      {12:hello          }{1:                                             }|
1543      {4:hullo          }{1:                                             }|
1544      {4:heee           }{1:                                             }|
1545      {5:-- Keyword completion (^N^P) }{6:match 1 of 3}                   |
1546    ]])
1547    feed('<Esc>')
1548    command('set shm+=c')
1549    feed('Sh<C-N>')
1550    screen:expect([[
1551      hello                                                       |
1552      hullo                                                       |
1553      heee                                                        |
1554      hello^                                                       |
1555      {12:hello          }{1:                                             }|
1556      {4:hullo          }{1:                                             }|
1557      {4:heee           }{1:                                             }|
1558      {5:-- INSERT --}                              4,6            All|
1559    ]])
1560  end)
1561
1562  -- oldtest: Test_autocompletedelay()
1563  it("'autocompletedelay' option", function()
1564    screen:try_resize(60, 10)
1565    source([[
1566      call setline(1, ['foo', 'foobar', 'foobarbaz'])
1567      setlocal autocomplete
1568    ]])
1569    screen:expect([[
1570      ^foo                                                         |
1571      foobar                                                      |
1572      foobarbaz                                                   |
1573      {1:~                                                           }|*6
1574                                                                  |
1575    ]])
1576    screen.timeout = 400
1577
1578    feed('Gof')
1579    screen:expect([[
1580      foo                                                         |
1581      foobar                                                      |
1582      foobarbaz                                                   |
1583      f^                                                           |
1584      {4:foobarbaz      }{1:                                             }|
1585      {4:foobar         }{1:                                             }|
1586      {4:foo            }{1:                                             }|
1587      {1:~                                                           }|*2
1588      {5:-- INSERT --}                                                |
1589    ]])
1590
1591    feed('<Esc>')
1592    command('set autocompletedelay=500')
1593    feed('Sf')
1594    screen:expect([[
1595      foo                                                         |
1596      foobar                                                      |
1597      foobarbaz                                                   |
1598      f^                                                           |
1599      {1:~                                                           }|*5
1600      {5:-- INSERT --}                                                |
1601    ]])
1602    feed('o')
1603    screen:expect([[
1604      foo                                                         |
1605      foobar                                                      |
1606      foobarbaz                                                   |
1607      fo^                                                          |
1608      {1:~                                                           }|*5
1609      {5:-- INSERT --}                                                |
1610    ]])
1611    vim.uv.sleep(500)
1612    screen:expect([[
1613      foo                                                         |
1614      foobar                                                      |
1615      foobarbaz                                                   |
1616      fo^                                                          |
1617      {4:foobarbaz      }{1:                                             }|
1618      {4:foobar         }{1:                                             }|
1619      {4:foo            }{1:                                             }|
1620      {1:~                                                           }|*2
1621      {5:-- INSERT --}                                                |
1622    ]])
1623    feed('<BS>')
1624    screen:expect([[
1625      foo                                                         |
1626      foobar                                                      |
1627      foobarbaz                                                   |
1628      f^                                                           |
1629      {4:foobarbaz      }{1:                                             }|
1630      {4:foobar         }{1:                                             }|
1631      {4:foo            }{1:                                             }|
1632      {1:~                                                           }|*2
1633      {5:-- INSERT --}                                                |
1634    ]])
1635    vim.uv.sleep(500)
1636    screen:expect_unchanged()
1637
1638    -- During delay wait, user can open menu using CTRL_N completion
1639    feed('<Esc>')
1640    command('set completeopt=menuone')
1641    feed('Sf<C-N>')
1642    screen:expect([[
1643      foo                                                         |
1644      foobar                                                      |
1645      foobarbaz                                                   |
1646      foo^                                                         |
1647      {12:foo            }{1:                                             }|
1648      {4:foobar         }{1:                                             }|
1649      {4:foobarbaz      }{1:                                             }|
1650      {1:~                                                           }|*2
1651      {5:-- Keyword completion (^N^P) }{6:match 1 of 3}                   |
1652    ]])
1653
1654    -- After the menu is open, ^N/^P and Up/Down should not delay.
1655    -- Wait a bit longer than 'autocompletedelay' so the popup is surely shown
1656    -- before sending CTRL-N, otherwise the keys race with the deferred popup.
1657    feed('<Esc>')
1658    command('set completeopt=menu')
1659    feed('Sf')
1660    screen:expect([[
1661      foo                                                         |
1662      foobar                                                      |
1663      foobarbaz                                                   |
1664      f^                                                           |
1665      {1:~                                                           }|*5
1666      {5:-- INSERT --}                                                |
1667    ]])
1668    vim.uv.sleep(600)
1669    screen:expect([[
1670      foo                                                         |
1671      foobar                                                      |
1672      foobarbaz                                                   |
1673      f^                                                           |
1674      {4:foobarbaz      }{1:                                             }|
1675      {4:foobar         }{1:                                             }|
1676      {4:foo            }{1:                                             }|
1677      {1:~                                                           }|*2
1678      {5:-- INSERT --}                                                |
1679    ]])
1680    feed('<C-N>')
1681    screen:expect([[
1682      foo                                                         |
1683      foobar                                                      |
1684      foobarbaz                                                   |
1685      foobarbaz^                                                   |
1686      {12:foobarbaz      }{1:                                             }|
1687      {4:foobar         }{1:                                             }|
1688      {4:foo            }{1:                                             }|
1689      {1:~                                                           }|*2
1690      {5:-- INSERT --}                                                |
1691    ]])
1692    feed('<Down>')
1693    screen:expect([[
1694      foo                                                         |
1695      foobar                                                      |
1696      foobarbaz                                                   |
1697      foobarbaz^                                                   |
1698      {4:foobarbaz      }{1:                                             }|
1699      {12:foobar         }{1:                                             }|
1700      {4:foo            }{1:                                             }|
1701      {1:~                                                           }|*2
1702      {5:-- INSERT --}                                                |
1703    ]])
1704
1705    -- When menu is not open Up/Down moves cursor to different line
1706    feed('<Esc>Sf')
1707    screen:expect([[
1708      foo                                                         |
1709      foobar                                                      |
1710      foobarbaz                                                   |
1711      f^                                                           |
1712      {1:~                                                           }|*5
1713      {5:-- INSERT --}                                                |
1714    ]])
1715    feed('<Up>')
1716    screen:expect([[
1717      foo                                                         |
1718      foobar                                                      |
1719      f^oobarbaz                                                   |
1720      f                                                           |
1721      {1:~                                                           }|*5
1722      {5:-- INSERT --}                                                |
1723    ]])
1724    feed('<Down>')
1725    screen:expect([[
1726      foo                                                         |
1727      foobar                                                      |
1728      foobarbaz                                                   |
1729      f^                                                           |
1730      {1:~                                                           }|*5
1731      {5:-- INSERT --}                                                |
1732    ]])
1733
1734    feed('<esc>')
1735  end)
1736
1737  local function run_test_autocompletedelay_ctrl_k(delay1, delay2)
1738    source([[
1739      new
1740      call setline(1, 'foo bar baz')
1741      set autocomplete autocompletedelay=200
1742    ]])
1743
1744    feed('ob')
1745    vim.uv.sleep(delay1)
1746    feed('<C-K>')
1747    vim.uv.sleep(delay2)
1748    feed('.,<Esc>')
1749    poke_eventloop()
1750    expect('foo bar baz\nb…')
1751
1752    source([[
1753      set autocomplete& autocompletedelay&
1754      bwipe!
1755    ]])
1756  end
1757
1758  -- oldtest: Test_autocompletedelay_ctrl_k()
1759  it("'autocompletedelay' doesn't interfere with i_CTRL-K", function()
1760    -- Ctrl-K typed after 'autocompletedelay' expires
1761    run_test_autocompletedelay_ctrl_k(250, 500)
1762    -- Ctrl-K typed before 'autocompletedelay' expires
1763    run_test_autocompletedelay_ctrl_k(150, 500)
1764  end)
1765
1766  -- oldtest: Test_fuzzy_select_item_when_acl()
1767  it([[first item isn't selected with "fuzzy" and 'acl']], function()
1768    screen:try_resize(60, 10)
1769    source([[
1770      call setline(1, ["v", "vi", "vim"])
1771      set autocomplete completeopt=menuone,noinsert,fuzzy autocompletedelay=300
1772    ]])
1773
1774    feed('Govi')
1775    screen:expect([[
1776      v                                                           |
1777      vi                                                          |
1778      vim                                                         |
1779      vi^                                                          |
1780      {4:vi             }{1:                                             }|
1781      {4:vim            }{1:                                             }|
1782      {1:~                                                           }|*3
1783      {5:-- INSERT --}                                                |
1784    ]])
1785
1786    feed('<Esc>Sv')
1787    screen:expect([[
1788      v                                                           |
1789      vi                                                          |
1790      vim                                                         |
1791      v^                                                           |
1792      {4:v              }{1:                                             }|
1793      {4:vi             }{1:                                             }|
1794      {4:vim            }{1:                                             }|
1795      {1:~                                                           }|*2
1796      {5:-- INSERT --}                                                |
1797    ]])
1798    feed('i')
1799    screen:expect([[
1800      v                                                           |
1801      vi                                                          |
1802      vim                                                         |
1803      vi^                                                          |
1804      {4:vi             }{1:                                             }|
1805      {4:vim            }{1:                                             }|
1806      {1:~                                                           }|*3
1807      {5:-- INSERT --}                                                |
1808    ]])
1809  end)
1810
1811  -- oldtest: Test_autocompletedelay_longest_preinsert()
1812  it("'autocompletedelay' with 'completeopt' longest/preinsert", function()
1813    source([[
1814      call setline(1, ['autocomplete', 'autocomxxx'])
1815      set autocomplete completeopt+=longest autocompletedelay=500
1816    ]])
1817    screen:expect([[
1818      ^autocomplete                                                |
1819      autocomxxx                                                  |
1820      {1:~                                                           }|*5
1821                                                                  |
1822    ]])
1823    screen.timeout = 400
1824
1825    -- No spurious characters when autocompletedelay is in effect
1826    feed('Goau')
1827    screen:expect([[
1828      autocomplete                                                |
1829      autocomxxx                                                  |
1830      au^                                                          |
1831      {1:~                                                           }|*4
1832      {5:-- INSERT --}                                                |
1833    ]])
1834    feed('toc')
1835    screen:expect([[
1836      autocomplete                                                |
1837      autocomxxx                                                  |
1838      autoc^                                                       |
1839      {1:~                                                           }|*4
1840      {5:-- INSERT --}                                                |
1841    ]])
1842    vim.uv.sleep(500)
1843    screen:expect([[
1844      autocomplete                                                |
1845      autocomxxx                                                  |
1846      autoc{102:^om}                                                     |
1847      {4:autocomxxx     }{1:                                             }|
1848      {4:autocomplete   }{1:                                             }|
1849      {1:~                                                           }|*2
1850      {5:-- INSERT --}                                                |
1851    ]])
1852
1853    -- Deleting a char should still show longest text
1854    feed('<Esc>Saut')
1855    screen:expect([[
1856      autocomplete                                                |
1857      autocomxxx                                                  |
1858      aut^                                                         |
1859      {1:~                                                           }|*4
1860      {5:-- INSERT --}                                                |
1861    ]])
1862    feed('<BS>')
1863    screen:expect([[
1864      autocomplete                                                |
1865      autocomxxx                                                  |
1866      au^                                                          |
1867      {1:~                                                           }|*4
1868      {5:-- INSERT --}                                                |
1869    ]])
1870    vim.uv.sleep(500)
1871    screen:expect([[
1872      autocomplete                                                |
1873      autocomxxx                                                  |
1874      au{102:^tocom}                                                     |
1875      {4:autocomxxx     }{1:                                             }|
1876      {4:autocomplete   }{1:                                             }|
1877      {1:~                                                           }|*2
1878      {5:-- INSERT --}                                                |
1879    ]])
1880
1881    -- Preinsert
1882    command('set completeopt& completeopt+=preinsert')
1883    feed('<Esc>Sau')
1884    screen:expect([[
1885      autocomplete                                                |
1886      autocomxxx                                                  |
1887      au^                                                          |
1888      {1:~                                                           }|*4
1889      {5:-- INSERT --}                                                |
1890    ]])
1891    vim.uv.sleep(500)
1892    screen:expect([[
1893      autocomplete                                                |
1894      autocomxxx                                                  |
1895      au{102:^tocomplete}                                                |
1896      {12:autocomplete   }{1:                                             }|
1897      {4:autocomxxx     }{1:                                             }|
1898      {1:~                                                           }|*2
1899      {5:-- INSERT --}                                                |
1900    ]])
1901  end)
1902
1903  -- oldtest: Test_fuzzy_filenames_compl_autocompl()
1904  it('fuzzy file name does not crash with autocomplete', function()
1905    feed('iset ac cot=fuzzy,longest<Esc>')
1906    command('source')
1907    feed('o')
1908    poke_eventloop()
1909    feed('.')
1910    poke_eventloop()
1911    feed('n')
1912    poke_eventloop()
1913    feed('a')
1914    poke_eventloop()
1915    feed('<C-X><C-F>') -- this used to cause segfault
1916    screen:expect([[
1917      set ac cot=fuzzy,longest                                    |
1918      .na^                                                         |
1919      {1:~                                                           }|*5
1920      {5:-- File name completion (^F^N^P) }{9:Pattern not found}          |
1921    ]])
1922  end)
1923
1924  it('returns all entries in the directory #25791', function()
1925    n.mkdir_p('Xtest/sub1')
1926    n.mkdir_p('Xtest/sub2')
1927    t.write_file('Xtest/A', '')
1928    t.write_file('Xtest/sub1/B', '')
1929    t.write_file('Xtest/sub2/C', '')
1930    finally(function()
1931      n.rmdir('Xtest')
1932    end)
1933    local path = ('%s/Xtest/**'):format(vim.fs.normalize(vim.uv.cwd()))
1934    command(('set path=%s'):format(path))
1935    eq({ 'A', 'B', 'C', 'sub1/', 'sub2/' }, fn.getcompletion('find ', 'cmdline'))
1936  end)
1937
1938  it('complete-items commit_chars', function()
1939    exec_lua(function()
1940      _G.Omnifunc = function(findstart, _)
1941        return findstart == 1 and 0 or { { word = 'foobar', commit_chars = '(' } }
1942      end
1943      vim.bo.omnifunc = 'v:lua.Omnifunc'
1944    end)
1945    n.command('set completeopt=menuone,noinsert')
1946    feed('i<C-x><C-o>')
1947    poke_eventloop()
1948    feed('(')
1949    eq('foobar(', n.api.nvim_get_current_line())
1950  end)
1951
1952  it('complete_info() reports equal, preselect and commit_chars', function()
1953    command('set completeopt=menuone,noinsert')
1954    source([[
1955      function! TestComplete() abort
1956        call complete(1, [
1957              \ {'word': 'foo', 'equal': 1},
1958              \ {'word': 'bar', 'preselect': v:true},
1959              \ {'word': 'baz', 'commit_chars': '('},
1960              \ {'word': 'qux'},
1961              \ ])
1962        return ''
1963      endfunction
1964    ]])
1965    feed('i<C-r>=TestComplete()<CR>')
1966    poke_eventloop()
1967
1968    local items = fn.complete_info({ 'items' }).items
1969    eq(4, #items)
1970    eq({ 1, nil }, { items[1].equal, items[1].preselect })
1971    eq(1, items[2].preselect)
1972    eq('(', items[3].commit_chars)
1973    eq({ nil, nil, nil }, { items[4].equal, items[4].preselect, items[4].commit_chars })
1974  end)
1975end)