robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
25.6 KiB736 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, feed, insert = n.clear, n.feed, n.insert
7local command = n.command
8local feed_command = n.feed_command
9local eq = t.eq
10local eval = n.eval
11local fn = n.fn
12local testprg = n.testprg
13
14describe('search highlighting', function()
15  local screen
16
17  before_each(function()
18    clear()
19    screen = Screen.new(40, 7)
20    screen:add_extra_attr_ids({
21      [100] = { foreground = Screen.colors.Gray100, background = Screen.colors.Grey0 },
22      [101] = { foreground = Screen.colors.White, background = Screen.colors.DarkGreen },
23      [102] = { background = Screen.colors.WebGreen, bold = true },
24      [103] = { background = Screen.colors.Magenta1, italic = true },
25      [104] = { background = Screen.colors.Yellow1, bold = true },
26    })
27  end)
28
29  it('is disabled by ":set nohlsearch"', function()
30    feed_command('set nohlsearch')
31    insert('some text\nmore text')
32    feed('gg/text<cr>')
33    screen:expect([[
34      some ^text                               |
35      more text                               |
36      {1:~                                       }|*4
37      /text                                   |
38    ]])
39  end)
40
41  it('is disabled in folded text', function()
42    insert('some text\nmore text')
43    feed_command('1,2fold')
44    feed('gg/text')
45    screen:expect {
46      grid = [[
47      {13:+--  2 lines: some text·················}|
48      {1:~                                       }|*5
49      /text^                                   |
50    ]],
51      win_viewport = {
52        [2] = {
53          win = 1000,
54          topline = 0,
55          botline = 3,
56          curline = 0,
57          curcol = 9,
58          linecount = 2,
59          sum_scroll_delta = 0,
60        },
61      },
62    }
63  end)
64
65  local function test_search_hl()
66    insert([[
67      some text
68      more textstuff
69      stupidtexttextstuff
70      a text word
71    ]])
72
73    -- 'hlsearch' is enabled by default. #2859
74    feed('gg/text<cr>')
75    screen:expect([[
76      some {100:^text}                               |
77      more {100:text}stuff                          |
78      stupid{100:texttext}stuff                     |
79      a {100:text} word                             |
80                                              |
81      {101:~                                       }|
82      /text                                   |
83    ]])
84
85    -- overlapping matches not allowed
86    feed('3nx')
87    screen:expect([[
88      some {100:text}                               |
89      more {100:text}stuff                          |
90      stupid{100:text}^extstuff                      |
91      a {100:text} word                             |
92                                              |
93      {101:~                                       }|
94      /text                                   |
95    ]])
96
97    feed('ggn*') -- search for entire word
98    screen:expect([[
99      some {100:text}                               |
100      more textstuff                          |
101      stupidtextextstuff                      |
102      a {100:^text} word                             |
103                                              |
104      {101:~                                       }|
105      /\<text\>                               |
106    ]])
107
108    feed_command('nohlsearch')
109    screen:expect([[
110      some text                               |
111      more textstuff                          |
112      stupidtextextstuff                      |
113      a ^text word                             |
114                                              |
115      {101:~                                       }|
116      :nohlsearch                             |
117    ]])
118  end
119
120  it("works when 'winhighlight' is not set", function()
121    screen:add_extra_attr_ids({
122      [100] = { background = Screen.colors.Yellow1 },
123      [101] = { foreground = Screen.colors.Blue1, bold = true },
124    })
125    test_search_hl()
126  end)
127
128  it("works when 'winhighlight' doesn't change Search highlight", function()
129    command('setlocal winhl=NonText:Underlined')
130    screen:add_extra_attr_ids({
131      [100] = { background = Screen.colors.Yellow },
132      [101] = { foreground = Screen.colors.SlateBlue, underline = true },
133    })
134    test_search_hl()
135  end)
136
137  it("works when 'winhighlight' changes Search highlight", function()
138    command('setlocal winhl=Search:Underlined')
139    screen:add_extra_attr_ids({
140      [100] = { foreground = Screen.colors.SlateBlue, underline = true },
141      [101] = { foreground = Screen.colors.Blue1, bold = true },
142    })
143    test_search_hl()
144  end)
145
146  describe('CurSearch highlight', function()
147    before_each(function()
148      command('highlight CurSearch guibg=Black guifg=White')
149    end)
150
151    it('works for match under cursor', function()
152      insert([[
153        There is no way that a bee should be
154        able to fly. Its wings are too small
155        to get its fat little body off the
156        ground. The bee, of course, flies
157        anyway because bees don't care what
158        humans think is impossible.]])
159
160      feed('/bee<CR>')
161      screen:expect([[
162        There is no way that a {100:^bee} should be    |
163        able to fly. Its wings are too small    |
164        to get its fat little body off the      |
165        ground. The {10:bee}, of course, flies       |
166        anyway because {10:bee}s don't care what     |
167        humans think is impossible.             |
168        {19:search hit BOTTOM, continuing at TOP}    |
169      ]])
170
171      feed('nn')
172      screen:expect([[
173        There is no way that a {10:bee} should be    |
174        able to fly. Its wings are too small    |
175        to get its fat little body off the      |
176        ground. The {10:bee}, of course, flies       |
177        anyway because {100:^bee}s don't care what     |
178        humans think is impossible.             |
179        /bee                                    |
180      ]])
181
182      feed('N')
183      screen:expect([[
184        There is no way that a {10:bee} should be    |
185        able to fly. Its wings are too small    |
186        to get its fat little body off the      |
187        ground. The {100:^bee}, of course, flies       |
188        anyway because {10:bee}s don't care what     |
189        humans think is impossible.             |
190        ?bee                                    |
191      ]])
192    end)
193
194    -- oldtest: Test_hlsearch_cursearch()
195    it('works for multiline match, no duplicate highlight', function()
196      command([[call setline(1, ['one', 'foo', 'bar', 'baz', 'foo the foo and foo', 'bar'])]])
197      feed('gg/foo<CR>')
198      screen:expect([[
199        one                                     |
200        {100:^foo}                                     |
201        bar                                     |
202        baz                                     |
203        {10:foo} the {10:foo} and {10:foo}                     |
204        bar                                     |
205        /foo                                    |
206      ]])
207      feed('n')
208      screen:expect([[
209        one                                     |
210        {10:foo}                                     |
211        bar                                     |
212        baz                                     |
213        {100:^foo} the {10:foo} and {10:foo}                     |
214        bar                                     |
215        /foo                                    |
216      ]])
217      feed('n')
218      screen:expect([[
219        one                                     |
220        {10:foo}                                     |
221        bar                                     |
222        baz                                     |
223        {10:foo} the {100:^foo} and {10:foo}                     |
224        bar                                     |
225        /foo                                    |
226      ]])
227      feed('n')
228      screen:expect([[
229        one                                     |
230        {10:foo}                                     |
231        bar                                     |
232        baz                                     |
233        {10:foo} the {10:foo} and {100:^foo}                     |
234        bar                                     |
235        /foo                                    |
236      ]])
237      command([[call setline(5, 'foo')]])
238      feed('0?<CR>')
239      screen:expect([[
240        one                                     |
241        {100:^foo}                                     |
242        bar                                     |
243        baz                                     |
244        {10:foo}                                     |
245        bar                                     |
246        ?foo                                    |
247      ]])
248      feed('gg/foo\\nbar<CR>')
249      screen:expect([[
250        one                                     |
251        {100:^foo }                                    |
252        {100:bar}                                     |
253        baz                                     |
254        {10:foo }                                    |
255        {10:bar}                                     |
256        /foo\nbar                               |
257      ]])
258      command([[call setline(1, ['---', 'abcdefg', 'hijkl', '---', 'abcdefg', 'hijkl'])]])
259      feed('gg/efg\\nhij<CR>')
260      screen:expect([[
261        ---                                     |
262        abcd{100:^efg }                                |
263        {100:hij}kl                                   |
264        ---                                     |
265        abcd{10:efg }                                |
266        {10:hij}kl                                   |
267        /efg\nhij                               |
268      ]])
269      feed('n')
270      screen:expect([[
271        ---                                     |
272        abcd{10:efg }                                |
273        {10:hij}kl                                   |
274        ---                                     |
275        abcd{100:^efg }                                |
276        {100:hij}kl                                   |
277        /efg\nhij                               |
278      ]])
279
280      -- check clearing CurSearch when using it for another match
281      feed('G?^abcd<CR>Y')
282      screen:expect([[
283        ---                                     |
284        {10:abcd}efg                                 |
285        hijkl                                   |
286        ---                                     |
287        {100:^abcd}efg                                 |
288        hijkl                                   |
289        ?^abcd                                  |
290      ]])
291      feed('kkP')
292      screen:expect([[
293        ---                                     |
294        {10:abcd}efg                                 |
295        {100:^abcd}efg                                 |
296        hijkl                                   |
297        ---                                     |
298        {10:abcd}efg                                 |
299        ?^abcd                                  |
300      ]])
301    end)
302  end)
303
304  it('highlights after EOL', function()
305    insert('\n\n\n\n\n\n')
306
307    feed('gg/^<cr>')
308    screen:expect([[
309      {10: }                                       |
310      {10:^ }                                       |
311      {10: }                                       |*4
312      /^                                      |
313    ]])
314
315    -- Test that highlights are preserved after moving the cursor.
316    feed('j')
317    screen:expect([[
318      {10: }                                       |*2
319      {10:^ }                                       |
320      {10: }                                       |*3
321      /^                                      |
322    ]])
323
324    -- Repeat the test in rightleft mode.
325    command('nohlsearch')
326    command('set rightleft')
327    feed('gg/^<cr>')
328
329    screen:expect([[
330                                             {10: }|
331                                             {10:^ }|
332                                             {10: }|*4
333      ^/                                      |
334    ]])
335
336    feed('j')
337    screen:expect([[
338                                             {10: }|*2
339                                             {10:^ }|
340                                             {10: }|*3
341      ^/                                      |
342    ]])
343  end)
344
345  it('is preserved during :terminal activity', function()
346    feed((':terminal "%s" REP 5000 foo<cr>'):format(testprg('shell-test')))
347    feed(':file term<CR>')
348    screen:expect([[
349      ^0: foo                                  |
350      1: foo                                  |
351      2: foo                                  |
352      3: foo                                  |
353      4: foo                                  |
354      5: foo                                  |
355      :file term                              |
356    ]])
357
358    feed('G') -- Follow :terminal output.
359    feed(':vnew<CR>')
360    insert([[
361      foo bar baz
362      bar baz foo
363      bar foo baz]])
364    feed('/foo')
365    screen:expect([[
366      {2:foo} bar baz         │{MATCH:%d+}: {10:foo}{MATCH:%s+}|
367      bar baz {10:foo}         │{MATCH:%d+}: {10:foo}{MATCH:%s+}|
368      bar {10:foo} baz         │{MATCH:%d+}: {10:foo}{MATCH:%s+}|
369      {1:~                   }│{MATCH:.*}|*2
370      {3:[No Name] [+]        }{101:term [-]           }|
371      /foo^                                    |
372    ]])
373  end)
374
375  it('works with incsearch', function()
376    command('set hlsearch')
377    command('set incsearch')
378    command('set laststatus=0')
379    insert([[
380      the first line
381      in a little file]])
382    command('vsplit')
383    feed('gg/li')
384    screen:expect([[
385      the first {2:li}ne      │the first {10:li}ne     |
386      in a {10:li}ttle file    │in a {10:li}ttle file   |
387      {1:~                   }│{1:~                  }|*4
388      /li^                                     |
389    ]])
390
391    -- check that consecutive matches are caught by C-g/C-t
392    feed('<C-g>')
393    screen:expect([[
394      the first {10:li}ne      │the first {10:li}ne     |
395      in a {2:li}ttle file    │in a {10:li}ttle file   |
396      {1:~                   }│{1:~                  }|*4
397      /li^                                     |
398    ]])
399
400    feed('<C-t>')
401    screen:expect([[
402      the first {2:li}ne      │the first {10:li}ne     |
403      in a {10:li}ttle file    │in a {10:li}ttle file   |
404      {1:~                   }│{1:~                  }|*4
405      /li^                                     |
406    ]])
407
408    feed('t')
409    screen:expect([[
410      the first line      │the first line     |
411      in a {2:lit}tle file    │in a {10:lit}tle file   |
412      {1:~                   }│{1:~                  }|*4
413      /lit^                                    |
414    ]])
415
416    feed('<cr>')
417    screen:expect([[
418      the first line      │the first line     |
419      in a {10:^lit}tle file    │in a {10:lit}tle file   |
420      {1:~                   }│{1:~                  }|*4
421      /lit                                    |
422    ]])
423
424    feed('/fir')
425    screen:expect([[
426      the {2:fir}st line      │the {10:fir}st line     |
427      in a little file    │in a little file   |
428      {1:~                   }│{1:~                  }|*4
429      /fir^                                    |
430    ]])
431
432    -- incsearch have priority over hlsearch
433    feed('<esc>/ttle')
434    screen:expect([[
435      the first line      │the first line     |
436      in a li{2:ttle} file    │in a li{10:ttle} file   |
437      {1:~                   }│{1:~                  }|*4
438      /ttle^                                   |
439    ]])
440
441    -- cancelling search resets to the old search term
442    feed('<esc>')
443    screen:expect([[
444      the first line      │the first line     |
445      in a {10:^lit}tle file    │in a {10:lit}tle file   |
446      {1:~                   }│{1:~                  }|*4
447                                              |
448    ]])
449    eq('lit', eval('@/'))
450
451    -- cancelling inc search restores the hl state
452    feed(':noh<cr>')
453    screen:expect([[
454      the first line      │the first line     |
455      in a ^little file    │in a little file   |
456      {1:~                   }│{1:~                  }|*4
457      :noh                                    |
458    ]])
459
460    feed('/first')
461    screen:expect([[
462      the {2:first} line      │the {10:first} line     |
463      in a little file    │in a little file   |
464      {1:~                   }│{1:~                  }|*4
465      /first^                                  |
466    ]])
467    feed('<esc>')
468    screen:expect([[
469      the first line      │the first line     |
470      in a ^little file    │in a little file   |
471      {1:~                   }│{1:~                  }|*4
472                                              |
473    ]])
474
475    -- test that pressing C-g in an empty command line does not move the cursor
476    feed('gg0')
477    command([[let @/ = 'i']])
478    -- moves to next match of previous search pattern, just like /<cr>
479    feed('/<c-g><cr>')
480    eq({ 0, 1, 6, 0 }, fn.getpos('.'))
481    -- moves to next match of previous search pattern, just like /<cr>
482    feed('/<cr>')
483    eq({ 0, 1, 12, 0 }, fn.getpos('.'))
484    -- moves to next match of previous search pattern, just like /<cr>
485    feed('/<c-t><cr>')
486    eq({ 0, 2, 1, 0 }, fn.getpos('.'))
487
488    -- 8.0.1304, test that C-g and C-t works with incsearch and empty pattern
489    feed('<esc>/fi<CR>')
490    screen:expect([[
491      the {10:fi}rst line      │the {10:fi}rst line     |
492      in a little {10:^fi}le    │in a little {10:fi}le   |
493      {1:~                   }│{1:~                  }|*4
494      /fi                                     |
495    ]])
496    feed('//')
497    screen:expect([[
498      the {2:fi}rst line      │the {10:fi}rst line     |
499      in a little {10:fi}le    │in a little {10:fi}le   |
500      {1:~                   }│{1:~                  }|*4
501      //^                                      |
502    ]])
503    feed('<C-g>')
504    screen:expect([[
505      the {10:fi}rst line      │the {10:fi}rst line     |
506      in a little {2:fi}le    │in a little {10:fi}le   |
507      {1:~                   }│{1:~                  }|*4
508      //^                                      |
509    ]])
510    feed('<Esc>')
511
512    -- incsearch works after c_CTRL-R_CTRL-R
513    command('let @" = "file"')
514    feed('/<C-R><C-R>"')
515    screen:expect([[
516      the first line      │the first line     |
517      in a little {2:file}    │in a little {10:file}   |
518      {1:~                   }│{1:~                  }|*4
519      /file^                                   |
520    ]])
521    feed('<Esc>')
522
523    command('set rtp^=test/functional/fixtures')
524    -- incsearch works after c_CTRL-R inserts clipboard register
525
526    command('let @* = "first"')
527    feed('/<C-R>*')
528    screen:expect([[
529      the {2:first} line      │the {10:first} line     |
530      in a little file    │in a little file   |
531      {1:~                   }│{1:~                  }|*4
532      /first^                                  |
533    ]])
534    feed('<Esc>')
535
536    command('let @+ = "little"')
537    feed('/<C-R>+')
538    screen:expect([[
539      the first line      │the first line     |
540      in a {2:little} file    │in a {10:little} file   |
541      {1:~                   }│{1:~                  }|*4
542      /little^                                 |
543    ]])
544    feed('<Esc>')
545  end)
546
547  it('works with incsearch and offset', function()
548    feed_command('set hlsearch')
549    feed_command('set incsearch')
550    insert([[
551      not the match you're looking for
552      the match is here]])
553
554    feed('gg/mat/e')
555    screen:expect([[
556      not the {2:mat}ch you're looking for        |
557      the {10:mat}ch is here                       |
558      {1:~                                       }|*4
559      /mat/e^                                  |
560    ]])
561
562    -- Search with count and /e offset fixed in Vim patch 7.4.532.
563    feed('<esc>2/mat/e')
564    screen:expect([[
565      not the {10:mat}ch you're looking for        |
566      the {2:mat}ch is here                       |
567      {1:~                                       }|*4
568      /mat/e^                                  |
569    ]])
570
571    feed('<cr>')
572    screen:expect([[
573      not the {10:mat}ch you're looking for        |
574      the {10:ma^t}ch is here                       |
575      {1:~                                       }|*4
576      /mat/e                                  |
577    ]])
578
579    command([[call setline(1, ['blah blah blah']) | 2,$delete_]])
580    feed('gg0/blah/e<C-g><cr>')
581    eq({ 0, 1, 9, 0 }, fn.getpos('.'))
582
583    feed('gg0/blah/e<C-g><C-g><cr>')
584    eq({ 0, 1, 14, 0 }, fn.getpos('.'))
585
586    feed('gg0/blah/e<C-g><C-g><C-t><cr>')
587    eq({ 0, 1, 9, 0 }, fn.getpos('.'))
588  end)
589
590  it('works with multiline regexps', function()
591    feed_command('set hlsearch')
592    feed('4oa  repeated line<esc>')
593    feed('/line\\na<cr>')
594    screen:expect([[
595                                              |
596      a  repeated {10:^line }                       |
597      {10:a}  repeated {10:line }                       |*2
598      {10:a}  repeated line                        |
599      {1:~                                       }|
600      {19:search hit BOTTOM, continuing at TOP}    |
601    ]])
602
603    -- it redraws rows above the changed one
604    feed('4Grb')
605    screen:expect([[
606                                              |
607      a  repeated {10:line }                       |
608      {10:a}  repeated line                        |
609      ^b  repeated {10:line }                       |
610      {10:a}  repeated line                        |
611      {1:~                                       }|
612      {19:search hit BOTTOM, continuing at TOP}    |
613    ]])
614  end)
615
616  it('works with matchadd and syntax', function()
617    feed_command('set hlsearch')
618    insert [[
619      very special text
620    ]]
621    feed_command('syntax on')
622    feed_command('highlight MyGroup guibg=Green gui=bold')
623    feed_command('highlight MyGroup2 guibg=Magenta gui=italic')
624    feed_command("call matchadd('MyGroup', 'special')")
625    feed_command("call matchadd('MyGroup2', 'text', 0)")
626
627    -- searchhl and matchadd matches are exclusive, only the highest priority
628    -- is used (and matches with lower priorities are not combined)
629    feed_command('/ial te')
630    screen:expect([[
631      very {102:spec^ial}{10: te}{103:xt}                       |
632                                              |
633      {1:~                                       }|*4
634      {19:search hit BOTTOM, continuing at TOP}    |
635    ]])
636
637    -- check highlights work also in folds
638    feed('zf4j')
639    screen:expect([[
640      {13:^+--  2 lines: very special text·········}|
641      {1:~                                       }|*5
642      {19:search hit BOTTOM, continuing at TOP}    |
643    ]])
644    command('%foldopen')
645    screen:expect([[
646      very {102:spec^ial}{10: te}{103:xt}                       |
647                                              |
648      {1:~                                       }|*4
649      {19:search hit BOTTOM, continuing at TOP}    |
650    ]])
651
652    feed_command('call clearmatches()')
653    screen:expect([[
654      very spec{10:^ial te}xt                       |
655                                              |
656      {1:~                                       }|*4
657      :call clearmatches()                    |
658    ]])
659
660    -- searchhl has priority over syntax, but in this case
661    -- nonconflicting attributes are combined
662    feed_command('syntax keyword MyGroup special')
663    screen:expect([[
664      very {102:spec}{104:^ial}{10: te}xt                       |
665                                              |
666      {1:~                                       }|*4
667      :syntax keyword MyGroup special         |
668    ]])
669  end)
670
671  it('highlights entire pattern on :%g@a/b', function()
672    command('set inccommand=nosplit')
673    feed('ia/b/c<Esc>')
674    feed(':%g@a/b')
675    screen:expect([[
676      {2:a/b}/c                                   |
677      {1:~                                       }|*5
678      :%g@a/b^                                 |
679    ]])
680  end)
681
682  it('incsearch is still visible after :redraw from K_EVENT', function()
683    fn.setline(1, { 'foo', 'bar' })
684    feed('/foo<CR>/bar')
685    screen:expect([[
686      foo                                     |
687      {2:bar}                                     |
688      {1:~                                       }|*4
689      /bar^                                    |
690    ]])
691    command('redraw!')
692    -- There is an intermediate state where :redraw! removes 'incsearch' highlight.
693    screen:expect_unchanged(true)
694  end)
695
696  it('no ml_get error with incsearch and <Cmd> mapping that opens window', function()
697    command('cnoremap <F3> <Cmd>vnew<Bar>redraw!<CR>')
698    fn.setline(1, { 'foo', 'bar', 'baz' })
699    feed('G/z')
700    screen:expect([[
701      foo                                     |
702      bar                                     |
703      ba{2:z}                                     |
704      {1:~                                       }|*3
705      /z^                                      |
706    ]])
707    feed('<F3>')
708    screen:expect([[
709                          │foo                |
710      {1:~                   }│bar                |
711      {1:~                   }│baz                |
712      {1:~                   }│{1:~                  }|*2
713      {3:[No Name]            }{2:[No Name] [+]      }|
714      /z^                                      |
715    ]])
716    eq('', n.api.nvim_get_vvar('errmsg'))
717    feed('<C-G>')
718    screen:expect_unchanged(true)
719    eq('', n.api.nvim_get_vvar('errmsg'))
720  end)
721
722  it('highlight is not after redraw during substitute confirm prompt', function()
723    fn.setline(1, { 'foo', 'bar' })
724    command('set nohlsearch')
725    feed(':%s/bar/baz/c<CR>')
726    screen:try_resize(screen._width, screen._height - 1)
727    screen:expect([[
728      foo                                     |
729      {2:bar}                                     |
730      {1:~                                       }|
731      {3:                                        }|
732      {6:replace with baz? (y)es/(n)o/(a)ll/(q)ui}|
733      {6:t/(l)ast/scroll up(^E)/down(^Y)}^         |
734    ]])
735  end)
736end)