robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
22.7 KiB924 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear = n.clear
7local command = n.command
8local eq = t.eq
9local eval = n.eval
10local feed = n.feed
11local fn = n.fn
12local poke_eventloop = n.poke_eventloop
13local exec = n.exec
14
15describe('search cmdline', function()
16  local screen
17
18  before_each(function()
19    clear()
20    command('set nohlsearch inccommand=')
21    screen = Screen.new(20, 3)
22  end)
23
24  local function tenlines()
25    fn.setline(1, {
26      '  1',
27      '  2 these',
28      '  3 the',
29      '  4 their',
30      '  5 there',
31      '  6 their',
32      '  7 the',
33      '  8 them',
34      '  9 these',
35      ' 10 foobar',
36    })
37    command('1')
38  end
39
40  it('history can be navigated with <C-N>/<C-P>', function()
41    tenlines()
42    command('set noincsearch')
43    feed('/foobar<CR>')
44    feed('/the<CR>')
45    eq('the', eval('@/'))
46    feed('/thes<C-P><C-P><CR>')
47    eq('foobar', eval('@/'))
48  end)
49
50  describe('can traverse matches', function()
51    before_each(tenlines)
52    local function forwarditer(wrapscan)
53      command('set incsearch ' .. wrapscan)
54      feed('/the')
55      screen:expect([[
56          1                 |
57          2 {2:the}se           |
58        /the^                |
59      ]])
60      feed('<C-G>')
61      screen:expect([[
62          2 these           |
63          3 {2:the}             |
64        /the^                |
65      ]])
66      eq({ 0, 0, 0, 0 }, fn.getpos('"'))
67      feed('<C-G>')
68      screen:expect([[
69          3 the             |
70          4 {2:the}ir           |
71        /the^                |
72      ]])
73      feed('<C-G>')
74      screen:expect([[
75          4 their           |
76          5 {2:the}re           |
77        /the^                |
78      ]])
79      feed('<C-G>')
80      screen:expect([[
81          5 there           |
82          6 {2:the}ir           |
83        /the^                |
84      ]])
85      feed('<C-G>')
86      screen:expect([[
87          6 their           |
88          7 {2:the}             |
89        /the^                |
90      ]])
91      feed('<C-G>')
92      screen:expect([[
93          7 the             |
94          8 {2:the}m            |
95        /the^                |
96      ]])
97      feed('<C-G>')
98      screen:expect([[
99          8 them            |
100          9 {2:the}se           |
101        /the^                |
102      ]])
103      screen.bell = false
104      feed('<C-G>')
105      if wrapscan == 'wrapscan' then
106        screen:expect([[
107            2 {2:the}se           |
108            3 the             |
109          /the^                |
110        ]])
111      else
112        screen:expect {
113          grid = [[
114            8 them            |
115            9 {2:the}se           |
116          /the^                |
117        ]],
118          condition = function()
119            eq(true, screen.bell)
120          end,
121        }
122        feed('<CR>')
123        eq({ 0, 0, 0, 0 }, fn.getpos('"'))
124      end
125    end
126
127    local function backiter(wrapscan)
128      command('set incsearch ' .. wrapscan)
129      command('$')
130
131      feed('?the')
132      screen:expect([[
133          9 {2:the}se           |
134         10 foobar          |
135        ?the^                |
136      ]])
137      screen.bell = false
138      if wrapscan == 'wrapscan' then
139        feed('<C-G>')
140        screen:expect([[
141            2 {2:the}se           |
142            3 the             |
143          ?the^                |
144        ]])
145        feed('<CR>')
146        screen:expect([[
147            2 ^these           |
148            3 the             |
149          ?the                |
150        ]])
151      else
152        feed('<C-G>')
153        screen:expect {
154          grid = [[
155            9 {2:the}se           |
156           10 foobar          |
157          ?the^                |
158        ]],
159          condition = function()
160            eq(true, screen.bell)
161          end,
162        }
163        feed('<CR>')
164        screen:expect([[
165            9 ^these           |
166           10 foobar          |
167          ?the                |
168        ]])
169      end
170      command('$')
171      feed('?the')
172      screen:expect([[
173          9 {2:the}se           |
174         10 foobar          |
175        ?the^                |
176      ]])
177      feed('<C-T>')
178      screen:expect([[
179          8 {2:the}m            |
180          9 these           |
181        ?the^                |
182      ]])
183      for i = 1, 6 do
184        feed('<C-T>')
185        -- Avoid sleep just before expect, otherwise expect will take the full
186        -- timeout
187        if i ~= 6 then
188          screen:sleep(1)
189        end
190      end
191      screen:expect([[
192          2 {2:the}se           |
193          3 the             |
194        ?the^                |
195      ]])
196      screen.bell = false
197      feed('<C-T>')
198      if wrapscan == 'wrapscan' then
199        screen:expect([[
200            9 {2:the}se           |
201           10 foobar          |
202          ?the^                |
203        ]])
204      else
205        screen:expect {
206          grid = [[
207            2 {2:the}se           |
208            3 the             |
209          ?the^                |
210        ]],
211          condition = function()
212            eq(true, screen.bell)
213          end,
214        }
215      end
216    end
217
218    it("using <C-G> and 'nowrapscan'", function()
219      forwarditer('nowrapscan')
220    end)
221
222    it("using <C-G> and 'wrapscan'", function()
223      forwarditer('wrapscan')
224    end)
225
226    it("using <C-T> and 'nowrapscan'", function()
227      backiter('nowrapscan')
228    end)
229
230    it("using <C-T> and 'wrapscan'", function()
231      backiter('wrapscan')
232    end)
233  end)
234
235  it('expands pattern with <C-L>', function()
236    tenlines()
237    command('set incsearch wrapscan')
238
239    feed('/the')
240    screen:expect([[
241        1                 |
242        2 {2:the}se           |
243      /the^                |
244    ]])
245    feed('<C-L>')
246    screen:expect([[
247        1                 |
248        2 {2:thes}e           |
249      /thes^               |
250    ]])
251    feed('<C-G>')
252    screen:expect([[
253        9 {2:thes}e           |
254       10 foobar          |
255      /thes^               |
256    ]])
257    feed('<C-G>')
258    screen:expect([[
259        2 {2:thes}e           |
260        3 the             |
261      /thes^               |
262    ]])
263    feed('<CR>')
264    screen:expect([[
265        2 ^these           |
266        3 the             |
267      /thes               |
268    ]])
269
270    command('1')
271    command('set nowrapscan')
272    feed('/the')
273    screen:expect([[
274        1                 |
275        2 {2:the}se           |
276      /the^                |
277    ]])
278    feed('<C-L>')
279    screen:expect([[
280        1                 |
281        2 {2:thes}e           |
282      /thes^               |
283    ]])
284    feed('<C-G>')
285    screen:expect([[
286        9 {2:thes}e           |
287       10 foobar          |
288      /thes^               |
289    ]])
290    feed('<C-G><CR>')
291    screen:expect([[
292        9 ^these           |
293       10 foobar          |
294      /thes               |
295    ]])
296  end)
297
298  it('reduces pattern with <BS> and keeps cursor position', function()
299    tenlines()
300    command('set incsearch wrapscan')
301
302    -- First match
303    feed('/thei')
304    screen:expect([[
305        3 the             |
306        4 {2:thei}r           |
307      /thei^               |
308    ]])
309    -- Match from initial cursor position when modifying search
310    feed('<BS>')
311    screen:expect([[
312        1                 |
313        2 {2:the}se           |
314      /the^                |
315    ]])
316    -- New text advances to next match
317    feed('s')
318    screen:expect([[
319        1                 |
320        2 {2:thes}e           |
321      /thes^               |
322    ]])
323    -- Stay on this match when deleting a character
324    feed('<BS>')
325    screen:expect([[
326        1                 |
327        2 {2:the}se           |
328      /the^                |
329    ]])
330    -- Advance to previous match
331    feed('<C-T>')
332    screen:expect([[
333        9 {2:the}se           |
334       10 foobar          |
335      /the^                |
336    ]])
337    -- Extend search to include next character
338    feed('<C-L>')
339    screen:expect([[
340        9 {2:thes}e           |
341       10 foobar          |
342      /thes^               |
343    ]])
344    -- Deleting all characters resets the cursor position
345    feed('<BS><BS><BS><BS>')
346    screen:expect([[
347        1                 |
348        2 these           |
349      /^                   |
350    ]])
351    feed('the')
352    screen:expect([[
353        1                 |
354        2 {2:the}se           |
355      /the^                |
356    ]])
357    feed('\\>')
358    screen:expect([[
359        2 these           |
360        3 {2:the}             |
361      /the\>^              |
362    ]])
363  end)
364
365  it('can traverse matches in the same line with <C-G>/<C-T>', function()
366    fn.setline(1, { '  1', '  2 these', '  3 the theother' })
367    command('1')
368    command('set incsearch')
369
370    -- First match
371    feed('/the')
372    screen:expect([[
373        1                 |
374        2 {2:the}se           |
375      /the^                |
376    ]])
377
378    -- Next match, different line
379    feed('<C-G>')
380    screen:expect([[
381        2 these           |
382        3 {2:the} theother    |
383      /the^                |
384    ]])
385
386    -- Next match, same line
387    feed('<C-G>')
388    screen:expect([[
389        2 these           |
390        3 the {2:the}other    |
391      /the^                |
392    ]])
393    feed('<C-G>')
394    screen:expect([[
395        2 these           |
396        3 the theo{2:the}r    |
397      /the^                |
398    ]])
399
400    -- Previous match, same line
401    feed('<C-T>')
402    screen:expect([[
403        2 these           |
404        3 the {2:the}other    |
405      /the^                |
406    ]])
407    feed('<C-T>')
408    screen:expect([[
409        2 these           |
410        3 {2:the} theother    |
411      /the^                |
412    ]])
413
414    -- Previous match, different line
415    feed('<C-T>')
416    screen:expect([[
417        2 {2:the}se           |
418        3 the theother    |
419      /the^                |
420    ]])
421  end)
422
423  it('keeps the view after deleting a char from the search', function()
424    screen:try_resize(20, 6)
425    tenlines()
426
427    feed('/foo')
428    screen:expect([[
429        6 their           |
430        7 the             |
431        8 them            |
432        9 these           |
433       10 {2:foo}bar          |
434      /foo^                |
435    ]])
436    feed('<BS>')
437    screen:expect([[
438        6 their           |
439        7 the             |
440        8 them            |
441        9 these           |
442       10 {2:fo}obar          |
443      /fo^                 |
444    ]])
445    feed('<CR>')
446    screen:expect([[
447        6 their           |
448        7 the             |
449        8 them            |
450        9 these           |
451       10 ^foobar          |
452      /fo                 |
453    ]])
454    eq({
455      lnum = 10,
456      leftcol = 0,
457      col = 4,
458      topfill = 0,
459      topline = 6,
460      coladd = 0,
461      skipcol = 0,
462      curswant = 4,
463    }, fn.winsaveview())
464  end)
465
466  it('restores original view after failed search', function()
467    screen:try_resize(40, 3)
468    tenlines()
469    feed('0')
470    feed('/foo')
471    screen:expect([[
472        9 these                               |
473       10 {2:foo}bar                              |
474      /foo^                                    |
475    ]])
476    feed('<C-W>')
477    screen:expect([[
478        1                                     |
479        2 these                               |
480      /^                                       |
481    ]])
482    feed('<CR>')
483    screen:expect([[
484      /                                       |
485      {9:E35: No previous regular expression}     |
486      {6:Press ENTER or type command to continue}^ |
487    ]])
488    feed('<CR>')
489    eq({
490      lnum = 1,
491      leftcol = 0,
492      col = 0,
493      topfill = 0,
494      topline = 1,
495      coladd = 0,
496      skipcol = 0,
497      curswant = 0,
498    }, fn.winsaveview())
499  end)
500
501  -- oldtest: Test_search_cmdline4().
502  it("CTRL-G with 'incsearch' and ? goes in the right direction", function()
503    screen:try_resize(40, 4)
504    command('enew!')
505    fn.setline(1, { '  1 the first', '  2 the second', '  3 the third' })
506    command('set laststatus=0 shortmess+=s')
507    command('set incsearch')
508    command('$')
509    -- Send the input in chunks, so the cmdline logic regards it as
510    -- "interactive".  This mimics Vim's test_override("char_avail").
511    -- (See legacy test: test_search.vim)
512    feed('?the')
513    poke_eventloop()
514    feed('<c-g>')
515    poke_eventloop()
516    feed('<cr>')
517    screen:expect([[
518        1 the first                           |
519        2 the second                          |
520        3 ^the third                           |
521      ?the                                    |
522    ]])
523
524    command('$')
525    feed('?the')
526    poke_eventloop()
527    feed('<c-g>')
528    poke_eventloop()
529    feed('<c-g>')
530    poke_eventloop()
531    feed('<cr>')
532    screen:expect([[
533        1 ^the first                           |
534        2 the second                          |
535        3 the third                           |
536      ?the                                    |
537    ]])
538
539    command('$')
540    feed('?the')
541    poke_eventloop()
542    feed('<c-g>')
543    poke_eventloop()
544    feed('<c-g>')
545    poke_eventloop()
546    feed('<c-g>')
547    poke_eventloop()
548    feed('<cr>')
549    screen:expect([[
550        1 the first                           |
551        2 ^the second                          |
552        3 the third                           |
553      ?the                                    |
554    ]])
555
556    command('$')
557    feed('?the')
558    poke_eventloop()
559    feed('<c-t>')
560    poke_eventloop()
561    feed('<cr>')
562    screen:expect([[
563        1 ^the first                           |
564        2 the second                          |
565        3 the third                           |
566      ?the                                    |
567    ]])
568
569    command('$')
570    feed('?the')
571    poke_eventloop()
572    feed('<c-t>')
573    poke_eventloop()
574    feed('<c-t>')
575    poke_eventloop()
576    feed('<cr>')
577    screen:expect([[
578        1 the first                           |
579        2 the second                          |
580        3 ^the third                           |
581      ?the                                    |
582    ]])
583
584    command('$')
585    feed('?the')
586    poke_eventloop()
587    feed('<c-t>')
588    poke_eventloop()
589    feed('<c-t>')
590    poke_eventloop()
591    feed('<c-t>')
592    poke_eventloop()
593    feed('<cr>')
594    screen:expect([[
595        1 the first                           |
596        2 ^the second                          |
597        3 the third                           |
598      ?the                                    |
599    ]])
600  end)
601
602  -- oldtest: Test_incsearch_sort_dump().
603  it('incsearch works with :sort', function()
604    screen:try_resize(20, 4)
605    command('set incsearch hlsearch scrolloff=0')
606    fn.setline(1, { 'another one 2', 'that one 3', 'the one 1' })
607
608    feed(':sort ni u /on')
609    screen:expect([[
610      another {2:on}e 2       |
611      that {10:on}e 3          |
612      the {10:on}e 1           |
613      :sort ni u /on^      |
614    ]])
615    feed('<Esc>')
616
617    feed(':sort! /on')
618    screen:expect([[
619      another {2:on}e 2       |
620      that {10:on}e 3          |
621      the {10:on}e 1           |
622      :sort! /on^          |
623    ]])
624    feed('<Esc>')
625  end)
626
627  -- oldtest: Test_incsearch_uniq_dump().
628  it('incsearch works with :uniq', function()
629    screen:try_resize(20, 4)
630    command('set incsearch hlsearch scrolloff=0')
631    fn.setline(1, { 'another one 2', 'that one 3', 'the one 1' })
632
633    feed(':uniq /on')
634    screen:expect([[
635      another {2:on}e 2       |
636      that {10:on}e 3          |
637      the {10:on}e 1           |
638      :uniq /on^           |
639    ]])
640    feed('<Esc>')
641
642    feed(':uniq! /on')
643    screen:expect([[
644      another {2:on}e 2       |
645      that {10:on}e 3          |
646      the {10:on}e 1           |
647      :uniq! /on^          |
648    ]])
649    feed('<Esc>')
650  end)
651
652  -- oldtest: Test_incsearch_vimgrep_dump().
653  it('incsearch works with :vimgrep family', function()
654    screen:try_resize(30, 4)
655    command('set incsearch hlsearch scrolloff=0')
656    fn.setline(1, { 'another one 2', 'that one 3', 'the one 1' })
657
658    feed(':vimgrep on')
659    screen:expect([[
660      another {2:on}e 2                 |
661      that {10:on}e 3                    |
662      the {10:on}e 1                     |
663      :vimgrep on^                   |
664    ]])
665    feed('<esc>')
666
667    feed(':vimg /on/ *.txt')
668    screen:expect([[
669      another {2:on}e 2                 |
670      that {10:on}e 3                    |
671      the {10:on}e 1                     |
672      :vimg /on/ *.txt^              |
673    ]])
674    feed('<esc>')
675
676    feed(':vimgrepadd "\\<LT>on')
677    screen:expect([[
678      another {2:on}e 2                 |
679      that {10:on}e 3                    |
680      the {10:on}e 1                     |
681      :vimgrepadd "\<on^             |
682    ]])
683    feed('<esc>')
684
685    feed(':lv "tha')
686    screen:expect([[
687      another one 2                 |
688      {2:tha}t one 3                    |
689      the one 1                     |
690      :lv "tha^                      |
691    ]])
692    feed('<esc>')
693
694    feed(':lvimgrepa "the" **/*.txt')
695    screen:expect([[
696      ano{2:the}r one 2                 |
697      that one 3                    |
698      {10:the} one 1                     |
699      :lvimgrepa "the" **/*.txt^     |
700    ]])
701    feed('<esc>')
702  end)
703
704  -- oldtest: Test_incsearch_substitute_dump2()
705  it('incsearch detects empty pattern properly vim-patch:8.2.2295', function()
706    screen:try_resize(70, 6)
707    exec([[
708      set incsearch hlsearch scrolloff=0
709      for n in range(1, 4)
710        call setline(n, "foo " . n)
711      endfor
712      call setline(5, "abc|def")
713      3
714    ]])
715
716    feed([[:%s/\vabc|]])
717    screen:expect([[
718      foo 1                                                                 |
719      foo 2                                                                 |
720      foo 3                                                                 |
721      foo 4                                                                 |
722      abc|def                                                               |
723      :%s/\vabc|^                                                            |
724    ]])
725    feed('<Esc>')
726
727    -- The following should not be highlighted
728    feed([[:1,5s/\v|]])
729    screen:expect([[
730      foo 1                                                                 |
731      foo 2                                                                 |
732      foo 3                                                                 |
733      foo 4                                                                 |
734      abc|def                                                               |
735      :1,5s/\v|^                                                             |
736    ]])
737  end)
738
739  -- oldtest: Test_incsearch_restore_view()
740  it('incsearch restores viewport', function()
741    screen:try_resize(20, 6)
742    exec([[
743      set incsearch nohlsearch
744      setlocal scrolloff=0 smoothscroll
745      call setline(1, [join(range(25), ' '), '', '', '', '', 'xxx'])
746      call feedkeys("2\<C-E>", 't')
747    ]])
748    local s = [[
749      {1:<<<} 18 19 20 21 22 2|
750      ^3 24                |
751                          |*4
752    ]]
753    screen:expect(s)
754    feed('/xx')
755    screen:expect([[
756                          |*4
757      {2:xx}x                 |
758      /xx^                 |
759    ]])
760    feed('x')
761    screen:expect([[
762                          |*4
763      {2:xxx}                 |
764      /xxx^                |
765    ]])
766    feed('<Esc>')
767    screen:expect(s)
768  end)
769
770  -- oldtest: Test_incsearch_delimiter_ctrlg()
771  it('incsearch does not include trailing delimiter', function()
772    screen:try_resize(20, 6)
773    exec([[
774      set incsearch hls
775      call setline(1, ["1 vim inc", "2 vim /", "3 vim /", "4 vim ?", "5 vim ?"])
776      normal gg
777      redraw
778    ]])
779
780    feed('/')
781    poke_eventloop()
782    feed('v')
783    poke_eventloop()
784    feed('i')
785    poke_eventloop()
786    feed('m')
787    poke_eventloop()
788    feed(' ')
789    poke_eventloop()
790    feed('/')
791    poke_eventloop()
792    feed('<C-G>')
793    screen:expect([[
794      1 {10:vim }inc           |
795      2 {2:vim }/             |
796      3 {10:vim }/             |
797      4 {10:vim }?             |
798      5 {10:vim }?             |
799      /vim /^              |
800    ]])
801    feed('<Esc>')
802
803    command('5')
804    feed('?')
805    poke_eventloop()
806    feed('v')
807    poke_eventloop()
808    feed('i')
809    poke_eventloop()
810    feed('m')
811    poke_eventloop()
812    feed(' ')
813    poke_eventloop()
814    feed('?')
815    poke_eventloop()
816    feed('<C-T>')
817    screen:expect([[
818      1 {10:vim }inc           |
819      2 {10:vim }/             |
820      3 {2:vim }/             |
821      4 {10:vim }?             |
822      5 {10:vim }?             |
823      ?vim ?^              |
824    ]])
825    feed('<Esc>')
826
827    feed('/')
828    poke_eventloop()
829    feed('v')
830    poke_eventloop()
831    feed('i')
832    poke_eventloop()
833    feed('m')
834    poke_eventloop()
835    feed(' ')
836    poke_eventloop()
837    feed('\\/')
838    poke_eventloop()
839    feed('<C-G>')
840    screen:expect([[
841      1 vim inc           |
842      2 {10:vim /}             |
843      3 {2:vim /}             |
844      4 vim ?             |
845      5 vim ?             |
846      /vim \/^             |
847    ]])
848    feed('<Esc>')
849
850    command('5')
851    feed('?')
852    poke_eventloop()
853    feed('v')
854    poke_eventloop()
855    feed('i')
856    poke_eventloop()
857    feed('m')
858    poke_eventloop()
859    feed(' ')
860    poke_eventloop()
861    feed('\\?')
862    poke_eventloop()
863    feed('<C-T>')
864    screen:expect([[
865      1 vim inc           |
866      2 vim /             |
867      3 vim /             |
868      4 {10:vim ?}             |
869      5 {2:vim ?}             |
870      ?vim \?^             |
871    ]])
872    feed('<Esc>')
873  end)
874end)
875
876describe('Search highlight', function()
877  before_each(clear)
878
879  -- oldtest: Test_hlsearch_dump()
880  it('beyond line end vim-patch:8.2.2542', function()
881    local screen = Screen.new(50, 6)
882    exec([[
883      set hlsearch noincsearch cursorline
884      call setline(1, ["xxx", "xxx", "xxx"])
885      /.*
886      2
887    ]])
888    feed([[/\_.*<CR>]])
889    screen:expect([[
890      {10:xxx }                                              |*2
891      {10:^xxx }{21:                                              }|
892      {1:~                                                 }|*2
893      /\_.*                                             |
894    ]])
895  end)
896
897  -- oldtest: Test_hlsearch_and_visual()
898  it('is combined with Visual highlight vim-patch:8.2.2797', function()
899    local screen = Screen.new(40, 6)
900    screen:add_extra_attr_ids {
901      [100] = {
902        foreground = Screen.colors.Black,
903        bold = true,
904        background = Screen.colors.LightGrey,
905      },
906      [101] = { bold = true, background = Screen.colors.Yellow },
907    }
908    exec([[
909      set hlsearch noincsearch
910      call setline(1, repeat(["xxx yyy zzz"], 3))
911      hi Search gui=bold
912      /yyy
913      call cursor(1, 6)
914    ]])
915    feed('vjj')
916    screen:expect([[
917      xxx {101:y}{100:yy}{17: zzz}                             |
918      {17:xxx }{100:yyy}{17: zzz}                             |
919      {17:xxx }{100:y^y}{101:y} zzz                             |
920      {1:~                                       }|*2
921      {5:-- VISUAL --}                            |
922    ]])
923  end)
924end)