robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
31.5 KiB913 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, pending = t.describe, t.it, t.before_each, t.pending
6local clear, feed, insert = n.clear, n.feed, n.insert
7local command, neq = n.command, t.neq
8local api = n.api
9local eq = t.eq
10local pcall_err = t.pcall_err
11local set_virtual_text = api.nvim_buf_set_virtual_text
12
13describe('Buffer highlighting', function()
14  local screen
15
16  before_each(function()
17    clear()
18    command('syntax on')
19    screen = Screen.new(40, 8)
20    screen:set_default_attr_ids({
21      [1] = { bold = true, foreground = Screen.colors.Blue },
22      [2] = { foreground = Screen.colors.Fuchsia }, -- String
23      [3] = { foreground = Screen.colors.Brown, bold = true }, -- Statement
24      [4] = { foreground = Screen.colors.SlateBlue }, -- Special
25      [5] = { bold = true, foreground = Screen.colors.SlateBlue },
26      [6] = { foreground = Screen.colors.DarkCyan }, -- Identifier
27      [7] = { bold = true },
28      [8] = { underline = true, bold = true, foreground = Screen.colors.SlateBlue },
29      [9] = { foreground = Screen.colors.SlateBlue, underline = true },
30      [10] = { foreground = Screen.colors.Red },
31      [11] = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
32      [12] = { foreground = Screen.colors.Blue1 },
33      [13] = { foreground = Screen.colors.Black, background = Screen.colors.LightGrey },
34      [14] = { background = Screen.colors.Gray90 },
35      [15] = { background = Screen.colors.Gray90, bold = true, foreground = Screen.colors.Brown },
36      [16] = { foreground = Screen.colors.Magenta, background = Screen.colors.Gray90 },
37      [17] = { foreground = Screen.colors.Magenta, background = Screen.colors.LightRed },
38      [18] = { background = Screen.colors.LightRed },
39      [19] = { foreground = Screen.colors.Blue1, background = Screen.colors.LightRed },
40      [20] = { underline = true, bold = true, foreground = Screen.colors.Cyan4 },
41    })
42  end)
43
44  local add_highlight = api.nvim_buf_add_highlight
45  local clear_namespace = api.nvim_buf_clear_namespace
46
47  it('works', function()
48    insert([[
49      these are some lines
50      with colorful text]])
51    feed('+')
52
53    screen:expect([[
54      these are some lines                    |
55      with colorful tex^t                      |
56      {1:~                                       }|*5
57                                              |
58    ]])
59
60    add_highlight(0, -1, 'String', 0, 10, 14)
61    add_highlight(0, -1, 'Statement', 1, 5, -1)
62
63    screen:expect([[
64      these are {2:some} lines                    |
65      with {3:colorful tex^t}                      |
66      {1:~                                       }|*5
67                                              |
68    ]])
69
70    feed('ggo<esc>')
71    screen:expect([[
72      these are {2:some} lines                    |
73      ^                                        |
74      with {3:colorful text}                      |
75      {1:~                                       }|*4
76                                              |
77    ]])
78
79    clear_namespace(0, -1, 0, -1)
80    screen:expect([[
81      these are some lines                    |
82      ^                                        |
83      with colorful text                      |
84      {1:~                                       }|*4
85                                              |
86    ]])
87  end)
88
89  describe('support using multiple namespaces', function()
90    local id1, id2
91    before_each(function()
92      insert([[
93        a longer example
94        in order to demonstrate
95        combining highlights
96        from different sources]])
97
98      command('hi ImportantWord gui=bold cterm=bold')
99      id1 = add_highlight(0, 0, 'ImportantWord', 0, 2, 8)
100      add_highlight(0, id1, 'ImportantWord', 1, 12, -1)
101      add_highlight(0, id1, 'ImportantWord', 2, 0, 9)
102      add_highlight(0, id1, 'ImportantWord', 3, 5, 14)
103
104      -- add_highlight can be called like this to get a new source
105      -- without adding any highlight
106      id2 = add_highlight(0, 0, '', 0, 0, 0)
107      neq(id1, id2)
108
109      add_highlight(0, id2, 'Special', 0, 2, 8)
110      add_highlight(0, id2, 'Identifier', 1, 3, 8)
111      add_highlight(0, id2, 'Special', 1, 14, 20)
112      add_highlight(0, id2, 'Underlined', 2, 6, 12)
113      add_highlight(0, id2, 'Underlined', 3, 0, 9)
114
115      screen:expect([[
116        a {5:longer} example                        |
117        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
118        {7:combin}{8:ing}{9: hi}ghlights                    |
119        {9:from }{8:diff}{7:erent} source^s                  |
120        {1:~                                       }|*3
121                                                |
122      ]])
123    end)
124
125    it('and clearing the first added', function()
126      clear_namespace(0, id1, 0, -1)
127      screen:expect([[
128        a {4:longer} example                        |
129        in {6:order} to de{4:monstr}ate                 |
130        combin{9:ing hi}ghlights                    |
131        {9:from diff}erent source^s                  |
132        {1:~                                       }|*3
133                                                |
134      ]])
135    end)
136
137    it('and clearing using deprecated name', function()
138      api.nvim_buf_clear_highlight(0, id1, 0, -1)
139      screen:expect([[
140        a {4:longer} example                        |
141        in {6:order} to de{4:monstr}ate                 |
142        combin{9:ing hi}ghlights                    |
143        {9:from diff}erent source^s                  |
144        {1:~                                       }|*3
145                                                |
146      ]])
147    end)
148
149    it('and clearing the second added', function()
150      clear_namespace(0, id2, 0, -1)
151      screen:expect([[
152        a {7:longer} example                        |
153        in order to {7:demonstrate}                 |
154        {7:combining} highlights                    |
155        from {7:different} source^s                  |
156        {1:~                                       }|*3
157                                                |
158      ]])
159    end)
160
161    it('and clearing line ranges', function()
162      clear_namespace(0, -1, 0, 1)
163      clear_namespace(0, id1, 1, 2)
164      clear_namespace(0, id2, 2, -1)
165      screen:expect([[
166        a longer example                        |
167        in {6:order} to de{4:monstr}ate                 |
168        {7:combining} highlights                    |
169        from {7:different} source^s                  |
170        {1:~                                       }|*3
171                                                |
172      ]])
173    end)
174
175    it('and renumbering lines', function()
176      feed('3Gddggo<esc>')
177      screen:expect([[
178        a {5:longer} example                        |
179        ^                                        |
180        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
181        {9:from }{8:diff}{7:erent} sources                  |
182        {1:~                                       }|*3
183                                                |
184      ]])
185
186      -- TODO(bfedl): this behaves a bit weirdly due to the highlight on
187      -- the deleted line wrapping around. we should invalidate
188      -- highlights when they are completely inside deleted text
189      command('3move 4')
190      screen:expect {
191        grid = [[
192        a {5:longer} example                        |
193                                                |
194        {8:from different sources}                  |
195        {8:^in }{20:order}{8: to demonstrate}                 |
196        {1:~                                       }|*3
197                                                |
198      ]],
199      }
200      --screen:expect([[
201      --  a {5:longer} example                        |
202      --                                          |
203      --  {9:from }{8:diff}{7:erent} sources                  |
204      --  ^in {6:order} to {7:de}{5:monstr}{7:ate}                 |
205      --  {1:~                                       }|*3
206      --                                          |
207      --]])
208
209      command('undo')
210      screen:expect {
211        grid = [[
212        a {5:longer} example                        |
213        ^                                        |
214        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
215        {9:from }{8:diff}{7:erent} sources                  |
216        {1:~                                       }|*3
217        1 change; before #4  {MATCH:.*}|
218      ]],
219      }
220
221      command('undo')
222      screen:expect {
223        grid = [[
224        ^a {5:longer} example                        |
225        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
226        {9:from }{8:diff}{7:erent} sources                  |
227        {1:~                                       }|*4
228        1 line less; before #3  {MATCH:.*}|
229      ]],
230      }
231
232      command('undo')
233      screen:expect {
234        grid = [[
235        a {5:longer} example                        |
236        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
237        {7:^combin}{8:ing}{9: hi}ghlights                    |
238        {9:from }{8:diff}{7:erent} sources                  |
239        {1:~                                       }|*3
240        1 more line; before #2  {MATCH:.*}|
241      ]],
242      }
243    end)
244
245    it('and moving lines around', function()
246      command('2move 3')
247      screen:expect {
248        grid = [[
249        a {5:longer} example                        |
250        {7:combin}{8:ing}{9: hi}ghlights                    |
251        ^in {6:order} to {7:de}{5:monstr}{7:ate}                 |
252        {9:from }{8:diff}{7:erent} sources                  |
253        {1:~                                       }|*3
254                                                |
255      ]],
256      }
257
258      command('1,2move 4')
259      screen:expect {
260        grid = [[
261        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
262        {9:from }{8:diff}{7:erent} sources                  |
263        a {5:longer} example                        |
264        {7:^combin}{8:ing}{9: hi}ghlights                    |
265        {1:~                                       }|*3
266                                                |
267      ]],
268      }
269
270      command('undo')
271      screen:expect {
272        grid = [[
273        a {5:longer} example                        |
274        {7:combin}{8:ing}{9: hi}ghlights                    |
275        ^in {6:order} to {7:de}{5:monstr}{7:ate}                 |
276        {9:from }{8:diff}{7:erent} sources                  |
277        {1:~                                       }|*3
278        2 changes; before #3  {MATCH:.*}|
279      ]],
280      }
281
282      command('undo')
283      screen:expect {
284        grid = [[
285        a {5:longer} example                        |
286        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
287        {7:combin}{8:ing}{9: hi}ghlights                    |
288        {9:from }{8:diff}{7:erent} source^s                  |
289        {1:~                                       }|*3
290        1 change; before #2  {MATCH:.*}|
291      ]],
292      }
293    end)
294
295    it('and adjusting columns', function()
296      -- insert before
297      feed('ggiquite <esc>')
298      screen:expect {
299        grid = [[
300        quite^ a {5:longer} example                  |
301        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
302        {7:combin}{8:ing}{9: hi}ghlights                    |
303        {9:from }{8:diff}{7:erent} sources                  |
304        {1:~                                       }|*3
305                                                |
306      ]],
307      }
308
309      feed('u')
310      screen:expect {
311        grid = [[
312        ^a {5:longer} example                        |
313        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
314        {7:combin}{8:ing}{9: hi}ghlights                    |
315        {9:from }{8:diff}{7:erent} sources                  |
316        {1:~                                       }|*3
317        1 change; before #2  {MATCH:.*}|
318      ]],
319      }
320
321      -- change/insert in the middle
322      feed('+fesAAAA')
323      screen:expect {
324        grid = [[
325        a {5:longer} example                        |
326        in {6:ordAAAA^r} to {7:de}{5:monstr}{7:ate}              |
327        {7:combin}{8:ing}{9: hi}ghlights                    |
328        {9:from }{8:diff}{7:erent} sources                  |
329        {1:~                                       }|*3
330        {7:-- INSERT --}                            |
331      ]],
332      }
333
334      feed('<esc>tdD')
335      screen:expect {
336        grid = [[
337        a {5:longer} example                        |
338        in {6:ordAAAAr} t^o                          |
339        {7:combin}{8:ing}{9: hi}ghlights                    |
340        {9:from }{8:diff}{7:erent} sources                  |
341        {1:~                                       }|*3
342                                                |
343      ]],
344      }
345
346      feed('u')
347      screen:expect {
348        grid = [[
349        a {5:longer} example                        |
350        in {6:ordAAAAr} to^ {7:de}{5:monstr}{7:ate}              |
351        {7:combin}{8:ing}{9: hi}ghlights                    |
352        {9:from }{8:diff}{7:erent} sources                  |
353        {1:~                                       }|*3
354        1 change; before #4  {MATCH:.*}|
355      ]],
356      }
357
358      feed('u')
359      screen:expect {
360        grid = [[
361        a {5:longer} example                        |
362        in {6:ord^er} to {7:de}{5:monstr}{7:ate}                 |
363        {7:combin}{8:ing}{9: hi}ghlights                    |
364        {9:from }{8:diff}{7:erent} sources                  |
365        {1:~                                       }|*3
366        1 change; before #3  {MATCH:.*}|
367      ]],
368      }
369    end)
370
371    it('and joining lines', function()
372      feed('ggJJJ')
373      screen:expect {
374        grid = [[
375        a {5:longer} example in {6:order} to {7:de}{5:monstr}{7:ate}|
376         {7:combin}{8:ing}{9: hi}ghlights^ {9:from }{8:diff}{7:erent} sou|
377        rces                                    |
378        {1:~                                       }|*4
379                                                |
380      ]],
381      }
382
383      feed('uuu')
384      screen:expect {
385        grid = [[
386        ^a {5:longer} example                        |
387        in {6:order} to {7:de}{5:monstr}{7:ate}                 |
388        {7:combin}{8:ing}{9: hi}ghlights                    |
389        {9:from }{8:diff}{7:erent} sources                  |
390        {1:~                                       }|*3
391        1 more line; before #2  {MATCH:.*}|
392      ]],
393      }
394    end)
395
396    it('and splitting lines', function()
397      feed('2Gtti<cr>')
398      screen:expect {
399        grid = [[
400        a {5:longer} example                        |
401        in {6:order}                                |
402        ^ to {7:de}{5:monstr}{7:ate}                         |
403        {7:combin}{8:ing}{9: hi}ghlights                    |
404        {9:from }{8:diff}{7:erent} sources                  |
405        {1:~                                       }|*2
406        {7:-- INSERT --}                            |
407      ]],
408      }
409
410      feed('<esc>tsi<cr>')
411      screen:expect {
412        grid = [[
413        a {5:longer} example                        |
414        in {6:order}                                |
415         to {7:de}{5:mo}                                |
416        {5:^nstr}{7:ate}                                 |
417        {7:combin}{8:ing}{9: hi}ghlights                    |
418        {9:from }{8:diff}{7:erent} sources                  |
419        {1:~                                       }|
420        {7:-- INSERT --}                            |
421      ]],
422      }
423
424      feed('<esc>u')
425      screen:expect {
426        grid = [[
427        a {5:longer} example                        |
428        in {6:order}                                |
429         to {7:de}{5:mo^nstr}{7:ate}                         |
430        {7:combin}{8:ing}{9: hi}ghlights                    |
431        {9:from }{8:diff}{7:erent} sources                  |
432        {1:~                                       }|*2
433        1 line less; before #3  {MATCH:.*}|
434      ]],
435      }
436
437      feed('<esc>u')
438      screen:expect {
439        grid = [[
440        a {5:longer} example                        |
441        in {6:order}^ to {7:de}{5:monstr}{7:ate}                 |
442        {7:combin}{8:ing}{9: hi}ghlights                    |
443        {9:from }{8:diff}{7:erent} sources                  |
444        {1:~                                       }|*3
445        1 line less; before #2  {MATCH:.*}|
446      ]],
447      }
448    end)
449  end)
450
451  pending('prioritizes latest added highlight', function()
452    insert([[
453      three overlapping colors]])
454    add_highlight(0, 0, 'Identifier', 0, 6, 17)
455    add_highlight(0, 0, 'String', 0, 14, 23)
456    local id = add_highlight(0, 0, 'Special', 0, 0, 9)
457
458    screen:expect([[
459      {4:three ove}{6:rlapp}{2:ing color}^s                |
460      {1:~                                       }|*6
461                                              |
462    ]])
463
464    clear_namespace(0, id, 0, 1)
465    screen:expect([[
466      three {6:overlapp}{2:ing color}^s                |
467      {1:~                                       }|*6
468                                              |
469    ]])
470  end)
471
472  it('prioritizes earlier highlight groups (TEMP)', function()
473    insert([[
474      three overlapping colors]])
475    add_highlight(0, 0, 'Identifier', 0, 6, 17)
476    add_highlight(0, 0, 'String', 0, 14, 23)
477    local id = add_highlight(0, 0, 'Special', 0, 0, 9)
478
479    screen:expect {
480      grid = [[
481      {4:three }{6:overlapp}{2:ing color}^s                |
482      {1:~                                       }|*6
483                                              |
484    ]],
485    }
486
487    clear_namespace(0, id, 0, 1)
488    screen:expect {
489      grid = [[
490      three {6:overlapp}{2:ing color}^s                |
491      {1:~                                       }|*6
492                                              |
493    ]],
494    }
495  end)
496
497  it('respects priority', function()
498    local id = api.nvim_create_namespace('')
499    insert [[foobar]]
500
501    api.nvim_buf_set_extmark(0, id, 0, 0, {
502      end_line = 0,
503      end_col = 5,
504      hl_group = 'Statement',
505      priority = 100,
506    })
507    api.nvim_buf_set_extmark(0, id, 0, 0, {
508      end_line = 0,
509      end_col = 6,
510      hl_group = 'String',
511      priority = 1,
512    })
513
514    screen:expect [[
515      {3:fooba}{2:^r}                                  |
516      {1:~                                       }|*6
517                                              |
518    ]]
519
520    clear_namespace(0, id, 0, -1)
521    screen:expect {
522      grid = [[
523      fooba^r                                  |
524      {1:~                                       }|*6
525                                              |
526    ]],
527    }
528
529    api.nvim_buf_set_extmark(0, id, 0, 0, {
530      end_line = 0,
531      end_col = 6,
532      hl_group = 'String',
533      priority = 1,
534    })
535    api.nvim_buf_set_extmark(0, id, 0, 0, {
536      end_line = 0,
537      end_col = 5,
538      hl_group = 'Statement',
539      priority = 100,
540    })
541
542    screen:expect [[
543      {3:fooba}{2:^r}                                  |
544      {1:~                                       }|*6
545                                              |
546    ]]
547  end)
548
549  it('works with multibyte text', function()
550    insert([[
551      Ta båten över sjön!]])
552    add_highlight(0, -1, 'Identifier', 0, 3, 9)
553    add_highlight(0, -1, 'String', 0, 16, 21)
554
555    screen:expect([[
556      Ta {6:båten} över {2:sjön}^!                     |
557      {1:~                                       }|*6
558                                              |
559    ]])
560  end)
561
562  it('works with new syntax groups', function()
563    insert([[
564      fancy code in a new fancy language]])
565    add_highlight(0, -1, 'FancyLangItem', 0, 0, 5)
566    screen:expect([[
567      fancy code in a new fancy languag^e      |
568      {1:~                                       }|*6
569                                              |
570    ]])
571
572    command('hi FancyLangItem guifg=red')
573    screen:expect([[
574      {10:fancy} code in a new fancy languag^e      |
575      {1:~                                       }|*6
576                                              |
577    ]])
578  end)
579
580  describe('virtual text decorations', function()
581    local id1, id2 ---@type integer, integer
582    before_each(function()
583      insert([[
584        1 + 2
585        3 +
586        x = 4]])
587      feed('O<esc>20A5, <esc>gg')
588      screen:expect([[
589        ^1 + 2                                   |
590        3 +                                     |
591        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
592        , 5, 5, 5, 5, 5, 5,                     |
593        x = 4                                   |
594        {1:~                                       }|*2
595                                                |
596      ]])
597
598      id1 = set_virtual_text(0, 0, 0, { { '=', 'Statement' }, { ' 3', 'Number' } }, {})
599      set_virtual_text(0, id1, 1, { { 'ERROR:', 'ErrorMsg' }, { ' invalid syntax' } }, {})
600      id2 = set_virtual_text(0, 0, 2, {
601        {
602          'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
603        },
604      }, {})
605      neq(id2, id1)
606    end)
607
608    it('works', function()
609      screen:expect([[
610        ^1 + 2 {3:=}{2: 3}                               |
611        3 + {11:ERROR:} invalid syntax               |
612        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
613        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
614        x = 4                                   |
615        {1:~                                       }|*2
616                                                |
617      ]])
618
619      clear_namespace(0, id1, 0, -1)
620      screen:expect([[
621        ^1 + 2                                   |
622        3 +                                     |
623        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
624        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
625        x = 4                                   |
626        {1:~                                       }|*2
627                                                |
628      ]])
629
630      -- Handles doublewidth chars, leaving a space if truncating
631      -- in the middle of a char
632      eq(
633        -1,
634        set_virtual_text(
635          0,
636          -1,
637          1,
638          { { '暗x事zz速野谷質結育副住新覚丸活解終事', 'Comment' } },
639          {}
640        )
641      )
642      screen:expect([[
643        ^1 + 2                                   |
644        3 + {12:暗x事zz速野谷質結育副住新覚丸活解終 }|
645        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
646        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
647        x = 4                                   |
648        {1:~                                       }|*2
649                                                |
650      ]])
651
652      feed('2Gx')
653      screen:expect([[
654        1 + 2                                   |
655        ^ + {12:暗x事zz速野谷質結育副住新覚丸活解終事}|
656        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
657        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
658        x = 4                                   |
659        {1:~                                       }|*2
660                                                |
661      ]])
662
663      feed('2Gdd')
664      -- TODO(bfredl): currently decorations get moved from a deleted line
665      -- to the next one. We might want to add "invalidation" when deleting
666      -- over a decoration.
667      screen:expect {
668        grid = [[
669        1 + 2                                   |
670        ^5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
671        , 5, 5, 5, 5, 5, 5,  {12:暗x事zz速野谷質結育}|
672        x = 4                                   |
673        {1:~                                       }|*3
674                                                |
675      ]],
676      }
677      --screen:expect([[
678      --  1 + 2                                   |
679      --  ^5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
680      --  , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
681      --  x = 4                                   |
682      --  {1:~                                       }|*3
683      --                                          |
684      --]])
685    end)
686
687    it('validates contents', function()
688      -- this used to leak memory
689      eq(
690        "Invalid 'chunk': expected Array, got String",
691        pcall_err(set_virtual_text, 0, id1, 0, { 'texty' }, {})
692      )
693      eq(
694        "Invalid 'chunk': expected Array, got String",
695        pcall_err(set_virtual_text, 0, id1, 0, { { 'very' }, 'texty' }, {})
696      )
697    end)
698
699    it('can be retrieved', function()
700      local get_extmarks = api.nvim_buf_get_extmarks
701      local line_count = api.nvim_buf_line_count
702
703      local s1 = { { 'Köttbullar', 'Comment' }, { 'Kräuterbutter' } }
704      local s2 = { { 'こんにちは', 'Comment' } }
705
706      set_virtual_text(0, id1, 0, s1, {})
707      eq({
708        {
709          1,
710          0,
711          0,
712          {
713            ns_id = id1,
714            priority = 0,
715            virt_text = s1,
716            -- other details
717            right_gravity = true,
718            virt_text_repeat_linebreak = false,
719            virt_text_pos = 'eol',
720            virt_text_hide = false,
721          },
722        },
723      }, get_extmarks(0, id1, { 0, 0 }, { 0, -1 }, { details = true }))
724
725      local lastline = line_count(0)
726      set_virtual_text(0, id1, line_count(0), s2, {})
727      eq({
728        {
729          3,
730          lastline,
731          0,
732          {
733            ns_id = id1,
734            priority = 0,
735            virt_text = s2,
736            -- other details
737            right_gravity = true,
738            virt_text_repeat_linebreak = false,
739            virt_text_pos = 'eol',
740            virt_text_hide = false,
741          },
742        },
743      }, get_extmarks(0, id1, { lastline, 0 }, { lastline, -1 }, { details = true }))
744
745      eq({}, get_extmarks(0, id1, { lastline + 9000, 0 }, { lastline + 9000, -1 }, {}))
746    end)
747
748    it('is not highlighted by visual selection', function()
749      feed('ggVG')
750      screen:expect([[
751        {13:1 + 2} {3:=}{2: 3}                               |
752        {13:3 +} {11:ERROR:} invalid syntax               |
753        {13:5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}|
754        {13:, 5, 5, 5, 5, 5, 5, } Lorem ipsum dolor s|
755        {13:^x = 4}                                   |
756        {1:~                                       }|*2
757        {7:-- VISUAL LINE --}                       |
758      ]])
759
760      feed('<esc>')
761      screen:expect([[
762        1 + 2 {3:=}{2: 3}                               |
763        3 + {11:ERROR:} invalid syntax               |
764        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
765        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
766        ^x = 4                                   |
767        {1:~                                       }|*2
768                                                |
769      ]])
770
771      -- special case: empty line has extra eol highlight
772      feed('ggd$')
773      screen:expect([[
774        ^ {3:=}{2: 3}                                    |
775        3 + {11:ERROR:} invalid syntax               |
776        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
777        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
778        x = 4                                   |
779        {1:~                                       }|*2
780                                                |
781      ]])
782
783      feed('jvk')
784      screen:expect([[
785        {13:^ }{3:=}{2: 3}                                    |
786        {13:3} + {11:ERROR:} invalid syntax               |
787        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
788        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
789        x = 4                                   |
790        {1:~                                       }|*2
791        {7:-- VISUAL --}                            |
792      ]])
793
794      feed('o')
795      screen:expect([[
796        {13: }{3:=}{2: 3}                                    |
797        {13:^3} + {11:ERROR:} invalid syntax               |
798        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
799        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
800        x = 4                                   |
801        {1:~                                       }|*2
802        {7:-- VISUAL --}                            |
803      ]])
804    end)
805
806    it('works with listchars', function()
807      command('set list listchars+=eol:$')
808      screen:expect([[
809        ^1 + 2{1:$}{3:=}{2: 3}                               |
810        3 +{1:$}{11:ERROR:} invalid syntax               |
811        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
812        , 5, 5, 5, 5, 5, 5,{1:-$}Lorem ipsum dolor s|
813        x = 4{1:$}                                  |
814        {1:~                                       }|*2
815                                                |
816      ]])
817
818      clear_namespace(0, -1, 0, -1)
819      screen:expect([[
820        ^1 + 2{1:$}                                  |
821        3 +{1:$}                                    |
822        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
823        , 5, 5, 5, 5, 5, 5,{1:-$}                   |
824        x = 4{1:$}                                  |
825        {1:~                                       }|*2
826                                                |
827      ]])
828    end)
829
830    it('works with cursorline', function()
831      command('set cursorline')
832
833      screen:expect {
834        grid = [[
835        {14:^1 + 2 }{3:=}{2: 3}{14:                               }|
836        3 + {11:ERROR:} invalid syntax               |
837        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
838        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
839        x = 4                                   |
840        {1:~                                       }|*2
841                                                |
842      ]],
843      }
844
845      feed('j')
846      screen:expect {
847        grid = [[
848        1 + 2 {3:=}{2: 3}                               |
849        {14:^3 + }{11:ERROR:} invalid syntax{14:               }|
850        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
851        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
852        x = 4                                   |
853        {1:~                                       }|*2
854                                                |
855      ]],
856      }
857
858      feed('j')
859      screen:expect {
860        grid = [[
861        1 + 2 {3:=}{2: 3}                               |
862        3 + {11:ERROR:} invalid syntax               |
863        {14:^5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}|
864        {14:, 5, 5, 5, 5, 5, 5,  }Lorem ipsum dolor s|
865        x = 4                                   |
866        {1:~                                       }|*2
867                                                |
868      ]],
869      }
870    end)
871
872    it('works with color column', function()
873      eq(-1, set_virtual_text(0, -1, 3, { { '暗x事', 'Comment' } }, {}))
874      screen:expect {
875        grid = [[
876        ^1 + 2 {3:=}{2: 3}                               |
877        3 + {11:ERROR:} invalid syntax               |
878        5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
879        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
880        x = 4 {12:暗x事}                             |
881        {1:~                                       }|*2
882                                                |
883      ]],
884      }
885
886      command('set colorcolumn=9')
887      screen:expect {
888        grid = [[
889        ^1 + 2 {3:=}{2: 3}                               |
890        3 + {11:ERROR:} invalid syntax               |
891        5, 5, 5,{18: }5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5|
892        , 5, 5, 5, 5, 5, 5,  Lorem ipsum dolor s|
893        x = 4 {12:暗x事}                             |
894        {1:~                                       }|*2
895                                                |
896      ]],
897      }
898    end)
899  end)
900
901  it('and virtual text use the same namespace counter', function()
902    local base = vim.iter(api.nvim_get_namespaces()):fold(0, function(acc, _, v)
903      return math.max(acc, v)
904    end)
905
906    eq(base + 1, add_highlight(0, 0, 'String', 0, 0, -1))
907    eq(base + 2, set_virtual_text(0, 0, 0, { { '= text', 'Comment' } }, {}))
908    eq(base + 3, api.nvim_create_namespace('my-ns'))
909    eq(base + 4, add_highlight(0, 0, 'String', 0, 0, -1))
910    eq(base + 5, set_virtual_text(0, 0, 0, { { '= text', 'Comment' } }, {}))
911    eq(base + 6, api.nvim_create_namespace('other-ns'))
912  end)
913end)