robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
333.8 KiB8757 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 = n.clear
7local feed = n.feed
8local insert = n.insert
9local exec_lua = n.exec_lua
10local exec = n.exec
11local expect_events = t.expect_events
12local api = n.api
13local fn = n.fn
14local command = n.command
15local eq = t.eq
16local assert_alive = n.assert_alive
17local pcall_err = t.pcall_err
18
19--- @return integer
20local function setup_provider(code)
21  return exec_lua([[
22    local api = vim.api
23    _G.ns1 = api.nvim_create_namespace "ns1"
24  ]] .. (code or [[
25    beamtrace = {}
26    local function on_do(kind, ...)
27      table.insert(beamtrace, {kind, ...})
28    end
29  ]]) .. [[
30    api.nvim_set_decoration_provider(_G.ns1, {
31      on_start = on_do; on_buf = on_do;
32      on_win = on_do; on_line = on_do; on_range = on_do;
33      on_end = on_do; _on_spell_nav = on_do;
34    })
35    return _G.ns1
36  ]])
37end
38
39local function setup_screen(screen)
40  screen:set_default_attr_ids {
41    [1] = { bold = true, foreground = Screen.colors.Blue },
42    [2] = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
43    [3] = { foreground = Screen.colors.Brown },
44    [4] = { foreground = Screen.colors.Blue1 },
45    [5] = { foreground = Screen.colors.Magenta },
46    [6] = { bold = true, foreground = Screen.colors.Brown },
47    [7] = { background = Screen.colors.Gray90 },
48    [8] = { bold = true, reverse = true },
49    [9] = { reverse = true },
50    [10] = { italic = true, background = Screen.colors.Magenta },
51    [11] = { foreground = Screen.colors.Red, background = tonumber('0x005028') },
52    [12] = { foreground = tonumber('0x990000') },
53    [13] = { background = Screen.colors.LightBlue },
54    [14] = { background = Screen.colors.WebGray, foreground = Screen.colors.DarkBlue },
55    [15] = { special = Screen.colors.Blue, undercurl = true },
56    [16] = { special = Screen.colors.Red, undercurl = true },
57    [17] = { foreground = Screen.colors.Red },
58    [18] = { bold = true, foreground = Screen.colors.SeaGreen },
59    [19] = { bold = true },
60    [20] = { foreground = Screen.colors.Grey0, background = Screen.colors.LightGrey },
61  }
62end
63
64describe('decorations providers', function()
65  local screen ---@type test.functional.ui.screen
66  before_each(function()
67    clear()
68    screen = Screen.new(40, 8)
69    setup_screen(screen)
70  end)
71
72  local mulholland = [[
73    // just to see if there was an accident
74    // on Mulholland Drive
75    try_start();
76    bufref_T save_buf;
77    switch_buffer(&save_buf, buf);
78    posp = getmark(mark, false);
79    restore_buffer(&save_buf); ]]
80
81  local function check_trace(expected)
82    local actual = exec_lua [[ local b = beamtrace beamtrace = {} return b ]]
83    expect_events(expected, actual, 'beam trace')
84  end
85
86  it('does not OOM when inserting, rather than appending, to the decoration provider vector', function()
87    -- Add a dummy decoration provider with a larger ns id than what setup_provider() creates.
88    -- This forces get_decor_provider() to insert into the providers vector,
89    -- rather than append, which used to spin in an infinite loop allocating
90    -- memory until nvim crashed/was killed.
91    setup_provider([[
92      local ns2 = api.nvim_create_namespace "ns2"
93      api.nvim_set_decoration_provider(ns2, {})
94    ]])
95    assert_alive()
96  end)
97
98  it('leave a trace', function()
99    insert(mulholland)
100
101    setup_provider()
102
103    screen:expect {
104      grid = [[
105      // just to see if there was an accident |
106      // on Mulholland Drive                  |
107      try_start();                            |
108      bufref_T save_buf;                      |
109      switch_buffer(&save_buf, buf);          |
110      posp = getmark(mark, false);            |
111      restore_buffer(&save_buf);^              |
112                                              |
113    ]],
114    }
115    check_trace {
116      { 'start', 4 },
117      { 'win', 1000, 1, 0, 6 },
118      { 'line', 1000, 1, 0 },
119      { 'range', 1000, 1, 0, 0, 1, 0 },
120      { 'line', 1000, 1, 1 },
121      { 'range', 1000, 1, 1, 0, 2, 0 },
122      { 'line', 1000, 1, 2 },
123      { 'range', 1000, 1, 2, 0, 3, 0 },
124      { 'line', 1000, 1, 3 },
125      { 'range', 1000, 1, 3, 0, 4, 0 },
126      { 'line', 1000, 1, 4 },
127      { 'range', 1000, 1, 4, 0, 5, 0 },
128      { 'line', 1000, 1, 5 },
129      { 'range', 1000, 1, 5, 0, 6, 0 },
130      { 'line', 1000, 1, 6 },
131      { 'range', 1000, 1, 6, 0, 7, 0 },
132      { 'end', 4 },
133    }
134
135    feed 'iü<esc>'
136    screen:expect {
137      grid = [[
138      // just to see if there was an accident |
139      // on Mulholland Drive                  |
140      try_start();                            |
141      bufref_T save_buf;                      |
142      switch_buffer(&save_buf, buf);          |
143      posp = getmark(mark, false);            |
144      restore_buffer(&save_buf);^ü             |
145                                              |
146    ]],
147    }
148    check_trace {
149      { 'start', 5 },
150      { 'buf', 1, 5 },
151      { 'win', 1000, 1, 0, 6 },
152      { 'line', 1000, 1, 6 },
153      { 'range', 1000, 1, 6, 0, 7, 0 },
154      { 'end', 5 },
155    }
156  end)
157
158  it('can have single provider', function()
159    insert(mulholland)
160    setup_provider [[
161      local hl = api.nvim_get_hl_id_by_name "ErrorMsg"
162      local test_ns = api.nvim_create_namespace "mulholland"
163      function on_do(event, ...)
164        if event == "line" then
165          local win, buf, line = ...
166          api.nvim_buf_set_extmark(buf, test_ns, line, line,
167                             { end_line = line, end_col = line+1,
168                               hl_group = hl,
169                               ephemeral = true
170                              })
171        end
172      end
173    ]]
174
175    screen:expect {
176      grid = [[
177      {2:/}/ just to see if there was an accident |
178      /{2:/} on Mulholland Drive                  |
179      tr{2:y}_start();                            |
180      buf{2:r}ef_T save_buf;                      |
181      swit{2:c}h_buffer(&save_buf, buf);          |
182      posp {2:=} getmark(mark, false);            |
183      restor{2:e}_buffer(&save_buf);^              |
184                                              |
185    ]],
186    }
187  end)
188
189  it('can indicate spellchecked points', function()
190    exec [[
191    set spell
192    set spelloptions=noplainbuffer
193    syntax off
194    ]]
195
196    insert [[
197    I am well written text.
198    i am not capitalized.
199    I am a speling mistakke.
200    ]]
201
202    setup_provider [[
203      local ns = api.nvim_create_namespace "spell"
204      beamtrace = {}
205      local function on_do(kind, ...)
206        if kind == 'win' or kind == 'spell' then
207          api.nvim_buf_set_extmark(0, ns, 0, 0, {
208            end_row = 2,
209            end_col = 23,
210            spell = true,
211            priority = 20,
212            ephemeral = true
213          })
214        end
215        table.insert(beamtrace, {kind, ...})
216      end
217    ]]
218
219    check_trace {
220      { 'start', 5 },
221      { 'win', 1000, 1, 0, 3 },
222      { 'line', 1000, 1, 0 },
223      { 'range', 1000, 1, 0, 0, 1, 0 },
224      { 'line', 1000, 1, 1 },
225      { 'range', 1000, 1, 1, 0, 2, 0 },
226      { 'line', 1000, 1, 2 },
227      { 'range', 1000, 1, 2, 0, 3, 0 },
228      { 'line', 1000, 1, 3 },
229      { 'range', 1000, 1, 3, 0, 4, 0 },
230      { 'end', 5 },
231    }
232
233    feed 'gg0'
234
235    screen:expect {
236      grid = [[
237      ^I am well written text.                 |
238      {15:i} am not capitalized.                   |
239      I am a {16:speling} {16:mistakke}.                |
240                                              |
241      {1:~                                       }|*3
242                                              |
243    ]],
244    }
245
246    feed ']s'
247    check_trace {
248      { 'spell', 1000, 1, 1, 0, 1, -1 },
249    }
250    screen:expect {
251      grid = [[
252      I am well written text.                 |
253      {15:^i} am not capitalized.                   |
254      I am a {16:speling} {16:mistakke}.                |
255                                              |
256      {1:~                                       }|*3
257                                              |
258    ]],
259    }
260
261    feed ']s'
262    check_trace {
263      { 'spell', 1000, 1, 2, 7, 2, -1 },
264    }
265    screen:expect {
266      grid = [[
267      I am well written text.                 |
268      {15:i} am not capitalized.                   |
269      I am a {16:^speling} {16:mistakke}.                |
270                                              |
271      {1:~                                       }|*3
272                                              |
273    ]],
274    }
275
276    -- spell=false with higher priority does disable spell
277    local ns = api.nvim_create_namespace 'spell'
278    local id = api.nvim_buf_set_extmark(0, ns, 0, 0, { priority = 30, end_row = 2, end_col = 23, spell = false })
279
280    screen:expect {
281      grid = [[
282      I am well written text.                 |
283      i am not capitalized.                   |
284      I am a ^speling mistakke.                |
285                                              |
286      {1:~                                       }|*3
287                                              |
288    ]],
289    }
290
291    feed ']s'
292    screen:expect {
293      grid = [[
294      I am well written text.                 |
295      i am not capitalized.                   |
296      I am a ^speling mistakke.                |
297                                              |
298      {1:~                                       }|*3
299      {17:search hit BOTTOM, continuing at TOP}    |
300    ]],
301    }
302    command('echo ""')
303
304    -- spell=false with lower priority doesn't disable spell
305    api.nvim_buf_set_extmark(0, ns, 0, 0, { id = id, priority = 10, end_row = 2, end_col = 23, spell = false })
306
307    screen:expect {
308      grid = [[
309      I am well written text.                 |
310      {15:i} am not capitalized.                   |
311      I am a {16:^speling} {16:mistakke}.                |
312                                              |
313      {1:~                                       }|*3
314                                              |
315    ]],
316    }
317
318    feed ']s'
319    screen:expect {
320      grid = [[
321      I am well written text.                 |
322      {15:i} am not capitalized.                   |
323      I am a {16:speling} {16:^mistakke}.                |
324                                              |
325      {1:~                                       }|*3
326                                              |
327    ]],
328    }
329  end)
330
331  it('can predefine highlights', function()
332    screen:try_resize(40, 16)
333    insert(mulholland)
334    exec [[
335      3
336      set ft=c
337      syntax on
338      set number cursorline
339      split
340    ]]
341    local ns1 = setup_provider()
342
343    for k, v in pairs {
344      LineNr = { italic = true, bg = 'Magenta' },
345      Comment = { fg = '#FF0000', bg = 80 * 256 + 40 },
346      CursorLine = { link = 'ErrorMsg' },
347    } do
348      api.nvim_set_hl(ns1, k, v)
349    end
350
351    screen:expect {
352      grid = [[
353      {3:  1 }{4:// just to see if there was an accid}|
354      {3:    }{4:ent}                                 |
355      {3:  2 }{4:// on Mulholland Drive}              |
356      {6:  3 }{7:^try_start();                        }|
357      {3:  4 }bufref_T save_buf;                  |
358      {3:  5 }switch_buffer(&save_buf, buf);      |
359      {3:  6 }posp = getmark(mark, {5:false});        |
360      {8:[No Name] [+]                           }|
361      {3:  2 }{4:// on Mulholland Drive}              |
362      {6:  3 }{7:try_start();                        }|
363      {3:  4 }bufref_T save_buf;                  |
364      {3:  5 }switch_buffer(&save_buf, buf);      |
365      {3:  6 }posp = getmark(mark, {5:false});        |
366      {3:  7 }restore_buffer(&save_buf);          |
367      {9:[No Name] [+]                           }|
368                                              |
369    ]],
370    }
371
372    api.nvim_set_hl_ns(ns1)
373    screen:expect {
374      grid = [[
375      {10:  1 }{11:// just to see if there was an accid}|
376      {10:    }{11:ent}                                 |
377      {10:  2 }{11:// on Mulholland Drive}              |
378      {6:  3 }{2:^try_start();                        }|
379      {10:  4 }bufref_T save_buf;                  |
380      {10:  5 }switch_buffer(&save_buf, buf);      |
381      {10:  6 }posp = getmark(mark, {5:false});        |
382      {8:[No Name] [+]                           }|
383      {10:  2 }{11:// on Mulholland Drive}              |
384      {6:  3 }{2:try_start();                        }|
385      {10:  4 }bufref_T save_buf;                  |
386      {10:  5 }switch_buffer(&save_buf, buf);      |
387      {10:  6 }posp = getmark(mark, {5:false});        |
388      {10:  7 }restore_buffer(&save_buf);          |
389      {9:[No Name] [+]                           }|
390                                              |
391    ]],
392    }
393
394    exec_lua [[
395      local api = vim.api
396      local thewin = api.nvim_get_current_win()
397      local ns2 = api.nvim_create_namespace 'ns2'
398      api.nvim_set_decoration_provider (ns2, {
399        on_win = function (_, win, buf)
400          api.nvim_set_hl_ns_fast(win == thewin and _G.ns1 or ns2)
401        end;
402      })
403    ]]
404    screen:expect {
405      grid = [[
406      {10:  1 }{11:// just to see if there was an accid}|
407      {10:    }{11:ent}                                 |
408      {10:  2 }{11:// on Mulholland Drive}              |
409      {6:  3 }{2:^try_start();                        }|
410      {10:  4 }bufref_T save_buf;                  |
411      {10:  5 }switch_buffer(&save_buf, buf);      |
412      {10:  6 }posp = getmark(mark, {5:false});        |
413      {8:[No Name] [+]                           }|
414      {3:  2 }{4:// on Mulholland Drive}              |
415      {6:  3 }{7:try_start();                        }|
416      {3:  4 }bufref_T save_buf;                  |
417      {3:  5 }switch_buffer(&save_buf, buf);      |
418      {3:  6 }posp = getmark(mark, {5:false});        |
419      {3:  7 }restore_buffer(&save_buf);          |
420      {9:[No Name] [+]                           }|
421                                              |
422    ]],
423    }
424  end)
425
426  it('can break an existing link', function()
427    insert(mulholland)
428    local ns1 = setup_provider()
429
430    exec [[
431      highlight OriginalGroup guifg='#990000'
432      highlight link LinkGroup OriginalGroup
433    ]]
434
435    api.nvim_buf_set_virtual_text(0, 0, 2, { { '- not red', 'LinkGroup' } }, {})
436    screen:expect {
437      grid = [[
438      // just to see if there was an accident |
439      // on Mulholland Drive                  |
440      try_start(); {12:- not red}                  |
441      bufref_T save_buf;                      |
442      switch_buffer(&save_buf, buf);          |
443      posp = getmark(mark, false);            |
444      restore_buffer(&save_buf);^              |
445                                              |
446    ]],
447    }
448
449    api.nvim_set_hl(ns1, 'LinkGroup', { fg = 'Blue' })
450    api.nvim_set_hl_ns(ns1)
451
452    screen:expect {
453      grid = [[
454      // just to see if there was an accident |
455      // on Mulholland Drive                  |
456      try_start(); {4:- not red}                  |
457      bufref_T save_buf;                      |
458      switch_buffer(&save_buf, buf);          |
459      posp = getmark(mark, false);            |
460      restore_buffer(&save_buf);^              |
461                                              |
462    ]],
463    }
464  end)
465
466  it("with 'default': do not break an existing link", function()
467    insert(mulholland)
468    local ns1 = setup_provider()
469
470    exec [[
471      highlight OriginalGroup guifg='#990000'
472      highlight link LinkGroup OriginalGroup
473    ]]
474
475    api.nvim_buf_set_virtual_text(0, 0, 2, { { '- not red', 'LinkGroup' } }, {})
476    screen:expect {
477      grid = [[
478      // just to see if there was an accident |
479      // on Mulholland Drive                  |
480      try_start(); {12:- not red}                  |
481      bufref_T save_buf;                      |
482      switch_buffer(&save_buf, buf);          |
483      posp = getmark(mark, false);            |
484      restore_buffer(&save_buf);^              |
485                                              |
486    ]],
487    }
488
489    api.nvim_set_hl(ns1, 'LinkGroup', { fg = 'Blue', default = true })
490    api.nvim_set_hl_ns(ns1)
491    feed 'k'
492
493    screen:expect {
494      grid = [[
495      // just to see if there was an accident |
496      // on Mulholland Drive                  |
497      try_start(); {12:- not red}                  |
498      bufref_T save_buf;                      |
499      switch_buffer(&save_buf, buf);          |
500      posp = getmark(mark, false^);            |
501      restore_buffer(&save_buf);              |
502                                              |
503    ]],
504    }
505  end)
506
507  it('can have virtual text', function()
508    insert(mulholland)
509    setup_provider [[
510      local hl = api.nvim_get_hl_id_by_name "ErrorMsg"
511      local test_ns = api.nvim_create_namespace "mulholland"
512      function on_do(event, ...)
513        if event == "line" then
514          local win, buf, line = ...
515          api.nvim_buf_set_extmark(buf, test_ns, line, 0, {
516            virt_text = {{'+', 'ErrorMsg'}};
517            virt_text_pos='overlay';
518            ephemeral = true;
519          })
520        end
521      end
522    ]]
523
524    screen:expect {
525      grid = [[
526      {2:+}/ just to see if there was an accident |
527      {2:+}/ on Mulholland Drive                  |
528      {2:+}ry_start();                            |
529      {2:+}ufref_T save_buf;                      |
530      {2:+}witch_buffer(&save_buf, buf);          |
531      {2:+}osp = getmark(mark, false);            |
532      {2:+}estore_buffer(&save_buf);^              |
533                                              |
534    ]],
535    }
536  end)
537
538  it('can have virtual text of the style: right_align', function()
539    insert(mulholland)
540    setup_provider [[
541      local hl = api.nvim_get_hl_id_by_name "ErrorMsg"
542      local test_ns = api.nvim_create_namespace "mulholland"
543      function on_do(event, ...)
544        if event == "line" then
545          local win, buf, line = ...
546          api.nvim_buf_set_extmark(buf, test_ns, line, 0, {
547            virt_text = {{'+'}, {string.rep(' ', line+1), 'ErrorMsg'}};
548            virt_text_pos='right_align';
549            ephemeral = true;
550          })
551        end
552      end
553    ]]
554
555    screen:expect {
556      grid = [[
557      // just to see if there was an acciden+{2: }|
558      // on Mulholland Drive               +{2:  }|
559      try_start();                        +{2:   }|
560      bufref_T save_buf;                 +{2:    }|
561      switch_buffer(&save_buf, buf);    +{2:     }|
562      posp = getmark(mark, false);     +{2:      }|
563      restore_buffer(&save_buf);^      +{2:       }|
564                                              |
565    ]],
566    }
567  end)
568
569  it('can have virtual text of the style: eol_right_align', function()
570    insert(mulholland)
571    setup_provider [[
572      local hl = api.nvim_get_hl_id_by_name "ErrorMsg"
573      local test_ns = api.nvim_create_namespace "mulholland"
574      function on_do(event, ...)
575        if event == "line" then
576          local win, buf, line = ...
577          api.nvim_buf_set_extmark(buf, test_ns, line, 0, {
578            virt_text = {{'+'}, {'1234567890', 'ErrorMsg'}};
579            virt_text_pos='eol_right_align';
580            ephemeral = true;
581          })
582        end
583      end
584    ]]
585
586    screen:expect {
587      grid = [[
588      // just to see if there was an accident |
589      // on Mulholland Drive       +{2:1234567890}|
590      try_start();                 +{2:1234567890}|
591      bufref_T save_buf;           +{2:1234567890}|
592      switch_buffer(&save_buf, buf); +{2:12345678}|
593      posp = getmark(mark, false); +{2:1234567890}|
594      restore_buffer(&save_buf);^   +{2:1234567890}|
595                                              |
596    ]],
597    }
598  end)
599
600  it('multiple eol_right_align', function()
601    insert(mulholland)
602    setup_provider [[
603      local hl = api.nvim_get_hl_id_by_name "ErrorMsg"
604      local test_ns = api.nvim_create_namespace "mulholland"
605      function on_do(event, ...)
606        if event == "line" then
607          local win, buf, line = ...
608          api.nvim_buf_set_extmark(buf, test_ns, line, 0, {
609            virt_text = {{'11111'}};
610            virt_text_pos='eol_right_align';
611            ephemeral = true;
612          })
613          api.nvim_buf_set_extmark(0, test_ns, line, 0, {
614            virt_text = {{'22222'}};
615            virt_text_pos='eol_right_align';
616            ephemeral = true;
617          })
618        end
619      end
620    ]]
621
622    screen:expect {
623      grid = [[
624      // just to see if there was an accident |
625      // on Mulholland Drive       11111 22222|
626      try_start();                 11111 22222|
627      bufref_T save_buf;           11111 22222|
628      switch_buffer(&save_buf, buf); 11111 222|
629      posp = getmark(mark, false); 11111 22222|
630      restore_buffer(&save_buf);^   11111 22222|
631                                              |
632    ]],
633    }
634  end)
635
636  it('eol_right_align: second text much longer than first', function()
637    insert('short')
638    setup_provider [[
639      local test_ns = api.nvim_create_namespace "test_length_diff"
640      function on_do(event, ...)
641        if event == "line" then
642          local win, buf, line = ...
643
644          api.nvim_buf_set_extmark(buf, test_ns, line, 0, {
645            virt_text = {{'AA', 'Comment'}};
646            virt_text_pos = 'eol_right_align';
647            priority = 100;
648            ephemeral = true;
649          })
650
651          api.nvim_buf_set_extmark(buf, test_ns, line, 0, {
652            virt_text = {{'BBBBBBBBBBBBBBBBBBBB', 'ErrorMsg'}};
653            virt_text_pos = 'eol_right_align';
654            priority = 200;
655            ephemeral = true;
656          })
657        end
658      end
659    ]]
660
661    screen:expect([[
662      shor^t            {4:AA} {2:BBBBBBBBBBBBBBBBBBBB}|
663      {1:~                                       }|*6
664                                              |
665    ]])
666  end)
667
668  it('virtual text works with wrapped lines', function()
669    insert(mulholland)
670    feed('ggJj3JjJ')
671    setup_provider [[
672      local hl = api.nvim_get_hl_id_by_name "ErrorMsg"
673      local test_ns = api.nvim_create_namespace "mulholland"
674      function on_do(event, ...)
675        if event == "line" then
676          local win, buf, line = ...
677          api.nvim_buf_set_extmark(buf, test_ns, line, 0, {
678            virt_text = {{string.rep('/', line+1), 'ErrorMsg'}};
679            virt_text_pos='eol';
680            ephemeral = true;
681          })
682          api.nvim_buf_set_extmark(buf, test_ns, line, 6, {
683            virt_text = {{string.rep('*', line+1), 'ErrorMsg'}};
684            virt_text_pos='overlay';
685            ephemeral = true;
686          })
687          api.nvim_buf_set_extmark(buf, test_ns, line, 39, {
688            virt_text = {{string.rep('!', line+1), 'ErrorMsg'}};
689            virt_text_win_col=20;
690            ephemeral = true;
691          })
692          api.nvim_buf_set_extmark(buf, test_ns, line, 40, {
693            virt_text = {{string.rep('?', line+1), 'ErrorMsg'}};
694            virt_text_win_col=10;
695            ephemeral = true;
696          })
697          api.nvim_buf_set_extmark(buf, test_ns, line, 40, {
698            virt_text = {{string.rep(';', line+1), 'ErrorMsg'}};
699            virt_text_pos='overlay';
700            ephemeral = true;
701          })
702          api.nvim_buf_set_extmark(buf, test_ns, line, 40, {
703            virt_text = {{'+'}, {string.rep(' ', line+1), 'ErrorMsg'}};
704            virt_text_pos='right_align';
705            ephemeral = true;
706          })
707        end
708      end
709    ]]
710
711    screen:expect {
712      grid = [[
713      // jus{2:*} to see if th{2:!}re was an accident |
714      {2:;}n Mulholl{2:?}nd Drive {2:/}                 +{2: }|
715      try_st{2:**}t(); bufref_{2:!!}save_buf; switch_b|
716      {2:;;}fer(&sav{2:??}buf, buf); {2://}            +{2:  }|
717      posp ={2:***}tmark(mark,{2:!!!}lse);^ restore_buf|
718      {2:;;;}(&save_{2:???});  {2:///}                +{2:   }|
719      {1:~                                       }|
720                                              |
721    ]],
722    }
723    command('setlocal breakindent breakindentopt=shift:2')
724    screen:expect {
725      grid = [[
726      // jus{2:*} to see if th{2:!}re was an accident |
727        {2:;}n Mulho{2:?}land Drive {2:/}               +{2: }|
728      try_st{2:**}t(); bufref_{2:!!}save_buf; switch_b|
729        {2:;;}fer(&s{2:??}e_buf, buf); {2://}          +{2:  }|
730      posp ={2:***}tmark(mark,{2:!!!}lse);^ restore_buf|
731        {2:;;;}(&sav{2:???}uf);  {2:///}              +{2:   }|
732      {1:~                                       }|
733                                              |
734    ]],
735    }
736  end)
737
738  it('can highlight beyond EOL', function()
739    insert(mulholland)
740    setup_provider [[
741      local test_ns = api.nvim_create_namespace "veberod"
742      function on_do(event, ...)
743        if event == "line" then
744          local win, buf, line = ...
745          if string.find(api.nvim_buf_get_lines(buf, line, line+1, true)[1], "buf") then
746            api.nvim_buf_set_extmark(buf, test_ns, line, 0, {
747              end_line = line+1;
748              hl_group = 'DiffAdd';
749              hl_eol = true;
750              ephemeral = true;
751            })
752          end
753        end
754      end
755    ]]
756
757    screen:expect {
758      grid = [[
759      // just to see if there was an accident |
760      // on Mulholland Drive                  |
761      try_start();                            |
762      {13:bufref_T save_buf;                      }|
763      {13:switch_buffer(&save_buf, buf);          }|
764      posp = getmark(mark, false);            |
765      {13:restore_buffer(&save_buf);^              }|
766                                              |
767    ]],
768    }
769  end)
770
771  it("'hl_eol' extends into 'linebreak'/'breakindent'/'showbreak' gaps", function()
772    command('set wrap linebreak breakindent breakindentopt=shift:2')
773    api.nvim_buf_set_lines(0, 0, -1, false, {
774      '  if vim.g.colors_name == "ferricferric" then',
775    })
776    local ns = api.nvim_create_namespace('code_bg')
777    local id = api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 1, hl_group = 'DiffAdd', hl_eol = true })
778    screen:expect([[
779      {13:^  if vim.g.colors_name ==               }|
780      {13:    "ferricferric" then                 }|
781      {1:~                                       }|*5
782                                              |
783    ]])
784
785    -- Extends across the 'showbreak' character too.
786    command('set showbreak=>')
787    screen:add_extra_attr_ids({
788      [100] = { background = Screen.colors.LightBlue },
789      [101] = { bold = true, foreground = Screen.colors.Blue1, background = Screen.colors.LightBlue },
790    })
791    screen:expect([[
792      {100:^  if vim.g.colors_name ==               }|
793      {100:    }{101:>}{100:"ferricferric" then                }|
794      {1:~                                       }|*5
795                                              |
796    ]])
797
798    -- Without 'hl_eol' the highlight decorates characters, so it leaves the gaps alone, the same
799    -- way it already leaves the cells past the end of the line alone.
800    api.nvim_buf_set_extmark(0, ns, 0, 0, { id = id, end_row = 1, hl_group = 'DiffAdd' })
801    screen:expect([[
802      {100:^  if vim.g.colors_name == }              |
803          {1:>}{100:"ferricferric" then}                |
804      {1:~                                       }|*5
805                                              |
806    ]])
807  end)
808
809  it('breakindent keeps the highlight after a real linebreak word-push', function()
810    -- The word pushed down by 'linebreak' resets decor_attr (see listlbr_spec.lua), which must not
811    -- then leak into 'breakindent's own gap on the row the word lands on.
812    screen:try_resize(30, 5)
813    screen:add_extra_attr_ids({ [100] = { background = Screen.colors.LightBlue } })
814    command('set wrap linebreak breakindent')
815    local line = '      This is a long indented line of plain text that will wrap nicely.'
816    api.nvim_buf_set_lines(0, 0, -1, false, { line })
817    local ns = api.nvim_create_namespace('code_bg2')
818    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 1, hl_group = 'DiffAdd', hl_eol = true })
819    screen:expect([[
820      {100:^      This is a long indented }|
821      {100:      line of plain text that }|
822      {100:      will wrap nicely.       }|
823      {1:~                             }|
824                                    |
825    ]])
826  end)
827
828  it('can create and remove signs when CursorMoved autocommand validates botline #18661', function()
829    exec_lua([[
830      local lines = {}
831      for i = 1, 200 do
832        lines[i] = 'hello' .. tostring(i)
833      end
834      vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
835    ]])
836    setup_provider([[
837      local function on_do(kind, winid, bufnr, topline, botline)
838        if kind == 'win' then
839          if topline < 100 and botline > 100 then
840            api.nvim_buf_set_extmark(bufnr, ns1, 99, -1, { sign_text = 'X' })
841          else
842            api.nvim_buf_clear_namespace(bufnr, ns1, 0, -1)
843          end
844        end
845      end
846    ]])
847    command([[autocmd CursorMoved * call line('w$')]])
848    api.nvim_win_set_cursor(0, { 100, 0 })
849    screen:expect([[
850      {14:  }hello97                               |
851      {14:  }hello98                               |
852      {14:  }hello99                               |
853      {14:X }^hello100                              |
854      {14:  }hello101                              |
855      {14:  }hello102                              |
856      {14:  }hello103                              |
857                                              |
858    ]])
859    api.nvim_win_set_cursor(0, { 1, 0 })
860    screen:expect([[
861      ^hello1                                  |
862      hello2                                  |
863      hello3                                  |
864      hello4                                  |
865      hello5                                  |
866      hello6                                  |
867      hello7                                  |
868                                              |
869    ]])
870  end)
871
872  it('does allow removing extmarks during on_line callbacks', function()
873    exec_lua([[
874      eok = true
875    ]])
876    setup_provider([[
877      local function on_do(kind, winid, bufnr, topline, botline)
878        if kind == 'line' then
879          api.nvim_buf_set_extmark(bufnr, ns1, 1, -1, { sign_text = 'X' })
880          eok = pcall(api.nvim_buf_clear_namespace, bufnr, ns1, 0, -1)
881        end
882      end
883    ]])
884    exec_lua([[
885      assert(eok == true)
886    ]])
887  end)
888
889  it('on_line is invoked only for buffer lines', function()
890    insert(mulholland)
891    command('vnew')
892    insert(mulholland)
893    feed('dd')
894    command('windo diffthis')
895
896    exec_lua([[
897      out_of_bound = false
898    ]])
899    setup_provider([[
900      local function on_do(kind, _, bufnr, row)
901        if kind == 'line' then
902          if not api.nvim_buf_get_lines(bufnr, row, row + 1, false)[1] then
903            out_of_bound = true
904          end
905        end
906      end
907    ]])
908
909    feed('<C-e>')
910
911    exec_lua([[
912      assert(out_of_bound == false)
913    ]])
914  end)
915
916  it('on_range is invoked on all visible characters', function()
917    clear()
918    screen = Screen.new(20, 4)
919    setup_screen(screen)
920
921    local function record()
922      exec_lua(function()
923        _G.p_min = { math.huge, math.huge }
924        _G.p_max = { -math.huge, -math.huge }
925        function _G.pos_gt(a, b)
926          return a[1] > b[1] or (a[1] == b[1] and a[2] > b[2])
927        end
928        function _G.pos_lt(a, b)
929          return a[1] < b[1] or (a[1] == b[1] and a[2] < b[2])
930        end
931      end)
932      setup_provider [[
933        local function on_do(kind, _, bufnr, br, bc, er, ec)
934          if kind == 'range' then
935            local b = { br, bc }
936            local e = { er, ec }
937            if _G.pos_gt(_G.p_min, b) then
938              _G.p_min = b
939            end
940            if _G.pos_lt(_G.p_max, e) then
941              _G.p_max = e
942            end
943          end
944        end
945      ]]
946    end
947    local function check(min, max)
948      local p_min = exec_lua('return _G.p_min')
949      assert(
950        p_min[1] < min[1] or (p_min[1] == min[1] and p_min[2] <= min[2]),
951        'minimum position ' .. vim.inspect(p_min) .. ' should be before the first char'
952      )
953      local p_max = exec_lua('return _G.p_max')
954      assert(
955        p_max[1] > max[1] or (p_max[1] == max[1] and p_max[2] >= max[2]),
956        'maximum position ' .. vim.inspect(p_max) .. ' should be on or after the last char'
957      )
958    end
959
960    -- Multiple lines.
961    exec_lua([[
962      local lines = { ('a'):rep(40), ('b'):rep(40), ('c'):rep(40) }
963      vim.api.nvim_buf_set_lines(0, 0, -1, true, lines)
964      vim.api.nvim_win_set_cursor(0, { 2, 0 })
965    ]])
966    record()
967    screen:expect([[
968      ^bbbbbbbbbbbbbbbbbbbb|
969      bbbbbbbbbbbbbbbbbbbb|
970      ccccccccccccccccc{1:@@@}|
971                          |
972    ]])
973    check({ 1, 0 }, { 2, 21 })
974
975    -- One long line.
976    exec_lua([[
977      local lines = { ('a'):rep(100) }
978      vim.api.nvim_buf_set_lines(0, 0, -1, true, lines)
979      vim.api.nvim_win_set_cursor(0, { 1, 70 })
980    ]])
981    record()
982    screen:expect([[
983      {1:<<<}aaaaaaaaaaaaaaaaa|
984      aaaaaaaaaaaaaaaaaaaa|
985      aaaaaaaaaa^aaaaaaaaaa|
986                          |
987    ]])
988    check({ 0, 20 }, { 0, 81 })
989
990    -- Multibyte characters.
991    exec_lua([[
992      local lines = { ('\195\162'):rep(100) }
993      vim.api.nvim_buf_set_lines(0, 0, -1, true, lines)
994      vim.api.nvim_win_set_cursor(0, { 1, 70 * 2 })
995    ]])
996    record()
997    screen:expect([[
998      {1:<<<}âââââââââââââââââ|
999      ââââââââââââââââââââ|
1000      ââââââââââ^ââââââââââ|
1001                          |
1002    ]])
1003    check({ 0, 20 * 2 }, { 0, 81 * 2 })
1004
1005    -- Tabs.
1006    exec_lua([[
1007      local lines = { 'a' .. ('\t'):rep(100) }
1008      vim.api.nvim_buf_set_lines(0, 0, -1, true, lines)
1009      vim.api.nvim_win_set_cursor(0, { 1, 39 })
1010    ]])
1011    record()
1012    screen:expect([[
1013      {1:<<<}                 |
1014                          |
1015                 ^         |
1016                          |
1017    ]])
1018    check({ 0, 33 }, { 0, 94 })
1019
1020    -- One long line without wrapping.
1021    command('set nowrap')
1022    exec_lua([[
1023      local lines = { ('a'):rep(50) .. ('b'):rep(50) }
1024      vim.api.nvim_buf_set_lines(0, 0, -1, true, lines)
1025      vim.api.nvim_win_set_cursor(0, { 1, 50 })
1026    ]])
1027    record()
1028    screen:expect([[
1029      aaaaaaaaaa^bbbbbbbbbb|
1030      {1:~                   }|*2
1031                          |
1032    ]])
1033    check({ 0, 40 }, { 0, 60 })
1034  end)
1035
1036  it('can add new providers during redraw #26652', function()
1037    setup_provider [[
1038    local ns = api.nvim_create_namespace('test_no_add')
1039    function on_do(...)
1040      api.nvim_set_decoration_provider(ns, {})
1041    end
1042    ]]
1043
1044    n.assert_alive()
1045  end)
1046
1047  it('is not invoked repeatedly in Visual mode with vim.schedule() #20235', function()
1048    exec_lua([[_G.cnt = 0]])
1049    setup_provider([[
1050      function on_do(event, ...)
1051        if event == 'win' then
1052          vim.schedule(function() end)
1053          _G.cnt = _G.cnt + 1
1054        end
1055      end
1056    ]])
1057    feed('v')
1058    screen:expect([[
1059      {20:^ }                                       |
1060      {1:~                                       }|*6
1061      {19:-- VISUAL --}                            |
1062    ]])
1063    eq(2, exec_lua([[return _G.cnt]]))
1064  end)
1065
1066  it('can do large changes to the marktree', function()
1067    insert('line1 with a lot of text\nline2 with a lot of text')
1068    setup_provider([[
1069      function on_do(event, _, _, row)
1070        if event == 'win' or (event == 'line' and row == 1) then
1071          vim.api.nvim_buf_clear_namespace(0, ns1, 0, -1)
1072          for i = 0,1 do
1073            for j = 0,23 do
1074              vim.api.nvim_buf_set_extmark(0, ns1, i, j, {hl_group='ErrorMsg', end_col = j+1})
1075            end
1076          end
1077        end
1078      end
1079    ]])
1080
1081    -- Doesn't crash when modifying the marktree between line1 and line2
1082    screen:expect([[
1083      {2:line1 with a lot of text}                |
1084      {2:line2 with a lot of tex^t}                |
1085      {1:~                                       }|*5
1086                                              |
1087    ]])
1088  end)
1089
1090  it('inline virt_text does not result in wrong cursor column #33153', function()
1091    insert('line1 with a lot of text\nline2 with a lot of text')
1092    setup_provider([[
1093      _G.do_win = false
1094      vim.keymap.set('n', 'f', function()
1095        _G.do_win = true
1096        vim.cmd('redraw!')
1097      end)
1098      vim.o.concealcursor = 'n'
1099      function on_do(event)
1100        if event == 'win' and _G.do_win then
1101          vim.api.nvim_buf_set_extmark(0, ns1, 1, 0, {
1102            virt_text = { { 'virt_text ' } },
1103            virt_text_pos = 'inline'
1104          })
1105        end
1106      end
1107    ]])
1108    feed('f')
1109    screen:expect([[
1110      line1 with a lot of text                |
1111      virt_text line2 with a lot of tex^t      |
1112      {1:~                                       }|*5
1113                                              |
1114    ]])
1115  end)
1116
1117  it('decor provider is enabled again for next redraw after on_win disabled it', function()
1118    exec_lua(function()
1119      vim.api.nvim_set_decoration_provider(vim.api.nvim_create_namespace(''), {
1120        on_win = function()
1121          return false
1122        end,
1123        on_buf = function()
1124          _G.did_buf = (_G.did_buf or 0) + 1
1125        end,
1126      })
1127    end)
1128    api.nvim_buf_set_lines(0, 0, -1, false, { 'foo' })
1129    screen:expect([[
1130      ^foo                                     |
1131      {1:~                                       }|*6
1132                                              |
1133    ]])
1134    eq(1, exec_lua('return _G.did_buf'))
1135  end)
1136end)
1137
1138describe('decoration_providers', function()
1139  it('errors and logs gracefully', function()
1140    local testlog = 'Xtest_decorations_log'
1141    clear({ env = { NVIM_LOG_FILE = testlog } })
1142    local screen = Screen.new(65, 7)
1143    setup_provider([[
1144      function on_do(...)
1145        error "Foo"
1146      end
1147    ]])
1148    screen:expect([[
1149      {3:                                                                 }|
1150      {9:Decoration provider "start" (ns=ns1):}                            |
1151      {9:Lua: [string "<nvim>"]:4: Foo}                                    |
1152      {9:stack traceback:}                                                 |
1153      {9:        [C]: in function 'error'}                                 |
1154      {9:        [string "<nvim>"]:4: in function <[string "<nvim>"]:3>}   |
1155      {6:Press ENTER or type command to continue}^                          |
1156    ]])
1157    t.assert_log('Error in decoration provider "start" %(ns=ns1%):', testlog, 100)
1158    t.assert_log('Lua: %[string "<nvim>"%]:4: Foo', testlog, 100)
1159    n.check_close()
1160    os.remove(testlog)
1161  end)
1162end)
1163
1164local example_text = [[
1165for _,item in ipairs(items) do
1166    local text, hl_id_cell, count = unpack(item)
1167    if hl_id_cell ~= nil then
1168        hl_id = hl_id_cell
1169    end
1170    for _ = 1, (count or 1) do
1171        local cell = line[colpos]
1172        cell.text = text
1173        cell.hl_id = hl_id
1174        colpos = colpos+1
1175    end
1176end]]
1177
1178describe('extmark decorations', function()
1179  local screen ---@type test.functional.ui.screen
1180  local ns ---@type integer
1181  before_each(function()
1182    clear()
1183    screen = Screen.new(50, 15)
1184    screen:set_default_attr_ids {
1185      [1] = { bold = true, foreground = Screen.colors.Blue },
1186      [2] = { foreground = Screen.colors.Brown },
1187      [3] = { bold = true, foreground = Screen.colors.SeaGreen },
1188      [4] = { background = Screen.colors.Red1, foreground = Screen.colors.Gray100 },
1189      [5] = { foreground = Screen.colors.Brown, bold = true },
1190      [6] = { foreground = Screen.colors.DarkCyan },
1191      [7] = { foreground = Screen.colors.Grey0, background = tonumber('0xff4c4c') },
1192      [8] = { foreground = tonumber('0x180606'), background = tonumber('0xff4c4c') },
1193      [9] = { foreground = tonumber('0xe40c0c'), background = tonumber('0xff4c4c'), bold = true },
1194      [10] = { foreground = tonumber('0xb20000'), background = tonumber('0xff4c4c') },
1195      [11] = { blend = 30, background = Screen.colors.Red1 },
1196      [12] = { foreground = Screen.colors.Brown, blend = 30, background = Screen.colors.Red1, bold = true },
1197      [13] = { foreground = Screen.colors.Fuchsia },
1198      [14] = { background = Screen.colors.Red1, foreground = Screen.colors.Black },
1199      [15] = { background = Screen.colors.Red1, foreground = tonumber('0xb20000') },
1200      [16] = { blend = 30, background = Screen.colors.Red1, foreground = Screen.colors.Magenta1 },
1201      [17] = { bold = true, foreground = Screen.colors.Brown, background = Screen.colors.LightGrey },
1202      [18] = { background = Screen.colors.LightGrey },
1203      [19] = { foreground = Screen.colors.DarkCyan, background = Screen.colors.LightGrey },
1204      [20] = { foreground = tonumber('0x180606'), background = tonumber('0xf13f3f') },
1205      [21] = { foreground = Screen.colors.Gray0, background = tonumber('0xf13f3f') },
1206      [22] = { foreground = tonumber('0xb20000'), background = tonumber('0xf13f3f') },
1207      [23] = { foreground = Screen.colors.Magenta1, background = Screen.colors.LightGrey },
1208      [24] = { bold = true },
1209      [25] = { background = Screen.colors.LightRed },
1210      [26] = { background = Screen.colors.DarkGrey, foreground = Screen.colors.LightGrey },
1211      [27] = { background = Screen.colors.LightGrey, foreground = Screen.colors.Black },
1212      [28] = { underline = true, foreground = Screen.colors.SlateBlue },
1213      [29] = { foreground = Screen.colors.SlateBlue, background = Screen.colors.LightGrey, underline = true },
1214      [30] = { foreground = Screen.colors.DarkCyan, background = Screen.colors.LightGrey, underline = true },
1215      [31] = { underline = true, foreground = Screen.colors.DarkCyan },
1216      [32] = { underline = true },
1217      [33] = { foreground = Screen.colors.DarkBlue, background = Screen.colors.LightGrey },
1218      [34] = { background = Screen.colors.Yellow },
1219      [35] = { background = Screen.colors.Yellow, bold = true, foreground = Screen.colors.Blue },
1220      [36] = { foreground = Screen.colors.Blue1, bold = true, background = Screen.colors.Red },
1221      [37] = { background = Screen.colors.WebGray, foreground = Screen.colors.DarkBlue },
1222      [38] = { background = Screen.colors.LightBlue },
1223      [39] = { foreground = Screen.colors.Blue1, background = Screen.colors.LightCyan1, bold = true },
1224      [40] = { reverse = true },
1225      [41] = { bold = true, reverse = true },
1226      [42] = { undercurl = true, special = Screen.colors.Red },
1227      [43] = { background = Screen.colors.Yellow, undercurl = true, special = Screen.colors.Red },
1228      [44] = { background = Screen.colors.LightMagenta },
1229      [45] = { background = Screen.colors.Red, special = Screen.colors.Red, foreground = Screen.colors.Red },
1230      [46] = { background = Screen.colors.Blue, foreground = Screen.colors.Blue, special = Screen.colors.Red },
1231      [47] = { background = Screen.colors.Green, foreground = Screen.colors.Blue, special = Screen.colors.Red },
1232    }
1233
1234    ns = api.nvim_create_namespace 'test'
1235  end)
1236
1237  it('empty virtual text at eol should not break colorcolumn #17860', function()
1238    insert(example_text)
1239    feed('gg')
1240    command('set colorcolumn=40')
1241    screen:expect([[
1242      ^for _,item in ipairs(items) do         {25: }          |
1243          local text, hl_id_cell, count = unp{25:a}ck(item)  |
1244          if hl_id_cell ~= nil then          {25: }          |
1245              hl_id = hl_id_cell             {25: }          |
1246          end                                {25: }          |
1247          for _ = 1, (count or 1) do         {25: }          |
1248              local cell = line[colpos]      {25: }          |
1249              cell.text = text               {25: }          |
1250              cell.hl_id = hl_id             {25: }          |
1251              colpos = colpos+1              {25: }          |
1252          end                                {25: }          |
1253      end                                    {25: }          |
1254      {1:~                                                 }|*2
1255                                                        |
1256    ]])
1257    api.nvim_buf_set_extmark(0, ns, 4, 0, { virt_text = { { '' } }, virt_text_pos = 'eol' })
1258    screen:expect_unchanged()
1259  end)
1260
1261  it('can have virtual text of overlay position', function()
1262    insert(example_text)
1263    feed 'gg'
1264
1265    for i = 1, 9 do
1266      api.nvim_buf_set_extmark(0, ns, i, 0, { virt_text = { { '|', 'LineNr' } }, virt_text_pos = 'overlay' })
1267      if i == 3 or (i >= 6 and i <= 9) then
1268        api.nvim_buf_set_extmark(0, ns, i, 4, { virt_text = { { '|', 'NonText' } }, virt_text_pos = 'overlay' })
1269      end
1270    end
1271    api.nvim_buf_set_extmark(
1272      0,
1273      ns,
1274      9,
1275      10,
1276      { virt_text = { { 'foo' }, { 'bar', 'MoreMsg' }, { '!!', 'ErrorMsg' } }, virt_text_pos = 'overlay' }
1277    )
1278
1279    -- can "float" beyond end of line
1280    api.nvim_buf_set_extmark(0, ns, 5, 28, { virt_text = { { 'loopy', 'ErrorMsg' } }, virt_text_pos = 'overlay' })
1281    -- bound check: right edge of window
1282    api.nvim_buf_set_extmark(
1283      0,
1284      ns,
1285      2,
1286      26,
1287      { virt_text = { { 'bork bork bork' }, { (' bork'):rep(10), 'ErrorMsg' } }, virt_text_pos = 'overlay' }
1288    )
1289    -- empty virt_text should not change anything
1290    api.nvim_buf_set_extmark(0, ns, 6, 16, { virt_text = { { '' } }, virt_text_pos = 'overlay' })
1291
1292    screen:expect {
1293      grid = [[
1294      ^for _,item in ipairs(items) do                    |
1295      {2:|}   local text, hl_id_cell, count = unpack(item)  |
1296      {2:|}   if hl_id_cell ~= nil tbork bork bork{4: bork bork}|
1297      {2:|}   {1:|}   hl_id = hl_id_cell                        |
1298      {2:|}   end                                           |
1299      {2:|}   for _ = 1, (count or 1) {4:loopy}                 |
1300      {2:|}   {1:|}   local cell = line[colpos]                 |
1301      {2:|}   {1:|}   cell.text = text                          |
1302      {2:|}   {1:|}   cell.hl_id = hl_id                        |
1303      {2:|}   {1:|}   cofoo{3:bar}{4:!!}olpos+1                         |
1304          end                                           |
1305      end                                               |
1306      {1:~                                                 }|*2
1307                                                        |
1308    ]],
1309    }
1310
1311    -- handles broken lines
1312    screen:try_resize(22, 25)
1313    screen:expect {
1314      grid = [[
1315      ^for _,item in ipairs(i|
1316      tems) do              |
1317      {2:|}   local text, hl_id_|
1318      cell, count = unpack(i|
1319      tem)                  |
1320      {2:|}   if hl_id_cell ~= n|
1321      il tbork bork bork{4: bor}|
1322      {2:|}   {1:|}   hl_id = hl_id_|
1323      cell                  |
1324      {2:|}   end               |
1325      {2:|}   for _ = 1, (count |
1326      or 1) {4:loopy}           |
1327      {2:|}   {1:|}   local cell = l|
1328      ine[colpos]           |
1329      {2:|}   {1:|}   cell.text = te|
1330      xt                    |
1331      {2:|}   {1:|}   cell.hl_id = h|
1332      l_id                  |
1333      {2:|}   {1:|}   cofoo{3:bar}{4:!!}olpo|
1334      s+1                   |
1335          end               |
1336      end                   |
1337      {1:~                     }|*2
1338                            |
1339    ]],
1340    }
1341
1342    -- truncating in the middle of a char leaves a space
1343    api.nvim_buf_set_lines(0, 0, 1, true, { 'for _,item in ipairs(items) do  -- 古古古' })
1344    api.nvim_buf_set_lines(0, 10, 12, true, { '    end  -- ??????????', 'end  -- ?古古古古?古古' })
1345    api.nvim_buf_set_extmark(0, ns, 0, 35, { virt_text = { { 'A', 'ErrorMsg' }, { 'AA' } }, virt_text_pos = 'overlay' })
1346    api.nvim_buf_set_extmark(0, ns, 10, 19, { virt_text = { { '口口口', 'ErrorMsg' } }, virt_text_pos = 'overlay' })
1347    api.nvim_buf_set_extmark(0, ns, 11, 21, { virt_text = { { '口口口', 'ErrorMsg' } }, virt_text_pos = 'overlay' })
1348    api.nvim_buf_set_extmark(0, ns, 11, 8, { virt_text = { { '口口', 'ErrorMsg' } }, virt_text_pos = 'overlay' })
1349    screen:expect {
1350      grid = [[
1351      ^for _,item in ipairs(i|
1352      tems) do  -- {4:A}AA 古   |
1353      {2:|}   local text, hl_id_|
1354      cell, count = unpack(i|
1355      tem)                  |
1356      {2:|}   if hl_id_cell ~= n|
1357      il tbork bork bork{4: bor}|
1358      {2:|}   {1:|}   hl_id = hl_id_|
1359      cell                  |
1360      {2:|}   end               |
1361      {2:|}   for _ = 1, (count |
1362      or 1) {4:loopy}           |
1363      {2:|}   {1:|}   local cell = l|
1364      ine[colpos]           |
1365      {2:|}   {1:|}   cell.text = te|
1366      xt                    |
1367      {2:|}   {1:|}   cell.hl_id = h|
1368      l_id                  |
1369      {2:|}   {1:|}   cofoo{3:bar}{4:!!}olpo|
1370      s+1                   |
1371          end  -- ???????{4:口 }|
1372      end  -- {4:口口} 古古{4:口口 }|
1373      {1:~                     }|*2
1374                            |
1375    ]],
1376    }
1377
1378    screen:try_resize(82, 13)
1379    screen:expect {
1380      grid = [[
1381      ^for _,item in ipairs(items) do  -- {4:A}AA 古                                         |
1382      {2:|}   local text, hl_id_cell, count = unpack(item)                                  |
1383      {2:|}   if hl_id_cell ~= nil tbork bork bork{4: bork bork bork bork bork bork bork bork b}|
1384      {2:|}   {1:|}   hl_id = hl_id_cell                                                        |
1385      {2:|}   end                                                                           |
1386      {2:|}   for _ = 1, (count or 1) {4:loopy}                                                 |
1387      {2:|}   {1:|}   local cell = line[colpos]                                                 |
1388      {2:|}   {1:|}   cell.text = text                                                          |
1389      {2:|}   {1:|}   cell.hl_id = hl_id                                                        |
1390      {2:|}   {1:|}   cofoo{3:bar}{4:!!}olpos+1                                                         |
1391          end  -- ???????{4:口口口}                                                         |
1392      end  -- {4:口口} 古古{4:口口口}                                                           |
1393                                                                                        |
1394    ]],
1395    }
1396
1397    api.nvim_buf_clear_namespace(0, ns, 0, -1)
1398    screen:expect {
1399      grid = [[
1400      ^for _,item in ipairs(items) do  -- 古古古                                         |
1401          local text, hl_id_cell, count = unpack(item)                                  |
1402          if hl_id_cell ~= nil then                                                     |
1403              hl_id = hl_id_cell                                                        |
1404          end                                                                           |
1405          for _ = 1, (count or 1) do                                                    |
1406              local cell = line[colpos]                                                 |
1407              cell.text = text                                                          |
1408              cell.hl_id = hl_id                                                        |
1409              colpos = colpos+1                                                         |
1410          end  -- ??????????                                                            |
1411      end  -- ?古古古古?古古                                                            |
1412                                                                                        |
1413    ]],
1414    }
1415  end)
1416
1417  it('overlay virtual text works with wrapped lines #25158', function()
1418    screen:try_resize(50, 6)
1419    insert(('ab'):rep(100))
1420    for i = 0, 9 do
1421      api.nvim_buf_set_extmark(0, ns, 0, 42 + i, { virt_text = { { tostring(i), 'ErrorMsg' } }, virt_text_pos = 'overlay' })
1422      api.nvim_buf_set_extmark(
1423        0,
1424        ns,
1425        0,
1426        91 + i,
1427        { virt_text = { { tostring(i), 'ErrorMsg' } }, virt_text_pos = 'overlay', virt_text_hide = true }
1428      )
1429    end
1430    screen:expect {
1431      grid = [[
1432      ababababababababababababababababababababab{4:01234567}|
1433      {4:89}abababababababababababababababababababa{4:012345678}|
1434      {4:9}babababababababababababababababababababababababab|
1435      ababababababababababababababababababababababababa^b|
1436      {1:~                                                 }|
1437                                                        |
1438    ]],
1439    }
1440
1441    command('set showbreak=++')
1442    screen:expect {
1443      grid = [[
1444      ababababababababababababababababababababab{4:01234567}|
1445      {1:++}{4:89}abababababababababababababababababababa{4:0123456}|
1446      {1:++}{4:789}babababababababababababababababababababababab|
1447      {1:++}abababababababababababababababababababababababab|
1448      {1:++}ababa^b                                          |
1449                                                        |
1450    ]],
1451    }
1452
1453    feed('2gkvg0')
1454    screen:expect {
1455      grid = [[
1456      ababababababababababababababababababababab{4:01234567}|
1457      {1:++}{4:89}abababababababababababababababababababa{4:0123456}|
1458      {1:++}{27:^ababab}ababababababababababababababababababababab|
1459      {1:++}abababababababababababababababababababababababab|
1460      {1:++}ababab                                          |
1461      {24:-- VISUAL --}                                      |
1462    ]],
1463    }
1464
1465    feed('o')
1466    screen:expect {
1467      grid = [[
1468      ababababababababababababababababababababab{4:01234567}|
1469      {1:++}{4:89}abababababababababababababababababababa{4:0123456}|
1470      {1:++}{27:ababa^b}ababababababababababababababababababababab|
1471      {1:++}abababababababababababababababababababababababab|
1472      {1:++}ababab                                          |
1473      {24:-- VISUAL --}                                      |
1474    ]],
1475    }
1476
1477    feed('gk')
1478    screen:expect {
1479      grid = [[
1480      ababababababababababababababababababababab{4:01234567}|
1481      {1:++}{4:89}aba{27:^bababababababababababababababababababababab}|
1482      {1:++}{27:a}{4:89}babababababababababababababababababababababab|
1483      {1:++}abababababababababababababababababababababababab|
1484      {1:++}ababab                                          |
1485      {24:-- VISUAL --}                                      |
1486    ]],
1487    }
1488
1489    feed('o')
1490    screen:expect {
1491      grid = [[
1492      ababababababababababababababababababababab{4:01234567}|
1493      {1:++}{4:89}aba{27:bababababababababababababababababababababab}|
1494      {1:++}{27:^a}{4:89}babababababababababababababababababababababab|
1495      {1:++}abababababababababababababababababababababababab|
1496      {1:++}ababab                                          |
1497      {24:-- VISUAL --}                                      |
1498    ]],
1499    }
1500
1501    feed('<Esc>$')
1502    command('set number showbreak=')
1503    screen:expect {
1504      grid = [[
1505      {2:  1 }ababababababababababababababababababababab{4:0123}|
1506      {2:    }{4:456789}abababababababababababababababababababa{4:0}|
1507      {2:    }{4:123456789}babababababababababababababababababab|
1508      {2:    }ababababababababababababababababababababababab|
1509      {2:    }abababababababa^b                              |
1510                                                        |
1511    ]],
1512    }
1513
1514    command('set cpoptions+=n')
1515    screen:expect {
1516      grid = [[
1517      {2:  1 }ababababababababababababababababababababab{4:0123}|
1518      {4:456789}abababababababababababababababababababa{4:01234}|
1519      {4:56789}babababababababababababababababababababababab|
1520      ababababababababababababababababababababababababab|
1521      aba^b                                              |
1522                                                        |
1523    ]],
1524    }
1525
1526    feed('0g$hi<Tab>')
1527    screen:expect {
1528      grid = [[
1529      {2:  1 }ababababababababababababababababababababab{4:01}  |
1530        {4:^23456789}abababababababababababababababababababa{4:0}|
1531      {4:123456789}babababababababababababababababababababab|
1532      ababababababababababababababababababababababababab|
1533      abababab                                          |
1534      {24:-- INSERT --}                                      |
1535    ]],
1536    }
1537  end)
1538
1539  it('virt_text_hide hides overlay virtual text when extmark is off-screen', function()
1540    screen:try_resize(50, 3)
1541    command('set nowrap')
1542    api.nvim_buf_set_lines(0, 0, -1, true, { '-- ' .. ('…'):rep(57) })
1543    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { '?????', 'ErrorMsg' } }, virt_text_pos = 'overlay', virt_text_hide = true })
1544    api.nvim_buf_set_extmark(0, ns, 0, 123, { virt_text = { { '!!!!!', 'ErrorMsg' } }, virt_text_pos = 'overlay', virt_text_hide = true })
1545    screen:expect {
1546      grid = [[
1547      {4:^?????}……………………………………………………………………………………………………{4:!!!!!}……|
1548      {1:~                                                 }|
1549                                                        |
1550    ]],
1551    }
1552    feed('40zl')
1553    screen:expect {
1554      grid = [[
1555      ^………{4:!!!!!}………………………………                              |
1556      {1:~                                                 }|
1557                                                        |
1558    ]],
1559    }
1560    feed('3zl')
1561    screen:expect {
1562      grid = [[
1563      {4:^!!!!!}………………………………                                 |
1564      {1:~                                                 }|
1565                                                        |
1566    ]],
1567    }
1568    feed('7zl')
1569    screen:expect {
1570      grid = [[
1571      ^…………………………                                        |
1572      {1:~                                                 }|
1573                                                        |
1574    ]],
1575    }
1576
1577    command('set wrap smoothscroll')
1578    screen:expect {
1579      grid = [[
1580      {4:?????}……………………………………………………………………………………………………{4:!!!!!}……|
1581      ^…………………………                                        |
1582                                                        |
1583    ]],
1584    }
1585    feed('<C-E>')
1586    screen:expect {
1587      grid = [[
1588      {1:<<<}………………^…                                        |
1589      {1:~                                                 }|
1590                                                        |
1591    ]],
1592    }
1593    screen:try_resize(40, 3)
1594    screen:expect {
1595      grid = [[
1596      {1:<<<}{4:!!!!!}……………………………^…                    |
1597      {1:~                                       }|
1598                                              |
1599    ]],
1600    }
1601    feed('<C-Y>')
1602    screen:expect {
1603      grid = [[
1604      {4:?????}……………………………………………………………………………………………|
1605      ………{4:!!!!!}……………………………^…                    |
1606                                              |
1607    ]],
1608    }
1609  end)
1610
1611  it('overlay virtual text works on and after a TAB #24022', function()
1612    screen:try_resize(40, 3)
1613    api.nvim_buf_set_lines(0, 0, -1, true, { '\t\tline 1' })
1614    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'AA', 'Search' } }, virt_text_pos = 'overlay', hl_mode = 'combine' })
1615    api.nvim_buf_set_extmark(0, ns, 0, 1, { virt_text = { { 'BB', 'Search' } }, virt_text_pos = 'overlay', hl_mode = 'combine' })
1616    api.nvim_buf_set_extmark(0, ns, 0, 2, { virt_text = { { 'CC', 'Search' } }, virt_text_pos = 'overlay', hl_mode = 'combine' })
1617    screen:expect {
1618      grid = [[
1619      {34:AA}     ^ {34:BB}      {34:CC}ne 1                  |
1620      {1:~                                       }|
1621                                              |
1622    ]],
1623    }
1624    command('setlocal list listchars=tab:<->')
1625    screen:expect {
1626      grid = [[
1627      {35:^AA}{1:----->}{35:BB}{1:----->}{34:CC}ne 1                  |
1628      {1:~                                       }|
1629                                              |
1630    ]],
1631    }
1632  end)
1633
1634  it('can have virtual text of overlay position and styling', function()
1635    insert(example_text)
1636    feed 'gg'
1637
1638    command 'set ft=lua'
1639    command 'syntax on'
1640
1641    screen:expect {
1642      grid = [[
1643      {5:^for} _,item {5:in} {6:ipairs}(items) {5:do}                    |
1644          {5:local} text, hl_id_cell, count {5:=} unpack(item)  |
1645          {5:if} hl_id_cell {5:~=} {13:nil} {5:then}                     |
1646              hl_id {5:=} hl_id_cell                        |
1647          {5:end}                                           |
1648          {5:for} _ {5:=} {13:1}, (count {5:or} {13:1}) {5:do}                    |
1649              {5:local} cell {5:=} line[colpos]                 |
1650              cell.text {5:=} text                          |
1651              cell.hl_id {5:=} hl_id                        |
1652              colpos {5:=} colpos{5:+}{13:1}                         |
1653          {5:end}                                           |
1654      {5:end}                                               |
1655      {1:~                                                 }|*2
1656                                                        |
1657    ]],
1658    }
1659
1660    command 'hi Blendy guibg=Red blend=30'
1661    command 'hi! Visual guifg=NONE guibg=LightGrey'
1662    api.nvim_buf_set_extmark(
1663      0,
1664      ns,
1665      1,
1666      5,
1667      { virt_text = { { 'blendy text - here', 'Blendy' } }, virt_text_pos = 'overlay', hl_mode = 'blend' }
1668    )
1669    api.nvim_buf_set_extmark(
1670      0,
1671      ns,
1672      2,
1673      5,
1674      { virt_text = { { 'combining color', 'Blendy' } }, virt_text_pos = 'overlay', hl_mode = 'combine' }
1675    )
1676    api.nvim_buf_set_extmark(
1677      0,
1678      ns,
1679      3,
1680      5,
1681      { virt_text = { { 'replacing color', 'Blendy' } }, virt_text_pos = 'overlay', hl_mode = 'replace' }
1682    )
1683
1684    api.nvim_buf_set_extmark(
1685      0,
1686      ns,
1687      4,
1688      5,
1689      { virt_text = { { 'blendy text - here', 'Blendy' } }, virt_text_pos = 'overlay', hl_mode = 'blend', virt_text_hide = true }
1690    )
1691    api.nvim_buf_set_extmark(
1692      0,
1693      ns,
1694      5,
1695      5,
1696      { virt_text = { { 'combining color', 'Blendy' } }, virt_text_pos = 'overlay', hl_mode = 'combine', virt_text_hide = true }
1697    )
1698    api.nvim_buf_set_extmark(
1699      0,
1700      ns,
1701      6,
1702      5,
1703      { virt_text = { { 'replacing color', 'Blendy' } }, virt_text_pos = 'overlay', hl_mode = 'replace', virt_text_hide = true }
1704    )
1705
1706    screen:expect {
1707      grid = [[
1708      {5:^for} _,item {5:in} {6:ipairs}(items) {5:do}                    |
1709          {5:l}{8:blen}{7:dy}{10:e}{7:text}{10:h}{7:-}{10:_}{7:here}ell, count {5:=} unpack(item)  |
1710          {5:i}{12:c}{11:ombining col}{12:or} {13:nil} {5:then}                     |
1711           {11:replacing color}d_cell                        |
1712          {5:e}{8:bl}{7:endy}{10: }{7:text}{10: }{7:-}{10: }{7:here}                           |
1713          {5:f}{12:co}{11:mbi}{12:n}{11:i}{16:n}{11:g color}t {5:or} {13:1}) {5:do}                    |
1714           {11:replacing color} line[colpos]                 |
1715              cell.text {5:=} text                          |
1716              cell.hl_id {5:=} hl_id                        |
1717              colpos {5:=} colpos{5:+}{13:1}                         |
1718          {5:end}                                           |
1719      {5:end}                                               |
1720      {1:~                                                 }|*2
1721                                                        |
1722    ]],
1723    }
1724
1725    feed 'V5G'
1726    screen:expect {
1727      grid = [[
1728      {17:for}{18: _,item }{17:in}{18: }{19:ipairs}{18:(items) }{17:do}                    |
1729      {18:    }{17:l}{20:blen}{21:dy}{22:e}{21:text}{22:h}{21:-}{22:_}{21:here}{18:ell, count }{17:=}{18: unpack(item)}  |
1730      {18:    }{17:i}{12:c}{11:ombining col}{12:or}{18: }{23:nil}{18: }{17:then}                     |
1731      {18:     }{11:replacing color}{18:d_cell}                        |
1732      {18:    }{17:^end}                                           |
1733          {5:f}{12:co}{11:mbi}{12:n}{11:i}{16:n}{11:g color}t {5:or} {13:1}) {5:do}                    |
1734           {11:replacing color} line[colpos]                 |
1735              cell.text {5:=} text                          |
1736              cell.hl_id {5:=} hl_id                        |
1737              colpos {5:=} colpos{5:+}{13:1}                         |
1738          {5:end}                                           |
1739      {5:end}                                               |
1740      {1:~                                                 }|*2
1741      {24:-- VISUAL LINE --}                                 |
1742    ]],
1743    }
1744
1745    feed 'jj'
1746    screen:expect {
1747      grid = [[
1748      {17:for}{18: _,item }{17:in}{18: }{19:ipairs}{18:(items) }{17:do}                    |
1749      {18:    }{17:l}{20:blen}{21:dy}{22:e}{21:text}{22:h}{21:-}{22:_}{21:here}{18:ell, count }{17:=}{18: unpack(item)}  |
1750      {18:    }{17:i}{12:c}{11:ombining col}{12:or}{18: }{23:nil}{18: }{17:then}                     |
1751      {18:     }{11:replacing color}{18:d_cell}                        |
1752      {18:    }{17:end}                                           |
1753      {18:    }{17:for}{18: _ }{17:=}{18: }{23:1}{18:, (count }{17:or}{18: }{23:1}{18:) }{17:do}                    |
1754      {18:    ^    }{17:local}{18: cell }{17:=}{18: line[colpos]}                 |
1755              cell.text {5:=} text                          |
1756              cell.hl_id {5:=} hl_id                        |
1757              colpos {5:=} colpos{5:+}{13:1}                         |
1758          {5:end}                                           |
1759      {5:end}                                               |
1760      {1:~                                                 }|*2
1761      {24:-- VISUAL LINE --}                                 |
1762    ]],
1763    }
1764  end)
1765
1766  it('can have virtual text of right_align and fixed win_col position', function()
1767    insert(example_text)
1768    feed 'gg'
1769    api.nvim_buf_set_extmark(0, ns, 1, 0, { virt_text = { { 'Very', 'ErrorMsg' } }, virt_text_win_col = 31, hl_mode = 'blend' })
1770    api.nvim_buf_set_extmark(0, ns, 1, 0, { virt_text = { { 'VERY', 'ErrorMsg' } }, virt_text_pos = 'right_align', hl_mode = 'blend' })
1771    api.nvim_buf_set_extmark(0, ns, 2, 10, { virt_text = { { 'Much', 'ErrorMsg' } }, virt_text_win_col = 31, hl_mode = 'blend' })
1772    api.nvim_buf_set_extmark(0, ns, 2, 10, { virt_text = { { 'MUCH', 'ErrorMsg' } }, virt_text_pos = 'right_align', hl_mode = 'blend' })
1773    api.nvim_buf_set_extmark(0, ns, 3, 14, { virt_text = { { 'Error', 'ErrorMsg' } }, virt_text_win_col = 31, hl_mode = 'blend' })
1774    api.nvim_buf_set_extmark(0, ns, 3, 14, { virt_text = { { 'ERROR', 'ErrorMsg' } }, virt_text_pos = 'right_align', hl_mode = 'blend' })
1775    api.nvim_buf_set_extmark(0, ns, 7, 21, { virt_text = { { '-', 'NonText' } }, virt_text_win_col = 4, hl_mode = 'blend' })
1776    api.nvim_buf_set_extmark(0, ns, 7, 21, { virt_text = { { '-', 'NonText' } }, virt_text_pos = 'right_align', hl_mode = 'blend' })
1777    -- empty virt_text should not change anything
1778    api.nvim_buf_set_extmark(0, ns, 8, 0, { virt_text = { { '' } }, virt_text_win_col = 14, hl_mode = 'blend' })
1779    api.nvim_buf_set_extmark(0, ns, 8, 0, { virt_text = { { '' } }, virt_text_pos = 'right_align', hl_mode = 'blend' })
1780
1781    screen:expect {
1782      grid = [[
1783      ^for _,item in ipairs(items) do                    |
1784          local text, hl_id_cell, cou{4:Very} unpack(ite{4:VERY}|
1785          if hl_id_cell ~= nil then  {4:Much}           {4:MUCH}|
1786              hl_id = hl_id_cell     {4:Error}         {4:ERROR}|
1787          end                                           |
1788          for _ = 1, (count or 1) do                    |
1789              local cell = line[colpos]                 |
1790          {1:-}   cell.text = text                         {1:-}|
1791              cell.hl_id = hl_id                        |
1792              colpos = colpos+1                         |
1793          end                                           |
1794      end                                               |
1795      {1:~                                                 }|*2
1796                                                        |
1797    ]],
1798    }
1799
1800    feed '3G12|i<cr><esc>'
1801    screen:expect {
1802      grid = [[
1803      for _,item in ipairs(items) do                    |
1804          local text, hl_id_cell, cou{4:Very} unpack(ite{4:VERY}|
1805          if hl_i                    {4:Much}           {4:MUCH}|
1806      ^d_cell ~= nil then                                |
1807              hl_id = hl_id_cell     {4:Error}         {4:ERROR}|
1808          end                                           |
1809          for _ = 1, (count or 1) do                    |
1810              local cell = line[colpos]                 |
1811          {1:-}   cell.text = text                         {1:-}|
1812              cell.hl_id = hl_id                        |
1813              colpos = colpos+1                         |
1814          end                                           |
1815      end                                               |
1816      {1:~                                                 }|
1817                                                        |
1818    ]],
1819    }
1820
1821    feed 'u:<cr>'
1822    screen:expect {
1823      grid = [[
1824      for _,item in ipairs(items) do                    |
1825          local text, hl_id_cell, cou{4:Very} unpack(ite{4:VERY}|
1826          if hl_i^d_cell ~= nil then  {4:Much}           {4:MUCH}|
1827              hl_id = hl_id_cell     {4:Error}         {4:ERROR}|
1828          end                                           |
1829          for _ = 1, (count or 1) do                    |
1830              local cell = line[colpos]                 |
1831          {1:-}   cell.text = text                         {1:-}|
1832              cell.hl_id = hl_id                        |
1833              colpos = colpos+1                         |
1834          end                                           |
1835      end                                               |
1836      {1:~                                                 }|*2
1837      :                                                 |
1838    ]],
1839    }
1840
1841    feed '8|i<cr><esc>'
1842    screen:expect {
1843      grid = [[
1844      for _,item in ipairs(items) do                    |
1845          local text, hl_id_cell, cou{4:Very} unpack(ite{4:VERY}|
1846          if                                            |
1847      ^hl_id_cell ~= nil then         {4:Much}           {4:MUCH}|
1848              hl_id = hl_id_cell     {4:Error}         {4:ERROR}|
1849          end                                           |
1850          for _ = 1, (count or 1) do                    |
1851              local cell = line[colpos]                 |
1852          {1:-}   cell.text = text                         {1:-}|
1853              cell.hl_id = hl_id                        |
1854              colpos = colpos+1                         |
1855          end                                           |
1856      end                                               |
1857      {1:~                                                 }|
1858                                                        |
1859    ]],
1860    }
1861
1862    feed 'jI-- <esc>..........'
1863    screen:expect {
1864      grid = [[
1865      for _,item in ipairs(items) do                    |
1866          local text, hl_id_cell, cou{4:Very} unpack(ite{4:VERY}|
1867          if                                            |
1868      hl_id_cell ~= nil then         {4:Much}           {4:MUCH}|
1869              --^ -- -- -- -- -- -- --{4:Error}- -- hl_i{4:ERROR}|
1870      l_id_cell                                         |
1871          end                                           |
1872          for _ = 1, (count or 1) do                    |
1873              local cell = line[colpos]                 |
1874          {1:-}   cell.text = text                         {1:-}|
1875              cell.hl_id = hl_id                        |
1876              colpos = colpos+1                         |
1877          end                                           |
1878      end                                               |
1879                                                        |
1880    ]],
1881    }
1882
1883    api.nvim_buf_set_extmark(0, ns, 4, 50, { virt_text = { { 'EOL', 'NonText' } } })
1884    screen:expect {
1885      grid = [[
1886      for _,item in ipairs(items) do                    |
1887          local text, hl_id_cell, cou{4:Very} unpack(ite{4:VERY}|
1888          if                                            |
1889      hl_id_cell ~= nil then         {4:Much}           {4:MUCH}|
1890              --^ -- -- -- -- -- -- --{4:Error}- -- hl_i{4:ERROR}|
1891      l_id_cell {1:EOL}                                     |
1892          end                                           |
1893          for _ = 1, (count or 1) do                    |
1894              local cell = line[colpos]                 |
1895          {1:-}   cell.text = text                         {1:-}|
1896              cell.hl_id = hl_id                        |
1897              colpos = colpos+1                         |
1898          end                                           |
1899      end                                               |
1900                                                        |
1901    ]],
1902    }
1903
1904    feed '.'
1905    screen:expect {
1906      grid = [[
1907      for _,item in ipairs(items) do                    |
1908          local text, hl_id_cell, cou{4:Very} unpack(ite{4:VERY}|
1909          if                                            |
1910      hl_id_cell ~= nil then         {4:Much}           {4:MUCH}|
1911              --^ -- -- -- -- -- -- -- -- -- -- -- hl_id |
1912      = hl_id_cell {1:EOL}               {4:Error}         {4:ERROR}|
1913          end                                           |
1914          for _ = 1, (count or 1) do                    |
1915              local cell = line[colpos]                 |
1916          {1:-}   cell.text = text                         {1:-}|
1917              cell.hl_id = hl_id                        |
1918              colpos = colpos+1                         |
1919          end                                           |
1920      end                                               |
1921                                                        |
1922    ]],
1923    }
1924
1925    command 'set number'
1926    screen:expect {
1927      grid = [[
1928      {2:  1 }for _,item in ipairs(items) do                |
1929      {2:  2 }    local text, hl_id_cell, cou{4:Very} unpack{4:VERY}|
1930      {2:    }m)                                            |
1931      {2:  3 }    if                                        |
1932      {2:  4 }hl_id_cell ~= nil then         {4:Much}       {4:MUCH}|
1933      {2:  5 }        --^ -- -- -- -- -- -- -- -- -- -- -- hl|
1934      {2:    }_id = hl_id_cell {1:EOL}           {4:Error}     {4:ERROR}|
1935      {2:  6 }    end                                       |
1936      {2:  7 }    for _ = 1, (count or 1) do                |
1937      {2:  8 }        local cell = line[colpos]             |
1938      {2:  9 }    {1:-}   cell.text = text                     {1:-}|
1939      {2: 10 }        cell.hl_id = hl_id                    |
1940      {2: 11 }        colpos = colpos+1                     |
1941      {2: 12 }    end                                       |
1942                                                        |
1943    ]],
1944    }
1945
1946    command 'set cpoptions+=n'
1947    screen:expect {
1948      grid = [[
1949      {2:  1 }for _,item in ipairs(items) do                |
1950      {2:  2 }    local text, hl_id_cell, cou{4:Very} unpack{4:VERY}|
1951      m)                                                |
1952      {2:  3 }    if                                        |
1953      {2:  4 }hl_id_cell ~= nil then         {4:Much}       {4:MUCH}|
1954      {2:  5 }        --^ -- -- -- -- -- -- -- -- -- -- -- hl|
1955      _id = hl_id_cell {1:EOL}           {4:Error}         {4:ERROR}|
1956      {2:  6 }    end                                       |
1957      {2:  7 }    for _ = 1, (count or 1) do                |
1958      {2:  8 }        local cell = line[colpos]             |
1959      {2:  9 }    {1:-}   cell.text = text                     {1:-}|
1960      {2: 10 }        cell.hl_id = hl_id                    |
1961      {2: 11 }        colpos = colpos+1                     |
1962      {2: 12 }    end                                       |
1963                                                        |
1964    ]],
1965    }
1966
1967    command 'set cpoptions-=n nowrap'
1968    screen:expect {
1969      grid = [[
1970      {2:  1 }for _,item in ipairs(items) do                |
1971      {2:  2 }    local text, hl_id_cell, cou{4:Very} unpack{4:VERY}|
1972      {2:  3 }    if                                        |
1973      {2:  4 }hl_id_cell ~= nil then         {4:Much}       {4:MUCH}|
1974      {2:  5 }        --^ -- -- -- -- -- -- --{4:Error}- -- {4:ERROR}|
1975      {2:  6 }    end                                       |
1976      {2:  7 }    for _ = 1, (count or 1) do                |
1977      {2:  8 }        local cell = line[colpos]             |
1978      {2:  9 }    {1:-}   cell.text = text                     {1:-}|
1979      {2: 10 }        cell.hl_id = hl_id                    |
1980      {2: 11 }        colpos = colpos+1                     |
1981      {2: 12 }    end                                       |
1982      {2: 13 }end                                           |
1983      {1:~                                                 }|
1984                                                        |
1985    ]],
1986    }
1987
1988    feed '12zl'
1989    screen:expect {
1990      grid = [[
1991      {2:  1 }n ipairs(items) do                            |
1992      {2:  2 }xt, hl_id_cell, count = unpack({4:Very})      {4:VERY}|
1993      {2:  3 }                                              |
1994      {2:  4 }= nil then                     {4:Much}       {4:MUCH}|
1995      {2:  5 }^- -- -- -- -- -- -- -- -- -- --{4:Error}d = h{4:ERROR}|
1996      {2:  6 }                                              |
1997      {2:  7 }1, (count or 1) do                            |
1998      {2:  8 }l cell = line[colpos]                         |
1999      {2:  9 }.tex{1:-} = text                                 {1:-}|
2000      {2: 10 }.hl_id = hl_id                                |
2001      {2: 11 }os = colpos+1                                 |
2002      {2: 12 }                                              |
2003      {2: 13 }                                              |
2004      {1:~                                                 }|
2005                                                        |
2006    ]],
2007    }
2008
2009    feed('fhi<Tab>')
2010    screen:expect {
2011      grid = [[
2012      {2:  1 }n ipairs(items) do                            |
2013      {2:  2 }xt, hl_id_cell, count = unpack({4:Very})      {4:VERY}|
2014      {2:  3 }                                              |
2015      {2:  4 }= nil then                     {4:Much}       {4:MUCH}|
2016      {2:  5 }- -- -- -- -- -- -- -- -- -- --{4:Error}^hl_id{4:ERROR}|
2017      {2:  6 }                                              |
2018      {2:  7 }1, (count or 1) do                            |
2019      {2:  8 }l cell = line[colpos]                         |
2020      {2:  9 }.tex{1:-} = text                                 {1:-}|
2021      {2: 10 }.hl_id = hl_id                                |
2022      {2: 11 }os = colpos+1                                 |
2023      {2: 12 }                                              |
2024      {2: 13 }                                              |
2025      {1:~                                                 }|
2026      {24:-- INSERT --}                                      |
2027    ]],
2028    }
2029
2030    feed('<Esc>0')
2031    screen:expect {
2032      grid = [[
2033      {2:  1 }for _,item in ipairs(items) do                |
2034      {2:  2 }    local text, hl_id_cell, cou{4:Very} unpack{4:VERY}|
2035      {2:  3 }    if                                        |
2036      {2:  4 }hl_id_cell ~= nil then         {4:Much}       {4:MUCH}|
2037      {2:  5 }^        -- -- -- -- -- -- -- --{4:Error}- -- {4:ERROR}|
2038      {2:  6 }    end                                       |
2039      {2:  7 }    for _ = 1, (count or 1) do                |
2040      {2:  8 }        local cell = line[colpos]             |
2041      {2:  9 }    {1:-}   cell.text = text                     {1:-}|
2042      {2: 10 }        cell.hl_id = hl_id                    |
2043      {2: 11 }        colpos = colpos+1                     |
2044      {2: 12 }    end                                       |
2045      {2: 13 }end                                           |
2046      {1:~                                                 }|
2047                                                        |
2048    ]],
2049    }
2050  end)
2051
2052  it('virtual text win_col out of window does not break display #25645', function()
2053    screen:try_resize(51, 6)
2054    command('vnew')
2055    api.nvim_buf_set_lines(0, 0, -1, false, { string.rep('a', 50) })
2056    screen:expect {
2057      grid = [[
2058      ^aaaaaaaaaaaaaaaaaaaaaaaaa│                         |
2059      aaaaaaaaaaaaaaaaaaaaaaaaa│{1:~                        }|
2060      {1:~                        }│{1:~                        }|*2
2061      {41:[No Name] [+]             }{40:[No Name]                }|
2062                                                         |
2063    ]],
2064    }
2065    local extmark_opts = { virt_text_win_col = 35, virt_text = { { ' ', 'Comment' } } }
2066    api.nvim_buf_set_extmark(0, ns, 0, 0, extmark_opts)
2067    screen:expect_unchanged()
2068    assert_alive()
2069  end)
2070
2071  it('can have virtual text on folded line', function()
2072    insert([[
2073      11111
2074      22222
2075      33333]])
2076    command('1,2fold')
2077    screen:try_resize(50, 3)
2078    feed('zb')
2079    -- XXX: the behavior of overlay virtual text at non-zero column is strange:
2080    -- 1. With 'wrap' it is never shown.
2081    -- 2. With 'nowrap' it is shown only if the extmark is hidden before leftcol.
2082    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'AA', 'Underlined' } }, hl_mode = 'combine', virt_text_pos = 'overlay' })
2083    api.nvim_buf_set_extmark(0, ns, 0, 5, { virt_text = { { 'BB', 'Underlined' } }, hl_mode = 'combine', virt_text_win_col = 10 })
2084    api.nvim_buf_set_extmark(0, ns, 0, 2, { virt_text = { { 'CC', 'Underlined' } }, hl_mode = 'combine', virt_text_pos = 'right_align' })
2085    screen:expect {
2086      grid = [[
2087      {29:AA}{33:-  2 lin}{29:BB}{33:: 11111·····························}{29:CC}|
2088      3333^3                                             |
2089                                                        |
2090    ]],
2091    }
2092    command('set nowrap')
2093    screen:expect_unchanged()
2094    feed('zl')
2095    screen:expect {
2096      grid = [[
2097      {29:AA}{33:-  2 lin}{29:BB}{33:: 11111·····························}{29:CC}|
2098      333^3                                              |
2099                                                        |
2100    ]],
2101    }
2102    feed('zl')
2103    screen:expect {
2104      grid = [[
2105      {29:AA}{33:-  2 lin}{29:BB}{33:: 11111·····························}{29:CC}|
2106      33^3                                               |
2107                                                        |
2108    ]],
2109    }
2110    feed('zl')
2111    screen:expect {
2112      grid = [[
2113      {29:AA}{33:-  2 lin}{29:BB}{33:: 11111·····························}{29:CC}|
2114      3^3                                                |
2115                                                        |
2116    ]],
2117    }
2118  end)
2119
2120  it('virtual text works below diff filler lines', function()
2121    screen:try_resize(53, 8)
2122    insert([[
2123      aaaaa
2124      bbbbb
2125      ccccc
2126      ddddd
2127      eeeee]])
2128    command('rightbelow vnew')
2129    insert([[
2130      bbbbb
2131      ccccc
2132      ddddd
2133      eeeee]])
2134    command('windo diffthis')
2135    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'AA', 'Underlined' } }, virt_text_pos = 'overlay' })
2136    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'BB', 'Underlined' } }, virt_text_win_col = 10 })
2137    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'CC', 'Underlined' } }, virt_text_pos = 'right_align' })
2138    screen:expect {
2139      grid = [[
2140      {37:  }{38:aaaaa                   }│{37:  }{39:------------------------}|
2141      {37:  }bbbbb                   │{37:  }{28:AA}bbb     {28:BB}          {28:CC}|
2142      {37:  }ccccc                   │{37:  }ccccc                   |
2143      {37:  }ddddd                   │{37:  }ddddd                   |
2144      {37:  }eeeee                   │{37:  }eeee^e                   |
2145      {1:~                         }│{1:~                         }|
2146      {40:[No Name] [+]              }{41:[No Name] [+]             }|
2147                                                           |
2148    ]],
2149    }
2150    command('windo set wrap')
2151    screen:expect_unchanged()
2152  end)
2153
2154  it('can have virtual text which combines foreground and background groups', function()
2155    screen:try_resize(20, 5)
2156
2157    screen:set_default_attr_ids {
2158      [1] = { bold = true, foreground = Screen.colors.Blue },
2159      [2] = { background = tonumber('0x123456'), foreground = tonumber('0xbbbbbb') },
2160      [3] = { background = tonumber('0x123456'), foreground = tonumber('0xcccccc') },
2161      [4] = { background = tonumber('0x234567'), foreground = tonumber('0xbbbbbb') },
2162      [5] = { background = tonumber('0x234567'), foreground = tonumber('0xcccccc') },
2163      [6] = { bold = true, foreground = tonumber('0xcccccc'), background = tonumber('0x234567') },
2164    }
2165
2166    exec [[
2167      hi BgOne guibg=#123456
2168      hi BgTwo guibg=#234567
2169      hi FgEin guifg=#bbbbbb
2170      hi FgZwei guifg=#cccccc
2171      hi VeryBold gui=bold
2172    ]]
2173
2174    insert('##')
2175    local vt = {
2176      { 'a', { 'BgOne', 'FgEin' } },
2177      { 'b', { 'BgOne', 'FgZwei' } },
2178      { 'c', { 'BgTwo', 'FgEin' } },
2179      { 'd', { 'BgTwo', 'FgZwei' } },
2180      { 'X', { 'BgTwo', 'FgZwei', 'VeryBold' } },
2181    }
2182    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = vt, virt_text_pos = 'eol' })
2183    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = vt, virt_text_pos = 'right_align' })
2184    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = vt, virt_text_pos = 'inline' })
2185    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_lines = { vt, vt } })
2186    screen:expect {
2187      grid = [[
2188      {2:a}{3:b}{4:c}{5:d}{6:X}#^# {2:a}{3:b}{4:c}{5:d}{6:X}  {2:a}{3:b}{4:c}{5:d}{6:X}|
2189      {2:a}{3:b}{4:c}{5:d}{6:X}               |*2
2190      {1:~                   }|
2191                          |
2192    ]],
2193    }
2194  end)
2195
2196  it('does not crash when deleting a cleared buffer #15212', function()
2197    exec_lua [[
2198      ns = vim.api.nvim_create_namespace("myplugin")
2199      vim.api.nvim_buf_set_extmark(0, ns, 0, 0, {virt_text = {{"a"}}, end_col = 0})
2200    ]]
2201    screen:expect {
2202      grid = [[
2203      ^ a                                                |
2204      {1:~                                                 }|*13
2205                                                        |
2206    ]],
2207    }
2208
2209    exec_lua [[
2210      vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
2211      vim.cmd("bdelete")
2212    ]]
2213    screen:expect {
2214      grid = [[
2215      ^                                                  |
2216      {1:~                                                 }|*13
2217                                                        |
2218    ]],
2219    }
2220    assert_alive()
2221  end)
2222
2223  it('conceal with conceal char #19007', function()
2224    screen:try_resize(50, 5)
2225    insert('foo\n')
2226    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 0, end_row = 2, conceal = 'X' })
2227    command('set conceallevel=2')
2228    screen:expect([[
2229      {26:X}                                                 |
2230      ^                                                  |
2231      {1:~                                                 }|*2
2232                                                        |
2233    ]])
2234    command('set conceallevel=1')
2235    screen:expect_unchanged()
2236
2237    eq('conceal char must be printable', pcall_err(api.nvim_buf_set_extmark, 0, ns, 0, 0, { end_col = 0, end_row = 2, conceal = '\255' }))
2238  end)
2239
2240  it('conceal with composed conceal char', function()
2241    screen:try_resize(50, 5)
2242    insert('foo\n')
2243    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 0, end_row = 2, conceal = 'ẍ̲' })
2244    command('set conceallevel=2')
2245    screen:expect([[
2246      {26:ẍ̲}                                                 |
2247      ^                                                  |
2248      {1:~                                                 }|*2
2249                                                        |
2250    ]])
2251    command('set conceallevel=1')
2252    screen:expect_unchanged()
2253
2254    -- this is rare, but could happen. Save at least the first codepoint
2255    api.nvim__invalidate_glyph_cache()
2256    screen:expect {
2257      grid = [[
2258      {26:x}                                                 |
2259      ^                                                  |
2260      {1:~                                                 }|*2
2261                                                        |
2262    ]],
2263    }
2264  end)
2265
2266  it('conceal without conceal char #24782', function()
2267    screen:try_resize(50, 5)
2268    insert('foobar\n')
2269    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 3, conceal = '' })
2270    command('set listchars=conceal:?')
2271    command('let &conceallevel=1')
2272    screen:expect([[
2273      {26:?}bar                                              |
2274      ^                                                  |
2275      {1:~                                                 }|*2
2276                                                        |
2277    ]])
2278    command('let &conceallevel=2')
2279    screen:expect([[
2280      bar                                               |
2281      ^                                                  |
2282      {1:~                                                 }|*2
2283                                                        |
2284    ]])
2285  end)
2286
2287  it('conceal works just before truncated double-width char #21486', function()
2288    screen:try_resize(40, 4)
2289    api.nvim_buf_set_lines(0, 0, -1, true, { '', ('a'):rep(37) .. '<>古' })
2290    api.nvim_buf_set_extmark(0, ns, 1, 37, { end_col = 39, conceal = '' })
2291    command('setlocal conceallevel=2')
2292    screen:expect {
2293      grid = [[
2294      ^                                        |
2295      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{1:>}  |
2296      古                                      |
2297                                              |
2298    ]],
2299    }
2300    feed('j')
2301    screen:expect {
2302      grid = [[
2303                                              |
2304      ^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<>{1:>}|
2305      古                                      |
2306                                              |
2307    ]],
2308    }
2309  end)
2310
2311  it('redraws properly when adding/removing conceal on non-current line', function()
2312    screen:try_resize(50, 5)
2313    api.nvim_buf_set_lines(0, 0, -1, true, { 'abcd', 'efgh', 'ijkl', 'mnop' })
2314    command('setlocal conceallevel=2')
2315    screen:expect {
2316      grid = [[
2317      ^abcd                                              |
2318      efgh                                              |
2319      ijkl                                              |
2320      mnop                                              |
2321                                                        |
2322    ]],
2323    }
2324    api.nvim_buf_set_extmark(0, ns, 2, 1, { end_col = 3, conceal = '' })
2325    screen:expect {
2326      grid = [[
2327      ^abcd                                              |
2328      efgh                                              |
2329      il                                                |
2330      mnop                                              |
2331                                                        |
2332    ]],
2333    }
2334    api.nvim_buf_clear_namespace(0, ns, 0, -1)
2335    screen:expect {
2336      grid = [[
2337      ^abcd                                              |
2338      efgh                                              |
2339      ijkl                                              |
2340      mnop                                              |
2341                                                        |
2342    ]],
2343    }
2344  end)
2345
2346  it('avoids redraw issue #20651', function()
2347    exec_lua [[
2348      vim.cmd.normal'10oXXX'
2349      vim.cmd.normal'gg'
2350      local ns = vim.api.nvim_create_namespace('ns')
2351
2352      local bufnr = vim.api.nvim_create_buf(false, true)
2353      vim.api.nvim_open_win(bufnr, false, { relative = 'win', height = 1, width = 1, row = 0, col = 0 })
2354
2355      vim.api.nvim_create_autocmd('CursorMoved', { callback = function()
2356        local row = vim.api.nvim_win_get_cursor(0)[1] - 1
2357        vim.api.nvim_buf_set_extmark(0, ns, row, 0, { id = 1 })
2358        vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, {})
2359        vim.schedule(function()
2360          vim.api.nvim_buf_set_extmark(0, ns, row, 0, {
2361            id = 1,
2362            virt_text = {{'HELLO', 'Normal'}},
2363          })
2364        end)
2365      end
2366      })
2367    ]]
2368
2369    for _ = 1, 3 do
2370      vim.uv.sleep(10)
2371      feed 'j'
2372    end
2373
2374    screen:expect {
2375      grid = [[
2376      {44: }                                                 |
2377      XXX                                               |*2
2378      ^XXX HELLO                                         |
2379      XXX                                               |*7
2380      {1:~                                                 }|*3
2381                                                        |
2382    ]],
2383    }
2384  end)
2385
2386  it('underline attribute with higher priority takes effect #22371', function()
2387    screen:try_resize(50, 3)
2388    insert('aaabbbaaa')
2389    exec([[
2390      hi TestUL gui=underline guifg=Blue
2391      hi TestUC gui=undercurl guisp=Red
2392      hi TestBold gui=bold
2393    ]])
2394    screen:set_default_attr_ids({
2395      [0] = { bold = true, foreground = Screen.colors.Blue },
2396      [1] = { underline = true, foreground = Screen.colors.Blue },
2397      [2] = { undercurl = true, special = Screen.colors.Red },
2398      [3] = { underline = true, foreground = Screen.colors.Blue, special = Screen.colors.Red },
2399      [4] = { undercurl = true, foreground = Screen.colors.Blue, special = Screen.colors.Red },
2400      [5] = { bold = true, underline = true, foreground = Screen.colors.Blue },
2401      [6] = { bold = true, undercurl = true, special = Screen.colors.Red },
2402    })
2403
2404    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 9, hl_group = 'TestUL', priority = 20 })
2405    api.nvim_buf_set_extmark(0, ns, 0, 3, { end_col = 6, hl_group = 'TestUC', priority = 30 })
2406    screen:expect([[
2407      {1:aaa}{4:bbb}{1:aa^a}                                         |
2408      {0:~                                                 }|
2409                                                        |
2410    ]])
2411    api.nvim_buf_clear_namespace(0, ns, 0, -1)
2412    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 9, hl_group = 'TestUC', priority = 20 })
2413    api.nvim_buf_set_extmark(0, ns, 0, 3, { end_col = 6, hl_group = 'TestUL', priority = 30 })
2414    screen:expect([[
2415      {2:aaa}{3:bbb}{2:aa^a}                                         |
2416      {0:~                                                 }|
2417                                                        |
2418    ]])
2419    api.nvim_buf_clear_namespace(0, ns, 0, -1)
2420    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 9, hl_group = 'TestUL', priority = 30 })
2421    api.nvim_buf_set_extmark(0, ns, 0, 3, { end_col = 6, hl_group = 'TestUC', priority = 20 })
2422    screen:expect([[
2423      {1:aaa}{3:bbb}{1:aa^a}                                         |
2424      {0:~                                                 }|
2425                                                        |
2426    ]])
2427    api.nvim_buf_clear_namespace(0, ns, 0, -1)
2428    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 9, hl_group = 'TestUC', priority = 30 })
2429    api.nvim_buf_set_extmark(0, ns, 0, 3, { end_col = 6, hl_group = 'TestUL', priority = 20 })
2430    screen:expect([[
2431      {2:aaa}{4:bbb}{2:aa^a}                                         |
2432      {0:~                                                 }|
2433                                                        |
2434    ]])
2435
2436    -- When only one highlight group has an underline attribute, it should always take effect.
2437    for _, d in ipairs({ -5, 5 }) do
2438      api.nvim_buf_clear_namespace(0, ns, 0, -1)
2439      screen:expect([[
2440        aaabbbaa^a                                         |
2441        {0:~                                                 }|
2442                                                          |
2443      ]])
2444      api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 9, hl_group = 'TestUL', priority = 25 + d })
2445      api.nvim_buf_set_extmark(0, ns, 0, 3, { end_col = 6, hl_group = 'TestBold', priority = 25 - d })
2446      screen:expect([[
2447        {1:aaa}{5:bbb}{1:aa^a}                                         |
2448        {0:~                                                 }|
2449                                                          |
2450      ]])
2451    end
2452    for _, d in ipairs({ -5, 5 }) do
2453      api.nvim_buf_clear_namespace(0, ns, 0, -1)
2454      screen:expect([[
2455        aaabbbaa^a                                         |
2456        {0:~                                                 }|
2457                                                          |
2458      ]])
2459      api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 9, hl_group = 'TestUC', priority = 25 + d })
2460      api.nvim_buf_set_extmark(0, ns, 0, 3, { end_col = 6, hl_group = 'TestBold', priority = 25 - d })
2461      screen:expect([[
2462        {2:aaa}{6:bbb}{2:aa^a}                                         |
2463        {0:~                                                 }|
2464                                                          |
2465      ]])
2466    end
2467  end)
2468
2469  it('highlight is combined with syntax and sign linehl #20004', function()
2470    screen:try_resize(50, 3)
2471    insert([[
2472      function Func()
2473      end]])
2474    feed('gg')
2475    command('set ft=lua')
2476    command('syntax on')
2477    command('hi default MyMark guibg=LightGrey')
2478    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 3, hl_mode = 'combine', hl_group = 'MyMark' })
2479    command('hi default MyLine gui=underline')
2480    command('sign define CurrentLine linehl=MyLine')
2481    fn.sign_place(6, 'Test', 'CurrentLine', '', { lnum = 1 })
2482    screen:expect {
2483      grid = [[
2484      {30:^fun}{31:ction}{32: Func()                                   }|
2485      {6:end}                                               |
2486                                                        |
2487    ]],
2488    }
2489  end)
2490
2491  it('highlight can combine multiple groups', function()
2492    screen:try_resize(50, 3)
2493    command('hi Group1 guibg=Red guifg=Red guisp=Red')
2494    command('hi Group2 guibg=Blue guifg=Blue')
2495    command('hi Group3 guibg=Green')
2496    insert([[example text]])
2497    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 1, hl_group = {} })
2498    screen:expect([[
2499      example tex^t                                      |
2500      {1:~                                                 }|
2501                                                        |
2502    ]])
2503
2504    api.nvim_buf_clear_namespace(0, ns, 0, -1)
2505    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 1, hl_group = { 'Group1' } })
2506    screen:expect([[
2507      {45:example tex^t}                                      |
2508      {1:~                                                 }|
2509                                                        |
2510    ]])
2511    api.nvim_buf_clear_namespace(0, ns, 0, -1)
2512    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 1, hl_group = { 'Group1', 'Group2' } })
2513    screen:expect([[
2514      {46:example tex^t}                                      |
2515      {1:~                                                 }|
2516                                                        |
2517    ]])
2518    api.nvim_buf_clear_namespace(0, ns, 0, -1)
2519    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 1, hl_group = { 'Group1', 'Group2', 'Group3' }, hl_eol = true })
2520    screen:expect([[
2521      {47:example tex^t                                      }|
2522      {1:~                                                 }|
2523                                                        |
2524    ]])
2525
2526    eq(
2527      "Invalid 'hl_group': 'hl_group item'",
2528      pcall_err(api.nvim_buf_set_extmark, 0, ns, 0, 0, { end_row = 1, hl_group = { 'Group1', 'Group2', { 'fail' } }, hl_eol = true })
2529    )
2530  end)
2531
2532  it('highlight works after TAB with sidescroll #14201', function()
2533    screen:try_resize(50, 3)
2534    command('set nowrap')
2535    api.nvim_buf_set_lines(0, 0, -1, true, { '\tword word word word' })
2536    api.nvim_buf_set_extmark(0, ns, 0, 1, { end_col = 3, hl_group = 'ErrorMsg' })
2537    screen:expect {
2538      grid = [[
2539             ^ {4:wo}rd word word word                       |
2540      {1:~                                                 }|
2541                                                        |
2542    ]],
2543    }
2544    feed('7zl')
2545    screen:expect {
2546      grid = [[
2547       {4:^wo}rd word word word                              |
2548      {1:~                                                 }|
2549                                                        |
2550    ]],
2551    }
2552    feed('zl')
2553    screen:expect {
2554      grid = [[
2555      {4:^wo}rd word word word                               |
2556      {1:~                                                 }|
2557                                                        |
2558    ]],
2559    }
2560    feed('zl')
2561    screen:expect {
2562      grid = [[
2563      {4:^o}rd word word word                                |
2564      {1:~                                                 }|
2565                                                        |
2566    ]],
2567    }
2568  end)
2569
2570  it('highlights the beginning of a TAB char correctly #23734', function()
2571    screen:try_resize(50, 3)
2572    api.nvim_buf_set_lines(0, 0, -1, true, { 'this is the\ttab' })
2573    api.nvim_buf_set_extmark(0, ns, 0, 11, { end_col = 15, hl_group = 'ErrorMsg' })
2574    screen:expect {
2575      grid = [[
2576      ^this is the{4:     tab}                               |
2577      {1:~                                                 }|
2578                                                        |
2579    ]],
2580    }
2581
2582    api.nvim_buf_clear_namespace(0, ns, 0, -1)
2583    api.nvim_buf_set_extmark(0, ns, 0, 12, { end_col = 15, hl_group = 'ErrorMsg' })
2584    screen:expect {
2585      grid = [[
2586      ^this is the     {4:tab}                               |
2587      {1:~                                                 }|
2588                                                        |
2589    ]],
2590    }
2591  end)
2592
2593  it('highlight applies to a full TAB on line with matches #20885', function()
2594    screen:try_resize(50, 3)
2595    api.nvim_buf_set_lines(0, 0, -1, true, { '\t-- match1', '        -- match2' })
2596    fn.matchadd('NonText', 'match')
2597    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 1, end_col = 0, hl_group = 'Search' })
2598    api.nvim_buf_set_extmark(0, ns, 1, 0, { end_row = 2, end_col = 0, hl_group = 'Search' })
2599    screen:expect {
2600      grid = [[
2601      {34:       ^ -- }{35:match}{34:1}                                 |
2602      {34:        -- }{35:match}{34:2}                                 |
2603                                                        |
2604    ]],
2605    }
2606  end)
2607
2608  it('highlight applies to a full TAB in visual block mode', function()
2609    screen:try_resize(50, 8)
2610    command('hi! Visual guifg=NONE guibg=LightGrey')
2611    api.nvim_buf_set_lines(0, 0, -1, true, { 'asdf', '\tasdf', '\tasdf', '\tasdf', 'asdf' })
2612    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 5, end_col = 0, hl_group = 'Underlined' })
2613    screen:expect([[
2614      {28:^asdf}                                              |
2615      {28:        asdf}                                      |*3
2616      {28:asdf}                                              |
2617      {1:~                                                 }|*2
2618                                                        |
2619    ]])
2620    feed('<C-V>Gll')
2621    screen:expect([[
2622      {29:asd}{28:f}                                              |
2623      {29:   }{28:     asdf}                                      |*3
2624      {29:as^d}{28:f}                                              |
2625      {1:~                                                 }|*2
2626      {24:-- VISUAL BLOCK --}                                |
2627    ]])
2628  end)
2629
2630  it('highlight works properly with multibyte text and spell #26771', function()
2631    insert('口口\n')
2632    screen:try_resize(50, 3)
2633    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_col = 3, hl_group = 'Search' })
2634    screen:expect([[
2635      {34:口}口                                              |
2636      ^                                                  |
2637                                                        |
2638    ]])
2639    command('setlocal spell')
2640    screen:expect([[
2641      {43:口}{42:口}                                              |
2642      ^                                                  |
2643                                                        |
2644    ]])
2645  end)
2646
2647  it('supports multiline highlights', function()
2648    insert(example_text)
2649    feed 'gg'
2650    for _, i in ipairs { 1, 2, 3, 5, 6, 7 } do
2651      for _, j in ipairs { 2, 5, 10, 15 } do
2652        api.nvim_buf_set_extmark(0, ns, i, j, { end_col = j + 2, hl_group = 'NonText' })
2653      end
2654    end
2655    screen:expect {
2656      grid = [[
2657      ^for _,item in ipairs(items) do                    |
2658        {1:  }l{1:oc}al {1:te}xt,{1: h}l_id_cell, count = unpack(item)  |
2659        {1:  }i{1:f }hl_{1:id}_ce{1:ll} ~= nil then                     |
2660        {1:  } {1:  } hl{1:_i}d ={1: h}l_id_cell                        |
2661          end                                           |
2662        {1:  }f{1:or} _ {1:= }1, {1:(c}ount or 1) do                    |
2663        {1:  } {1:  } lo{1:ca}l c{1:el}l = line[colpos]                 |
2664        {1:  } {1:  } ce{1:ll}.te{1:xt} = text                          |
2665              cell.hl_id = hl_id                        |
2666              colpos = colpos+1                         |
2667          end                                           |
2668      end                                               |
2669      {1:~                                                 }|*2
2670                                                        |
2671    ]],
2672    }
2673    feed '5<c-e>'
2674    screen:expect {
2675      grid = [[
2676      ^  {1:  }f{1:or} _ {1:= }1, {1:(c}ount or 1) do                    |
2677        {1:  } {1:  } lo{1:ca}l c{1:el}l = line[colpos]                 |
2678        {1:  } {1:  } ce{1:ll}.te{1:xt} = text                          |
2679              cell.hl_id = hl_id                        |
2680              colpos = colpos+1                         |
2681          end                                           |
2682      end                                               |
2683      {1:~                                                 }|*7
2684                                                        |
2685    ]],
2686    }
2687
2688    api.nvim_buf_set_extmark(0, ns, 1, 0, { end_line = 8, end_col = 10, hl_group = 'ErrorMsg' })
2689    screen:expect {
2690      grid = [[
2691      {4:^  }{36:  }{4:f}{36:or}{4: _ }{36:= }{4:1, }{36:(c}{4:ount or 1) do}                    |
2692      {4:  }{36:  }{4: }{36:  }{4: lo}{36:ca}{4:l c}{36:el}{4:l = line[colpos]}                 |
2693      {4:  }{36:  }{4: }{36:  }{4: ce}{36:ll}{4:.te}{36:xt}{4: = text}                          |
2694      {4:        ce}ll.hl_id = hl_id                        |
2695              colpos = colpos+1                         |
2696          end                                           |
2697      end                                               |
2698      {1:~                                                 }|*7
2699                                                        |
2700    ]],
2701    }
2702  end)
2703
2704  local function with_undo_restore(val)
2705    screen:try_resize(50, 5)
2706    insert(example_text)
2707    feed 'gg'
2708    api.nvim_buf_set_extmark(0, ns, 0, 6, { end_col = 13, hl_group = 'NonText', undo_restore = val })
2709    screen:expect {
2710      grid = [[
2711      ^for _,{1:item in} ipairs(items) do                    |
2712          local text, hl_id_cell, count = unpack(item)  |
2713          if hl_id_cell ~= nil then                     |
2714              hl_id = hl_id_cell                        |
2715                                                        |
2716    ]],
2717    }
2718
2719    api.nvim_buf_set_text(0, 0, 4, 0, 8, { '' })
2720    screen:expect {
2721      grid = [[
2722      ^for {1:em in} ipairs(items) do                        |
2723          local text, hl_id_cell, count = unpack(item)  |
2724          if hl_id_cell ~= nil then                     |
2725              hl_id = hl_id_cell                        |
2726                                                        |
2727    ]],
2728    }
2729  end
2730
2731  it('highlights do reapply to restored text after delete', function()
2732    with_undo_restore(true) -- also default behavior
2733
2734    command('silent undo')
2735    screen:expect {
2736      grid = [[
2737      ^for _,{1:item in} ipairs(items) do                    |
2738          local text, hl_id_cell, count = unpack(item)  |
2739          if hl_id_cell ~= nil then                     |
2740              hl_id = hl_id_cell                        |
2741                                                        |
2742    ]],
2743    }
2744  end)
2745
2746  it("highlights don't reapply to restored text after delete with undo_restore=false", function()
2747    with_undo_restore(false)
2748
2749    command('silent undo')
2750    screen:expect {
2751      grid = [[
2752      ^for _,it{1:em in} ipairs(items) do                    |
2753          local text, hl_id_cell, count = unpack(item)  |
2754          if hl_id_cell ~= nil then                     |
2755              hl_id = hl_id_cell                        |
2756                                                        |
2757    ]],
2758    }
2759
2760    eq({
2761      {
2762        1,
2763        0,
2764        8,
2765        {
2766          end_col = 13,
2767          end_right_gravity = false,
2768          end_row = 0,
2769          hl_eol = false,
2770          hl_group = 'NonText',
2771          undo_restore = false,
2772          ns_id = ns,
2773          priority = 4096,
2774          right_gravity = true,
2775        },
2776      },
2777    }, api.nvim_buf_get_extmarks(0, ns, { 0, 0 }, { 0, -1 }, { details = true }))
2778  end)
2779
2780  it('virtual text works with rightleft', function()
2781    screen:try_resize(50, 3)
2782    insert('abcdefghijklmn')
2783    feed('0')
2784    command('set rightleft')
2785    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'EOL', 'Underlined' } } })
2786    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'right_align', 'Underlined' } }, virt_text_pos = 'right_align' })
2787    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'win_col', 'Underlined' } }, virt_text_win_col = 20 })
2788    api.nvim_buf_set_extmark(0, ns, 0, 2, { virt_text = { { 'overlayed', 'Underlined' } }, virt_text_pos = 'overlay' })
2789    screen:expect {
2790      grid = [[
2791      {28:ngila_thgir}            {28:loc_niw}  {28:LOE} nml{28:deyalrevo}b^a|
2792      {1:                                                 ~}|
2793                                                        |
2794    ]],
2795    }
2796
2797    insert(('#'):rep(32))
2798    feed('0')
2799    screen:expect {
2800      grid = [[
2801      {28:ngila_tdeyalrevo}ba#####{28:loc_niw}###################^#|
2802      {1:                                                 ~}|
2803                                                        |
2804    ]],
2805    }
2806
2807    insert(('#'):rep(16))
2808    feed('0')
2809    screen:expect {
2810      grid = [[
2811      {28:ngila_thgir}############{28:loc_niw}###################^#|
2812                                        {28:LOE} nml{28:deyalrevo}|
2813                                                        |
2814    ]],
2815    }
2816
2817    insert('###')
2818    feed('0')
2819    screen:expect {
2820      grid = [[
2821      #################################################^#|
2822      {28:ngila_thgir}            {28:loc_niw} {28:LOE} nml{28:deyalrevo}ba#|
2823                                                        |
2824    ]],
2825    }
2826
2827    command('set number')
2828    screen:expect {
2829      grid = [[
2830      #############################################^#{2: 1  }|
2831      {28:ngila_thgir}        {28:loc_niw} nml{28:deyalrevo}ba#####{2:    }|
2832                                                        |
2833    ]],
2834    }
2835
2836    command('set cpoptions+=n')
2837    screen:expect {
2838      grid = [[
2839      #############################################^#{2: 1  }|
2840      {28:ngila_thgir}            {28:loc_niw} nml{28:deyalrevo}ba#####|
2841                                                        |
2842    ]],
2843    }
2844  end)
2845
2846  it('virtual text overwrites double-width char properly', function()
2847    screen:try_resize(50, 3)
2848    insert('abcdefghij口klmnopqrstu口vwx口yz')
2849    feed('0')
2850    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { '!!!!!', 'Underlined' } }, virt_text_win_col = 11 })
2851    screen:expect {
2852      grid = [[
2853      ^abcdefghij {28:!!!!!}opqrstu口vwx口yz                  |
2854      {1:~                                                 }|
2855                                                        |
2856    ]],
2857    }
2858    feed('8x')
2859    screen:expect {
2860      grid = [[
2861      ^ij口klmnopq{28:!!!!!} vwx口yz                          |
2862      {1:~                                                 }|
2863                                                        |
2864    ]],
2865    }
2866    feed('3l5x')
2867    screen:expect {
2868      grid = [[
2869      ij口^pqrstu {28:!!!!!} yz                               |
2870      {1:~                                                 }|
2871                                                        |
2872    ]],
2873    }
2874    feed('5x')
2875    screen:expect {
2876      grid = [[
2877      ij口^u口vwx {28:!!!!!}                                  |
2878      {1:~                                                 }|
2879                                                        |
2880    ]],
2881    }
2882  end)
2883
2884  it('virtual text blending space does not overwrite double-width char', function()
2885    screen:try_resize(50, 3)
2886    insert('abcdefghij口klmnopqrstu口vwx口yz')
2887    feed('0')
2888    command('hi Blendy guibg=Red blend=30')
2889    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { ' !  ! ', 'Blendy' } }, virt_text_win_col = 8, hl_mode = 'blend' })
2890    screen:expect {
2891      grid = [[
2892      ^abcdefgh{10:i}{7:!}{10:口}{7:!}{10:l}mnopqrstu口vwx口yz                  |
2893      {1:~                                                 }|
2894                                                        |
2895    ]],
2896    }
2897    feed('x')
2898    screen:expect {
2899      grid = [[
2900      ^bcdefghi{10:j}{7:!}{10: k}{7:!}{10:m}nopqrstu口vwx口yz                   |
2901      {1:~                                                 }|
2902                                                        |
2903    ]],
2904    }
2905    feed('x')
2906    screen:expect {
2907      grid = [[
2908      ^cdefghij{10: }{7:!}{10:kl}{7:!}{10:n}opqrstu口vwx口yz                    |
2909      {1:~                                                 }|
2910                                                        |
2911    ]],
2912    }
2913    feed('x')
2914    screen:expect {
2915      grid = [[
2916      ^defghij口{7:!}{10:lm}{7:!}{10:o}pqrstu口vwx口yz                     |
2917      {1:~                                                 }|
2918                                                        |
2919    ]],
2920    }
2921    feed('7x')
2922    screen:expect {
2923      grid = [[
2924      ^口klmnop{10:q}{7:!}{10:st}{7:!}{10:口}vwx口yz                            |
2925      {1:~                                                 }|
2926                                                        |
2927    ]],
2928    }
2929  end)
2930
2931  it('virtual text works with double-width char and rightleft', function()
2932    screen:try_resize(50, 3)
2933    insert('abcdefghij口klmnopqrstu口vwx口yz')
2934    feed('0')
2935    command('set rightleft')
2936    screen:expect {
2937      grid = [[
2938                        zy口xwv口utsrqponmlk口jihgfedcb^a|
2939      {1:                                                 ~}|
2940                                                        |
2941    ]],
2942    }
2943    api.nvim_buf_set_extmark(0, ns, 0, 2, { virt_text = { { 'overlayed', 'Underlined' } }, virt_text_pos = 'overlay' })
2944    api.nvim_buf_set_extmark(0, ns, 0, 14, { virt_text = { { '古', 'Underlined' } }, virt_text_pos = 'overlay' })
2945    api.nvim_buf_set_extmark(0, ns, 0, 20, { virt_text = { { '\t', 'Underlined' } }, virt_text_pos = 'overlay' })
2946    api.nvim_buf_set_extmark(0, ns, 0, 29, { virt_text = { { '古', 'Underlined' } }, virt_text_pos = 'overlay' })
2947    screen:expect {
2948      grid = [[
2949                        zy {28:古}wv {28:     }qpon{28:古}k {28:deyalrevo}b^a|
2950      {1:                                                 ~}|
2951                                                        |
2952    ]],
2953    }
2954  end)
2955
2956  it('virtual text is drawn correctly after delete and undo #27368', function()
2957    insert('aaa\nbbb\nccc\nddd\neee')
2958    command('vsplit')
2959    api.nvim_buf_set_extmark(0, ns, 2, 0, { virt_text = { { 'EOL' } } })
2960    feed('3gg')
2961    screen:expect {
2962      grid = [[
2963      aaa                      │aaa                     |
2964      bbb                      │bbb                     |
2965      ^ccc EOL                  │ccc EOL                 |
2966      ddd                      │ddd                     |
2967      eee                      │eee                     |
2968      {1:~                        }│{1:~                       }|*8
2969      {41:[No Name] [+]             }{40:[No Name] [+]           }|
2970                                                        |
2971    ]],
2972    }
2973    feed('dd')
2974    screen:expect {
2975      grid = [[
2976      aaa                      │aaa                     |
2977      bbb                      │bbb                     |
2978      ^ddd EOL                  │ddd EOL                 |
2979      eee                      │eee                     |
2980      {1:~                        }│{1:~                       }|*9
2981      {41:[No Name] [+]             }{40:[No Name] [+]           }|
2982                                                        |
2983    ]],
2984    }
2985    command('silent undo')
2986    screen:expect {
2987      grid = [[
2988      aaa                      │aaa                     |
2989      bbb                      │bbb                     |
2990      ^ccc EOL                  │ccc EOL                 |
2991      ddd                      │ddd                     |
2992      eee                      │eee                     |
2993      {1:~                        }│{1:~                       }|*8
2994      {41:[No Name] [+]             }{40:[No Name] [+]           }|
2995                                                        |
2996    ]],
2997    }
2998  end)
2999
3000  it('virtual text does not crash with blend, conceal and wrap #27836', function()
3001    screen:try_resize(50, 3)
3002    insert(('a'):rep(45) .. '|hidden|' .. ('b'):rep(45))
3003    command('syntax match test /|hidden|/ conceal')
3004    command('set conceallevel=2 concealcursor=n')
3005    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'FOO' } }, virt_text_pos = 'right_align', hl_mode = 'blend' })
3006    screen:expect {
3007      grid = [[
3008      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  FOO|
3009      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb^b     |
3010                                                        |
3011    ]],
3012    }
3013  end)
3014
3015  it('works with both hl_group and sign_hl_group', function()
3016    screen:try_resize(50, 3)
3017    screen:add_extra_attr_ids({
3018      [100] = { background = Screen.colors.WebGray, foreground = Screen.colors.Blue, bold = true },
3019    })
3020    insert('abcdefghijklmn')
3021    api.nvim_buf_set_extmark(0, ns, 0, 0, { sign_text = 'S', sign_hl_group = 'NonText', hl_group = 'Error', end_col = 14 })
3022    screen:expect([[
3023      {100:S }{9:abcdefghijklm^n}                                  |
3024      {1:~                                                 }|
3025                                                        |
3026    ]])
3027  end)
3028
3029  it('virt_text_repeat_linebreak repeats virtual text on wrapped lines', function()
3030    screen:try_resize(40, 5)
3031    api.nvim_set_option_value('breakindent', true, {})
3032    insert(example_text)
3033    api.nvim_buf_set_extmark(
3034      0,
3035      ns,
3036      1,
3037      0,
3038      { virt_text = { { '│', 'NonText' } }, virt_text_pos = 'overlay', virt_text_repeat_linebreak = true }
3039    )
3040    api.nvim_buf_set_extmark(
3041      0,
3042      ns,
3043      1,
3044      3,
3045      { virt_text = { { '│', 'NonText' } }, virt_text_pos = 'overlay', virt_text_repeat_linebreak = true }
3046    )
3047    command('norm gg')
3048    screen:expect {
3049      grid = [[
3050      ^for _,item in ipairs(items) do          |
3051      {1:│}  {1:│}local text, hl_id_cell, count = unpa|
3052      {1:│}  {1:│}ck(item)                            |
3053          if hl_id_cell ~= nil then           |
3054                                              |
3055    ]],
3056    }
3057    api.nvim_buf_clear_namespace(0, ns, 0, -1)
3058    api.nvim_buf_set_extmark(
3059      0,
3060      ns,
3061      1,
3062      0,
3063      { virt_text = { { '│', 'NonText' } }, virt_text_repeat_linebreak = true, virt_text_win_col = 0 }
3064    )
3065    api.nvim_buf_set_extmark(
3066      0,
3067      ns,
3068      1,
3069      0,
3070      { virt_text = { { '│', 'NonText' } }, virt_text_repeat_linebreak = true, virt_text_win_col = 2 }
3071    )
3072    screen:expect {
3073      grid = [[
3074      ^for _,item in ipairs(items) do          |
3075      {1:│} {1:│} local text, hl_id_cell, count = unpa|
3076      {1:│} {1:│} ck(item)                            |
3077          if hl_id_cell ~= nil then           |
3078                                              |
3079    ]],
3080    }
3081  end)
3082
3083  it('supports URLs', function()
3084    insert(example_text)
3085
3086    local url1 = 'https://example.com'
3087    local url2 = 'http://127.0.0.1'
3088
3089    screen:add_extra_attr_ids {
3090      u = { url = url1 },
3091      uh = { url = url2, background = Screen.colors.Yellow },
3092    }
3093
3094    api.nvim_buf_set_extmark(0, ns, 1, 4, {
3095      end_col = 14,
3096      url = url1,
3097    })
3098    api.nvim_buf_set_extmark(0, ns, 2, 4, {
3099      end_col = 17,
3100      hl_group = 'Search',
3101      url = url2,
3102    })
3103
3104    screen:expect([[
3105      for _,item in ipairs(items) do                    |
3106          {u:local text}, hl_id_cell, count = unpack(item)  |
3107          {uh:if hl_id_cell} ~= nil then                     |
3108              hl_id = hl_id_cell                        |
3109          end                                           |
3110          for _ = 1, (count or 1) do                    |
3111              local cell = line[colpos]                 |
3112              cell.text = text                          |
3113              cell.hl_id = hl_id                        |
3114              colpos = colpos+1                         |
3115          end                                           |
3116      en^d                                               |
3117      {1:~                                                 }|*2
3118                                                        |
3119    ]])
3120  end)
3121
3122  it('can replace marks in place with different decorations #27211', function()
3123    local mark = api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_lines = { { { 'foo', 'ErrorMsg' } } } })
3124    screen:expect {
3125      grid = [[
3126      ^                                                  |
3127      {4:foo}                                               |
3128      {1:~                                                 }|*12
3129                                                        |
3130    ]],
3131    }
3132
3133    api.nvim_buf_set_extmark(0, ns, 0, 0, {
3134      id = mark,
3135      virt_text = { { 'testing', 'NonText' } },
3136      virt_text_pos = 'inline',
3137    })
3138    screen:expect {
3139      grid = [[
3140      {1:^testing}                                           |
3141      {1:~                                                 }|*13
3142                                                        |
3143    ]],
3144    }
3145
3146    api.nvim_buf_del_extmark(0, ns, mark)
3147    screen:expect {
3148      grid = [[
3149      ^                                                  |
3150      {1:~                                                 }|*13
3151                                                        |
3152    ]],
3153    }
3154
3155    n.assert_alive()
3156  end)
3157
3158  it('priority ordering of overlay or win_col virtual text at same position', function()
3159    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'A' } }, virt_text_pos = 'overlay', priority = 100 })
3160    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'A' } }, virt_text_win_col = 30, priority = 100 })
3161    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'BB' } }, virt_text_pos = 'overlay', priority = 90 })
3162    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'BB' } }, virt_text_win_col = 30, priority = 90 })
3163    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'CCC' } }, virt_text_pos = 'overlay', priority = 80 })
3164    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'CCC' } }, virt_text_win_col = 30, priority = 80 })
3165    screen:expect([[
3166      ^ABC                           ABC                 |
3167      {1:~                                                 }|*13
3168                                                        |
3169    ]])
3170  end)
3171
3172  it('priority ordering of inline and non-inline virtual text at same char', function()
3173    insert(('?'):rep(40) .. ('!'):rep(30))
3174    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'A' } }, virt_text_pos = 'overlay', priority = 10 })
3175    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'a' } }, virt_text_win_col = 15, priority = 10 })
3176    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'BBBB' } }, virt_text_pos = 'inline', priority = 15 })
3177    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'C' } }, virt_text_pos = 'overlay', priority = 20 })
3178    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'c' } }, virt_text_win_col = 17, priority = 20 })
3179    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'DDDD' } }, virt_text_pos = 'inline', priority = 25 })
3180    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'E' } }, virt_text_pos = 'overlay', priority = 30 })
3181    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'e' } }, virt_text_win_col = 19, priority = 30 })
3182    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'FFFF' } }, virt_text_pos = 'inline', priority = 35 })
3183    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'G' } }, virt_text_pos = 'overlay', priority = 40 })
3184    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'g' } }, virt_text_win_col = 21, priority = 40 })
3185    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'HHHH' } }, virt_text_pos = 'inline', priority = 45 })
3186    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'I' } }, virt_text_pos = 'overlay', priority = 50 })
3187    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'i' } }, virt_text_win_col = 23, priority = 50 })
3188    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'JJJJ' } }, virt_text_pos = 'inline', priority = 55 })
3189    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'K' } }, virt_text_pos = 'overlay', priority = 60 })
3190    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { 'k' } }, virt_text_win_col = 25, priority = 60 })
3191    screen:expect([[
3192      ???????????????a?c?e????????????????????ABBBCDDDEF|
3193      FFGHHHIJJJK!!!!!!!!!!g!i!k!!!!!!!!!!!!!^!          |
3194      {1:~                                                 }|*12
3195                                                        |
3196    ]])
3197    feed('02x$')
3198    screen:expect([[
3199      ???????????????a?c?e??????????????????ABBBCDDDEFFF|
3200      GHHHIJJJK!!!!!!!!!!!!g!i!k!!!!!!!!!!!^!            |
3201      {1:~                                                 }|*12
3202                                                        |
3203    ]])
3204    feed('02x$')
3205    screen:expect([[
3206      ???????????????a?c?e?g??????????????ABBBCDDDEFFFGH|
3207      HHIJJJK!!!!!!!!!!!!!!!!i!k!!!!!!!!!^!              |
3208      {1:~                                                 }|*12
3209                                                        |
3210    ]])
3211    feed('02x$')
3212    screen:expect([[
3213      ???????????????a?c?e?g????????????ABBBCDDDEFFFGHHH|
3214      IJJJK!!!!!!!!!!!!!!!!!!i!k!!!!!!!^!                |
3215      {1:~                                                 }|*12
3216                                                        |
3217    ]])
3218    command('set nowrap')
3219    feed('0')
3220    screen:expect([[
3221      ^???????????????a?c?e?g?i?k????????ABBBCDDDEFFFGHHH|
3222      {1:~                                                 }|*13
3223                                                        |
3224    ]])
3225    feed('2x')
3226    screen:expect([[
3227      ^???????????????a?c?e?g?i?k??????ABBBCDDDEFFFGHHHIJ|
3228      {1:~                                                 }|*13
3229                                                        |
3230    ]])
3231    feed('2x')
3232    screen:expect([[
3233      ^???????????????a?c?e?g?i?k????ABBBCDDDEFFFGHHHIJJJ|
3234      {1:~                                                 }|*13
3235                                                        |
3236    ]])
3237    feed('2x')
3238    screen:expect([[
3239      ^???????????????a?c?e?g?i?k??ABBBCDDDEFFFGHHHIJJJK!|
3240      {1:~                                                 }|*13
3241                                                        |
3242    ]])
3243  end)
3244
3245  it('conceal_lines', function()
3246    insert(example_text)
3247    exec('set number conceallevel=3')
3248    feed('ggj')
3249    local not_concealed = {
3250      grid = [[
3251        {2:  1 }for _,item in ipairs(items) do                |
3252        {2:  2 }^    local text, hl_id_cell, count = unpack(ite|
3253        {2:    }m)                                            |
3254        {2:  3 }    if hl_id_cell ~= nil then                 |
3255        {2:  4 }        hl_id = hl_id_cell                    |
3256        {2:  5 }    end                                       |
3257        {2:  6 }    for _ = 1, (count or 1) do                |
3258        {2:  7 }        local cell = line[colpos]             |
3259        {2:  8 }        cell.text = text                      |
3260        {2:  9 }        cell.hl_id = hl_id                    |
3261        {2: 10 }        colpos = colpos+1                     |
3262        {2: 11 }    end                                       |
3263        {2: 12 }end                                           |
3264        {1:~                                                 }|
3265                                                          |
3266      ]],
3267    }
3268    screen:expect(not_concealed)
3269    api.nvim_buf_set_extmark(0, ns, 1, 0, { conceal_lines = '' })
3270    screen:expect_unchanged()
3271    feed('j')
3272    local concealed = {
3273      grid = [[
3274        {2:  1 }for _,item in ipairs(items) do                |
3275        {2:  3 }^    if hl_id_cell ~= nil then                 |
3276        {2:  4 }        hl_id = hl_id_cell                    |
3277        {2:  5 }    end                                       |
3278        {2:  6 }    for _ = 1, (count or 1) do                |
3279        {2:  7 }        local cell = line[colpos]             |
3280        {2:  8 }        cell.text = text                      |
3281        {2:  9 }        cell.hl_id = hl_id                    |
3282        {2: 10 }        colpos = colpos+1                     |
3283        {2: 11 }    end                                       |
3284        {2: 12 }end                                           |
3285        {1:~                                                 }|*3
3286                                                          |
3287      ]],
3288    }
3289    screen:expect(concealed)
3290    feed('k')
3291    screen:expect(not_concealed)
3292    exec('set concealcursor=n')
3293    screen:expect(concealed)
3294    api.nvim_buf_set_extmark(0, ns, 3, 0, { conceal_lines = '' })
3295    screen:expect({
3296      grid = [[
3297        {2:  1 }for _,item in ipairs(items) do                |
3298        {2:  3 }^    if hl_id_cell ~= nil then                 |
3299        {2:  5 }    end                                       |
3300        {2:  6 }    for _ = 1, (count or 1) do                |
3301        {2:  7 }        local cell = line[colpos]             |
3302        {2:  8 }        cell.text = text                      |
3303        {2:  9 }        cell.hl_id = hl_id                    |
3304        {2: 10 }        colpos = colpos+1                     |
3305        {2: 11 }    end                                       |
3306        {2: 12 }end                                           |
3307        {1:~                                                 }|*4
3308                                                          |
3309      ]],
3310    })
3311    feed('kjj')
3312    screen:expect_unchanged()
3313    api.nvim_buf_set_extmark(0, ns, 4, 0, { conceal_lines = '' })
3314    feed('kjjjC')
3315    screen:expect({
3316      grid = [[
3317        {2:  1 }for _,item in ipairs(items) do                |
3318        {2:  3 }    if hl_id_cell ~= nil then                 |
3319        {2:  5 }^                                              |
3320        {2:  6 }    for _ = 1, (count or 1) do                |
3321        {2:  7 }        local cell = line[colpos]             |
3322        {2:  8 }        cell.text = text                      |
3323        {2:  9 }        cell.hl_id = hl_id                    |
3324        {2: 10 }        colpos = colpos+1                     |
3325        {2: 11 }    end                                       |
3326        {2: 12 }end                                           |
3327        {1:~                                                 }|*4
3328        {24:-- INSERT --}                                      |
3329      ]],
3330    })
3331    feed('<esc>')
3332    screen:expect({
3333      grid = [[
3334        {2:  1 }for _,item in ipairs(items) do                |
3335        {2:  3 }    if hl_id_cell ~= nil then                 |
3336        {2:  6 }^    for _ = 1, (count or 1) do                |
3337        {2:  7 }        local cell = line[colpos]             |
3338        {2:  8 }        cell.text = text                      |
3339        {2:  9 }        cell.hl_id = hl_id                    |
3340        {2: 10 }        colpos = colpos+1                     |
3341        {2: 11 }    end                                       |
3342        {2: 12 }end                                           |
3343        {1:~                                                 }|*5
3344                                                          |
3345      ]],
3346    })
3347    feed('kji')
3348    screen:expect({
3349      grid = [[
3350        {2:  1 }for _,item in ipairs(items) do                |
3351        {2:  3 }    if hl_id_cell ~= nil then                 |
3352        {2:  5 }^                                              |
3353        {2:  6 }    for _ = 1, (count or 1) do                |
3354        {2:  7 }        local cell = line[colpos]             |
3355        {2:  8 }        cell.text = text                      |
3356        {2:  9 }        cell.hl_id = hl_id                    |
3357        {2: 10 }        colpos = colpos+1                     |
3358        {2: 11 }    end                                       |
3359        {2: 12 }end                                           |
3360        {1:~                                                 }|*4
3361        {24:-- INSERT --}                                      |
3362      ]],
3363    })
3364    feed('conceal text')
3365    screen:expect({
3366      grid = [[
3367        {2:  1 }for _,item in ipairs(items) do                |
3368        {2:  3 }    if hl_id_cell ~= nil then                 |
3369        {2:  5 }conceal text^                                  |
3370        {2:  6 }    for _ = 1, (count or 1) do                |
3371        {2:  7 }        local cell = line[colpos]             |
3372        {2:  8 }        cell.text = text                      |
3373        {2:  9 }        cell.hl_id = hl_id                    |
3374        {2: 10 }        colpos = colpos+1                     |
3375        {2: 11 }    end                                       |
3376        {2: 12 }end                                           |
3377        {1:~                                                 }|*4
3378        {24:-- INSERT --}                                      |
3379      ]],
3380    })
3381    feed('<esc>')
3382    screen:expect({
3383      grid = [[
3384        {2:  1 }for _,item in ipairs(items) do                |
3385        {2:  3 }    if hl_id_cell ~= nil then                 |
3386        {2:  6 }    for _ =^ 1, (count or 1) do                |
3387        {2:  7 }        local cell = line[colpos]             |
3388        {2:  8 }        cell.text = text                      |
3389        {2:  9 }        cell.hl_id = hl_id                    |
3390        {2: 10 }        colpos = colpos+1                     |
3391        {2: 11 }    end                                       |
3392        {2: 12 }end                                           |
3393        {1:~                                                 }|*5
3394                                                          |
3395      ]],
3396    })
3397    feed('ggzfj')
3398    screen:expect({
3399      grid = [[
3400        {2:  1 }{33:^+--  2 lines: for _,item in ipairs(items) do··}|
3401        {2:  3 }    if hl_id_cell ~= nil then                 |
3402        {2:  6 }    for _ = 1, (count or 1) do                |
3403        {2:  7 }        local cell = line[colpos]             |
3404        {2:  8 }        cell.text = text                      |
3405        {2:  9 }        cell.hl_id = hl_id                    |
3406        {2: 10 }        colpos = colpos+1                     |
3407        {2: 11 }    end                                       |
3408        {2: 12 }end                                           |
3409        {1:~                                                 }|*5
3410                                                          |
3411      ]],
3412    })
3413    feed('j')
3414    screen:expect({
3415      grid = [[
3416        {2:  1 }{33:+--  2 lines: for _,item in ipairs(items) do··}|
3417        {2:  3 }^    if hl_id_cell ~= nil then                 |
3418        {2:  6 }    for _ = 1, (count or 1) do                |
3419        {2:  7 }        local cell = line[colpos]             |
3420        {2:  8 }        cell.text = text                      |
3421        {2:  9 }        cell.hl_id = hl_id                    |
3422        {2: 10 }        colpos = colpos+1                     |
3423        {2: 11 }    end                                       |
3424        {2: 12 }end                                           |
3425        {1:~                                                 }|*5
3426                                                          |
3427      ]],
3428    })
3429    feed('ggzdjzfj')
3430    screen:expect({
3431      grid = [[
3432        {2:  1 }for _,item in ipairs(items) do                |
3433        {2:  6 }^    for _ = 1, (count or 1) do                |
3434        {2:  7 }        local cell = line[colpos]             |
3435        {2:  8 }        cell.text = text                      |
3436        {2:  9 }        cell.hl_id = hl_id                    |
3437        {2: 10 }        colpos = colpos+1                     |
3438        {2: 11 }    end                                       |
3439        {2: 12 }end                                           |
3440        {1:~                                                 }|*6
3441                                                          |
3442      ]],
3443    })
3444    feed('jj')
3445    screen:expect({
3446      grid = [[
3447        {2:  1 }^for _,item in ipairs(items) do                |
3448        {2:  6 }    for _ = 1, (count or 1) do                |
3449        {2:  7 }        local cell = line[colpos]             |
3450        {2:  8 }        cell.text = text                      |
3451        {2:  9 }        cell.hl_id = hl_id                    |
3452        {2: 10 }        colpos = colpos+1                     |
3453        {2: 11 }    end                                       |
3454        {2: 12 }end                                           |
3455        {1:~                                                 }|*6
3456                                                          |
3457      ]],
3458    })
3459    -- Below virtual line belonging to line above concealed line is drawn.
3460    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_lines = { { { 'line 1 below' } } } })
3461    -- Above virtual line belonging to concealed line isn't.
3462    api.nvim_buf_set_extmark(0, ns, 1, 0, {
3463      virt_lines = { { { 'line 2 above' } } },
3464      virt_lines_above = true,
3465    })
3466    screen:expect([[
3467      {2:  1 }for _,item in ipairs(items) do                |
3468      {2:    }line 1 below                                  |
3469      {2:  6 }^    for _ = 1, (count or 1) do                |
3470      {2:  7 }        local cell = line[colpos]             |
3471      {2:  8 }        cell.text = text                      |
3472      {2:  9 }        cell.hl_id = hl_id                    |
3473      {2: 10 }        colpos = colpos+1                     |
3474      {2: 11 }    end                                       |
3475      {2: 12 }end                                           |
3476      {1:~                                                 }|*5
3477                                                        |
3478    ]])
3479    -- w_lines.wl_lastlnum values are valid
3480    command('set relativenumber concealcursor=')
3481    api.nvim_buf_clear_namespace(0, ns, 0, -1)
3482    api.nvim_buf_set_extmark(0, ns, 1, 0, { conceal_lines = '' })
3483    api.nvim_buf_set_extmark(0, ns, 4, 0, { conceal_lines = '' })
3484    feed('zE')
3485    screen:expect([[
3486      {2:  4 }for _,item in ipairs(items) do                |
3487      {2:  2 }    if hl_id_cell ~= nil then                 |
3488      {2:  1 }        hl_id = hl_id_cell                    |
3489      {2:5   }^conceal text                                  |
3490      {2:  1 }    for _ = 1, (count or 1) do                |
3491      {2:  2 }        local cell = line[colpos]             |
3492      {2:  3 }        cell.text = text                      |
3493      {2:  4 }        cell.hl_id = hl_id                    |
3494      {2:  5 }        colpos = colpos+1                     |
3495      {2:  6 }    end                                       |
3496      {2:  7 }end                                           |
3497      {1:~                                                 }|*3
3498                                                        |
3499    ]])
3500    feed('jj')
3501    screen:expect([[
3502      {2:  6 }for _,item in ipairs(items) do                |
3503      {2:  4 }    if hl_id_cell ~= nil then                 |
3504      {2:  3 }        hl_id = hl_id_cell                    |
3505      {2:  1 }    for _ = 1, (count or 1) do                |
3506      {2:7   }^        local cell = line[colpos]             |
3507      {2:  1 }        cell.text = text                      |
3508      {2:  2 }        cell.hl_id = hl_id                    |
3509      {2:  3 }        colpos = colpos+1                     |
3510      {2:  4 }    end                                       |
3511      {2:  5 }end                                           |
3512      {1:~                                                 }|*4
3513                                                        |
3514    ]])
3515    -- Correct relativenumber for line below concealed line #33694
3516    feed('4Gk')
3517    screen:expect([[
3518      {2:  2 }for _,item in ipairs(items) do                |
3519      {2:3   }    if h^l_id_cell ~= nil then                 |
3520      {2:  1 }        hl_id = hl_id_cell                    |
3521      {2:  3 }    for _ = 1, (count or 1) do                |
3522      {2:  4 }        local cell = line[colpos]             |
3523      {2:  5 }        cell.text = text                      |
3524      {2:  6 }        cell.hl_id = hl_id                    |
3525      {2:  7 }        colpos = colpos+1                     |
3526      {2:  8 }    end                                       |
3527      {2:  9 }end                                           |
3528      {1:~                                                 }|*4
3529                                                        |
3530    ]])
3531    -- Also with above virtual line #32744
3532    command('set nornu')
3533    api.nvim_buf_set_extmark(0, ns, 3, 0, { virt_lines = { { { 'virt_below 4' } } } })
3534    feed('6G')
3535    screen:expect([[
3536      {2:  1 }for _,item in ipairs(items) do                |
3537      {2:  3 }    if hl_id_cell ~= nil then                 |
3538      {2:  4 }        hl_id = hl_id_cell                    |
3539      {2:    }virt_below 4                                  |
3540      {2:  6 }    ^for _ = 1, (count or 1) do                |
3541      {2:  7 }        local cell = line[colpos]             |
3542      {2:  8 }        cell.text = text                      |
3543      {2:  9 }        cell.hl_id = hl_id                    |
3544      {2: 10 }        colpos = colpos+1                     |
3545      {2: 11 }    end                                       |
3546      {2: 12 }end                                           |
3547      {1:~                                                 }|*3
3548                                                        |
3549    ]])
3550    feed('j')
3551    screen:expect([[
3552      {2:  1 }for _,item in ipairs(items) do                |
3553      {2:  3 }    if hl_id_cell ~= nil then                 |
3554      {2:  4 }        hl_id = hl_id_cell                    |
3555      {2:    }virt_below 4                                  |
3556      {2:  6 }    for _ = 1, (count or 1) do                |
3557      {2:  7 }    ^    local cell = line[colpos]             |
3558      {2:  8 }        cell.text = text                      |
3559      {2:  9 }        cell.hl_id = hl_id                    |
3560      {2: 10 }        colpos = colpos+1                     |
3561      {2: 11 }    end                                       |
3562      {2: 12 }end                                           |
3563      {1:~                                                 }|*3
3564                                                        |
3565    ]])
3566    -- Even when virtual line is added as line is concealed #32762
3567    feed('5G')
3568    api.nvim_buf_clear_namespace(0, ns, 3, 4)
3569    feed('j')
3570    api.nvim_buf_set_extmark(0, ns, 3, 0, { virt_lines = { { { 'virt_below 4' } } } })
3571    screen:expect([[
3572      {2:  1 }for _,item in ipairs(items) do                |
3573      {2:  3 }    if hl_id_cell ~= nil then                 |
3574      {2:  4 }        hl_id = hl_id_cell                    |
3575      {2:    }virt_below 4                                  |
3576      {2:  6 }^    for _ = 1, (count or 1) do                |
3577      {2:  7 }        local cell = line[colpos]             |
3578      {2:  8 }        cell.text = text                      |
3579      {2:  9 }        cell.hl_id = hl_id                    |
3580      {2: 10 }        colpos = colpos+1                     |
3581      {2: 11 }    end                                       |
3582      {2: 12 }end                                           |
3583      {1:~                                                 }|*3
3584                                                        |
3585    ]])
3586    -- No scrolling for concealed topline #33033
3587    api.nvim_buf_clear_namespace(0, ns, 0, -1)
3588    api.nvim_buf_set_extmark(0, ns, 1, 0, { virt_lines_above = true, virt_lines = { { { 'virt_above 2' } } } })
3589    api.nvim_buf_set_extmark(0, ns, 0, 0, { conceal_lines = '' })
3590    feed('ggjj')
3591    screen:expect([[
3592      {2:    }virt_above 2                                  |
3593      {2:  2 }    local text, hl_id_cell, count = unpack(ite|
3594      {2:    }m)                                            |
3595      {2:  3 }^    if hl_id_cell ~= nil then                 |
3596      {2:  4 }        hl_id = hl_id_cell                    |
3597      {2:  5 }conceal text                                  |
3598      {2:  6 }    for _ = 1, (count or 1) do                |
3599      {2:  7 }        local cell = line[colpos]             |
3600      {2:  8 }        cell.text = text                      |
3601      {2:  9 }        cell.hl_id = hl_id                    |
3602      {2: 10 }        colpos = colpos+1                     |
3603      {2: 11 }    end                                       |
3604      {2: 12 }end                                           |
3605      {1:~                                                 }|
3606                                                        |
3607    ]])
3608    -- No asymmetric topline for <C-E><C-Y> #33182
3609    feed('4<C-E>')
3610    exec('set concealcursor=n')
3611    api.nvim_buf_set_extmark(0, ns, 4, 0, { conceal_lines = '' })
3612    eq(5, n.fn.line('w0'))
3613    feed('<C-E><C-Y>')
3614    eq(5, n.fn.line('w0'))
3615  end)
3616
3617  it('conceal_lines not checking on invalid row #36057', function()
3618    exec_lua(function()
3619      vim.fn.setline(1, { 'foo', 'bar', 'baz' })
3620      vim.api.nvim_command('set conceallevel=3 scrolloff=3')
3621      vim.api.nvim_open_win(0, true, { width = 1, height = 1, relative = 'editor', row = 0, col = 0 })
3622      vim.api.nvim_buf_set_extmark(0, ns, 1, 0, { conceal_lines = '' })
3623      vim.api.nvim__redraw({ flush = true })
3624    end)
3625    n.assert_alive()
3626  end)
3627
3628  it('redraws the line from which a left gravity mark has moved #27369', function()
3629    fn.setline(1, { 'aaa', 'bbb', 'ccc', 'ddd' })
3630    api.nvim_buf_set_extmark(0, ns, 1, 0, { virt_text = { { 'foo' } }, right_gravity = false })
3631    feed('yyp')
3632    screen:expect([[
3633      aaa                                               |
3634      ^aaa foo                                           |
3635      bbb                                               |
3636      ccc                                               |
3637      ddd                                               |
3638      {1:~                                                 }|*9
3639                                                        |
3640    ]])
3641  end)
3642
3643  it('redraws extmark that starts and ends outside the screen', function()
3644    local lines = vim.split(('1'):rep(20), '', { plain = true })
3645    api.nvim_buf_set_lines(0, 0, -1, true, lines)
3646    api.nvim_buf_set_extmark(0, ns, 0, 0, { hl_group = 'ErrorMsg', end_row = 19, end_col = 0 })
3647    screen:expect({
3648      grid = [[
3649        {4:^1}                                                 |
3650        {4:1}                                                 |*13
3651                                                          |
3652      ]],
3653    })
3654    feed('<C-e>')
3655    -- Newly visible line should also have the highlight.
3656    screen:expect({
3657      grid = [[
3658        {4:^1}                                                 |
3659        {4:1}                                                 |*13
3660                                                          |
3661      ]],
3662    })
3663  end)
3664
3665  it('line("w$", win) considers conceal_lines', function()
3666    api.nvim_buf_set_lines(0, 0, -1, true, { 'line 1', 'line 2', 'line 3' })
3667    api.nvim_buf_set_extmark(0, ns, 0, 0, { conceal_lines = '' }) -- conceal line 1
3668
3669    local win = exec_lua(function()
3670      local provider_ns = vim.api.nvim_create_namespace('test_f_line')
3671      _G.line_w_dollar = {}
3672      vim.api.nvim_set_decoration_provider(provider_ns, {
3673        on_start = function()
3674          for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
3675            table.insert(_G.line_w_dollar, { win, vim.fn.line('w$', win) })
3676          end
3677        end,
3678      })
3679
3680      local win = vim.api.nvim_open_win(0, false, {
3681        relative = 'editor',
3682        width = 20,
3683        height = 1,
3684        row = 0,
3685        col = 0,
3686        border = 'single',
3687      })
3688      vim.api.nvim_set_option_value('conceallevel', 2, { scope = 'local', win = win })
3689
3690      return win
3691    end)
3692
3693    local line_w_dollar = exec_lua('return _G.line_w_dollar')
3694    for _, win_line in ipairs(line_w_dollar) do
3695      if win_line[1] == win then
3696        eq(2, win_line[2])
3697      end
3698    end
3699  end)
3700end)
3701
3702describe('decorations: inline virtual text', function()
3703  local screen ---@type test.functional.ui.screen
3704  local ns ---@type integer
3705  before_each(function()
3706    clear()
3707    screen = Screen.new(50, 3)
3708    screen:set_default_attr_ids {
3709      [1] = { bold = true, foreground = Screen.colors.Blue },
3710      [2] = { foreground = Screen.colors.Brown },
3711      [3] = { bold = true, foreground = Screen.colors.SeaGreen },
3712      [4] = { background = Screen.colors.Red1, foreground = Screen.colors.Gray100 },
3713      [5] = { background = Screen.colors.Red1, bold = true },
3714      [6] = { foreground = Screen.colors.DarkCyan },
3715      [7] = { background = Screen.colors.LightGrey, foreground = Screen.colors.Black },
3716      [8] = { bold = true },
3717      [9] = { background = Screen.colors.Plum1 },
3718      [10] = { foreground = Screen.colors.SlateBlue },
3719      [11] = { blend = 30, background = Screen.colors.Red1 },
3720      [12] = { background = Screen.colors.Yellow },
3721      [13] = { reverse = true },
3722      [14] = { foreground = Screen.colors.SlateBlue, background = Screen.colors.LightMagenta },
3723      [15] = { bold = true, reverse = true },
3724      [16] = { foreground = Screen.colors.Red },
3725      [17] = { background = Screen.colors.LightGrey, foreground = Screen.colors.DarkBlue },
3726      [18] = { background = Screen.colors.LightGrey, foreground = Screen.colors.Red },
3727      [19] = { background = Screen.colors.Yellow, foreground = Screen.colors.SlateBlue },
3728      [20] = { background = Screen.colors.LightGrey, foreground = Screen.colors.SlateBlue },
3729      [21] = { reverse = true, foreground = Screen.colors.SlateBlue },
3730      [22] = { background = Screen.colors.Gray90 },
3731      [23] = { background = Screen.colors.Gray90, foreground = Screen.colors.Blue, bold = true },
3732      [24] = { foreground = Screen.colors.Blue },
3733    }
3734
3735    ns = api.nvim_create_namespace 'test'
3736  end)
3737
3738  it('works', function()
3739    screen:try_resize(50, 10)
3740    insert(example_text)
3741    feed 'gg'
3742    screen:expect {
3743      grid = [[
3744      ^for _,item in ipairs(items) do                    |
3745          local text, hl_id_cell, count = unpack(item)  |
3746          if hl_id_cell ~= nil then                     |
3747              hl_id = hl_id_cell                        |
3748          end                                           |
3749          for _ = 1, (count or 1) do                    |
3750              local cell = line[colpos]                 |
3751              cell.text = text                          |
3752              cell.hl_id = hl_id                        |
3753                                                        |
3754    ]],
3755    }
3756
3757    api.nvim_buf_set_extmark(0, ns, 1, 14, { virt_text = { { ': ', 'Special' }, { 'string', 'Type' } }, virt_text_pos = 'inline' })
3758    screen:expect {
3759      grid = [[
3760      ^for _,item in ipairs(items) do                    |
3761          local text{10:: }{3:string}, hl_id_cell, count = unpack|
3762      (item)                                            |
3763          if hl_id_cell ~= nil then                     |
3764              hl_id = hl_id_cell                        |
3765          end                                           |
3766          for _ = 1, (count or 1) do                    |
3767              local cell = line[colpos]                 |
3768              cell.text = text                          |
3769                                                        |
3770    ]],
3771    }
3772
3773    screen:try_resize(55, 10)
3774    screen:expect {
3775      grid = [[
3776      ^for _,item in ipairs(items) do                         |
3777          local text{10:: }{3:string}, hl_id_cell, count = unpack(item|
3778      )                                                      |
3779          if hl_id_cell ~= nil then                          |
3780              hl_id = hl_id_cell                             |
3781          end                                                |
3782          for _ = 1, (count or 1) do                         |
3783              local cell = line[colpos]                      |
3784              cell.text = text                               |
3785                                                             |
3786    ]],
3787    }
3788
3789    screen:try_resize(56, 10)
3790    screen:expect {
3791      grid = [[
3792      ^for _,item in ipairs(items) do                          |
3793          local text{10:: }{3:string}, hl_id_cell, count = unpack(item)|
3794          if hl_id_cell ~= nil then                           |
3795              hl_id = hl_id_cell                              |
3796          end                                                 |
3797          for _ = 1, (count or 1) do                          |
3798              local cell = line[colpos]                       |
3799              cell.text = text                                |
3800              cell.hl_id = hl_id                              |
3801                                                              |
3802    ]],
3803    }
3804  end)
3805
3806  it('works with 0-width chunk', function()
3807    screen:try_resize(50, 10)
3808    insert(example_text)
3809    feed 'gg'
3810    screen:expect {
3811      grid = [[
3812      ^for _,item in ipairs(items) do                    |
3813          local text, hl_id_cell, count = unpack(item)  |
3814          if hl_id_cell ~= nil then                     |
3815              hl_id = hl_id_cell                        |
3816          end                                           |
3817          for _ = 1, (count or 1) do                    |
3818              local cell = line[colpos]                 |
3819              cell.text = text                          |
3820              cell.hl_id = hl_id                        |
3821                                                        |
3822    ]],
3823    }
3824
3825    api.nvim_buf_set_extmark(0, ns, 0, 5, { virt_text = { { '' }, { '' } }, virt_text_pos = 'inline' })
3826    api.nvim_buf_set_extmark(0, ns, 1, 14, { virt_text = { { '' }, { ': ', 'Special' } }, virt_text_pos = 'inline' })
3827    api.nvim_buf_set_extmark(0, ns, 1, 48, { virt_text = { { '' }, { '' } }, virt_text_pos = 'inline' })
3828    screen:expect {
3829      grid = [[
3830      ^for _,item in ipairs(items) do                    |
3831          local text{10:: }, hl_id_cell, count = unpack(item)|
3832          if hl_id_cell ~= nil then                     |
3833              hl_id = hl_id_cell                        |
3834          end                                           |
3835          for _ = 1, (count or 1) do                    |
3836              local cell = line[colpos]                 |
3837              cell.text = text                          |
3838              cell.hl_id = hl_id                        |
3839                                                        |
3840    ]],
3841    }
3842
3843    api.nvim_buf_set_extmark(0, ns, 1, 14, { virt_text = { { '' }, { 'string', 'Type' } }, virt_text_pos = 'inline' })
3844    feed('V')
3845    screen:expect {
3846      grid = [[
3847      {7:^for _,item in ipairs(items) do}                    |
3848          local text{10:: }{3:string}, hl_id_cell, count = unpack|
3849      (item)                                            |
3850          if hl_id_cell ~= nil then                     |
3851              hl_id = hl_id_cell                        |
3852          end                                           |
3853          for _ = 1, (count or 1) do                    |
3854              local cell = line[colpos]                 |
3855              cell.text = text                          |
3856      {8:-- VISUAL LINE --}                                 |
3857    ]],
3858    }
3859
3860    feed('<Esc>jf,')
3861    screen:expect {
3862      grid = [[
3863      for _,item in ipairs(items) do                    |
3864          local text{10:: }{3:string}^, hl_id_cell, count = unpack|
3865      (item)                                            |
3866          if hl_id_cell ~= nil then                     |
3867              hl_id = hl_id_cell                        |
3868          end                                           |
3869          for _ = 1, (count or 1) do                    |
3870              local cell = line[colpos]                 |
3871              cell.text = text                          |
3872                                                        |
3873    ]],
3874    }
3875  end)
3876
3877  it('Normal mode "gM" command works properly', function()
3878    command([[call setline(1, '123456789')]])
3879    api.nvim_buf_set_extmark(0, ns, 0, 2, { virt_text = { { 'bbb', 'Special' } }, virt_text_pos = 'inline' })
3880    api.nvim_buf_set_extmark(0, ns, 0, 7, { virt_text = { { 'bbb', 'Special' } }, virt_text_pos = 'inline' })
3881    feed('gM')
3882    screen:expect {
3883      grid = [[
3884      12{10:bbb}34^567{10:bbb}89                                   |
3885      {1:~                                                 }|
3886                                                        |
3887    ]],
3888    }
3889  end)
3890
3891  local function test_normal_gj_gk()
3892    screen:try_resize(60, 6)
3893    command([[call setline(1, repeat([repeat('a', 55)], 2))]])
3894    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { ('b'):rep(10), 'Special' } }, virt_text_pos = 'inline' })
3895    api.nvim_buf_set_extmark(0, ns, 1, 40, { virt_text = { { ('b'):rep(10), 'Special' } }, virt_text_pos = 'inline' })
3896    screen:expect {
3897      grid = [[
3898      ^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3899      aaaaa                                                       |
3900      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3901      aaaaa                                                       |
3902      {1:~                                                           }|
3903                                                                  |
3904    ]],
3905    }
3906    feed('gj')
3907    screen:expect {
3908      grid = [[
3909      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3910      ^aaaaa                                                       |
3911      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3912      aaaaa                                                       |
3913      {1:~                                                           }|
3914                                                                  |
3915    ]],
3916    }
3917    feed('gj')
3918    screen:expect {
3919      grid = [[
3920      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3921      aaaaa                                                       |
3922      ^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3923      aaaaa                                                       |
3924      {1:~                                                           }|
3925                                                                  |
3926    ]],
3927    }
3928    feed('gj')
3929    screen:expect {
3930      grid = [[
3931      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3932      aaaaa                                                       |
3933      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3934      ^aaaaa                                                       |
3935      {1:~                                                           }|
3936                                                                  |
3937    ]],
3938    }
3939    feed('gk')
3940    screen:expect {
3941      grid = [[
3942      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3943      aaaaa                                                       |
3944      ^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3945      aaaaa                                                       |
3946      {1:~                                                           }|
3947                                                                  |
3948    ]],
3949    }
3950    feed('gk')
3951    screen:expect {
3952      grid = [[
3953      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3954      ^aaaaa                                                       |
3955      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3956      aaaaa                                                       |
3957      {1:~                                                           }|
3958                                                                  |
3959    ]],
3960    }
3961    feed('gk')
3962    screen:expect {
3963      grid = [[
3964      ^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3965      aaaaa                                                       |
3966      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbbbbbbbbb}aaaaaaaaaa|
3967      aaaaa                                                       |
3968      {1:~                                                           }|
3969                                                                  |
3970    ]],
3971    }
3972  end
3973
3974  describe('Normal mode "gj" "gk" commands work properly', function()
3975    it('with virtualedit=', function()
3976      test_normal_gj_gk()
3977    end)
3978
3979    it('with virtualedit=all', function()
3980      command('set virtualedit=all')
3981      test_normal_gj_gk()
3982    end)
3983  end)
3984
3985  it('cursor positions are correct with multiple inline virtual text', function()
3986    insert('12345678')
3987    api.nvim_buf_set_extmark(0, ns, 0, 4, { virt_text = { { ' virtual text ', 'Special' } }, virt_text_pos = 'inline' })
3988    api.nvim_buf_set_extmark(0, ns, 0, 4, { virt_text = { { ' virtual text ', 'Special' } }, virt_text_pos = 'inline' })
3989    feed '^'
3990    feed '4l'
3991    screen:expect {
3992      grid = [[
3993      1234{10: virtual text  virtual text }^5678              |
3994      {1:~                                                 }|
3995                                                        |
3996    ]],
3997    }
3998  end)
3999
4000  it('adjusts cursor location correctly when inserting around inline virtual text', function()
4001    insert('12345678')
4002    feed '$'
4003    api.nvim_buf_set_extmark(0, ns, 0, 4, { virt_text = { { ' virtual text ', 'Special' } }, virt_text_pos = 'inline' })
4004
4005    screen:expect {
4006      grid = [[
4007      1234{10: virtual text }567^8                            |
4008      {1:~                                                 }|
4009                                                        |
4010    ]],
4011    }
4012  end)
4013
4014  it('has correct highlighting with multi-byte characters', function()
4015    insert('12345678')
4016    api.nvim_buf_set_extmark(0, ns, 0, 4, { virt_text = { { 'múlti-byté chñröcters 修补', 'Special' } }, virt_text_pos = 'inline' })
4017
4018    screen:expect {
4019      grid = [[
4020      1234{10:múlti-byté chñröcters 修补}567^8                |
4021      {1:~                                                 }|
4022                                                        |
4023    ]],
4024    }
4025  end)
4026
4027  it('has correct cursor position when inserting around virtual text', function()
4028    insert('12345678')
4029    api.nvim_buf_set_extmark(0, ns, 0, 4, { virt_text = { { 'virtual text', 'Special' } }, virt_text_pos = 'inline' })
4030    feed '^'
4031    feed '3l'
4032    feed 'a'
4033    screen:expect {
4034      grid = [[
4035      1234{10:^virtual text}5678                              |
4036      {1:~                                                 }|
4037      {8:-- INSERT --}                                      |
4038    ]],
4039    }
4040    feed '<ESC>'
4041    screen:expect {
4042      grid = [[
4043      123^4{10:virtual text}5678                              |
4044      {1:~                                                 }|
4045                                                        |
4046    ]],
4047    }
4048    feed '^'
4049    feed '4l'
4050    feed 'i'
4051    screen:expect {
4052      grid = [[
4053      1234{10:^virtual text}5678                              |
4054      {1:~                                                 }|
4055      {8:-- INSERT --}                                      |
4056    ]],
4057    }
4058  end)
4059
4060  it('has correct cursor position with virtual text on an empty line', function()
4061    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'virtual text', 'Special' } }, virt_text_pos = 'inline' })
4062    screen:expect {
4063      grid = [[
4064      {10:^virtual text}                                      |
4065      {1:~                                                 }|
4066                                                        |
4067    ]],
4068    }
4069  end)
4070
4071  it('text is drawn correctly with a wrapping virtual text', function()
4072    screen:try_resize(60, 8)
4073    exec([[
4074      call setline(1, ['', 'aaa', '', 'bbbbbb'])
4075      normal gg0
4076    ]])
4077    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { string.rep('X', 60), 'Special' } }, virt_text_pos = 'inline' })
4078    api.nvim_buf_set_extmark(0, ns, 2, 0, { virt_text = { { string.rep('X', 61), 'Special' } }, virt_text_pos = 'inline' })
4079    feed('$')
4080    screen:expect {
4081      grid = [[
4082      {10:^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4083      aaa                                                         |
4084      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4085      {10:X}                                                           |
4086      bbbbbb                                                      |
4087      {1:~                                                           }|*2
4088                                                                  |
4089    ]],
4090    }
4091    feed('j')
4092    screen:expect {
4093      grid = [[
4094      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4095      aa^a                                                         |
4096      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4097      {10:X}                                                           |
4098      bbbbbb                                                      |
4099      {1:~                                                           }|*2
4100                                                                  |
4101    ]],
4102    }
4103    feed('j')
4104    screen:expect {
4105      grid = [[
4106      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4107      aaa                                                         |
4108      {10:^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4109      {10:X}                                                           |
4110      bbbbbb                                                      |
4111      {1:~                                                           }|*2
4112                                                                  |
4113    ]],
4114    }
4115    feed('j')
4116    screen:expect {
4117      grid = [[
4118      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4119      aaa                                                         |
4120      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4121      {10:X}                                                           |
4122      bbbbb^b                                                      |
4123      {1:~                                                           }|*2
4124                                                                  |
4125    ]],
4126    }
4127    feed('0<C-V>2l2k')
4128    screen:expect {
4129      grid = [[
4130      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4131      {7:aa^a}                                                         |
4132      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4133      {10:X}                                                           |
4134      {7:bbb}bbb                                                      |
4135      {1:~                                                           }|*2
4136      {8:-- VISUAL BLOCK --}                                          |
4137    ]],
4138    }
4139    feed([[<Esc>/aaa\n\%V<CR>]])
4140    screen:expect {
4141      grid = [[
4142      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4143      {12:^aaa }                                                        |
4144      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4145      {10:X}                                                           |
4146      bbbbbb                                                      |
4147      {1:~                                                           }|*2
4148      {16:search hit BOTTOM, continuing at TOP}                        |
4149    ]],
4150    }
4151    feed('3ggic')
4152    screen:expect {
4153      grid = [[
4154      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4155      {12:aaa }                                                        |
4156      c{10:^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4157      {10:XX}                                                          |
4158      bbbbbb                                                      |
4159      {1:~                                                           }|*2
4160      {8:-- INSERT --}                                                |
4161    ]],
4162    }
4163    feed([[<Esc>/aaa\nc\%V<CR>]])
4164    screen:expect {
4165      grid = [[
4166      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4167      {12:^aaa }                                                        |
4168      {12:c}{10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4169      {10:XX}                                                          |
4170      bbbbbb                                                      |
4171      {1:~                                                           }|*2
4172      {16:search hit BOTTOM, continuing at TOP}                        |
4173    ]],
4174    }
4175  end)
4176
4177  it('cursor position is correct with virtual text attached to hard TABs', function()
4178    command('set noexpandtab')
4179    feed('i')
4180    feed('<TAB>')
4181    feed('<TAB>')
4182    feed('test')
4183    feed('<ESC>')
4184    api.nvim_buf_set_extmark(0, ns, 0, 1, { virt_text = { { 'virtual text', 'Special' } }, virt_text_pos = 'inline' })
4185    feed('0')
4186    screen:expect {
4187      grid = [[
4188             ^ {10:virtual text}    test                      |
4189      {1:~                                                 }|
4190                                                        |
4191    ]],
4192    }
4193
4194    feed('l')
4195    screen:expect {
4196      grid = [[
4197              {10:virtual text}   ^ test                      |
4198      {1:~                                                 }|
4199                                                        |
4200    ]],
4201    }
4202
4203    feed('l')
4204    screen:expect {
4205      grid = [[
4206              {10:virtual text}    ^test                      |
4207      {1:~                                                 }|
4208                                                        |
4209    ]],
4210    }
4211
4212    feed('l')
4213    screen:expect {
4214      grid = [[
4215              {10:virtual text}    t^est                      |
4216      {1:~                                                 }|
4217                                                        |
4218    ]],
4219    }
4220
4221    feed('l')
4222    screen:expect {
4223      grid = [[
4224              {10:virtual text}    te^st                      |
4225      {1:~                                                 }|
4226                                                        |
4227    ]],
4228    }
4229  end)
4230
4231  it('cursor position is correct with virtual text on an empty line', function()
4232    command('set linebreak')
4233    insert('one twoword')
4234    feed('0')
4235    api.nvim_buf_set_extmark(0, ns, 0, 3, { virt_text = { { ': virtual text', 'Special' } }, virt_text_pos = 'inline' })
4236    screen:expect {
4237      grid = [[
4238      ^one{10:: virtual text} twoword                         |
4239      {1:~                                                 }|
4240                                                        |
4241    ]],
4242    }
4243  end)
4244
4245  it('search highlight is correct', function()
4246    insert('foo foo foo bar\nfoo foo foo bar')
4247    feed('gg0')
4248    api.nvim_buf_set_extmark(0, ns, 0, 9, { virt_text = { { 'AAA', 'Special' } }, virt_text_pos = 'inline' })
4249    api.nvim_buf_set_extmark(0, ns, 0, 9, { virt_text = { { 'BBB', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
4250    api.nvim_buf_set_extmark(0, ns, 1, 9, { virt_text = { { 'CCC', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
4251    api.nvim_buf_set_extmark(0, ns, 1, 9, { virt_text = { { 'DDD', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'replace' })
4252    screen:expect {
4253      grid = [[
4254      ^foo foo f{10:AAABBB}oo bar                             |
4255      foo foo f{10:CCCDDD}oo bar                             |
4256                                                        |
4257    ]],
4258    }
4259
4260    feed('/foo')
4261    screen:expect {
4262      grid = [[
4263      {12:foo} {13:foo} {12:f}{10:AAA}{19:BBB}{12:oo} bar                             |
4264      {12:foo} {12:foo} {12:f}{19:CCC}{10:DDD}{12:oo} bar                             |
4265      /foo^                                              |
4266    ]],
4267    }
4268
4269    api.nvim_buf_set_extmark(0, ns, 0, 13, { virt_text = { { 'EEE', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
4270    feed('<C-G>')
4271    screen:expect {
4272      grid = [[
4273      {12:foo} {12:foo} {13:f}{10:AAA}{21:BBB}{13:oo} b{10:EEE}ar                          |
4274      {12:foo} {12:foo} {12:f}{19:CCC}{10:DDD}{12:oo} bar                             |
4275      /foo^                                              |
4276    ]],
4277    }
4278  end)
4279
4280  it('Visual select highlight is correct', function()
4281    insert('foo foo foo bar\nfoo foo foo bar')
4282    feed('gg0')
4283    api.nvim_buf_set_extmark(0, ns, 0, 8, { virt_text = { { 'AAA', 'Special' } }, virt_text_pos = 'inline' })
4284    api.nvim_buf_set_extmark(0, ns, 0, 8, { virt_text = { { 'BBB', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
4285    api.nvim_buf_set_extmark(0, ns, 1, 8, { virt_text = { { 'CCC', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
4286    api.nvim_buf_set_extmark(0, ns, 1, 8, { virt_text = { { 'DDD', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'replace' })
4287    feed('8l')
4288    screen:expect {
4289      grid = [[
4290      foo foo {10:AAABBB}^foo bar                             |
4291      foo foo {10:CCCDDD}foo bar                             |
4292                                                        |
4293    ]],
4294    }
4295
4296    feed('<C-V>')
4297    feed('2hj')
4298    screen:expect {
4299      grid = [[
4300      foo fo{7:o }{10:AAA}{20:BBB}{7:f}oo bar                             |
4301      foo fo{7:^o }{20:CCC}{10:DDD}{7:f}oo bar                             |
4302      {8:-- VISUAL BLOCK --}                                |
4303    ]],
4304    }
4305
4306    api.nvim_buf_set_extmark(0, ns, 0, 10, { virt_text = { { 'EEE', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
4307    screen:expect {
4308      grid = [[
4309      foo fo{7:o }{10:AAA}{20:BBB}{7:f}o{10:EEE}o bar                          |
4310      foo fo{7:^o }{20:CCC}{10:DDD}{7:f}oo bar                             |
4311      {8:-- VISUAL BLOCK --}                                |
4312    ]],
4313    }
4314  end)
4315
4316  it('inside highlight range of another extmark', function()
4317    insert('foo foo foo bar\nfoo foo foo bar')
4318    api.nvim_buf_set_extmark(0, ns, 0, 8, { virt_text = { { 'AAA', 'Special' } }, virt_text_pos = 'inline' })
4319    api.nvim_buf_set_extmark(0, ns, 0, 8, { virt_text = { { 'BBB', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
4320    api.nvim_buf_set_extmark(0, ns, 1, 8, { virt_text = { { 'CCC', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
4321    api.nvim_buf_set_extmark(0, ns, 1, 8, { virt_text = { { 'DDD', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'replace' })
4322    api.nvim_buf_set_extmark(0, ns, 0, 4, { end_col = 11, hl_group = 'Search' })
4323    api.nvim_buf_set_extmark(0, ns, 1, 4, { end_col = 11, hl_group = 'Search' })
4324    screen:expect {
4325      grid = [[
4326      foo {12:foo }{10:AAA}{19:BBB}{12:foo} bar                             |
4327      foo {12:foo }{19:CCC}{10:DDD}{12:foo} ba^r                             |
4328                                                        |
4329    ]],
4330    }
4331  end)
4332
4333  it('inside highlight range of syntax', function()
4334    insert('foo foo foo bar\nfoo foo foo bar')
4335    api.nvim_buf_set_extmark(0, ns, 0, 8, { virt_text = { { 'AAA', 'Special' } }, virt_text_pos = 'inline' })
4336    api.nvim_buf_set_extmark(0, ns, 0, 8, { virt_text = { { 'BBB', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
4337    api.nvim_buf_set_extmark(0, ns, 1, 8, { virt_text = { { 'CCC', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
4338    api.nvim_buf_set_extmark(0, ns, 1, 8, { virt_text = { { 'DDD', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'replace' })
4339    command([[syntax match Search 'foo \zsfoo foo\ze bar']])
4340    screen:expect {
4341      grid = [[
4342      foo {12:foo }{10:AAA}{19:BBB}{12:foo} bar                             |
4343      foo {12:foo }{19:CCC}{10:DDD}{12:foo} ba^r                             |
4344                                                        |
4345    ]],
4346    }
4347  end)
4348
4349  it('cursor position is correct when inserting around a virtual text with left gravity', function()
4350    screen:try_resize(27, 4)
4351    insert(('a'):rep(15))
4352    api.nvim_buf_set_extmark(0, ns, 0, 8, { virt_text = { { ('>'):rep(43), 'Special' } }, virt_text_pos = 'inline', right_gravity = false })
4353    command('setlocal showbreak=+ breakindent breakindentopt=shift:2')
4354    feed('08l')
4355    screen:expect {
4356      grid = [[
4357      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>}|
4358        {1:+}{10:>>>>>>>>>>>>>>>>>>>>>>>>}|
4359        {1:+}^aaaaaaa                 |
4360                                 |
4361    ]],
4362    }
4363    feed('i')
4364    screen:expect {
4365      grid = [[
4366      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>}|
4367        {1:+}{10:>>>>>>>>>>>>>>>>>>>>>>>>}|
4368        {1:+}^aaaaaaa                 |
4369      {8:-- INSERT --}               |
4370    ]],
4371    }
4372    feed([[<C-\><C-O>]])
4373    screen:expect {
4374      grid = [[
4375      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>}|
4376        {1:+}{10:>>>>>>>>>>>>>>>>>>>>>>>>}|
4377        {1:+}^aaaaaaa                 |
4378      {8:-- (insert) --}             |
4379    ]],
4380    }
4381    feed('D')
4382    screen:expect {
4383      grid = [[
4384      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>}|
4385        {1:+}{10:>>>>>>>>>>>>>>>>>>>>>>>>}|
4386      {1:^~                          }|
4387      {8:-- INSERT --}               |
4388    ]],
4389    }
4390    command('setlocal list listchars=eol:$')
4391    screen:expect {
4392      grid = [[
4393      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>}|
4394        {1:+}{10:>>>>>>>>>>>>>>>>>>>>>>>>}|
4395        {1:+^$}                       |
4396      {8:-- INSERT --}               |
4397    ]],
4398    }
4399    feed('<C-U>')
4400    screen:expect {
4401      grid = [[
4402      {10:>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4403        {1:+}{10:>>>>>>>>>>>>>>>>}{1:^$}       |
4404      {1:~                          }|
4405      {8:-- INSERT --}               |
4406    ]],
4407    }
4408    feed('a')
4409    screen:expect {
4410      grid = [[
4411      {10:>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4412        {1:+}{10:>>>>>>>>>>>>>>>>}a{1:^$}      |
4413      {1:~                          }|
4414      {8:-- INSERT --}               |
4415    ]],
4416    }
4417    feed('<Esc>')
4418    screen:expect {
4419      grid = [[
4420      {10:>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4421        {1:+}{10:>>>>>>>>>>>>>>>>}^a{1:$}      |
4422      {1:~                          }|
4423                                 |
4424    ]],
4425    }
4426    feed('x')
4427    screen:expect {
4428      grid = [[
4429      {10:^>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4430        {1:+}{10:>>>>>>>>>>>>>>>>}{1:$}       |
4431      {1:~                          }|
4432                                 |
4433    ]],
4434    }
4435  end)
4436
4437  it('cursor position is correct when inserting around virtual texts with both left and right gravity', function()
4438    screen:try_resize(30, 4)
4439    command('setlocal showbreak=+ breakindent breakindentopt=shift:2')
4440    insert(('a'):rep(15))
4441    api.nvim_buf_set_extmark(0, ns, 0, 8, { virt_text = { { ('>'):rep(32), 'Special' } }, virt_text_pos = 'inline', right_gravity = false })
4442    api.nvim_buf_set_extmark(0, ns, 0, 8, { virt_text = { { ('<'):rep(32), 'Special' } }, virt_text_pos = 'inline', right_gravity = true })
4443    feed('08l')
4444    screen:expect {
4445      grid = [[
4446      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>>>>}|
4447        {1:+}{10:>>>>>>>>>><<<<<<<<<<<<<<<<<}|
4448        {1:+}{10:<<<<<<<<<<<<<<<}^aaaaaaa     |
4449                                    |
4450    ]],
4451    }
4452    feed('i')
4453    screen:expect {
4454      grid = [[
4455      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>>>>}|
4456        {1:+}{10:>>>>>>>>>>^<<<<<<<<<<<<<<<<<}|
4457        {1:+}{10:<<<<<<<<<<<<<<<}aaaaaaa     |
4458      {8:-- INSERT --}                  |
4459    ]],
4460    }
4461    feed('a')
4462    screen:expect {
4463      grid = [[
4464      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>>>>}|
4465        {1:+}{10:>>>>>>>>>>}a{10:^<<<<<<<<<<<<<<<<}|
4466        {1:+}{10:<<<<<<<<<<<<<<<<}aaaaaaa    |
4467      {8:-- INSERT --}                  |
4468    ]],
4469    }
4470    feed([[<C-\><C-O>]])
4471    screen:expect {
4472      grid = [[
4473      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>>>>}|
4474        {1:+}{10:>>>>>>>>>>}a{10:<<<<<<<<<<<<<<<<}|
4475        {1:+}{10:<<<<<<<<<<<<<<<<}^aaaaaaa    |
4476      {8:-- (insert) --}                |
4477    ]],
4478    }
4479    feed('D')
4480    screen:expect {
4481      grid = [[
4482      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>>>>}|
4483        {1:+}{10:>>>>>>>>>>}a{10:^<<<<<<<<<<<<<<<<}|
4484        {1:+}{10:<<<<<<<<<<<<<<<<}           |
4485      {8:-- INSERT --}                  |
4486    ]],
4487    }
4488    feed('<BS>')
4489    screen:expect {
4490      grid = [[
4491      aaaaaaaa{10:>>>>>>>>>>>>>>>>>>>>>>}|
4492        {1:+}{10:>>>>>>>>>>^<<<<<<<<<<<<<<<<<}|
4493        {1:+}{10:<<<<<<<<<<<<<<<}            |
4494      {8:-- INSERT --}                  |
4495    ]],
4496    }
4497    feed('<C-U>')
4498    screen:expect {
4499      grid = [[
4500      {10:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4501        {1:+}{10:>>^<<<<<<<<<<<<<<<<<<<<<<<<<}|
4502        {1:+}{10:<<<<<<<}                    |
4503      {8:-- INSERT --}                  |
4504    ]],
4505    }
4506    feed('a')
4507    screen:expect {
4508      grid = [[
4509      {10:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4510        {1:+}{10:>>}a{10:^<<<<<<<<<<<<<<<<<<<<<<<<}|
4511        {1:+}{10:<<<<<<<<}                   |
4512      {8:-- INSERT --}                  |
4513    ]],
4514    }
4515    feed('<Esc>')
4516    screen:expect {
4517      grid = [[
4518      {10:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4519        {1:+}{10:>>}^a{10:<<<<<<<<<<<<<<<<<<<<<<<<}|
4520        {1:+}{10:<<<<<<<<}                   |
4521                                    |
4522    ]],
4523    }
4524    feed('x')
4525    screen:expect {
4526      grid = [[
4527      {10:^>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4528        {1:+}{10:>><<<<<<<<<<<<<<<<<<<<<<<<<}|
4529        {1:+}{10:<<<<<<<}                    |
4530                                    |
4531    ]],
4532    }
4533    feed('i')
4534    screen:expect {
4535      grid = [[
4536      {10:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4537        {1:+}{10:>>^<<<<<<<<<<<<<<<<<<<<<<<<<}|
4538        {1:+}{10:<<<<<<<}                    |
4539      {8:-- INSERT --}                  |
4540    ]],
4541    }
4542    screen:try_resize(32, 4)
4543    screen:expect {
4544      grid = [[
4545      {10:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4546        {1:+}{10:^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<}|
4547        {1:+}{10:<<<}                          |
4548      {8:-- INSERT --}                    |
4549    ]],
4550    }
4551    command('setlocal nobreakindent')
4552    screen:expect {
4553      grid = [[
4554      {10:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}|
4555      {1:+}{10:^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<}|
4556      {1:+}{10:<}                              |
4557      {8:-- INSERT --}                    |
4558    ]],
4559    }
4560  end)
4561
4562  it('draws correctly with no wrap multiple virtual text, where one is hidden', function()
4563    insert('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz')
4564    command('set nowrap')
4565    api.nvim_buf_set_extmark(0, ns, 0, 50, { virt_text = { { 'virtual text', 'Special' } }, virt_text_pos = 'inline' })
4566    api.nvim_buf_set_extmark(0, ns, 0, 2, { virt_text = { { 'virtual text', 'Special' } }, virt_text_pos = 'inline' })
4567    feed('$')
4568    screen:expect {
4569      grid = [[
4570      opqrstuvwxyzabcdefghijklmnopqrstuvwx{10:virtual text}y^z|
4571      {1:~                                                 }|
4572                                                        |
4573    ]],
4574    }
4575  end)
4576
4577  it('draws correctly with no wrap and a long virtual text', function()
4578    insert('abcdefghi')
4579    command('set nowrap')
4580    api.nvim_buf_set_extmark(0, ns, 0, 2, { virt_text = { { string.rep('X', 55), 'Special' } }, virt_text_pos = 'inline' })
4581    feed('$')
4582    screen:expect([[
4583      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}cdefgh^i|
4584      {1:~                                                 }|
4585                                                        |
4586    ]])
4587    command('set list listchars+=precedes:!')
4588    screen:expect([[
4589      {1:!}{10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}cdefgh^i|
4590      {1:~                                                 }|
4591                                                        |
4592    ]])
4593  end)
4594
4595  it('draws correctly with no wrap and multibyte virtual text', function()
4596    insert('12345678')
4597    command('set nowrap')
4598    api.nvim_buf_set_extmark(0, ns, 0, 2, {
4599      hl_mode = 'replace',
4600      virt_text = { { 'α口β̳γ̲=', 'Special' }, { '❤️', 'Special' } },
4601      virt_text_pos = 'inline',
4602    })
4603    screen:expect([[
4604      12{10:α口β̳γ̲=❤️}34567^8                                  |
4605      {1:~                                                 }|
4606                                                        |
4607    ]])
4608    feed('zl')
4609    screen:expect([[
4610      2{10:α口β̳γ̲=❤️}34567^8                                   |
4611      {1:~                                                 }|
4612                                                        |
4613    ]])
4614    feed('zl')
4615    screen:expect([[
4616      {10:α口β̳γ̲=❤️}34567^8                                    |
4617      {1:~                                                 }|
4618                                                        |
4619    ]])
4620    feed('zl')
4621    screen:expect([[
4622      {10:口β̳γ̲=❤️}34567^8                                     |
4623      {1:~                                                 }|
4624                                                        |
4625    ]])
4626    feed('V')
4627    screen:expect([[
4628      {10:口β̳γ̲=❤️}{7:34567^8}                                     |
4629      {1:~                                                 }|
4630      {8:-- VISUAL LINE --}                                 |
4631    ]])
4632    command('set list listchars+=precedes:!')
4633    screen:expect([[
4634      {1:!<}{10:β̳γ̲=❤️}{7:34567^8}                                     |
4635      {1:~                                                 }|
4636      {8:-- VISUAL LINE --}                                 |
4637    ]])
4638    feed('zl')
4639    screen:expect([[
4640      {1:!}{10:β̳γ̲=❤️}{7:34567^8}                                      |
4641      {1:~                                                 }|
4642      {8:-- VISUAL LINE --}                                 |
4643    ]])
4644    command('set nolist')
4645    screen:expect([[
4646      {1:<}{10:β̳γ̲=❤️}{7:34567^8}                                      |
4647      {1:~                                                 }|
4648      {8:-- VISUAL LINE --}                                 |
4649    ]])
4650    feed('<Esc>')
4651    screen:expect([[
4652      {1:<}{10:β̳γ̲=❤️}34567^8                                      |
4653      {1:~                                                 }|
4654                                                        |
4655    ]])
4656    feed('zl')
4657    screen:expect([[
4658      {10:β̳γ̲=❤️}34567^8                                       |
4659      {1:~                                                 }|
4660                                                        |
4661    ]])
4662    feed('zl')
4663    screen:expect([[
4664      {10:γ̲=❤️}34567^8                                        |
4665      {1:~                                                 }|
4666                                                        |
4667    ]])
4668    feed('zl')
4669    screen:expect([[
4670      {10:=❤️}34567^8                                         |
4671      {1:~                                                 }|
4672                                                        |
4673    ]])
4674    feed('zl')
4675    screen:expect([[
4676      {10:❤️}34567^8                                          |
4677      {1:~                                                 }|
4678                                                        |
4679    ]])
4680    command('set list')
4681    screen:expect([[
4682      {1:!<}34567^8                                          |
4683      {1:~                                                 }|
4684                                                        |
4685    ]])
4686    feed('zl')
4687    screen:expect([[
4688      {1:!}34567^8                                           |
4689      {1:~                                                 }|
4690                                                        |
4691    ]])
4692    command('set nolist')
4693    screen:expect([[
4694      {1:<}34567^8                                           |
4695      {1:~                                                 }|
4696                                                        |
4697    ]])
4698    feed('zl')
4699    screen:expect([[
4700      34567^8                                            |
4701      {1:~                                                 }|
4702                                                        |
4703    ]])
4704    feed('zl')
4705    screen:expect([[
4706      4567^8                                             |
4707      {1:~                                                 }|
4708                                                        |
4709    ]])
4710  end)
4711
4712  it('tabs are the correct length with no wrap following virtual text', function()
4713    command('set nowrap')
4714    feed('itest<TAB>a<ESC>')
4715    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { string.rep('a', 55), 'Special' } }, virt_text_pos = 'inline' })
4716    feed('gg$')
4717    screen:expect {
4718      grid = [[
4719      {10:aaaaaaaaaaaaaaaaaaaaaaaaa}test     ^a               |
4720      {1:~                                                 }|
4721                                                        |
4722    ]],
4723    }
4724  end)
4725
4726  it('highlighting does not extend with no wrap and a long virtual text', function()
4727    insert('abcdef')
4728    command('set nowrap')
4729    api.nvim_buf_set_extmark(0, ns, 0, 3, { virt_text = { { string.rep('X', 50), 'Special' } }, virt_text_pos = 'inline' })
4730    feed('$')
4731    screen:expect {
4732      grid = [[
4733      {10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}de^f|
4734      {1:~                                                 }|
4735                                                        |
4736    ]],
4737    }
4738  end)
4739
4740  it('hidden virtual text does not interfere with Visual highlight', function()
4741    insert('abcdef')
4742    command('set nowrap')
4743    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'XXX', 'Special' } }, virt_text_pos = 'inline' })
4744    feed('V2zl')
4745    screen:expect {
4746      grid = [[
4747      {10:X}{7:abcde^f}                                           |
4748      {1:~                                                 }|
4749      {8:-- VISUAL LINE --}                                 |
4750    ]],
4751    }
4752    feed('zl')
4753    screen:expect {
4754      grid = [[
4755      {7:abcde^f}                                            |
4756      {1:~                                                 }|
4757      {8:-- VISUAL LINE --}                                 |
4758    ]],
4759    }
4760    feed('zl')
4761    screen:expect {
4762      grid = [[
4763      {7:bcde^f}                                             |
4764      {1:~                                                 }|
4765      {8:-- VISUAL LINE --}                                 |
4766    ]],
4767    }
4768  end)
4769
4770  it('highlighting is correct when virtual text wraps with number', function()
4771    screen:try_resize(50, 5)
4772    insert([[
4773    test
4774    test]])
4775    command('set number')
4776    api.nvim_buf_set_extmark(0, ns, 0, 1, { virt_text = { { string.rep('X', 55), 'Special' } }, virt_text_pos = 'inline' })
4777    feed('gg0')
4778    screen:expect {
4779      grid = [[
4780      {2:  1 }^t{10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4781      {2:    }{10:XXXXXXXXXX}est                                 |
4782      {2:  2 }test                                          |
4783      {1:~                                                 }|
4784                                                        |
4785    ]],
4786    }
4787  end)
4788
4789  it('highlighting is correct when virtual text is proceeded with a match', function()
4790    insert([[test]])
4791    api.nvim_buf_set_extmark(0, ns, 0, 2, { virt_text = { { 'virtual text', 'Special' } }, virt_text_pos = 'inline' })
4792    feed('gg0')
4793    command('match ErrorMsg /e/')
4794    screen:expect {
4795      grid = [[
4796      ^t{4:e}{10:virtual text}st                                  |
4797      {1:~                                                 }|
4798                                                        |
4799    ]],
4800    }
4801    command('match ErrorMsg /s/')
4802    screen:expect {
4803      grid = [[
4804      ^te{10:virtual text}{4:s}t                                  |
4805      {1:~                                                 }|
4806                                                        |
4807    ]],
4808    }
4809  end)
4810
4811  it('smoothscroll works correctly when virtual text wraps', function()
4812    insert('foobar')
4813    api.nvim_buf_set_extmark(0, ns, 0, 3, { virt_text = { { string.rep('X', 55), 'Special' } }, virt_text_pos = 'inline' })
4814    command('setlocal smoothscroll')
4815    screen:expect {
4816      grid = [[
4817      foo{10:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}|
4818      {10:XXXXXXXX}ba^r                                       |
4819                                                        |
4820    ]],
4821    }
4822    feed('<C-E>')
4823    screen:expect {
4824      grid = [[
4825      {1:<<<}{10:XXXXX}ba^r                                       |
4826      {1:~                                                 }|
4827                                                        |
4828    ]],
4829    }
4830  end)
4831
4832  it('in diff mode is highlighted correct', function()
4833    screen:try_resize(50, 10)
4834    insert([[
4835    9000
4836    0009
4837    0009
4838    9000
4839    0009
4840    ]])
4841    insert('aaa\tbbb')
4842    command('set diff')
4843    api.nvim_buf_set_extmark(0, ns, 0, 1, { virt_text = { { 'test', 'Special' } }, virt_text_pos = 'inline', right_gravity = false })
4844    api.nvim_buf_set_extmark(0, ns, 5, 0, { virt_text = { { '!', 'Special' } }, virt_text_pos = 'inline' })
4845    api.nvim_buf_set_extmark(0, ns, 5, 3, { virt_text = { { '' } }, virt_text_pos = 'inline' })
4846    command('vnew')
4847    insert([[
4848    000
4849    000
4850    000
4851    000
4852    000
4853    ]])
4854    insert('aaabbb')
4855    command('set diff')
4856    feed('gg0')
4857    screen:expect {
4858      grid = [[
4859      {9:^000                      }│{5:9}{14:test}{9:000                }|
4860      {9:000                      }│{9:000}{5:9}{9:                    }|*2
4861      {9:000                      }│{5:9}{9:000                    }|
4862      {9:000                      }│{9:000}{5:9}{9:                    }|
4863      {9:aaabbb                   }│{14:!}{9:aaa}{5:    }{9:bbb             }|
4864      {1:~                        }│{1:~                       }|*2
4865      {15:[No Name] [+]             }{13:[No Name] [+]           }|
4866                                                        |
4867    ]],
4868    }
4869    command('wincmd w | set nowrap')
4870    feed('zl')
4871    screen:expect {
4872      grid = [[
4873      {9:000                      }│{14:test}{9:000                 }|
4874      {9:000                      }│{9:00}{5:9}{9:                     }|*2
4875      {9:000                      }│{9:000                     }|
4876      {9:000                      }│{9:00}{5:9}{9:                     }|
4877      {9:aaabbb                   }│{9:aaa}{5:    }{9:bb^b              }|
4878      {1:~                        }│{1:~                       }|*2
4879      {13:[No Name] [+]             }{15:[No Name] [+]           }|
4880                                                        |
4881    ]],
4882    }
4883  end)
4884
4885  it('correctly draws when there are multiple overlapping virtual texts on the same line with nowrap', function()
4886    command('set nowrap')
4887    insert('a')
4888    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { string.rep('a', 55), 'Special' } }, virt_text_pos = 'inline' })
4889    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { string.rep('b', 55), 'Special' } }, virt_text_pos = 'inline' })
4890    feed('$')
4891    screen:expect {
4892      grid = [[
4893      {10:bbbbbbbbbbbbbbbbbbbbbbbbb}^a                        |
4894      {1:~                                                 }|
4895                                                        |
4896    ]],
4897    }
4898  end)
4899
4900  it('correctly draws when overflowing virtual text is followed by TAB with no wrap', function()
4901    command('set nowrap')
4902    feed('i<TAB>test<ESC>')
4903    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { string.rep('a', 60), 'Special' } }, virt_text_pos = 'inline' })
4904    feed('0')
4905    screen:expect({
4906      grid = [[
4907      {10:aaaaaaaaaaaaaaaaaaaaaa}   ^ test                    |
4908      {1:~                                                 }|
4909                                                        |
4910    ]],
4911    })
4912  end)
4913
4914  it('does not crash at column 0 when folded in a wide window', function()
4915    screen:try_resize(82, 5)
4916    command('hi! CursorLine guibg=NONE guifg=Red gui=NONE')
4917    command('set cursorline')
4918    insert([[
4919      aaaaa
4920      bbbbb
4921
4922      ccccc]])
4923    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'foo' } }, virt_text_pos = 'inline' })
4924    api.nvim_buf_set_extmark(0, ns, 2, 0, { virt_text = { { 'bar' } }, virt_text_pos = 'inline' })
4925    screen:expect {
4926      grid = [[
4927      fooaaaaa                                                                          |
4928      bbbbb                                                                             |
4929      bar                                                                               |
4930      {16:cccc^c                                                                             }|
4931                                                                                        |
4932    ]],
4933    }
4934    command('1,2fold')
4935    screen:expect {
4936      grid = [[
4937      {17:+--  2 lines: aaaaa·······························································}|
4938      bar                                                                               |
4939      {16:cccc^c                                                                             }|
4940      {1:~                                                                                 }|
4941                                                                                        |
4942    ]],
4943    }
4944    feed('2k')
4945    screen:expect {
4946      grid = [[
4947      {18:^+--  2 lines: aaaaa·······························································}|
4948      bar                                                                               |
4949      ccccc                                                                             |
4950      {1:~                                                                                 }|
4951                                                                                        |
4952    ]],
4953    }
4954    command('3,4fold')
4955    screen:expect {
4956      grid = [[
4957      {18:^+--  2 lines: aaaaa·······························································}|
4958      {17:+--  2 lines: ccccc·······························································}|
4959      {1:~                                                                                 }|*2
4960                                                                                        |
4961    ]],
4962    }
4963    feed('j')
4964    screen:expect {
4965      grid = [[
4966      {17:+--  2 lines: aaaaa·······························································}|
4967      {18:^+--  2 lines: ccccc·······························································}|
4968      {1:~                                                                                 }|*2
4969                                                                                        |
4970    ]],
4971    }
4972  end)
4973
4974  it('does not crash at right edge of wide window #23848', function()
4975    screen:try_resize(82, 5)
4976    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { ('a'):rep(82) }, { 'b' } }, virt_text_pos = 'inline' })
4977    screen:expect {
4978      grid = [[
4979      ^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
4980      b                                                                                 |
4981      {1:~                                                                                 }|*2
4982                                                                                        |
4983    ]],
4984    }
4985    command('set nowrap')
4986    screen:expect {
4987      grid = [[
4988      ^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
4989      {1:~                                                                                 }|*3
4990                                                                                        |
4991    ]],
4992    }
4993    feed('82i0<Esc>0')
4994    screen:expect {
4995      grid = [[
4996      ^0000000000000000000000000000000000000000000000000000000000000000000000000000000000|
4997      {1:~                                                                                 }|*3
4998                                                                                        |
4999    ]],
5000    }
5001    command('set wrap')
5002    screen:expect {
5003      grid = [[
5004      ^0000000000000000000000000000000000000000000000000000000000000000000000000000000000|
5005      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
5006      b                                                                                 |
5007      {1:~                                                                                 }|
5008                                                                                        |
5009    ]],
5010    }
5011  end)
5012
5013  it('lcs-extends is drawn with inline virtual text at end of screen line', function()
5014    exec([[
5015      setlocal nowrap list listchars=extends:!
5016      call setline(1, repeat('a', 51))
5017    ]])
5018    api.nvim_buf_set_extmark(0, ns, 0, 50, { virt_text = { { 'bbb', 'Special' } }, virt_text_pos = 'inline' })
5019    feed('20l')
5020    screen:expect {
5021      grid = [[
5022      aaaaaaaaaaaaaaaaaaaa^aaaaaaaaaaaaaaaaaaaaaaaaaaaaa{1:!}|
5023      {1:~                                                 }|
5024                                                        |
5025    ]],
5026    }
5027    feed('zl')
5028    screen:expect {
5029      grid = [[
5030      aaaaaaaaaaaaaaaaaaa^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{1:!}|
5031      {1:~                                                 }|
5032                                                        |
5033    ]],
5034    }
5035    feed('zl')
5036    screen:expect {
5037      grid = [[
5038      aaaaaaaaaaaaaaaaaa^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:b}{1:!}|
5039      {1:~                                                 }|
5040                                                        |
5041    ]],
5042    }
5043    feed('zl')
5044    screen:expect {
5045      grid = [[
5046      aaaaaaaaaaaaaaaaa^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bb}{1:!}|
5047      {1:~                                                 }|
5048                                                        |
5049    ]],
5050    }
5051    feed('zl')
5052    screen:expect {
5053      grid = [[
5054      aaaaaaaaaaaaaaaa^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{10:bbb}a|
5055      {1:~                                                 }|
5056                                                        |
5057    ]],
5058    }
5059  end)
5060
5061  it('lcs-extends is drawn with only inline virtual text offscreen', function()
5062    command('set nowrap')
5063    command('set list')
5064    command('set listchars+=extends:c')
5065    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text = { { 'test', 'Special' } }, virt_text_pos = 'inline' })
5066    insert(string.rep('a', 50))
5067    feed('gg0')
5068    screen:expect {
5069      grid = [[
5070      ^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{1:c}|
5071      {1:~                                                 }|
5072                                                        |
5073    ]],
5074    }
5075  end)
5076
5077  it('blockwise Visual highlight with double-width virtual text (replace)', function()
5078    screen:try_resize(60, 6)
5079    insert('123456789\n123456789\n123456789\n123456789')
5080    api.nvim_buf_set_extmark(0, ns, 1, 1, { virt_text = { { '-口-', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'replace' })
5081    api.nvim_buf_set_extmark(0, ns, 2, 2, { virt_text = { { '口', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'replace' })
5082    feed('gg0')
5083    screen:expect {
5084      grid = [[
5085      ^123456789                                                   |
5086      1{10:-口-}23456789                                               |
5087      12{10:口}3456789                                                 |
5088      123456789                                                   |
5089      {1:~                                                           }|
5090                                                                  |
5091    ]],
5092    }
5093    feed('<C-V>3jl')
5094    screen:expect {
5095      grid = [[
5096      {7:12}3456789                                                   |
5097      {7:1}{10:-口-}23456789                                               |
5098      {7:12}{10:口}3456789                                                 |
5099      {7:1^2}3456789                                                   |
5100      {1:~                                                           }|
5101      {8:-- VISUAL BLOCK --}                                          |
5102    ]],
5103    }
5104    feed('l')
5105    screen:expect {
5106      grid = [[
5107      {7:123}456789                                                   |
5108      {7:1}{10:-口-}23456789                                               |
5109      {7:12}{10:口}3456789                                                 |
5110      {7:12^3}456789                                                   |
5111      {1:~                                                           }|
5112      {8:-- VISUAL BLOCK --}                                          |
5113    ]],
5114    }
5115    feed('4l')
5116    screen:expect {
5117      grid = [[
5118      {7:1234567}89                                                   |
5119      {7:1}{10:-口-}{7:23}456789                                               |
5120      {7:12}{10:口}{7:345}6789                                                 |
5121      {7:123456^7}89                                                   |
5122      {1:~                                                           }|
5123      {8:-- VISUAL BLOCK --}                                          |
5124    ]],
5125    }
5126    feed('Ol')
5127    screen:expect {
5128      grid = [[
5129      1{7:234567}89                                                   |
5130      1{10:-口-}{7:23}456789                                               |
5131      1{7:2}{10:口}{7:345}6789                                                 |
5132      1{7:^234567}89                                                   |
5133      {1:~                                                           }|
5134      {8:-- VISUAL BLOCK --}                                          |
5135    ]],
5136    }
5137    feed('l')
5138    screen:expect {
5139      grid = [[
5140      12{7:34567}89                                                   |
5141      1{10:-口-}{7:23}456789                                               |
5142      12{10:口}{7:345}6789                                                 |
5143      12{7:^34567}89                                                   |
5144      {1:~                                                           }|
5145      {8:-- VISUAL BLOCK --}                                          |
5146    ]],
5147    }
5148    feed('l')
5149    screen:expect {
5150      grid = [[
5151      123{7:4567}89                                                   |
5152      1{10:-口-}{7:23}456789                                               |
5153      12{10:口}{7:345}6789                                                 |
5154      123{7:^4567}89                                                   |
5155      {1:~                                                           }|
5156      {8:-- VISUAL BLOCK --}                                          |
5157    ]],
5158    }
5159  end)
5160
5161  it('blockwise Visual highlight with double-width virtual text (combine)', function()
5162    screen:try_resize(60, 6)
5163    insert('123456789\n123456789\n123456789\n123456789')
5164    api.nvim_buf_set_extmark(0, ns, 1, 1, { virt_text = { { '-口-', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
5165    api.nvim_buf_set_extmark(0, ns, 2, 2, { virt_text = { { '口', 'Special' } }, virt_text_pos = 'inline', hl_mode = 'combine' })
5166    feed('gg0')
5167    screen:expect {
5168      grid = [[
5169      ^123456789                                                   |
5170      1{10:-口-}23456789                                               |
5171      12{10:口}3456789                                                 |
5172      123456789                                                   |
5173      {1:~                                                           }|
5174                                                                  |
5175    ]],
5176    }
5177    feed('<C-V>3jl')
5178    screen:expect {
5179      grid = [[
5180      {7:12}3456789                                                   |
5181      {7:1}{20:-}{10:口-}23456789                                               |
5182      {7:12}{10:口}3456789                                                 |
5183      {7:1^2}3456789                                                   |
5184      {1:~                                                           }|
5185      {8:-- VISUAL BLOCK --}                                          |
5186    ]],
5187    }
5188    feed('l')
5189    screen:expect {
5190      grid = [[
5191      {7:123}456789                                                   |
5192      {7:1}{20:-口}{10:-}23456789                                               |
5193      {7:12}{20:口}3456789                                                 |
5194      {7:12^3}456789                                                   |
5195      {1:~                                                           }|
5196      {8:-- VISUAL BLOCK --}                                          |
5197    ]],
5198    }
5199    feed('4l')
5200    screen:expect {
5201      grid = [[
5202      {7:1234567}89                                                   |
5203      {7:1}{20:-口-}{7:23}456789                                               |
5204      {7:12}{20:口}{7:345}6789                                                 |
5205      {7:123456^7}89                                                   |
5206      {1:~                                                           }|
5207      {8:-- VISUAL BLOCK --}                                          |
5208    ]],
5209    }
5210    feed('Ol')
5211    screen:expect {
5212      grid = [[
5213      1{7:234567}89                                                   |
5214      1{20:-口-}{7:23}456789                                               |
5215      1{7:2}{20:口}{7:345}6789                                                 |
5216      1{7:^234567}89                                                   |
5217      {1:~                                                           }|
5218      {8:-- VISUAL BLOCK --}                                          |
5219    ]],
5220    }
5221    feed('l')
5222    screen:expect {
5223      grid = [[
5224      12{7:34567}89                                                   |
5225      1{10:-}{20:口-}{7:23}456789                                               |
5226      12{20:口}{7:345}6789                                                 |
5227      12{7:^34567}89                                                   |
5228      {1:~                                                           }|
5229      {8:-- VISUAL BLOCK --}                                          |
5230    ]],
5231    }
5232    feed('l')
5233    screen:expect {
5234      grid = [[
5235      123{7:4567}89                                                   |
5236      1{10:-}{20:口-}{7:23}456789                                               |
5237      12{20:口}{7:345}6789                                                 |
5238      123{7:^4567}89                                                   |
5239      {1:~                                                           }|
5240      {8:-- VISUAL BLOCK --}                                          |
5241    ]],
5242    }
5243  end)
5244
5245  local function test_virt_inline_showbreak_smoothscroll()
5246    screen:try_resize(30, 6)
5247    exec([[
5248      highlight! link LineNr Normal
5249      setlocal number showbreak=+ breakindent breakindentopt=shift:2
5250      setlocal scrolloff=0 smoothscroll
5251      call setline(1, repeat('a', 28))
5252      normal! $
5253    ]])
5254    api.nvim_buf_set_extmark(0, ns, 0, 27, { virt_text = { { ('123'):rep(23) } }, virt_text_pos = 'inline' })
5255    feed(':<CR>') -- Have a screen line that doesn't start with spaces
5256    screen:expect {
5257      grid = [[
5258        1 aaaaaaaaaaaaaaaaaaaaaaaaaa|
5259            {1:+}a1231231231231231231231|
5260            {1:+}23123123123123123123123|
5261            {1:+}12312312312312312312312|
5262            {1:+}3^a                     |
5263      :                             |
5264    ]],
5265    }
5266    feed('<C-E>')
5267    screen:expect {
5268      grid = [[
5269            {1:+}a1231231231231231231231|
5270            {1:+}23123123123123123123123|
5271            {1:+}12312312312312312312312|
5272            {1:+}3^a                     |
5273      {1:~                             }|
5274      :                             |
5275    ]],
5276    }
5277    feed('<C-E>')
5278    screen:expect {
5279      grid = [[
5280            {1:+}23123123123123123123123|
5281            {1:+}12312312312312312312312|
5282            {1:+}3^a                     |
5283      {1:~                             }|*2
5284      :                             |
5285    ]],
5286    }
5287    feed('<C-E>')
5288    screen:expect {
5289      grid = [[
5290            {1:+}12312312312312312312312|
5291            {1:+}3^a                     |
5292      {1:~                             }|*3
5293      :                             |
5294    ]],
5295    }
5296    feed('<C-E>')
5297    screen:expect {
5298      grid = [[
5299            {1:+}3^a                     |
5300      {1:~                             }|*4
5301      :                             |
5302    ]],
5303    }
5304    feed('zbi')
5305    screen:expect {
5306      grid = [[
5307        1 aaaaaaaaaaaaaaaaaaaaaaaaaa|
5308            {1:+}a^1231231231231231231231|
5309            {1:+}23123123123123123123123|
5310            {1:+}12312312312312312312312|
5311            {1:+}3a                     |
5312      {8:-- INSERT --}                  |
5313    ]],
5314    }
5315    feed('<BS>')
5316    screen:expect {
5317      grid = [[
5318        1 aaaaaaaaaaaaaaaaaaaaaaaaaa|
5319            {1:+}^12312312312312312312312|
5320            {1:+}31231231231231231231231|
5321            {1:+}23123123123123123123123|
5322            {1:+}a                      |
5323      {8:-- INSERT --}                  |
5324    ]],
5325    }
5326    feed('<Esc>l')
5327    feed(':<CR>') -- Have a screen line that doesn't start with spaces
5328    screen:expect {
5329      grid = [[
5330        1 aaaaaaaaaaaaaaaaaaaaaaaaaa|
5331            {1:+}12312312312312312312312|
5332            {1:+}31231231231231231231231|
5333            {1:+}23123123123123123123123|
5334            {1:+}^a                      |
5335      :                             |
5336    ]],
5337    }
5338    feed('<C-E>')
5339    screen:expect {
5340      grid = [[
5341            {1:+}12312312312312312312312|
5342            {1:+}31231231231231231231231|
5343            {1:+}23123123123123123123123|
5344            {1:+}^a                      |
5345      {1:~                             }|
5346      :                             |
5347    ]],
5348    }
5349    feed('<C-E>')
5350    screen:expect {
5351      grid = [[
5352            {1:+}31231231231231231231231|
5353            {1:+}23123123123123123123123|
5354            {1:+}^a                      |
5355      {1:~                             }|*2
5356      :                             |
5357    ]],
5358    }
5359    feed('<C-E>')
5360    screen:expect {
5361      grid = [[
5362            {1:+}23123123123123123123123|
5363            {1:+}^a                      |
5364      {1:~                             }|*3
5365      :                             |
5366    ]],
5367    }
5368    feed('<C-E>')
5369    screen:expect {
5370      grid = [[
5371            {1:+}^a                      |
5372      {1:~                             }|*4
5373      :                             |
5374    ]],
5375    }
5376    feed('023x$')
5377    screen:expect {
5378      grid = [[
5379        1 aaa12312312312312312312312|
5380            {1:+}31231231231231231231231|
5381            {1:+}23123123123123123123123|
5382            {1:+}^a                      |
5383      {1:~                             }|
5384      :                             |
5385    ]],
5386    }
5387    feed('<C-E>')
5388    screen:expect {
5389      grid = [[
5390            {1:+}31231231231231231231231|
5391            {1:+}23123123123123123123123|
5392            {1:+}^a                      |
5393      {1:~                             }|*2
5394      :                             |
5395    ]],
5396    }
5397    feed('<C-E>')
5398    screen:expect {
5399      grid = [[
5400            {1:+}23123123123123123123123|
5401            {1:+}^a                      |
5402      {1:~                             }|*3
5403      :                             |
5404    ]],
5405    }
5406    feed('<C-E>')
5407    screen:expect {
5408      grid = [[
5409            {1:+}^a                      |
5410      {1:~                             }|*4
5411      :                             |
5412    ]],
5413    }
5414    feed('zbi')
5415    screen:expect {
5416      grid = [[
5417        1 aaa^12312312312312312312312|
5418            {1:+}31231231231231231231231|
5419            {1:+}23123123123123123123123|
5420            {1:+}a                      |
5421      {1:~                             }|
5422      {8:-- INSERT --}                  |
5423    ]],
5424    }
5425    feed('<C-U>')
5426    screen:expect {
5427      grid = [[
5428        1 ^12312312312312312312312312|
5429            {1:+}31231231231231231231231|
5430            {1:+}23123123123123123123a  |
5431      {1:~                             }|*2
5432      {8:-- INSERT --}                  |
5433    ]],
5434    }
5435    feed('<Esc>')
5436    screen:expect {
5437      grid = [[
5438        1 12312312312312312312312312|
5439            {1:+}31231231231231231231231|
5440            {1:+}23123123123123123123^a  |
5441      {1:~                             }|*2
5442                                    |
5443    ]],
5444    }
5445    feed('<C-E>')
5446    screen:expect {
5447      grid = [[
5448            {1:+}31231231231231231231231|
5449            {1:+}23123123123123123123^a  |
5450      {1:~                             }|*3
5451                                    |
5452    ]],
5453    }
5454    feed('<C-E>')
5455    screen:expect {
5456      grid = [[
5457            {1:+}23123123123123123123^a  |
5458      {1:~                             }|*4
5459                                    |
5460    ]],
5461    }
5462    feed('zbx')
5463    screen:expect {
5464      grid = [[
5465        1 ^12312312312312312312312312|
5466            {1:+}31231231231231231231231|
5467            {1:+}23123123123123123123   |
5468      {1:~                             }|*2
5469                                    |
5470    ]],
5471    }
5472    feed('26ia<Esc>a')
5473    screen:expect {
5474      grid = [[
5475        1 aaaaaaaaaaaaaaaaaaaaaaaaaa|
5476            {1:+}^12312312312312312312312|
5477            {1:+}31231231231231231231231|
5478            {1:+}23123123123123123123123|
5479      {1:~                             }|
5480      {8:-- INSERT --}                  |
5481    ]],
5482    }
5483    feed([[<C-\><C-O>:setlocal breakindentopt=<CR>]])
5484    screen:expect {
5485      grid = [[
5486        1 aaaaaaaaaaaaaaaaaaaaaaaaaa|
5487          {1:+}^1231231231231231231231231|
5488          {1:+}2312312312312312312312312|
5489          {1:+}3123123123123123123      |
5490      {1:~                             }|
5491      {8:-- INSERT --}                  |
5492    ]],
5493    }
5494  end
5495
5496  describe('with showbreak, smoothscroll', function()
5497    it('and cpoptions-=n', function()
5498      test_virt_inline_showbreak_smoothscroll()
5499    end)
5500
5501    it('and cpoptions+=n', function()
5502      command('set cpoptions+=n')
5503      -- because of 'breakindent' the screen states are the same
5504      test_virt_inline_showbreak_smoothscroll()
5505    end)
5506  end)
5507
5508  it('before TABs with smoothscroll', function()
5509    screen:try_resize(30, 6)
5510    exec([[
5511      setlocal list listchars=tab:<-> scrolloff=0 smoothscroll
5512      call setline(1, repeat("\t", 4) .. 'a')
5513      normal! $
5514    ]])
5515    api.nvim_buf_set_extmark(0, ns, 0, 3, { virt_text = { { ('12'):rep(32) } }, virt_text_pos = 'inline' })
5516    screen:expect {
5517      grid = [[
5518      {1:<------><------><------>}121212|
5519      121212121212121212121212121212|
5520      1212121212121212121212121212{1:<-}|
5521      {1:----->}^a                       |
5522      {1:~                             }|
5523                                    |
5524    ]],
5525    }
5526    feed('<C-E>')
5527    screen:expect {
5528      grid = [[
5529      {1:<<<}212121212121212121212121212|
5530      1212121212121212121212121212{1:<-}|
5531      {1:----->}^a                       |
5532      {1:~                             }|*2
5533                                    |
5534    ]],
5535    }
5536    feed('<C-E>')
5537    screen:expect {
5538      grid = [[
5539      {1:<<<}2121212121212121212121212{1:<-}|
5540      {1:----->}^a                       |
5541      {1:~                             }|*3
5542                                    |
5543    ]],
5544    }
5545    feed('<C-E>')
5546    screen:expect {
5547      grid = [[
5548      {1:<<<-->}^a                       |
5549      {1:~                             }|*4
5550                                    |
5551    ]],
5552    }
5553    feed('zbh')
5554    screen:expect {
5555      grid = [[
5556      {1:<------><------><------>}121212|
5557      121212121212121212121212121212|
5558      1212121212121212121212121212{1:^<-}|
5559      {1:----->}a                       |
5560      {1:~                             }|
5561                                    |
5562    ]],
5563    }
5564    feed('i')
5565    screen:expect {
5566      grid = [[
5567      {1:<------><------><------>}^121212|
5568      121212121212121212121212121212|
5569      1212121212121212121212121212{1:<-}|
5570      {1:----->}a                       |
5571      {1:~                             }|
5572      {8:-- INSERT --}                  |
5573    ]],
5574    }
5575    feed('<C-O>:setlocal nolist<CR>')
5576    screen:expect {
5577      grid = [[
5578                              ^121212|
5579      121212121212121212121212121212|
5580      1212121212121212121212121212  |
5581            a                       |
5582      {1:~                             }|
5583      {8:-- INSERT --}                  |
5584    ]],
5585    }
5586    feed('<Esc>l')
5587    screen:expect {
5588      grid = [[
5589                              121212|
5590      121212121212121212121212121212|
5591      1212121212121212121212121212  |
5592           ^ a                       |
5593      {1:~                             }|
5594                                    |
5595    ]],
5596    }
5597    feed('<C-E>')
5598    screen:expect {
5599      grid = [[
5600      {1:<<<}212121212121212121212121212|
5601      1212121212121212121212121212  |
5602           ^ a                       |
5603      {1:~                             }|*2
5604                                    |
5605    ]],
5606    }
5607    feed('<C-E>')
5608    screen:expect {
5609      grid = [[
5610      {1:<<<}2121212121212121212121212  |
5611           ^ a                       |
5612      {1:~                             }|*3
5613                                    |
5614    ]],
5615    }
5616    feed('<C-E>')
5617    screen:expect {
5618      grid = [[
5619      {1:<<<}  ^ a                       |
5620      {1:~                             }|*4
5621                                    |
5622    ]],
5623    }
5624  end)
5625
5626  it('before a space with linebreak', function()
5627    screen:try_resize(50, 6)
5628    exec([[
5629      setlocal linebreak showbreak=+ breakindent breakindentopt=shift:2
5630      call setline(1, repeat('a', 50) .. ' ' .. repeat('c', 45))
5631      normal! $
5632    ]])
5633    api.nvim_buf_set_extmark(0, ns, 0, 50, { virt_text = { { ('b'):rep(10) } }, virt_text_pos = 'inline' })
5634    screen:expect {
5635      grid = [[
5636      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
5637        {1:+}bbbbbbbbbb                                     |
5638        {1:+}cccccccccccccccccccccccccccccccccccccccccccc^c  |
5639      {1:~                                                 }|*2
5640                                                        |
5641    ]],
5642    }
5643    feed('05x$')
5644    screen:expect {
5645      grid = [[
5646      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbb|
5647        {1:+}bbbbb                                          |
5648        {1:+}cccccccccccccccccccccccccccccccccccccccccccc^c  |
5649      {1:~                                                 }|*2
5650                                                        |
5651    ]],
5652    }
5653  end)
5654
5655  it('is counted when linebreak decides if a word fits', function()
5656    screen:try_resize(20, 4)
5657    exec([[
5658      setlocal linebreak
5659      call setline(1, repeat('a', 12) .. ' bbcc dd')
5660    ]])
5661    api.nvim_buf_set_extmark(0, ns, 0, 19, { virt_text = { { '[hint]' } }, virt_text_pos = 'inline' })
5662    -- "dd" plus the virtual text does not fit in the 2 remaining cells, so the whole word moves
5663    -- down instead of the virtual text being split across the boundary.
5664    screen:expect([[
5665      ^aaaaaaaaaaaa bbcc   |
5666      d[hint]d            |
5667      {1:~                   }|
5668                          |
5669    ]])
5670  end)
5671
5672  it('before a TAB affects its width in linebreak lookahead', function()
5673    screen:try_resize(20, 4)
5674    exec([[
5675      setlocal linebreak tabstop=8
5676      let &l:breakat = ' '
5677      call setline(1, 'aaaa ' .. "\t" .. repeat('b', 11))
5678    ]])
5679    api.nvim_buf_set_extmark(0, ns, 0, 5, {
5680      virt_text = { { 'VV' } },
5681      virt_text_pos = 'inline',
5682    })
5683    screen:expect([[
5684      ^aaaa VV bbbbbbbbbbb |
5685      {1:~                   }|*2
5686                          |
5687    ]])
5688  end)
5689
5690  it('before double-width char that wraps', function()
5691    exec([[
5692      call setline(1, repeat('a', 40) .. '口' .. '12345')
5693      normal! $
5694    ]])
5695    api.nvim_buf_set_extmark(0, ns, 0, 40, { virt_text = { { ('b'):rep(9) } }, virt_text_pos = 'inline' })
5696    screen:expect([[
5697      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbb{1:>}|
5698      口1234^5                                           |
5699                                                        |
5700    ]])
5701    feed('g0')
5702    screen:expect([[
5703      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbb{1:>}|
5704      ^口12345                                           |
5705                                                        |
5706    ]])
5707    command('set showbreak=+++')
5708    screen:expect([[
5709      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbb{1:>}|
5710      {1:+++}^口12345                                        |
5711                                                        |
5712    ]])
5713  end)
5714
5715  it('cursor position is correct if end_row or end_col is specified', function()
5716    screen:try_resize(50, 8)
5717    api.nvim_buf_set_lines(0, 0, -1, false, { ('a'):rep(48), ('b'):rep(48), ('c'):rep(48), ('d'):rep(48) })
5718    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 2, virt_text_pos = 'inline', virt_text = { { 'I1', 'NonText' } } })
5719    api.nvim_buf_set_extmark(0, ns, 3, 0, { end_col = 2, virt_text_pos = 'inline', virt_text = { { 'I2', 'NonText' } } })
5720    feed('$')
5721    screen:expect([[
5722      {1:I1}aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa^a|
5723      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb  |
5724      cccccccccccccccccccccccccccccccccccccccccccccccc  |
5725      {1:I2}dddddddddddddddddddddddddddddddddddddddddddddddd|
5726      {1:~                                                 }|*3
5727                                                        |
5728    ]])
5729    feed('j')
5730    screen:expect([[
5731      {1:I1}aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
5732      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb^b  |
5733      cccccccccccccccccccccccccccccccccccccccccccccccc  |
5734      {1:I2}dddddddddddddddddddddddddddddddddddddddddddddddd|
5735      {1:~                                                 }|*3
5736                                                        |
5737    ]])
5738    feed('j')
5739    screen:expect([[
5740      {1:I1}aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
5741      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb  |
5742      ccccccccccccccccccccccccccccccccccccccccccccccc^c  |
5743      {1:I2}dddddddddddddddddddddddddddddddddddddddddddddddd|
5744      {1:~                                                 }|*3
5745                                                        |
5746    ]])
5747    feed('j')
5748    screen:expect([[
5749      {1:I1}aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
5750      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb  |
5751      cccccccccccccccccccccccccccccccccccccccccccccccc  |
5752      {1:I2}ddddddddddddddddddddddddddddddddddddddddddddddd^d|
5753      {1:~                                                 }|*3
5754                                                        |
5755    ]])
5756  end)
5757
5758  it('is redrawn correctly after delete or redo #27370', function()
5759    screen:try_resize(50, 12)
5760    exec([[
5761      call setline(1, ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'])
5762      call setline(3, repeat('c', winwidth(0) - 1))
5763    ]])
5764    api.nvim_buf_set_extmark(0, ns, 1, 0, { virt_text = { { '!!!' } }, virt_text_pos = 'inline' })
5765    feed('j')
5766    local before_delete = [[
5767      aaa                                               |
5768      !!!^bbb                                            |
5769      ccccccccccccccccccccccccccccccccccccccccccccccccc |
5770      ddd                                               |
5771      eee                                               |
5772      fff                                               |
5773      {1:~                                                 }|*5
5774                                                        |
5775    ]]
5776    screen:expect(before_delete)
5777    feed('dd')
5778    local after_delete = [[
5779      aaa                                               |
5780      !!!^ccccccccccccccccccccccccccccccccccccccccccccccc|
5781      cc                                                |
5782      ddd                                               |
5783      eee                                               |
5784      fff                                               |
5785      {1:~                                                 }|*5
5786                                                        |
5787    ]]
5788    screen:expect(after_delete)
5789    command('silent undo')
5790    screen:expect(before_delete)
5791    command('silent redo')
5792    screen:expect(after_delete)
5793    command('silent undo')
5794    screen:expect(before_delete)
5795    command('set report=100')
5796    feed('yypk2P')
5797    before_delete = [[
5798      aaa                                               |
5799      ^bbb                                               |
5800      bbb                                               |
5801      !!!bbb                                            |
5802      bbb                                               |
5803      ccccccccccccccccccccccccccccccccccccccccccccccccc |
5804      ddd                                               |
5805      eee                                               |
5806      fff                                               |
5807      {1:~                                                 }|*2
5808                                                        |
5809    ]]
5810    screen:expect(before_delete)
5811    feed('4dd')
5812    screen:expect(after_delete)
5813    command('silent undo')
5814    screen:expect(before_delete)
5815    command('silent redo')
5816    screen:expect(after_delete)
5817  end)
5818
5819  it('cursor position is correct with invalidated inline virt text', function()
5820    screen:try_resize(50, 8)
5821    api.nvim_buf_set_lines(0, 0, -1, false, { ('a'):rep(48), ('b'):rep(48) })
5822    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_text_pos = 'inline', virt_text = { { 'INLINE', 'NonText' } }, invalidate = true })
5823    screen:expect([[
5824      {1:INLINE}^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
5825      aaaa                                              |
5826      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb  |
5827      {1:~                                                 }|*4
5828                                                        |
5829    ]])
5830    feed('dd$')
5831    screen:expect([[
5832      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb^b  |
5833      {1:~                                                 }|*6
5834                                                        |
5835    ]])
5836  end)
5837
5838  it('line size is correct with inline virt text at EOL and showbreak', function()
5839    screen:try_resize(50, 8)
5840    insert(('0123456789'):rep(5) .. '\nfoo\nbar')
5841    api.nvim_buf_set_extmark(0, ns, 0, 50, { virt_text = { { ('x'):rep(145), 'ErrorMsg' } }, virt_text_pos = 'inline' })
5842
5843    command([[set cursorline scrolloff=0 showbreak=>\  smoothscroll]])
5844    screen:expect([[
5845      01234567890123456789012345678901234567890123456789|
5846      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*3
5847      {1:> }{4:x}                                               |
5848      foo                                               |
5849      {22:ba^r                                               }|
5850                                                        |
5851    ]])
5852    eq(5, api.nvim_win_text_height(0, { start_row = 0, end_row = 0 }).all)
5853    feed('<C-E>')
5854    screen:expect([[
5855      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*3
5856      {1:> }{4:x}                                               |
5857      foo                                               |
5858      {22:ba^r                                               }|
5859      {1:~                                                 }|
5860                                                        |
5861    ]])
5862    feed('<C-E>')
5863    screen:expect([[
5864      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*2
5865      {1:> }{4:x}                                               |
5866      foo                                               |
5867      {22:ba^r                                               }|
5868      {1:~                                                 }|*2
5869                                                        |
5870    ]])
5871    feed('<C-E>')
5872    screen:expect([[
5873      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|
5874      {1:> }{4:x}                                               |
5875      foo                                               |
5876      {22:ba^r                                               }|
5877      {1:~                                                 }|*3
5878                                                        |
5879    ]])
5880    feed('<C-E>')
5881    screen:expect([[
5882      {1:> }{4:x}                                               |
5883      foo                                               |
5884      {22:ba^r                                               }|
5885      {1:~                                                 }|*4
5886                                                        |
5887    ]])
5888    feed('<C-E>')
5889    screen:expect([[
5890      foo                                               |
5891      {22:ba^r                                               }|
5892      {1:~                                                 }|*5
5893                                                        |
5894    ]])
5895
5896    feed('gg$xG$')
5897    screen:expect([[
5898      0123456789012345678901234567890123456789012345678{4:x}|
5899      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*3
5900      foo                                               |
5901      {22:ba^r                                               }|
5902      {1:~                                                 }|
5903                                                        |
5904    ]])
5905    eq(4, api.nvim_win_text_height(0, { start_row = 0, end_row = 0 }).all)
5906    feed('<C-E>')
5907    screen:expect([[
5908      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*3
5909      foo                                               |
5910      {22:ba^r                                               }|
5911      {1:~                                                 }|*2
5912                                                        |
5913    ]])
5914    feed('<C-E>')
5915    screen:expect([[
5916      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*2
5917      foo                                               |
5918      {22:ba^r                                               }|
5919      {1:~                                                 }|*3
5920                                                        |
5921    ]])
5922    feed('<C-E>')
5923    screen:expect([[
5924      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|
5925      foo                                               |
5926      {22:ba^r                                               }|
5927      {1:~                                                 }|*4
5928                                                        |
5929    ]])
5930    feed('<C-E>')
5931    screen:expect([[
5932      foo                                               |
5933      {22:ba^r                                               }|
5934      {1:~                                                 }|*5
5935                                                        |
5936    ]])
5937
5938    feed('zb')
5939    command('set list listchars=eol:$')
5940    screen:expect([[
5941      0123456789012345678901234567890123456789012345678{4:x}|
5942      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*3
5943      {1:> $}                                               |
5944      foo{1:$}                                              |
5945      {22:ba^r}{23:$}{22:                                              }|
5946                                                        |
5947    ]])
5948    eq(5, api.nvim_win_text_height(0, { start_row = 0, end_row = 0 }).all)
5949    feed('<C-E>')
5950    screen:expect([[
5951      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*3
5952      {1:> $}                                               |
5953      foo{1:$}                                              |
5954      {22:ba^r}{23:$}{22:                                              }|
5955      {1:~                                                 }|
5956                                                        |
5957    ]])
5958    feed('<C-E>')
5959    screen:expect([[
5960      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*2
5961      {1:> $}                                               |
5962      foo{1:$}                                              |
5963      {22:ba^r}{23:$}{22:                                              }|
5964      {1:~                                                 }|*2
5965                                                        |
5966    ]])
5967    feed('<C-E>')
5968    screen:expect([[
5969      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|
5970      {1:> $}                                               |
5971      foo{1:$}                                              |
5972      {22:ba^r}{23:$}{22:                                              }|
5973      {1:~                                                 }|*3
5974                                                        |
5975    ]])
5976    feed('<C-E>')
5977    screen:expect([[
5978      {1:> $}                                               |
5979      foo{1:$}                                              |
5980      {22:ba^r}{23:$}{22:                                              }|
5981      {1:~                                                 }|*4
5982                                                        |
5983    ]])
5984    feed('<C-E>')
5985    screen:expect([[
5986      foo{1:$}                                              |
5987      {22:ba^r}{23:$}{22:                                              }|
5988      {1:~                                                 }|*5
5989                                                        |
5990    ]])
5991
5992    feed('gg$xG$')
5993    screen:expect([[
5994      012345678901234567890123456789012345678901234567{4:xx}|
5995      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*2
5996      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}{1:$}|
5997      foo{1:$}                                              |
5998      {22:ba^r}{23:$}{22:                                              }|
5999      {1:~                                                 }|
6000                                                        |
6001    ]])
6002    eq(4, api.nvim_win_text_height(0, { start_row = 0, end_row = 0 }).all)
6003    feed('<C-E>')
6004    screen:expect([[
6005      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|*2
6006      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}{1:$}|
6007      foo{1:$}                                              |
6008      {22:ba^r}{23:$}{22:                                              }|
6009      {1:~                                                 }|*2
6010                                                        |
6011    ]])
6012    feed('<C-E>')
6013    screen:expect([[
6014      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}|
6015      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}{1:$}|
6016      foo{1:$}                                              |
6017      {22:ba^r}{23:$}{22:                                              }|
6018      {1:~                                                 }|*3
6019                                                        |
6020    ]])
6021    feed('<C-E>')
6022    screen:expect([[
6023      {1:> }{4:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}{1:$}|
6024      foo{1:$}                                              |
6025      {22:ba^r}{23:$}{22:                                              }|
6026      {1:~                                                 }|*4
6027                                                        |
6028    ]])
6029    feed('<C-E>')
6030    screen:expect([[
6031      foo{1:$}                                              |
6032      {22:ba^r}{23:$}{22:                                              }|
6033      {1:~                                                 }|*5
6034                                                        |
6035    ]])
6036  end)
6037
6038  it("virtcol('$') is correct with inline virt text at EOL", function()
6039    insert(('1234567890\n'):rep(6))
6040    for _, v in ipairs({ { 2, 'a' }, { 3, 'ab' }, { 4, 'abc' }, { 5, 'abcd' }, { 6, 'αβγ口' } }) do
6041      local ln, tx = unpack(v)
6042      local co = fn.col({ ln, '$' })
6043      eq(11, fn.virtcol({ ln, '$' }))
6044      api.nvim_buf_set_extmark(0, ns, ln - 1, co - 1, { virt_text = { { tx } }, virt_text_pos = 'inline' })
6045      eq(11 + fn.strwidth(tx), fn.virtcol({ ln, '$' }))
6046    end
6047  end)
6048end)
6049
6050describe('decorations: virtual lines', function()
6051  local screen ---@type test.functional.ui.screen
6052  local ns ---@type integer
6053
6054  before_each(function()
6055    clear()
6056    screen = Screen.new(50, 12)
6057    screen:add_extra_attr_ids {
6058      [100] = { foreground = Screen.colors.Blue, background = Screen.colors.Yellow },
6059    }
6060
6061    ns = api.nvim_create_namespace 'test'
6062  end)
6063
6064  local example_text2 = [[
6065if (h->n_buckets < new_n_buckets) { // expand
6066  khkey_t *new_keys = (khkey_t *)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t));
6067  h->keys = new_keys;
6068  if (kh_is_map && val_size) {
6069    char *new_vals = krealloc( h->vals_buf, new_n_buckets * val_size);
6070    h->vals_buf = new_vals;
6071  }
6072}]]
6073
6074  it('works with one line', function()
6075    insert(example_text2)
6076    feed '2gg'
6077    screen:expect {
6078      grid = [[
6079      if (h->n_buckets < new_n_buckets) { // expand     |
6080        ^khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6081      h->keys, new_n_buckets * sizeof(khkey_t));        |
6082        h->keys = new_keys;                             |
6083        if (kh_is_map && val_size) {                    |
6084          char *new_vals = krealloc( h->vals_buf, new_n_|
6085      buckets * val_size);                              |
6086          h->vals_buf = new_vals;                       |
6087        }                                               |
6088      }                                                 |
6089      {1:~                                                 }|
6090                                                        |
6091    ]],
6092    }
6093
6094    api.nvim_buf_set_extmark(0, ns, 1, 33, {
6095      virt_lines = { { { '>> ', 'NonText' }, { 'krealloc', 'Identifier' }, { ': change the size of an allocation' } } },
6096      virt_lines_above = true,
6097    })
6098
6099    screen:expect {
6100      grid = [[
6101      if (h->n_buckets < new_n_buckets) { // expand     |
6102      {1:>> }{25:krealloc}: change the size of an allocation     |
6103        ^khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6104      h->keys, new_n_buckets * sizeof(khkey_t));        |
6105        h->keys = new_keys;                             |
6106        if (kh_is_map && val_size) {                    |
6107          char *new_vals = krealloc( h->vals_buf, new_n_|
6108      buckets * val_size);                              |
6109          h->vals_buf = new_vals;                       |
6110        }                                               |
6111      }                                                 |
6112                                                        |
6113    ]],
6114    }
6115
6116    feed '/krealloc<cr>'
6117    screen:expect {
6118      grid = [[
6119      if (h->n_buckets < new_n_buckets) { // expand     |
6120      {1:>> }{25:krealloc}: change the size of an allocation     |
6121        khkey_t *new_keys = (khkey_t *){10:^krealloc}((void *)|
6122      h->keys, new_n_buckets * sizeof(khkey_t));        |
6123        h->keys = new_keys;                             |
6124        if (kh_is_map && val_size) {                    |
6125          char *new_vals = {10:krealloc}( h->vals_buf, new_n_|
6126      buckets * val_size);                              |
6127          h->vals_buf = new_vals;                       |
6128        }                                               |
6129      }                                                 |
6130      /krealloc                                         |
6131    ]],
6132    }
6133
6134    -- virtual line remains anchored to the extmark
6135    feed 'i<cr>'
6136    screen:expect {
6137      grid = [[
6138      if (h->n_buckets < new_n_buckets) { // expand     |
6139        khkey_t *new_keys = (khkey_t *)                 |
6140      {1:>> }{25:krealloc}: change the size of an allocation     |
6141      {10:^krealloc}((void *)h->keys, new_n_buckets * sizeof(k|
6142      hkey_t));                                         |
6143        h->keys = new_keys;                             |
6144        if (kh_is_map && val_size) {                    |
6145          char *new_vals = {10:krealloc}( h->vals_buf, new_n_|
6146      buckets * val_size);                              |
6147          h->vals_buf = new_vals;                       |
6148        }                                               |
6149      {5:-- INSERT --}                                      |
6150    ]],
6151    }
6152
6153    feed '<esc>3+'
6154    screen:expect {
6155      grid = [[
6156      if (h->n_buckets < new_n_buckets) { // expand     |
6157        khkey_t *new_keys = (khkey_t *)                 |
6158      {1:>> }{25:krealloc}: change the size of an allocation     |
6159      {10:krealloc}((void *)h->keys, new_n_buckets * sizeof(k|
6160      hkey_t));                                         |
6161        h->keys = new_keys;                             |
6162        if (kh_is_map && val_size) {                    |
6163          ^char *new_vals = {10:krealloc}( h->vals_buf, new_n_|
6164      buckets * val_size);                              |
6165          h->vals_buf = new_vals;                       |
6166        }                                               |
6167                                                        |
6168    ]],
6169    }
6170
6171    api.nvim_buf_set_extmark(0, ns, 5, 0, {
6172      virt_lines = { { { '^^ REVIEW:', 'Todo' }, { ' new_vals variable seems unnecessary?', 'Comment' } } },
6173    })
6174    screen:expect {
6175      grid = [[
6176      if (h->n_buckets < new_n_buckets) { // expand     |
6177        khkey_t *new_keys = (khkey_t *)                 |
6178      {1:>> }{25:krealloc}: change the size of an allocation     |
6179      {10:krealloc}((void *)h->keys, new_n_buckets * sizeof(k|
6180      hkey_t));                                         |
6181        h->keys = new_keys;                             |
6182        if (kh_is_map && val_size) {                    |
6183          ^char *new_vals = {10:krealloc}( h->vals_buf, new_n_|
6184      buckets * val_size);                              |
6185      {100:^^ REVIEW:}{18: new_vals variable seems unnecessary?}   |
6186          h->vals_buf = new_vals;                       |
6187                                                        |
6188    ]],
6189    }
6190
6191    api.nvim_buf_clear_namespace(0, ns, 0, -1)
6192    screen:expect {
6193      grid = [[
6194      if (h->n_buckets < new_n_buckets) { // expand     |
6195        khkey_t *new_keys = (khkey_t *)                 |
6196      {10:krealloc}((void *)h->keys, new_n_buckets * sizeof(k|
6197      hkey_t));                                         |
6198        h->keys = new_keys;                             |
6199        if (kh_is_map && val_size) {                    |
6200          ^char *new_vals = {10:krealloc}( h->vals_buf, new_n_|
6201      buckets * val_size);                              |
6202          h->vals_buf = new_vals;                       |
6203        }                                               |
6204      }                                                 |
6205                                                        |
6206    ]],
6207    }
6208  end)
6209
6210  it('works with text at the beginning of the buffer', function()
6211    insert(example_text2)
6212    feed 'gg'
6213
6214    screen:expect {
6215      grid = [[
6216      ^if (h->n_buckets < new_n_buckets) { // expand     |
6217        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6218      h->keys, new_n_buckets * sizeof(khkey_t));        |
6219        h->keys = new_keys;                             |
6220        if (kh_is_map && val_size) {                    |
6221          char *new_vals = krealloc( h->vals_buf, new_n_|
6222      buckets * val_size);                              |
6223          h->vals_buf = new_vals;                       |
6224        }                                               |
6225      }                                                 |
6226      {1:~                                                 }|
6227                                                        |
6228    ]],
6229    }
6230
6231    api.nvim_buf_set_extmark(0, ns, 0, 0, {
6232      virt_lines = {
6233        { { 'refactor(khash): ', 'Special' }, { 'take size of values as parameter' } },
6234        { { 'Author: Dev Devsson, ' }, { 'Tue Aug 31 10:13:37 2021', 'Comment' } },
6235      },
6236      virt_lines_above = true,
6237      right_gravity = false,
6238    })
6239
6240    -- placing virt_text on topline does not automatically cause a scroll
6241    screen:expect {
6242      grid = [[
6243      ^if (h->n_buckets < new_n_buckets) { // expand     |
6244        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6245      h->keys, new_n_buckets * sizeof(khkey_t));        |
6246        h->keys = new_keys;                             |
6247        if (kh_is_map && val_size) {                    |
6248          char *new_vals = krealloc( h->vals_buf, new_n_|
6249      buckets * val_size);                              |
6250          h->vals_buf = new_vals;                       |
6251        }                                               |
6252      }                                                 |
6253      {1:~                                                 }|
6254                                                        |
6255    ]],
6256      unchanged = true,
6257    }
6258
6259    feed '<c-b>'
6260    screen:expect {
6261      grid = [[
6262      {16:refactor(khash): }take size of values as parameter |
6263      Author: Dev Devsson, {18:Tue Aug 31 10:13:37 2021}     |
6264      if (h->n_buckets < new_n_buckets) { // expand     |
6265        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6266      h->keys, new_n_buckets * sizeof(khkey_t));        |
6267        h->keys = new_keys;                             |
6268        if (kh_is_map && val_size) {                    |
6269          char *new_vals = krealloc( h->vals_buf, new_n_|
6270      buckets * val_size);                              |
6271          h->vals_buf = new_vals;                       |
6272        ^}                                               |
6273                                                        |
6274    ]],
6275    }
6276  end)
6277
6278  it('works with text at the end of the buffer', function()
6279    insert(example_text2)
6280    feed 'G'
6281
6282    screen:expect {
6283      grid = [[
6284      if (h->n_buckets < new_n_buckets) { // expand     |
6285        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6286      h->keys, new_n_buckets * sizeof(khkey_t));        |
6287        h->keys = new_keys;                             |
6288        if (kh_is_map && val_size) {                    |
6289          char *new_vals = krealloc( h->vals_buf, new_n_|
6290      buckets * val_size);                              |
6291          h->vals_buf = new_vals;                       |
6292        }                                               |
6293      ^}                                                 |
6294      {1:~                                                 }|
6295                                                        |
6296    ]],
6297    }
6298
6299    local id = api.nvim_buf_set_extmark(0, ns, 7, 0, {
6300      virt_lines = { { { 'Grugg' } } },
6301      right_gravity = false,
6302    })
6303
6304    screen:expect {
6305      grid = [[
6306      if (h->n_buckets < new_n_buckets) { // expand     |
6307        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6308      h->keys, new_n_buckets * sizeof(khkey_t));        |
6309        h->keys = new_keys;                             |
6310        if (kh_is_map && val_size) {                    |
6311          char *new_vals = krealloc( h->vals_buf, new_n_|
6312      buckets * val_size);                              |
6313          h->vals_buf = new_vals;                       |
6314        }                                               |
6315      ^}                                                 |
6316      Grugg                                             |
6317                                                        |
6318    ]],
6319    }
6320
6321    screen:try_resize(50, 11)
6322    feed('gg')
6323    screen:expect {
6324      grid = [[
6325      ^if (h->n_buckets < new_n_buckets) { // expand     |
6326        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6327      h->keys, new_n_buckets * sizeof(khkey_t));        |
6328        h->keys = new_keys;                             |
6329        if (kh_is_map && val_size) {                    |
6330          char *new_vals = krealloc( h->vals_buf, new_n_|
6331      buckets * val_size);                              |
6332          h->vals_buf = new_vals;                       |
6333        }                                               |
6334      }                                                 |
6335                                                        |
6336    ]],
6337    }
6338
6339    feed('G<C-E>')
6340    screen:expect {
6341      grid = [[
6342        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6343      h->keys, new_n_buckets * sizeof(khkey_t));        |
6344        h->keys = new_keys;                             |
6345        if (kh_is_map && val_size) {                    |
6346          char *new_vals = krealloc( h->vals_buf, new_n_|
6347      buckets * val_size);                              |
6348          h->vals_buf = new_vals;                       |
6349        }                                               |
6350      ^}                                                 |
6351      Grugg                                             |
6352                                                        |
6353    ]],
6354    }
6355
6356    feed('gg')
6357    screen:expect {
6358      grid = [[
6359      ^if (h->n_buckets < new_n_buckets) { // expand     |
6360        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6361      h->keys, new_n_buckets * sizeof(khkey_t));        |
6362        h->keys = new_keys;                             |
6363        if (kh_is_map && val_size) {                    |
6364          char *new_vals = krealloc( h->vals_buf, new_n_|
6365      buckets * val_size);                              |
6366          h->vals_buf = new_vals;                       |
6367        }                                               |
6368      }                                                 |
6369                                                        |
6370    ]],
6371    }
6372
6373    screen:try_resize(50, 12)
6374    feed('G')
6375    screen:expect {
6376      grid = [[
6377      if (h->n_buckets < new_n_buckets) { // expand     |
6378        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6379      h->keys, new_n_buckets * sizeof(khkey_t));        |
6380        h->keys = new_keys;                             |
6381        if (kh_is_map && val_size) {                    |
6382          char *new_vals = krealloc( h->vals_buf, new_n_|
6383      buckets * val_size);                              |
6384          h->vals_buf = new_vals;                       |
6385        }                                               |
6386      ^}                                                 |
6387      Grugg                                             |
6388                                                        |
6389    ]],
6390    }
6391
6392    api.nvim_buf_del_extmark(0, ns, id)
6393    screen:expect {
6394      grid = [[
6395      if (h->n_buckets < new_n_buckets) { // expand     |
6396        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6397      h->keys, new_n_buckets * sizeof(khkey_t));        |
6398        h->keys = new_keys;                             |
6399        if (kh_is_map && val_size) {                    |
6400          char *new_vals = krealloc( h->vals_buf, new_n_|
6401      buckets * val_size);                              |
6402          h->vals_buf = new_vals;                       |
6403        }                                               |
6404      ^}                                                 |
6405      {1:~                                                 }|
6406                                                        |
6407    ]],
6408    }
6409  end)
6410
6411  it('works beyond end of the buffer with virt_lines_above', function()
6412    insert(example_text2)
6413    feed 'G'
6414
6415    screen:expect {
6416      grid = [[
6417      if (h->n_buckets < new_n_buckets) { // expand     |
6418        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6419      h->keys, new_n_buckets * sizeof(khkey_t));        |
6420        h->keys = new_keys;                             |
6421        if (kh_is_map && val_size) {                    |
6422          char *new_vals = krealloc( h->vals_buf, new_n_|
6423      buckets * val_size);                              |
6424          h->vals_buf = new_vals;                       |
6425        }                                               |
6426      ^}                                                 |
6427      {1:~                                                 }|
6428                                                        |
6429    ]],
6430    }
6431
6432    local id = api.nvim_buf_set_extmark(0, ns, 8, 0, {
6433      virt_lines = { { { 'Grugg' } } },
6434      virt_lines_above = true,
6435    })
6436
6437    screen:expect {
6438      grid = [[
6439      if (h->n_buckets < new_n_buckets) { // expand     |
6440        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6441      h->keys, new_n_buckets * sizeof(khkey_t));        |
6442        h->keys = new_keys;                             |
6443        if (kh_is_map && val_size) {                    |
6444          char *new_vals = krealloc( h->vals_buf, new_n_|
6445      buckets * val_size);                              |
6446          h->vals_buf = new_vals;                       |
6447        }                                               |
6448      ^}                                                 |
6449      Grugg                                             |
6450                                                        |
6451    ]],
6452    }
6453
6454    feed('dd')
6455    screen:expect {
6456      grid = [[
6457      if (h->n_buckets < new_n_buckets) { // expand     |
6458        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6459      h->keys, new_n_buckets * sizeof(khkey_t));        |
6460        h->keys = new_keys;                             |
6461        if (kh_is_map && val_size) {                    |
6462          char *new_vals = krealloc( h->vals_buf, new_n_|
6463      buckets * val_size);                              |
6464          h->vals_buf = new_vals;                       |
6465        ^}                                               |
6466      Grugg                                             |
6467      {1:~                                                 }|
6468                                                        |
6469    ]],
6470    }
6471
6472    feed('dk')
6473    screen:expect {
6474      grid = [[
6475      if (h->n_buckets < new_n_buckets) { // expand     |
6476        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6477      h->keys, new_n_buckets * sizeof(khkey_t));        |
6478        h->keys = new_keys;                             |
6479        if (kh_is_map && val_size) {                    |
6480          ^char *new_vals = krealloc( h->vals_buf, new_n_|
6481      buckets * val_size);                              |
6482      Grugg                                             |
6483      {1:~                                                 }|*3
6484                                                        |
6485    ]],
6486    }
6487
6488    feed('dgg')
6489    screen:expect {
6490      grid = [[
6491      ^                                                  |
6492      Grugg                                             |
6493      {1:~                                                 }|*9
6494      --No lines in buffer--                            |
6495    ]],
6496    }
6497
6498    api.nvim_buf_del_extmark(0, ns, id)
6499    screen:expect {
6500      grid = [[
6501      ^                                                  |
6502      {1:~                                                 }|*10
6503      --No lines in buffer--                            |
6504    ]],
6505    }
6506  end)
6507
6508  it('does not cause syntax ml_get error at the end of a buffer #17816', function()
6509    command([[syntax region foo keepend start='^foo' end='^$']])
6510    command('syntax sync minlines=100')
6511    insert('foo')
6512    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_lines = { { { 'bar', 'Comment' } } } })
6513    screen:expect([[
6514      fo^o                                               |
6515      {18:bar}                                               |
6516      {1:~                                                 }|*9
6517                                                        |
6518    ]])
6519  end)
6520
6521  it('works with a block scrolling up', function()
6522    screen:try_resize(30, 7)
6523    insert('aa\nbb\ncc\ndd\nee\nff\ngg\nhh')
6524    feed 'gg'
6525
6526    api.nvim_buf_set_extmark(0, ns, 6, 0, {
6527      virt_lines = {
6528        { { 'they see me' } },
6529        { { 'scrolling', 'Special' } },
6530        { { 'they' } },
6531        { { "hatin'", 'Special' } },
6532      },
6533    })
6534
6535    screen:expect {
6536      grid = [[
6537      ^aa                            |
6538      bb                            |
6539      cc                            |
6540      dd                            |
6541      ee                            |
6542      ff                            |
6543                                    |
6544    ]],
6545    }
6546
6547    feed '<c-e>'
6548    screen:expect {
6549      grid = [[
6550      ^bb                            |
6551      cc                            |
6552      dd                            |
6553      ee                            |
6554      ff                            |
6555      gg                            |
6556                                    |
6557    ]],
6558    }
6559
6560    feed '<c-e>'
6561    screen:expect {
6562      grid = [[
6563      ^cc                            |
6564      dd                            |
6565      ee                            |
6566      ff                            |
6567      gg                            |
6568      they see me                   |
6569                                    |
6570    ]],
6571    }
6572
6573    feed '<c-e>'
6574    screen:expect {
6575      grid = [[
6576      ^dd                            |
6577      ee                            |
6578      ff                            |
6579      gg                            |
6580      they see me                   |
6581      {16:scrolling}                     |
6582                                    |
6583    ]],
6584    }
6585
6586    feed '<c-e>'
6587    screen:expect {
6588      grid = [[
6589      ^ee                            |
6590      ff                            |
6591      gg                            |
6592      they see me                   |
6593      {16:scrolling}                     |
6594      they                          |
6595                                    |
6596    ]],
6597    }
6598
6599    feed '<c-e>'
6600    screen:expect {
6601      grid = [[
6602      ^ff                            |
6603      gg                            |
6604      they see me                   |
6605      {16:scrolling}                     |
6606      they                          |
6607      {16:hatin'}                        |
6608                                    |
6609    ]],
6610    }
6611
6612    feed '<c-e>'
6613    screen:expect {
6614      grid = [[
6615      ^gg                            |
6616      they see me                   |
6617      {16:scrolling}                     |
6618      they                          |
6619      {16:hatin'}                        |
6620      hh                            |
6621                                    |
6622    ]],
6623    }
6624
6625    feed '<c-e>'
6626    screen:expect {
6627      grid = [[
6628      they see me                   |
6629      {16:scrolling}                     |
6630      they                          |
6631      {16:hatin'}                        |
6632      ^hh                            |
6633      {1:~                             }|
6634                                    |
6635    ]],
6636    }
6637
6638    feed '<c-e>'
6639    screen:expect {
6640      grid = [[
6641      {16:scrolling}                     |
6642      they                          |
6643      {16:hatin'}                        |
6644      ^hh                            |
6645      {1:~                             }|*2
6646                                    |
6647    ]],
6648    }
6649
6650    feed '<c-e>'
6651    screen:expect {
6652      grid = [[
6653      they                          |
6654      {16:hatin'}                        |
6655      ^hh                            |
6656      {1:~                             }|*3
6657                                    |
6658    ]],
6659    }
6660
6661    feed '<c-e>'
6662    screen:expect {
6663      grid = [[
6664      {16:hatin'}                        |
6665      ^hh                            |
6666      {1:~                             }|*4
6667                                    |
6668    ]],
6669    }
6670
6671    feed '<c-e>'
6672    screen:expect {
6673      grid = [[
6674      ^hh                            |
6675      {1:~                             }|*5
6676                                    |
6677    ]],
6678    }
6679  end)
6680
6681  it('works with sign and numbercolumns', function()
6682    insert(example_text2)
6683    feed 'gg'
6684    command 'set number signcolumn=yes'
6685    screen:expect([[
6686      {7:  }{8:  1 }^if (h->n_buckets < new_n_buckets) { // expan|
6687      {7:  }{8:    }d                                           |
6688      {7:  }{8:  2 }  khkey_t *new_keys = (khkey_t *)krealloc((v|
6689      {7:  }{8:    }oid *)h->keys, new_n_buckets * sizeof(khkey_|
6690      {7:  }{8:    }t));                                        |
6691      {7:  }{8:  3 }  h->keys = new_keys;                       |
6692      {7:  }{8:  4 }  if (kh_is_map && val_size) {              |
6693      {7:  }{8:  5 }    char *new_vals = krealloc( h->vals_buf, |
6694      {7:  }{8:    }new_n_buckets * val_size);                  |
6695      {7:  }{8:  6 }    h->vals_buf = new_vals;                 |
6696      {7:  }{8:  7 }  }                                         |
6697                                                        |
6698    ]])
6699
6700    local markid = api.nvim_buf_set_extmark(0, ns, 2, 0, {
6701      virt_lines = {
6702        { { 'Some special', 'Special' } },
6703        { { 'remark about codes', 'Comment' } },
6704      },
6705    })
6706
6707    screen:expect([[
6708      {7:  }{8:  1 }^if (h->n_buckets < new_n_buckets) { // expan|
6709      {7:  }{8:    }d                                           |
6710      {7:  }{8:  2 }  khkey_t *new_keys = (khkey_t *)krealloc((v|
6711      {7:  }{8:    }oid *)h->keys, new_n_buckets * sizeof(khkey_|
6712      {7:  }{8:    }t));                                        |
6713      {7:  }{8:  3 }  h->keys = new_keys;                       |
6714      {7:  }{8:    }{16:Some special}                                |
6715      {7:  }{8:    }{18:remark about codes}                          |
6716      {7:  }{8:  4 }  if (kh_is_map && val_size) {              |
6717      {7:  }{8:  5 }    char *new_vals = krealloc( h->vals_buf, |
6718      {7:  }{8:    }new_n_buckets * val_size);                  |
6719                                                        |
6720    ]])
6721
6722    api.nvim_buf_set_extmark(0, ns, 2, 0, {
6723      virt_lines = {
6724        { { 'Some special', 'Special' } },
6725        { { 'remark about codes', 'Comment' } },
6726      },
6727      virt_lines_leftcol = true,
6728      id = markid,
6729    })
6730    screen:expect([[
6731      {7:  }{8:  1 }^if (h->n_buckets < new_n_buckets) { // expan|
6732      {7:  }{8:    }d                                           |
6733      {7:  }{8:  2 }  khkey_t *new_keys = (khkey_t *)krealloc((v|
6734      {7:  }{8:    }oid *)h->keys, new_n_buckets * sizeof(khkey_|
6735      {7:  }{8:    }t));                                        |
6736      {7:  }{8:  3 }  h->keys = new_keys;                       |
6737      {16:Some special}                                      |
6738      {18:remark about codes}                                |
6739      {7:  }{8:  4 }  if (kh_is_map && val_size) {              |
6740      {7:  }{8:  5 }    char *new_vals = krealloc( h->vals_buf, |
6741      {7:  }{8:    }new_n_buckets * val_size);                  |
6742                                                        |
6743    ]])
6744
6745    command('set nonumber relativenumber')
6746    screen:expect([[
6747      {7:  }{8:  0 }^if (h->n_buckets < new_n_buckets) { // expan|
6748      {7:  }{8:    }d                                           |
6749      {7:  }{8:  1 }  khkey_t *new_keys = (khkey_t *)krealloc((v|
6750      {7:  }{8:    }oid *)h->keys, new_n_buckets * sizeof(khkey_|
6751      {7:  }{8:    }t));                                        |
6752      {7:  }{8:  2 }  h->keys = new_keys;                       |
6753      {16:Some special}                                      |
6754      {18:remark about codes}                                |
6755      {7:  }{8:  3 }  if (kh_is_map && val_size) {              |
6756      {7:  }{8:  4 }    char *new_vals = krealloc( h->vals_buf, |
6757      {7:  }{8:    }new_n_buckets * val_size);                  |
6758                                                        |
6759    ]])
6760
6761    -- 'relativenumber' is redrawn with virt_lines_leftcol #34649
6762    feed('j')
6763    screen:expect([[
6764      {7:  }{8:  1 }if (h->n_buckets < new_n_buckets) { // expan|
6765      {7:  }{8:    }d                                           |
6766      {7:  }{8:  0 }^  khkey_t *new_keys = (khkey_t *)krealloc((v|
6767      {7:  }{8:    }oid *)h->keys, new_n_buckets * sizeof(khkey_|
6768      {7:  }{8:    }t));                                        |
6769      {7:  }{8:  1 }  h->keys = new_keys;                       |
6770      {16:Some special}                                      |
6771      {18:remark about codes}                                |
6772      {7:  }{8:  2 }  if (kh_is_map && val_size) {              |
6773      {7:  }{8:  3 }    char *new_vals = krealloc( h->vals_buf, |
6774      {7:  }{8:    }new_n_buckets * val_size);                  |
6775                                                        |
6776    ]])
6777  end)
6778
6779  it('works with hard TABs', function()
6780    insert(example_text2)
6781    feed 'gg'
6782    api.nvim_buf_set_extmark(0, ns, 1, 0, {
6783      virt_lines = { { { '>>', 'NonText' }, { '\tvery\ttabby', 'Identifier' }, { 'text\twith\ttabs' } } },
6784    })
6785    screen:expect {
6786      grid = [[
6787      ^if (h->n_buckets < new_n_buckets) { // expand     |
6788        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6789      h->keys, new_n_buckets * sizeof(khkey_t));        |
6790      {1:>>}{25:      very    tabby}text       with    tabs      |
6791        h->keys = new_keys;                             |
6792        if (kh_is_map && val_size) {                    |
6793          char *new_vals = krealloc( h->vals_buf, new_n_|
6794      buckets * val_size);                              |
6795          h->vals_buf = new_vals;                       |
6796        }                                               |
6797      }                                                 |
6798                                                        |
6799    ]],
6800    }
6801
6802    command 'set tabstop=4'
6803    screen:expect {
6804      grid = [[
6805      ^if (h->n_buckets < new_n_buckets) { // expand     |
6806        khkey_t *new_keys = (khkey_t *)krealloc((void *)|
6807      h->keys, new_n_buckets * sizeof(khkey_t));        |
6808      {1:>>}{25:  very    tabby}text   with    tabs              |
6809        h->keys = new_keys;                             |
6810        if (kh_is_map && val_size) {                    |
6811          char *new_vals = krealloc( h->vals_buf, new_n_|
6812      buckets * val_size);                              |
6813          h->vals_buf = new_vals;                       |
6814        }                                               |
6815      }                                                 |
6816                                                        |
6817    ]],
6818    }
6819
6820    command 'set number'
6821    screen:expect {
6822      grid = [[
6823      {8:  1 }^if (h->n_buckets < new_n_buckets) { // expand |
6824      {8:  2 }  khkey_t *new_keys = (khkey_t *)krealloc((voi|
6825      {8:    }d *)h->keys, new_n_buckets * sizeof(khkey_t));|
6826      {8:    }{1:>>}{25:  very    tabby}text   with    tabs          |
6827      {8:  3 }  h->keys = new_keys;                         |
6828      {8:  4 }  if (kh_is_map && val_size) {                |
6829      {8:  5 }    char *new_vals = krealloc( h->vals_buf, ne|
6830      {8:    }w_n_buckets * val_size);                      |
6831      {8:  6 }    h->vals_buf = new_vals;                   |
6832      {8:  7 }  }                                           |
6833      {8:  8 }}                                             |
6834                                                        |
6835    ]],
6836    }
6837
6838    command 'set tabstop&'
6839    screen:expect {
6840      grid = [[
6841      {8:  1 }^if (h->n_buckets < new_n_buckets) { // expand |
6842      {8:  2 }  khkey_t *new_keys = (khkey_t *)krealloc((voi|
6843      {8:    }d *)h->keys, new_n_buckets * sizeof(khkey_t));|
6844      {8:    }{1:>>}{25:      very    tabby}text       with    tabs  |
6845      {8:  3 }  h->keys = new_keys;                         |
6846      {8:  4 }  if (kh_is_map && val_size) {                |
6847      {8:  5 }    char *new_vals = krealloc( h->vals_buf, ne|
6848      {8:    }w_n_buckets * val_size);                      |
6849      {8:  6 }    h->vals_buf = new_vals;                   |
6850      {8:  7 }  }                                           |
6851      {8:  8 }}                                             |
6852                                                        |
6853    ]],
6854    }
6855  end)
6856
6857  it('scrolls horizontally with virt_lines_overflow=scroll #31000', function()
6858    command('set nowrap signcolumn=yes')
6859    insert('abcdefghijklmnopqrstuvwxyz')
6860    api.nvim_buf_set_extmark(0, ns, 0, 0, {
6861      virt_lines = {
6862        { { '12α口β̳γ̲=', 'Special' }, { '❤️345678', 'Special' } },
6863        { { '123\t45\t678', 'NonText' } },
6864      },
6865      virt_lines_overflow = 'scroll',
6866    })
6867    screen:expect([[
6868      {7:  }abcdefghijklmnopqrstuvwxy^z                      |
6869      {7:  }{16:12α口β̳γ̲=❤️345678}                                |
6870      {7:  }{1:123     45      678}                             |
6871      {1:~                                                 }|*8
6872                                                        |
6873    ]])
6874    feed('zl')
6875    screen:expect([[
6876      {7:  }bcdefghijklmnopqrstuvwxy^z                       |
6877      {7:  }{16:2α口β̳γ̲=❤️345678}                                 |
6878      {7:  }{1:23     45      678}                              |
6879      {1:~                                                 }|*8
6880                                                        |
6881    ]])
6882    feed('zl')
6883    screen:expect([[
6884      {7:  }cdefghijklmnopqrstuvwxy^z                        |
6885      {7:  }{16:α口β̳γ̲=❤️345678}                                  |
6886      {7:  }{1:3     45      678}                               |
6887      {1:~                                                 }|*8
6888                                                        |
6889    ]])
6890    feed('zl')
6891    screen:expect([[
6892      {7:  }defghijklmnopqrstuvwxy^z                         |
6893      {7:  }{16:口β̳γ̲=❤️345678}                                   |
6894      {7:  }{1:     45      678}                                |
6895      {1:~                                                 }|*8
6896                                                        |
6897    ]])
6898    feed('zl')
6899    screen:expect([[
6900      {7:  }efghijklmnopqrstuvwxy^z                          |
6901      {7:  }{16: β̳γ̲=❤️345678}                                    |
6902      {7:  }{1:    45      678}                                 |
6903      {1:~                                                 }|*8
6904                                                        |
6905    ]])
6906    feed('zl')
6907    screen:expect([[
6908      {7:  }fghijklmnopqrstuvwxy^z                           |
6909      {7:  }{16:β̳γ̲=❤️345678}                                     |
6910      {7:  }{1:   45      678}                                  |
6911      {1:~                                                 }|*8
6912                                                        |
6913    ]])
6914    feed('zl')
6915    screen:expect([[
6916      {7:  }ghijklmnopqrstuvwxy^z                            |
6917      {7:  }{16:γ̲=❤️345678}                                      |
6918      {7:  }{1:  45      678}                                   |
6919      {1:~                                                 }|*8
6920                                                        |
6921    ]])
6922    feed('zl')
6923    screen:expect([[
6924      {7:  }hijklmnopqrstuvwxy^z                             |
6925      {7:  }{16:=❤️345678}                                       |
6926      {7:  }{1: 45      678}                                    |
6927      {1:~                                                 }|*8
6928                                                        |
6929    ]])
6930    feed('zl')
6931    screen:expect([[
6932      {7:  }ijklmnopqrstuvwxy^z                              |
6933      {7:  }{16:❤️345678}                                        |
6934      {7:  }{1:45      678}                                     |
6935      {1:~                                                 }|*8
6936                                                        |
6937    ]])
6938    feed('zl')
6939    screen:expect([[
6940      {7:  }jklmnopqrstuvwxy^z                               |
6941      {7:  }{16: 345678}                                         |
6942      {7:  }{1:5      678}                                      |
6943      {1:~                                                 }|*8
6944                                                        |
6945    ]])
6946    feed('zl')
6947    screen:expect([[
6948      {7:  }klmnopqrstuvwxy^z                                |
6949      {7:  }{16:345678}                                          |
6950      {7:  }{1:      678}                                       |
6951      {1:~                                                 }|*8
6952                                                        |
6953    ]])
6954    feed('zl')
6955    screen:expect([[
6956      {7:  }lmnopqrstuvwxy^z                                 |
6957      {7:  }{16:45678}                                           |
6958      {7:  }{1:     678}                                        |
6959      {1:~                                                 }|*8
6960                                                        |
6961    ]])
6962    feed('zl')
6963    screen:expect([[
6964      {7:  }mnopqrstuvwxy^z                                  |
6965      {7:  }{16:5678}                                            |
6966      {7:  }{1:    678}                                         |
6967      {1:~                                                 }|*8
6968                                                        |
6969    ]])
6970    api.nvim_buf_set_extmark(0, ns, 0, 1, {
6971      virt_lines = { { { '123\t45\t67', 'NonText' } } },
6972      virt_lines_leftcol = true,
6973      virt_lines_overflow = 'trunc',
6974    })
6975    api.nvim_buf_set_extmark(0, ns, 0, 2, {
6976      virt_lines = { { { '123\t45\t6', 'NonText' } } },
6977      virt_lines_leftcol = false,
6978      virt_lines_overflow = 'trunc',
6979    })
6980    screen:expect([[
6981      {7:  }mnopqrstuvwxy^z                                  |
6982      {7:  }{16:5678}                                            |
6983      {7:  }{1:    678}                                         |
6984      {1:123     45      67}                                |
6985      {7:  }{1:123     45      6}                               |
6986      {1:~                                                 }|*6
6987                                                        |
6988    ]])
6989  end)
6990
6991  it('virt_lines_overflow=wrap', function()
6992    command('set wrap signcolumn=yes')
6993    insert('line1\nline2\nline3\n')
6994    api.nvim_buf_set_extmark(0, ns, 0, 0, {
6995      virt_lines = {
6996        {
6997          {
6998            string.rep(
6999              'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean non felis dapibus, semper sapien vitae, fringilla orci.',
7000              5
7001            ),
7002            'Special',
7003          },
7004        },
7005      },
7006      virt_lines_overflow = 'wrap',
7007    })
7008    screen:expect([[
7009      {7:  }{16:rem ipsum dolor sit amet, consectetur adipiscing}|
7010      {7:  }{16: elit. Aenean non felis dapibus, semper sapien v}|
7011      {7:  }{16:itae, fringilla orci.Lorem ipsum dolor sit amet,}|
7012      {7:  }{16: consectetur adipiscing elit. Aenean non felis d}|
7013      {7:  }{16:apibus, semper sapien vitae, fringilla orci.Lore}|
7014      {7:  }{16:m ipsum dolor sit amet, consectetur adipiscing e}|
7015      {7:  }{16:lit. Aenean non felis dapibus, semper sapien vit}|
7016      {7:  }{16:ae, fringilla orci.}                             |
7017      {7:  }line2                                           |
7018      {7:  }line3                                           |
7019      {7:  }^                                                |
7020                                                        |
7021    ]])
7022    feed('<C-e>')
7023    screen:expect([[
7024      {7:  }{16: elit. Aenean non felis dapibus, semper sapien v}|
7025      {7:  }{16:itae, fringilla orci.Lorem ipsum dolor sit amet,}|
7026      {7:  }{16: consectetur adipiscing elit. Aenean non felis d}|
7027      {7:  }{16:apibus, semper sapien vitae, fringilla orci.Lore}|
7028      {7:  }{16:m ipsum dolor sit amet, consectetur adipiscing e}|
7029      {7:  }{16:lit. Aenean non felis dapibus, semper sapien vit}|
7030      {7:  }{16:ae, fringilla orci.}                             |
7031      {7:  }line2                                           |
7032      {7:  }line3                                           |
7033      {7:  }^                                                |
7034      {1:~                                                 }|
7035                                                        |
7036    ]])
7037    feed('<C-e>')
7038    screen:expect([[
7039      {7:  }{16:itae, fringilla orci.Lorem ipsum dolor sit amet,}|
7040      {7:  }{16: consectetur adipiscing elit. Aenean non felis d}|
7041      {7:  }{16:apibus, semper sapien vitae, fringilla orci.Lore}|
7042      {7:  }{16:m ipsum dolor sit amet, consectetur adipiscing e}|
7043      {7:  }{16:lit. Aenean non felis dapibus, semper sapien vit}|
7044      {7:  }{16:ae, fringilla orci.}                             |
7045      {7:  }line2                                           |
7046      {7:  }line3                                           |
7047      {7:  }^                                                |
7048      {1:~                                                 }|*2
7049                                                        |
7050    ]])
7051  end)
7052
7053  it('virt_lines_overflow=wrap with 2 cell character', function()
7054    command('set wrap signcolumn=yes')
7055    insert('line1\nline2\nline3\n')
7056    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7057      virt_lines = {
7058        { { string.rep('12345678古', 5), 'Special' } },
7059      },
7060      virt_lines_overflow = 'wrap',
7061    })
7062    screen:expect([[
7063      {7:  }line1                                           |
7064      {7:  }{16:12345678古12345678古12345678古12345678古12345678}|
7065      {7:  }{16:古}                                              |
7066      {7:  }line2                                           |
7067      {7:  }line3                                           |
7068      {7:  }^                                                |
7069      {1:~                                                 }|*5
7070                                                        |
7071    ]])
7072    feed('<C-e>')
7073
7074    screen:expect([[
7075      {7:  }{16:12345678古12345678古12345678古12345678古12345678}|
7076      {7:  }{16:古}                                              |
7077      {7:  }line2                                           |
7078      {7:  }line3                                           |
7079      {7:  }^                                                |
7080      {1:~                                                 }|*6
7081                                                        |
7082    ]])
7083
7084    feed('<C-e>')
7085    screen:expect([[
7086      {7:  }{16:古}                                              |
7087      {7:  }line2                                           |
7088      {7:  }line3                                           |
7089      {7:  }^                                                |
7090      {1:~                                                 }|*7
7091                                                        |
7092    ]])
7093  end)
7094
7095  it('virt_lines_overflow=wrap with tabs', function()
7096    command('set wrap signcolumn=yes')
7097    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7098      virt_lines = {
7099        { { string.rep('1234\t\t\t5\t6\t78古', 10), 'Special' } },
7100      },
7101      virt_lines_overflow = 'wrap',
7102    })
7103    screen:expect([[
7104      {7:  }^                                                |
7105      {7:  }{16:1234                    5       6       78古1234}|
7106      {7:  }{16:                        5       6       78古1234}|*8
7107      {7:  }{16:                        5       6       78古}    |
7108                                                        |
7109    ]])
7110  end)
7111
7112  it('virt_lines_overflow=wrap with 2 cell character at the boundary', function()
7113    insert('x')
7114    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7115      virt_lines = { { { string.rep('a', 49) .. string.rep('古', 4), 'Special' } } },
7116      virt_lines_overflow = 'wrap',
7117    })
7118    screen:expect([[
7119      ^x                                                 |
7120      {16:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa }|
7121      {16:古古古古}                                          |
7122      {1:~                                                 }|*8
7123                                                        |
7124    ]])
7125    api.nvim_buf_clear_namespace(0, ns, 0, -1)
7126    screen:try_resize(13, 10)
7127    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7128      virt_lines = {
7129        { { 'AB', 'Special' } },
7130        { { string.rep('古', 13), 'Special' } },
7131      },
7132      virt_lines_overflow = 'wrap',
7133    })
7134    screen:expect([[
7135      ^x            |
7136      {16:AB}           |
7137      {16:古古古古古古 }|*2
7138      {16:古}           |
7139      {1:~            }|*4
7140                   |
7141    ]])
7142  end)
7143
7144  it('virt_lines_overflow=wrap with nowrap', function()
7145    command('set nowrap signcolumn=yes')
7146    insert(string.rep('abcdefghij', 6) .. 'line1\nline2\nline3\n')
7147    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7148      virt_lines = {
7149        { { string.rep('12345678古', 5), 'Special' } },
7150      },
7151      virt_lines_overflow = 'wrap',
7152    })
7153    feed('ggzlzl')
7154    screen:expect([[
7155      {7:  }^cdefghijabcdefghijabcdefghijabcdefghijabcdefghij|
7156      {7:  }{16:12345678古12345678古12345678古12345678古12345678}|
7157      {7:  }{16:古}                                              |
7158      {7:  }ne2                                             |
7159      {7:  }ne3                                             |
7160      {7:  }                                                |
7161      {1:~                                                 }|*5
7162                                                        |
7163    ]])
7164  end)
7165
7166  it('virt_lines_overflow=auto with wrap', function()
7167    command('set wrap signcolumn=yes')
7168    insert('line1\nline2\nline3\n')
7169    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7170      virt_lines = {
7171        { { string.rep('Thequickbrownfoxjumpsoverthelazydog. The quick brown fox jumps over the lazy dog', 20), 'Special' } },
7172      },
7173      virt_lines_overflow = 'auto',
7174    })
7175    screen:expect([[
7176      {7:  }{16:rown fox jumps over the lazy dogThequickbrownfox}|
7177      {7:  }{16:jumpsoverthelazydog. The quick brown fox jumps o}|
7178      {7:  }{16:ver the lazy dogThequickbrownfoxjumpsoverthelazy}|
7179      {7:  }{16:dog. The quick brown fox jumps over the lazy dog}|
7180      {7:  }{16:Thequickbrownfoxjumpsoverthelazydog. The quick b}|
7181      {7:  }{16:rown fox jumps over the lazy dogThequickbrownfox}|
7182      {7:  }{16:jumpsoverthelazydog. The quick brown fox jumps o}|
7183      {7:  }{16:ver the lazy dog}                                |
7184      {7:  }line2                                           |
7185      {7:  }line3                                           |
7186      {7:  }^                                                |
7187                                                        |
7188    ]])
7189  end)
7190
7191  it('virt_lines_overflow=auto and nowrap scrolls horizontally', function()
7192    command('set nowrap signcolumn=yes')
7193    insert('abcdefghijklmnopqrstuvwxyz')
7194    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7195      virt_lines = {
7196        { { '12α口β̳γ̲=', 'Special' }, { '❤️345678', 'Special' } },
7197        { { '123\t45\t678', 'NonText' } },
7198      },
7199      virt_lines_overflow = 'auto',
7200    })
7201    screen:expect([[
7202      {7:  }abcdefghijklmnopqrstuvwxy^z                      |
7203      {7:  }{16:12α口β̳γ̲=❤️345678}                                |
7204      {7:  }{1:123     45      678}                             |
7205      {1:~                                                 }|*8
7206                                                        |
7207    ]])
7208    feed('zl')
7209    screen:expect([[
7210      {7:  }bcdefghijklmnopqrstuvwxy^z                       |
7211      {7:  }{16:2α口β̳γ̲=❤️345678}                                 |
7212      {7:  }{1:23     45      678}                              |
7213      {1:~                                                 }|*8
7214                                                        |
7215    ]])
7216  end)
7217
7218  it('virt_lines_overflow=wrap with virt_lines_above + virt_lines_leftcol', function()
7219    command('set wrap signcolumn=yes')
7220    insert('line1\nline2\nline3\n')
7221    feed('2gg')
7222    api.nvim_buf_set_extmark(0, ns, 1, 0, {
7223      virt_lines = {
7224        { { string.rep('Lorem ipsum dolor sit amet, consectetur. ', 6), 'Special' } },
7225      },
7226      virt_lines_overflow = 'wrap',
7227      virt_lines_above = true,
7228      virt_lines_leftcol = true,
7229    })
7230    screen:expect([[
7231      {7:  }line1                                           |
7232      {16:Lorem ipsum dolor sit amet, consectetur. Lorem ips}|
7233      {16:um dolor sit amet, consectetur. Lorem ipsum dolor }|
7234      {16:sit amet, consectetur. Lorem ipsum dolor sit amet,}|
7235      {16: consectetur. Lorem ipsum dolor sit amet, consecte}|
7236      {16:tur. Lorem ipsum dolor sit amet, consectetur. }    |
7237      {7:  }^line2                                           |
7238      {7:  }line3                                           |
7239      {7:  }                                                |
7240      {1:~                                                 }|*2
7241                                                        |
7242    ]])
7243  end)
7244
7245  it('virt_lines_overflow=wrap with wrapped rows for statuscolumn', function()
7246    command('set wrap statuscolumn=[%{v:lnum}]')
7247    insert('line1\nline2\nline3\n')
7248    feed('2gg')
7249    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7250      virt_lines = {
7251        { { 'BELOW ' .. string.rep('Lorem ipsum dolor sit amet, consectetur. ', 4), 'Special' } },
7252      },
7253      virt_lines_overflow = 'wrap',
7254    })
7255    api.nvim_buf_set_extmark(0, ns, 1, 0, {
7256      virt_lines = {
7257        { { 'ABOVE ' .. string.rep('Lorem ipsum dolor sit amet, consectetur. ', 4), 'Special' } },
7258      },
7259      virt_lines_overflow = 'wrap',
7260      virt_lines_above = true,
7261    })
7262    screen:expect([[
7263      {8:[1]}line1                                          |
7264      {8:[1]}{16:BELOW Lorem ipsum dolor sit amet, consectetur. }|
7265      {8:[1]}{16:Lorem ipsum dolor sit amet, consectetur. Lorem }|
7266      {8:[1]}{16:ipsum dolor sit amet, consectetur. Lorem ipsum }|
7267      {8:[1]}{16:dolor sit amet, consectetur. }                  |
7268      {8:[2]}{16:ABOVE Lorem ipsum dolor sit amet, consectetur. }|
7269      {8:[2]}{16:Lorem ipsum dolor sit amet, consectetur. Lorem }|
7270      {8:[2]}{16:ipsum dolor sit amet, consectetur. Lorem ipsum }|
7271      {8:[2]}{16:dolor sit amet, consectetur. }                  |
7272      {8:[2]}^line2                                          |
7273      {8:[3]}line3                                          |
7274                                                        |
7275    ]])
7276  end)
7277
7278  it('mixing virt_lines_overflow=wrap and virt_lines_overflow=scroll', function()
7279    command('set nowrap signcolumn=yes')
7280    insert('line1\nline2\nline3\n')
7281    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7282      virt_lines = { { { 'BELOW ' .. string.rep('a', 60), 'Special' } } },
7283      virt_lines_overflow = 'wrap',
7284    })
7285    api.nvim_buf_set_extmark(0, ns, 1, 0, {
7286      virt_lines = { { { 'ABOVE ' .. string.rep('b', 60), 'Special' } } },
7287      virt_lines_overflow = 'scroll',
7288      virt_lines_above = true,
7289    })
7290    api.nvim_buf_set_extmark(0, ns, 1, 0, {
7291      virt_lines = { { { 'BELOW ' .. string.rep('c', 60), 'Special' } } },
7292      virt_lines_overflow = 'scroll',
7293    })
7294    api.nvim_buf_set_extmark(0, ns, 2, 0, {
7295      virt_lines = { { { 'ABOVE ' .. string.rep('d', 60), 'Special' } } },
7296      virt_lines_overflow = 'wrap',
7297      virt_lines_above = true,
7298    })
7299    feed('2ggzlzl')
7300    screen:expect([[
7301      {7:  }ne1                                             |
7302      {7:  }{16:BELOW aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}|
7303      {7:  }{16:aaaaaaaaaaaaaaaaaa}                              |
7304      {7:  }{16:OVE bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb}|
7305      {7:  }^ne2                                             |
7306      {7:  }{16:LOW cccccccccccccccccccccccccccccccccccccccccccc}|
7307      {7:  }{16:ABOVE dddddddddddddddddddddddddddddddddddddddddd}|
7308      {7:  }{16:dddddddddddddddddd}                              |
7309      {7:  }ne3                                             |
7310      {7:  }                                                |
7311      {1:~                                                 }|
7312                                                        |
7313    ]])
7314  end)
7315
7316  it('highlight till eol with virt_lines_overflow=trunc', function()
7317    insert('line1')
7318    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7319      virt_lines = { { { 'VIRT LINE1', 'String' }, { '', 'Visual' } }, { { 'VIRT LINE2', 'String' } } },
7320    })
7321    screen:expect([[
7322      line^1                                             |
7323      {26:VIRT LINE1}{17:                                        }|
7324      {26:VIRT LINE2}                                        |
7325      {1:~                                                 }|*8
7326                                                        |
7327    ]])
7328  end)
7329
7330  it('highlight till eol with virt_lines_overflow=wrap', function()
7331    insert('line1')
7332    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7333      virt_lines = { { { 'VIRT LINE', 'String' }, { string.rep('-', 50), 'Visual' }, { '', 'Visual' } } },
7334      virt_lines_overflow = 'wrap',
7335    })
7336    screen:expect([[
7337      line^1                                             |
7338      {26:VIRT LINE}{17:-----------------------------------------}|
7339      {17:---------                                         }|
7340      {1:~                                                 }|*8
7341                                                        |
7342    ]])
7343  end)
7344
7345  it('highlight till eol with virt_lines_overflow=scroll', function()
7346    command('set nowrap')
7347    insert('abcdefghijklmnopqrstuvwxyz')
7348    api.nvim_buf_set_extmark(0, ns, 0, 0, {
7349      virt_lines = { { { 'VIRT LINE', 'String' }, { '', 'Visual' } } },
7350      virt_lines_overflow = 'scroll',
7351    })
7352    screen:expect([[
7353      abcdefghijklmnopqrstuvwxy^z                        |
7354      {26:VIRT LINE}{17:                                         }|
7355      {1:~                                                 }|*9
7356                                                        |
7357    ]])
7358
7359    feed('zl')
7360    screen:expect([[
7361      bcdefghijklmnopqrstuvwxy^z                         |
7362      {26:IRT LINE}{17:                                          }|
7363      {1:~                                                 }|*9
7364                                                        |
7365    ]])
7366
7367    feed('zl')
7368    screen:expect([[
7369      cdefghijklmnopqrstuvwxy^z                          |
7370      {26:RT LINE}{17:                                           }|
7371      {1:~                                                 }|*9
7372                                                        |
7373    ]])
7374
7375    feed('6zl')
7376    screen:expect([[
7377      ijklmnopqrstuvwxy^z                                |
7378      {26:E}{17:                                                 }|
7379      {1:~                                                 }|*9
7380                                                        |
7381    ]])
7382
7383    feed('zl')
7384    screen:expect([[
7385      jklmnopqrstuvwxy^z                                 |
7386      {17:                                                  }|
7387      {1:~                                                 }|*9
7388                                                        |
7389    ]])
7390  end)
7391
7392  it('does not show twice if end_row or end_col is specified #18622', function()
7393    screen:try_resize(50, 8)
7394    insert([[
7395      aaa
7396      bbb
7397      ccc
7398      ddd]])
7399    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 2, virt_lines = { { { 'VIRT LINE 1', 'NonText' } } } })
7400    api.nvim_buf_set_extmark(0, ns, 3, 0, { end_col = 2, virt_lines = { { { 'VIRT LINE 2', 'NonText' } } } })
7401    screen:expect {
7402      grid = [[
7403      aaa                                               |
7404      {1:VIRT LINE 1}                                       |
7405      bbb                                               |
7406      ccc                                               |
7407      dd^d                                               |
7408      {1:VIRT LINE 2}                                       |
7409      {1:~                                                 }|
7410                                                        |
7411    ]],
7412    }
7413  end)
7414
7415  it('works with rightleft', function()
7416    screen:try_resize(50, 8)
7417    insert([[
7418      aaa
7419      bbb
7420      ccc
7421      ddd]])
7422    command('set number rightleft')
7423    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_lines = { { { 'VIRT LINE 1', 'NonText' } } }, virt_lines_leftcol = true })
7424    api.nvim_buf_set_extmark(0, ns, 3, 0, { virt_lines = { { { 'VIRT LINE 2', 'NonText' } } } })
7425    screen:expect {
7426      grid = [[
7427                                                 aaa{8: 1  }|
7428                                             {1:1 ENIL TRIV}|
7429                                                 bbb{8: 2  }|
7430                                                 ccc{8: 3  }|
7431                                                 ^ddd{8: 4  }|
7432                                         {1:2 ENIL TRIV}{8:    }|
7433      {1:                                                 ~}|
7434                                                        |
7435    ]],
7436    }
7437  end)
7438
7439  it('works when using dd or yyp #23915 #23916', function()
7440    insert([[
7441      line1
7442      line2
7443      line3
7444      line4
7445      line5]])
7446    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_lines = { { { 'foo' } }, { { 'bar' } }, { { 'baz' } } } })
7447    screen:expect {
7448      grid = [[
7449      line1                                             |
7450      foo                                               |
7451      bar                                               |
7452      baz                                               |
7453      line2                                             |
7454      line3                                             |
7455      line4                                             |
7456      line^5                                             |
7457      {1:~                                                 }|*3
7458                                                        |
7459    ]],
7460    }
7461
7462    feed('gg')
7463    feed('yyp')
7464    screen:expect {
7465      grid = [[
7466      line1                                             |
7467      foo                                               |
7468      bar                                               |
7469      baz                                               |
7470      ^line1                                             |
7471      line2                                             |
7472      line3                                             |
7473      line4                                             |
7474      line5                                             |
7475      {1:~                                                 }|*2
7476                                                        |
7477    ]],
7478    }
7479
7480    feed('dd')
7481    screen:expect {
7482      grid = [[
7483      line1                                             |
7484      foo                                               |
7485      bar                                               |
7486      baz                                               |
7487      ^line2                                             |
7488      line3                                             |
7489      line4                                             |
7490      line5                                             |
7491      {1:~                                                 }|*3
7492                                                        |
7493    ]],
7494    }
7495
7496    feed('kdd')
7497    screen:expect([[
7498      ^line2                                             |
7499      foo                                               |
7500      bar                                               |
7501      baz                                               |
7502      line3                                             |
7503      line4                                             |
7504      line5                                             |
7505      {1:~                                                 }|*4
7506                                                        |
7507    ]])
7508  end)
7509
7510  it('does not break cursor position with concealcursor #27887', function()
7511    command('vsplit')
7512    insert('\n')
7513    api.nvim_set_option_value('conceallevel', 2, {})
7514    api.nvim_set_option_value('concealcursor', 'niv', {})
7515    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_lines = { { { 'VIRT1' } }, { { 'VIRT2' } } } })
7516    screen:expect([[
7517                               │                        |
7518      VIRT1                    │VIRT1                   |
7519      VIRT2                    │VIRT2                   |
7520      ^                         │                        |
7521      {1:~                        }│{1:~                       }|*6
7522      {3:[No Name] [+]             }{2:[No Name] [+]           }|
7523                                                        |
7524    ]])
7525  end)
7526
7527  it('works with full page scrolling #28290', function()
7528    screen:try_resize(20, 8)
7529    command('call setline(1, range(20))')
7530    api.nvim_buf_set_extmark(0, ns, 10, 0, { virt_lines = { { { 'VIRT1' } }, { { 'VIRT2' } } } })
7531    screen:expect([[
7532      ^0                   |
7533      1                   |
7534      2                   |
7535      3                   |
7536      4                   |
7537      5                   |
7538      6                   |
7539                          |
7540    ]])
7541    feed('<C-F>')
7542    screen:expect([[
7543      ^5                   |
7544      6                   |
7545      7                   |
7546      8                   |
7547      9                   |
7548      10                  |
7549      VIRT1               |
7550                          |
7551    ]])
7552    feed('<C-F>')
7553    screen:expect([[
7554      ^10                  |
7555      VIRT1               |
7556      VIRT2               |
7557      11                  |
7558      12                  |
7559      13                  |
7560      14                  |
7561                          |
7562    ]])
7563    feed('<C-F>')
7564    screen:expect([[
7565      ^13                  |
7566      14                  |
7567      15                  |
7568      16                  |
7569      17                  |
7570      18                  |
7571      19                  |
7572                          |
7573    ]])
7574    feed('<C-B>')
7575    screen:expect([[
7576      10                  |
7577      VIRT1               |
7578      VIRT2               |
7579      11                  |
7580      12                  |
7581      13                  |
7582      ^14                  |
7583                          |
7584    ]])
7585    feed('<C-B>')
7586    screen:expect([[
7587      5                   |
7588      6                   |
7589      7                   |
7590      8                   |
7591      9                   |
7592      ^10                  |
7593      VIRT1               |
7594                          |
7595    ]])
7596    feed('<C-B>')
7597    screen:expect([[
7598      0                   |
7599      1                   |
7600      2                   |
7601      3                   |
7602      4                   |
7603      5                   |
7604      ^6                   |
7605                          |
7606    ]])
7607  end)
7608
7609  it('not drawn when invalid', function()
7610    api.nvim_buf_set_lines(0, 0, -1, false, { 'foo', 'bar' })
7611    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_lines = { { { 'VIRT1' } } }, invalidate = true })
7612    screen:expect({
7613      grid = [[
7614        ^foo                                               |
7615        VIRT1                                             |
7616        bar                                               |
7617        {1:~                                                 }|*8
7618                                                          |
7619      ]],
7620    })
7621    feed('dd')
7622    screen:expect({
7623      grid = [[
7624        ^bar                                               |
7625        {1:~                                                 }|*10
7626                                                          |
7627      ]],
7628    })
7629  end)
7630
7631  it("not revealed before skipcol scrolling up with 'smoothscroll'", function()
7632    api.nvim_set_option_value('smoothscroll', true, {})
7633    api.nvim_buf_set_lines(0, 0, -1, false, { ('x'):rep(screen._width * 2) })
7634    api.nvim_buf_set_extmark(0, ns, 0, 0, { virt_lines_above = true, virt_lines = { { { 'VIRT1' } } } })
7635    feed('<C-E>')
7636    screen:expect([[
7637      {1:<<<}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx^x|
7638      {1:~                                                 }|*10
7639                                                        |
7640    ]])
7641    feed('<C-Y>')
7642    screen:expect([[
7643      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
7644      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx^x|
7645      {1:~                                                 }|*9
7646                                                        |
7647    ]])
7648    feed('<C-Y>')
7649    screen:expect([[
7650      VIRT1                                             |
7651      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|
7652      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx^x|
7653      {1:~                                                 }|*8
7654                                                        |
7655    ]])
7656  end)
7657end)
7658
7659describe('decorations: signs', function()
7660  local screen ---@type test.functional.ui.screen
7661  local ns ---@type integer
7662
7663  before_each(function()
7664    clear()
7665    screen = Screen.new(50, 10)
7666    screen:add_extra_attr_ids {
7667      [100] = { foreground = Screen.colors.Blue, background = Screen.colors.Yellow },
7668    }
7669
7670    ns = api.nvim_create_namespace 'test'
7671    api.nvim_set_option_value('signcolumn', 'auto:9', {})
7672  end)
7673
7674  local example_test3 = [[
7675l1
7676l2
7677l3
7678l4
7679l5
7680]]
7681
7682  it('can add a single sign (no end row)', function()
7683    insert(example_test3)
7684    feed 'gg'
7685
7686    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'S' })
7687
7688    screen:expect([[
7689      {7:  }^l1                                              |
7690      {7:S }l2                                              |
7691      {7:  }l3                                              |
7692      {7:  }l4                                              |
7693      {7:  }l5                                              |
7694      {7:  }                                                |
7695      {1:~                                                 }|*3
7696                                                        |
7697    ]])
7698  end)
7699
7700  it('can add a single sign (with end row)', function()
7701    insert(example_test3)
7702    feed 'gg'
7703
7704    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'S', end_row = 1 })
7705
7706    screen:expect([[
7707      {7:  }^l1                                              |
7708      {7:S }l2                                              |
7709      {7:  }l3                                              |
7710      {7:  }l4                                              |
7711      {7:  }l5                                              |
7712      {7:  }                                                |
7713      {1:~                                                 }|*3
7714                                                        |
7715    ]])
7716  end)
7717
7718  it('can add a single sign and text highlight', function()
7719    insert(example_test3)
7720    feed 'gg'
7721
7722    api.nvim_buf_set_extmark(0, ns, 1, 0, { sign_text = 'S', hl_group = 'Todo', end_col = 1 })
7723    screen:expect([[
7724      {7:  }^l1                                              |
7725      {7:S }{100:l}2                                              |
7726      {7:  }l3                                              |
7727      {7:  }l4                                              |
7728      {7:  }l5                                              |
7729      {7:  }                                                |
7730      {1:~                                                 }|*3
7731                                                        |
7732    ]])
7733
7734    api.nvim_buf_clear_namespace(0, ns, 0, -1)
7735  end)
7736
7737  it('can add multiple signs (single extmark)', function()
7738    insert(example_test3)
7739    feed 'gg'
7740
7741    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'S', end_row = 2 })
7742
7743    screen:expect([[
7744      {7:  }^l1                                              |
7745      {7:S }l2                                              |
7746      {7:S }l3                                              |
7747      {7:  }l4                                              |
7748      {7:  }l5                                              |
7749      {7:  }                                                |
7750      {1:~                                                 }|*3
7751                                                        |
7752    ]])
7753  end)
7754
7755  it('can add multiple signs (multiple extmarks)', function()
7756    insert(example_test3)
7757    feed 'gg'
7758
7759    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'S1' })
7760    api.nvim_buf_set_extmark(0, ns, 3, -1, { sign_text = 'S2', end_row = 4 })
7761
7762    screen:expect([[
7763      {7:  }^l1                                              |
7764      {7:S1}l2                                              |
7765      {7:  }l3                                              |
7766      {7:S2}l4                                              |
7767      {7:S2}l5                                              |
7768      {7:  }                                                |
7769      {1:~                                                 }|*3
7770                                                        |
7771    ]])
7772  end)
7773
7774  it('can add multiple signs (multiple extmarks) 2', function()
7775    insert(example_test3)
7776    feed 'gg'
7777
7778    api.nvim_buf_set_extmark(0, ns, 3, -1, { sign_text = 'S1' })
7779    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'S2', end_row = 3 })
7780    screen:expect([[
7781      {7:    }^l1                                            |
7782      {7:S2  }l2                                            |
7783      {7:S2  }l3                                            |
7784      {7:S2S1}l4                                            |
7785      {7:    }l5                                            |
7786      {7:    }                                              |
7787      {1:~                                                 }|*3
7788                                                        |
7789    ]])
7790  end)
7791
7792  it('can add multiple signs (multiple extmarks) 3', function()
7793    insert(example_test3)
7794    feed 'gg'
7795
7796    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'S1', end_row = 2 })
7797    api.nvim_buf_set_extmark(0, ns, 2, -1, { sign_text = 'S2', end_row = 3 })
7798
7799    screen:expect([[
7800      {7:    }^l1                                            |
7801      {7:S1  }l2                                            |
7802      {7:S2S1}l3                                            |
7803      {7:S2  }l4                                            |
7804      {7:    }l5                                            |
7805      {7:    }                                              |
7806      {1:~                                                 }|*3
7807                                                        |
7808    ]])
7809  end)
7810
7811  it('can add multiple signs (multiple extmarks) 4', function()
7812    insert(example_test3)
7813    feed 'gg'
7814
7815    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S1', end_row = 0 })
7816    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'S2', end_row = 1 })
7817
7818    screen:expect([[
7819      {7:S1}^l1                                              |
7820      {7:S2}l2                                              |
7821      {7:  }l3                                              |
7822      {7:  }l4                                              |
7823      {7:  }l5                                              |
7824      {7:  }                                                |
7825      {1:~                                                 }|*3
7826                                                        |
7827    ]])
7828  end)
7829
7830  it('works with old signs', function()
7831    insert(example_test3)
7832    feed 'gg'
7833
7834    n.command('sign define Oldsign text=x')
7835    n.command([[exe 'sign place 42 line=2 name=Oldsign buffer=' . bufnr('')]])
7836
7837    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S1' })
7838    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'S2' })
7839    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S4' })
7840    api.nvim_buf_set_extmark(0, ns, 2, -1, { sign_text = 'S5' })
7841
7842    screen:expect([[
7843      {7:S4S1}^l1                                            |
7844      {7:S2x }l2                                            |
7845      {7:S5  }l3                                            |
7846      {7:    }l4                                            |
7847      {7:    }l5                                            |
7848      {7:    }                                              |
7849      {1:~                                                 }|*3
7850                                                        |
7851    ]])
7852  end)
7853
7854  it('works with old signs (with range)', function()
7855    insert(example_test3)
7856    feed 'gg'
7857
7858    n.command('sign define Oldsign text=x')
7859    n.command([[exe 'sign place 42 line=2 name=Oldsign buffer=' . bufnr('')]])
7860
7861    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S1' })
7862    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'S2' })
7863    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S3', end_row = 4 })
7864    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S4' })
7865    api.nvim_buf_set_extmark(0, ns, 2, -1, { sign_text = 'S5' })
7866
7867    screen:expect([[
7868      {7:S4S3S1}^l1                                          |
7869      {7:S3S2x }l2                                          |
7870      {7:S5S3  }l3                                          |
7871      {7:S3    }l4                                          |
7872      {7:S3    }l5                                          |
7873      {7:      }                                            |
7874      {1:~                                                 }|*3
7875                                                        |
7876    ]])
7877  end)
7878
7879  it('can add a ranged sign (with start out of view)', function()
7880    insert(example_test3)
7881    command 'set signcolumn=yes:2'
7882    feed 'gg'
7883    feed '2<C-e>'
7884
7885    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'X', end_row = 3 })
7886
7887    screen:expect([[
7888      {7:X   }^l3                                            |
7889      {7:X   }l4                                            |
7890      {7:    }l5                                            |
7891      {7:    }                                              |
7892      {1:~                                                 }|*5
7893                                                        |
7894    ]])
7895  end)
7896
7897  it('can add lots of signs', function()
7898    screen:try_resize(40, 10)
7899    command 'normal 10oa b c d e f g h'
7900
7901    for i = 1, 10 do
7902      api.nvim_buf_set_extmark(0, ns, i, 0, { end_col = 1, hl_group = 'Todo' })
7903      api.nvim_buf_set_extmark(0, ns, i, 2, { end_col = 3, hl_group = 'Todo' })
7904      api.nvim_buf_set_extmark(0, ns, i, 4, { end_col = 5, hl_group = 'Todo' })
7905      api.nvim_buf_set_extmark(0, ns, i, 6, { end_col = 7, hl_group = 'Todo' })
7906      api.nvim_buf_set_extmark(0, ns, i, 8, { end_col = 9, hl_group = 'Todo' })
7907      api.nvim_buf_set_extmark(0, ns, i, 10, { end_col = 11, hl_group = 'Todo' })
7908      api.nvim_buf_set_extmark(0, ns, i, 12, { end_col = 13, hl_group = 'Todo' })
7909      api.nvim_buf_set_extmark(0, ns, i, 14, { end_col = 15, hl_group = 'Todo' })
7910      api.nvim_buf_set_extmark(0, ns, i, -1, { sign_text = 'W' })
7911      api.nvim_buf_set_extmark(0, ns, i, -1, { sign_text = 'X' })
7912      api.nvim_buf_set_extmark(0, ns, i, -1, { sign_text = 'Y' })
7913      api.nvim_buf_set_extmark(0, ns, i, -1, { sign_text = 'Z' })
7914    end
7915
7916    screen:expect([[
7917      {7:Z Y X W }{100:a} {100:b} {100:c} {100:d} {100:e} {100:f} {100:g} {100:h}                 |*8
7918      {7:Z Y X W }{100:a} {100:b} {100:c} {100:d} {100:e} {100:f} {100:g} {100:^h}                 |
7919                                              |
7920    ]])
7921  end)
7922
7923  it('works with priority #19716', function()
7924    screen:try_resize(20, 3)
7925    insert(example_test3)
7926    feed 'gg'
7927
7928    command('sign define Oldsign text=O3')
7929    command([[exe 'sign place 42 line=1 name=Oldsign priority=10 buffer=' . bufnr('')]])
7930
7931    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S4', priority = 100 })
7932    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S2', priority = 5 })
7933    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S5', priority = 200 })
7934    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S1', priority = 1 })
7935
7936    screen:expect([[
7937      {7:S5S4O3S2S1}^l1        |
7938      {7:          }l2        |
7939                          |
7940    ]])
7941
7942    -- Check truncation works too
7943    api.nvim_set_option_value('signcolumn', 'auto', {})
7944
7945    screen:expect([[
7946      {7:S5}^l1                |
7947      {7:  }l2                |
7948                          |
7949    ]])
7950  end)
7951
7952  it('does not overflow with many old signs #23852', function()
7953    screen:try_resize(20, 3)
7954
7955    command('set signcolumn:auto:9')
7956    command('sign define Oldsign text=O3')
7957    command([[exe 'sign place 01 line=1 name=Oldsign priority=10 buffer=' . bufnr('')]])
7958    command([[exe 'sign place 02 line=1 name=Oldsign priority=10 buffer=' . bufnr('')]])
7959    command([[exe 'sign place 03 line=1 name=Oldsign priority=10 buffer=' . bufnr('')]])
7960    command([[exe 'sign place 04 line=1 name=Oldsign priority=10 buffer=' . bufnr('')]])
7961    command([[exe 'sign place 05 line=1 name=Oldsign priority=10 buffer=' . bufnr('')]])
7962    command([[exe 'sign place 06 line=1 name=Oldsign priority=10 buffer=' . bufnr('')]])
7963    command([[exe 'sign place 07 line=1 name=Oldsign priority=10 buffer=' . bufnr('')]])
7964    command([[exe 'sign place 08 line=1 name=Oldsign priority=10 buffer=' . bufnr('')]])
7965    command([[exe 'sign place 09 line=1 name=Oldsign priority=10 buffer=' . bufnr('')]])
7966    screen:expect([[
7967      {7:O3O3O3O3O3O3O3O3O3}^  |
7968      {1:~                   }|
7969                          |
7970    ]])
7971
7972    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S1', priority = 1 })
7973    screen:expect_unchanged()
7974
7975    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S5', priority = 200 })
7976    screen:expect([[
7977      {7:S5O3O3O3O3O3O3O3O3}^  |
7978      {1:~                   }|
7979                          |
7980    ]])
7981
7982    assert_alive()
7983  end)
7984
7985  it('does not set signcolumn for signs without text', function()
7986    screen:try_resize(20, 3)
7987    api.nvim_set_option_value('signcolumn', 'auto', {})
7988    insert(example_test3)
7989    feed 'gg'
7990    api.nvim_buf_set_extmark(0, ns, 0, -1, { number_hl_group = 'Error' })
7991    screen:expect { grid = [[
7992      ^l1                  |
7993      l2                  |
7994                          |
7995    ]] }
7996  end)
7997
7998  it('correct width when removing multiple signs from sentinel line', function()
7999    screen:try_resize(20, 4)
8000    insert(example_test3)
8001    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S1', end_row = 3 })
8002    api.nvim_buf_set_extmark(0, ns, 1, -1, { invalidate = true, sign_text = 'S2' })
8003    api.nvim_buf_set_extmark(0, ns, 1, -1, { invalidate = true, sign_text = 'S3' })
8004    feed('2Gdd')
8005
8006    screen:expect([[
8007      {7:S1}l1                |
8008      {7:S1}^l3                |
8009      {7:S1}l4                |
8010                          |
8011    ]])
8012  end)
8013
8014  it('correct width with multiple overlapping signs', function()
8015    screen:try_resize(20, 4)
8016    insert(example_test3)
8017    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S1' })
8018    api.nvim_buf_set_extmark(0, ns, 0, -1, { sign_text = 'S2', end_row = 2 })
8019    api.nvim_buf_set_extmark(0, ns, 1, -1, { sign_text = 'S3', end_row = 2 })
8020    feed('gg')
8021
8022    local s1 = [[
8023      {7:S2S1}^l1              |
8024      {7:S3S2}l2              |
8025      {7:S3S2}l3              |
8026                          |
8027    ]]
8028    screen:expect(s1)
8029    -- Correct width when :move'ing a line with signs
8030    command('move2')
8031    screen:expect([[
8032      {7:S3    }l2            |
8033      {7:S3S2S1}^l1            |
8034      {7:      }l3            |
8035                          |
8036    ]])
8037    command('silent undo')
8038    screen:expect { grid = s1 }
8039    command('d')
8040    screen:expect([[
8041      {7:S3S2S1}^l2            |
8042      {7:S3S2  }l3            |
8043      {7:      }l4            |
8044                          |
8045    ]])
8046    command('d')
8047    screen:expect([[
8048      {7:S3S2S1}^l3            |
8049      {7:      }l4            |
8050      {7:      }l5            |
8051                          |
8052    ]])
8053  end)
8054
8055  it('correct width when adding and removing multiple signs', function()
8056    screen:try_resize(20, 4)
8057    insert(example_test3)
8058    feed('gg')
8059    command([[
8060      let ns = nvim_create_namespace('')
8061      call nvim_buf_set_extmark(0, ns, 0, 0, {'sign_text':'S1', 'end_row':3})
8062      let s1 = nvim_buf_set_extmark(0, ns, 2, 0, {'sign_text':'S2', 'end_row':4})
8063      let s2 = nvim_buf_set_extmark(0, ns, 5, 0, {'sign_text':'S3'})
8064      let s3 = nvim_buf_set_extmark(0, ns, 6, 0, {'sign_text':'S3'})
8065      let s4 = nvim_buf_set_extmark(0, ns, 5, 0, {'sign_text':'S3'})
8066      let s5 = nvim_buf_set_extmark(0, ns, 6, 0, {'sign_text':'S3'})
8067      redraw!
8068      call nvim_buf_del_extmark(0, ns, s2)
8069      call nvim_buf_del_extmark(0, ns, s3)
8070      call nvim_buf_del_extmark(0, ns, s4)
8071      call nvim_buf_del_extmark(0, ns, s5)
8072      redraw!
8073      call nvim_buf_del_extmark(0, ns, s1)
8074    ]])
8075    screen:expect([[
8076      {7:S1}^l1                |
8077      {7:S1}l2                |
8078      {7:S1}l3                |
8079                          |
8080    ]])
8081  end)
8082
8083  it('correct width when deleting lines', function()
8084    screen:try_resize(20, 4)
8085    insert(example_test3)
8086    feed('gg')
8087    command([[
8088      let ns = nvim_create_namespace('')
8089      call nvim_buf_set_extmark(0, ns, 4, 0, {'sign_text':'S1'})
8090      call nvim_buf_set_extmark(0, ns, 4, 0, {'sign_text':'S2'})
8091      let s3 =  nvim_buf_set_extmark(0, ns, 5, 0, {'sign_text':'S3'})
8092      call nvim_buf_del_extmark(0, ns, s3)
8093      norm 4Gdd
8094    ]])
8095    screen:expect([[
8096      {7:    }l3              |
8097      {7:S2S1}l5              |
8098      {7:    }^                |
8099                          |
8100    ]])
8101  end)
8102
8103  it('correct width when splitting lines with signs on different columns', function()
8104    screen:try_resize(20, 4)
8105    insert(example_test3)
8106    feed('gg')
8107    api.nvim_buf_set_extmark(0, ns, 0, 0, { sign_text = 'S1' })
8108    api.nvim_buf_set_extmark(0, ns, 0, 1, { sign_text = 'S2' })
8109    feed('a<cr><esc>')
8110    screen:expect([[
8111      {7:S1}l                 |
8112      {7:S2}^1                 |
8113      {7:  }l2                |
8114                          |
8115    ]])
8116  end)
8117
8118  it('correct width after wiping a buffer', function()
8119    screen:try_resize(20, 4)
8120    insert(example_test3)
8121    feed('gg')
8122    local buf = api.nvim_get_current_buf()
8123    api.nvim_buf_set_extmark(buf, ns, 0, 0, { sign_text = 'h' })
8124    screen:expect([[
8125      {7:h }^l1                |
8126      {7:  }l2                |
8127      {7:  }l3                |
8128                          |
8129    ]])
8130    api.nvim_win_set_buf(0, api.nvim_create_buf(false, true))
8131    api.nvim_buf_delete(buf, { unload = true, force = true })
8132    api.nvim_buf_set_lines(buf, 0, -1, false, { '' })
8133    api.nvim_win_set_buf(0, buf)
8134    screen:expect { grid = [[
8135      ^                    |
8136      {1:~                   }|*2
8137                          |
8138    ]] }
8139  end)
8140
8141  it('correct width with moved marks before undo savepos', function()
8142    screen:try_resize(20, 4)
8143    insert(example_test3)
8144    feed('gg')
8145    exec_lua([[
8146      local ns = vim.api.nvim_create_namespace('')
8147      vim.api.nvim_buf_set_extmark(0, ns, 0, 0, { sign_text = 'S1' })
8148      vim.api.nvim_buf_set_extmark(0, ns, 1, 0, { sign_text = 'S2' })
8149      local s3 = vim.api.nvim_buf_set_extmark(0, ns, 2, 0, { sign_text = 'S3' })
8150      local s4 = vim.api.nvim_buf_set_extmark(0, ns, 2, 0, { sign_text = 'S4' })
8151      vim.schedule(function()
8152        vim.cmd('silent d3')
8153        vim.api.nvim_buf_set_extmark(0, ns, 2, 0, { id = s3, sign_text = 'S3' })
8154        vim.api.nvim_buf_set_extmark(0, ns, 2, 0, { id = s4, sign_text = 'S4' })
8155        vim.cmd('silent undo')
8156        vim.api.nvim_buf_del_extmark(0, ns, s3)
8157      end)
8158    ]])
8159
8160    screen:expect([[
8161      {7:S1}^l1                |
8162      {7:S2}l2                |
8163      {7:S4}l3                |
8164                          |
8165    ]])
8166  end)
8167
8168  it('no crash with sign after many marks #27137', function()
8169    screen:try_resize(20, 4)
8170    insert('a')
8171    for _ = 0, 104 do
8172      api.nvim_buf_set_extmark(0, ns, 0, 0, { hl_group = 'Error', end_col = 1 })
8173    end
8174    api.nvim_buf_set_extmark(0, ns, 0, 0, { sign_text = 'S1' })
8175
8176    screen:expect([[
8177      {7:S1}{9:^a}                 |
8178      {1:~                   }|*2
8179                          |
8180    ]])
8181  end)
8182
8183  it('correct sort order with multiple namespaces and same id', function()
8184    local ns2 = api.nvim_create_namespace('')
8185    api.nvim_buf_set_extmark(0, ns, 0, 0, { sign_text = 'S1', id = 1 })
8186    api.nvim_buf_set_extmark(0, ns2, 0, 0, { sign_text = 'S2', id = 1 })
8187
8188    screen:expect([[
8189      {7:S2S1}^                                              |
8190      {1:~                                                 }|*8
8191                                                        |
8192    ]])
8193  end)
8194
8195  it('correct number of signs after deleting text (#27046)', function()
8196    command('call setline(1, ["foo"]->repeat(31))')
8197    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 0, sign_text = 'S1' })
8198    api.nvim_buf_set_extmark(0, ns, 0, 0, { end_row = 0, end_col = 3, hl_group = 'Error' })
8199    api.nvim_buf_set_extmark(0, ns, 9, 0, { end_row = 9, sign_text = 'S2' })
8200    api.nvim_buf_set_extmark(0, ns, 9, 0, { end_row = 9, end_col = 3, hl_group = 'Error' })
8201    api.nvim_buf_set_extmark(0, ns, 19, 0, { end_row = 19, sign_text = 'S3' })
8202    api.nvim_buf_set_extmark(0, ns, 19, 0, { end_row = 19, end_col = 3, hl_group = 'Error' })
8203    api.nvim_buf_set_extmark(0, ns, 29, 0, { end_row = 29, sign_text = 'S4' })
8204    api.nvim_buf_set_extmark(0, ns, 29, 0, { end_row = 29, end_col = 3, hl_group = 'Error' })
8205    api.nvim_buf_set_extmark(0, ns, 30, 0, { end_row = 30, sign_text = 'S5' })
8206    api.nvim_buf_set_extmark(0, ns, 30, 0, { end_row = 30, end_col = 3, hl_group = 'Error' })
8207    command('0d29')
8208
8209    screen:expect([[
8210      {7:S4S3S2S1}{9:^foo}                                       |
8211      {7:S5      }{9:foo}                                       |
8212      {1:~                                                 }|*7
8213      29 fewer lines                                    |
8214    ]])
8215
8216    api.nvim_buf_clear_namespace(0, ns, 0, -1)
8217  end)
8218
8219  it([[correct numberwidth with 'signcolumn' set to "number" #28984]], function()
8220    command('set number numberwidth=1 signcolumn=number')
8221    api.nvim_buf_set_extmark(0, ns, 0, 0, { sign_text = 'S1' })
8222    screen:expect([[
8223      {7:S1 }^                                               |
8224      {1:~                                                 }|*8
8225                                                        |
8226    ]])
8227    api.nvim_buf_del_extmark(0, ns, 1)
8228    screen:expect([[
8229      {8:1 }^                                                |
8230      {1:~                                                 }|*8
8231                                                        |
8232    ]])
8233  end)
8234
8235  it('supports emoji as signs', function()
8236    insert(example_test3)
8237    feed 'gg'
8238    api.nvim_buf_set_extmark(0, ns, 1, 0, { sign_text = '🧑‍🌾' })
8239    -- VS16 can change width of character
8240    api.nvim_buf_set_extmark(0, ns, 2, 0, { sign_text = '❤️' })
8241    api.nvim_buf_set_extmark(0, ns, 3, 0, { sign_text = '❤' })
8242    api.nvim_buf_set_extmark(0, ns, 4, 0, { sign_text = '❤x' })
8243    screen:expect([[
8244      {7:  }^l1                                              |
8245      {7:🧑‍🌾}l2                                              |
8246      {7:❤️}l3                                              |
8247      {7:❤ }l4                                              |
8248      {7:❤x}l5                                              |
8249      {7:  }                                                |
8250      {1:~                                                 }|*3
8251                                                        |
8252    ]])
8253    eq("Invalid 'sign_text'", pcall_err(api.nvim_buf_set_extmark, 0, ns, 5, 0, { sign_text = '❤️x' }))
8254  end)
8255
8256  it('auto signcolumn hides with invalidated sign', function()
8257    api.nvim_set_option_value('signcolumn', 'auto', {})
8258    api.nvim_buf_set_extmark(0, ns, 0, 0, { sign_text = 'S1', invalidate = true })
8259    feed('ia<cr>b<esc>dd')
8260    screen:expect({
8261      grid = [[
8262        ^a                                                 |
8263        {1:~                                                 }|*8
8264                                                          |
8265      ]],
8266    })
8267  end)
8268
8269  it('signcolumn correctly tracked with signs beyond eob and pair end before start', function()
8270    api.nvim_set_option_value('signcolumn', 'auto:2', {})
8271    api.nvim_set_option_value('filetype', 'lua', {})
8272    api.nvim_buf_set_lines(0, 0, -1, false, { 'foo', 'bar' })
8273    api.nvim_buf_set_extmark(0, ns, 2, 0, { sign_text = 'S1' })
8274    api.nvim_set_hl(0, 'SignColumn', { link = 'Error' })
8275    screen:expect([[
8276      ^foo                                               |
8277      bar                                               |
8278      {1:~                                                 }|*7
8279                                                        |
8280    ]])
8281    api.nvim_buf_set_extmark(0, ns, 0, 0, { sign_text = 'S2', end_row = 1 })
8282    api.nvim_buf_set_lines(0, 0, -1, false, { '-- foo', '-- bar' })
8283    api.nvim_buf_clear_namespace(0, ns, 0, -1)
8284    screen:expect([[
8285      ^-- foo                                            |
8286      -- bar                                            |
8287      {1:~                                                 }|*7
8288                                                        |
8289    ]])
8290    assert_alive()
8291  end)
8292end)
8293
8294describe('decorations: virt_text', function()
8295  local ns, screen ---@type integer, test.functional.ui.screen
8296
8297  before_each(function()
8298    clear()
8299    screen = Screen.new(50, 10)
8300    ns = api.nvim_create_namespace('test')
8301  end)
8302
8303  it('avoids regression in #17638', function()
8304    command 'set number relativenumber'
8305    command 'normal 4ohello'
8306    command 'normal aVIRTUAL'
8307
8308    api.nvim_buf_set_extmark(0, ns, 2, 0, {
8309      virt_text = { { 'hello', 'String' } },
8310      virt_text_win_col = 20,
8311    })
8312
8313    screen:expect {
8314      grid = [[
8315      {8:  4 }                                              |
8316      {8:  3 }hello                                         |
8317      {8:  2 }hello               {26:hello}                     |
8318      {8:  1 }hello                                         |
8319      {8:5   }helloVIRTUA^L                                  |
8320      {1:~                                                 }|*4
8321                                                        |
8322    ]],
8323    }
8324
8325    -- Trigger a screen update
8326    feed('k')
8327
8328    screen:expect {
8329      grid = [[
8330      {8:  3 }                                              |
8331      {8:  2 }hello                                         |
8332      {8:  1 }hello               {26:hello}                     |
8333      {8:4   }hell^o                                         |
8334      {8:  1 }helloVIRTUAL                                  |
8335      {1:~                                                 }|*4
8336                                                        |
8337    ]],
8338    }
8339  end)
8340
8341  it('redraws correctly when re-using extmark ids', function()
8342    command 'normal 5ohello'
8343
8344    screen:expect {
8345      grid = [[
8346                                                        |
8347      hello                                             |*4
8348      hell^o                                             |
8349      {1:~                                                 }|*3
8350                                                        |
8351    ]],
8352    }
8353
8354    for row = 1, 5 do
8355      api.nvim_buf_set_extmark(0, ns, row, 0, { id = 1, virt_text = { { 'world', 'Normal' } } })
8356    end
8357
8358    screen:expect {
8359      grid = [[
8360                                                        |
8361      hello                                             |*4
8362      hell^o world                                       |
8363      {1:~                                                 }|*3
8364                                                        |
8365    ]],
8366    }
8367  end)
8368
8369  it('redraws correctly when removing mark whose end ends up in front of start', function()
8370    command('normal 5ohello')
8371    api.nvim_buf_set_extmark(0, ns, 2, 0, { end_col = 1, virt_text = { { 'world', 'Normal' } } })
8372    screen:expect([[
8373                                                        |
8374      hello                                             |
8375      hello world                                       |
8376      hello                                             |*2
8377      hell^o                                             |
8378      {1:~                                                 }|*3
8379                                                        |
8380    ]])
8381    feed('3Gdd')
8382    api.nvim_buf_clear_namespace(0, ns, 0, -1)
8383    screen:expect([[
8384                                                        |
8385      hello                                             |
8386      ^hello                                             |
8387      hello                                             |*2
8388      {1:~                                                 }|*4
8389                                                        |
8390    ]])
8391  end)
8392end)
8393
8394describe('decorations: window scoped', function()
8395  local screen ---@type test.functional.ui.screen
8396  local ns ---@type integer
8397  local win_other ---@type integer
8398
8399  local url = 'https://example.com'
8400  before_each(function()
8401    clear()
8402    screen = Screen.new(20, 10)
8403    screen:add_extra_attr_ids {
8404      [100] = { special = Screen.colors.Red, undercurl = true },
8405      [101] = { url = 'https://example.com' },
8406    }
8407
8408    ns = api.nvim_create_namespace 'test'
8409
8410    insert('12345')
8411
8412    win_other = api.nvim_open_win(0, false, {
8413      col = 0,
8414      row = 0,
8415      width = 20,
8416      height = 10,
8417      relative = 'win',
8418      style = 'minimal',
8419      hide = true,
8420    })
8421  end)
8422
8423  local noextmarks = {
8424    grid = [[
8425      1234^5               |
8426      {1:~                   }|*8
8427                          |
8428    ]],
8429  }
8430
8431  local function set_extmark(line, col, opts)
8432    return api.nvim_buf_set_extmark(0, ns, line, col, opts)
8433  end
8434
8435  it('rejects an invalid window without leaking the window set', function()
8436    -- The bad handle comes second, so the set already owns an allocation by
8437    -- the time validation fails and returns.
8438    eq('Invalid window id: 9999', t.pcall_err(api.nvim__ns_set, ns, { wins = { api.nvim_get_current_win(), 9999 } }))
8439    assert_alive()
8440  end)
8441
8442  it('hl_group', function()
8443    set_extmark(0, 0, {
8444      hl_group = 'Comment',
8445      end_col = 3,
8446    })
8447
8448    api.nvim__ns_set(ns, { wins = { 0 } })
8449
8450    screen:expect {
8451      grid = [[
8452      {18:123}4^5               |
8453      {1:~                   }|*8
8454                          |
8455    ]],
8456    }
8457
8458    command 'split'
8459    command 'only'
8460
8461    screen:expect(noextmarks)
8462  end)
8463
8464  it('virt_text', function()
8465    set_extmark(0, 0, {
8466      virt_text = { { 'a', 'Comment' } },
8467      virt_text_pos = 'eol',
8468    })
8469    set_extmark(0, 5, {
8470      virt_text = { { 'b', 'Comment' } },
8471      virt_text_pos = 'inline',
8472    })
8473    set_extmark(0, 1, {
8474      virt_text = { { 'c', 'Comment' } },
8475      virt_text_pos = 'overlay',
8476    })
8477    set_extmark(0, 1, {
8478      virt_text = { { 'd', 'Comment' } },
8479      virt_text_pos = 'right_align',
8480    })
8481
8482    api.nvim__ns_set(ns, { wins = { 0 } })
8483
8484    screen:expect {
8485      grid = [[
8486      1{18:c}34^5{18:b} {18:a}           {18:d}|
8487      {1:~                   }|*8
8488                          |
8489    ]],
8490    }
8491
8492    command 'split'
8493    command 'only'
8494
8495    screen:expect(noextmarks)
8496
8497    api.nvim__ns_set(ns, { wins = {} })
8498
8499    screen:expect {
8500      grid = [[
8501      1{18:c}34^5{18:b} {18:a}           {18:d}|
8502      {1:~                   }|*8
8503                          |
8504    ]],
8505    }
8506  end)
8507
8508  it('virt_lines', function()
8509    set_extmark(0, 0, {
8510      virt_lines = { { { 'a', 'Comment' } } },
8511    })
8512
8513    api.nvim__ns_set(ns, { wins = { 0 } })
8514
8515    screen:expect {
8516      grid = [[
8517      1234^5               |
8518      {18:a}                   |
8519      {1:~                   }|*7
8520                          |
8521    ]],
8522    }
8523
8524    command 'split'
8525    command 'only'
8526
8527    screen:expect(noextmarks)
8528  end)
8529
8530  it('redraws correctly with inline virt_text and wrapping', function()
8531    set_extmark(0, 2, {
8532      virt_text = { { ('b'):rep(18), 'Comment' } },
8533      virt_text_pos = 'inline',
8534    })
8535
8536    api.nvim__ns_set(ns, { wins = { 0 } })
8537
8538    screen:expect {
8539      grid = [[
8540      12{18:bbbbbbbbbbbbbbbbbb}|
8541      34^5                 |
8542      {1:~                   }|*7
8543                          |
8544    ]],
8545    }
8546
8547    api.nvim__ns_set(ns, { wins = { win_other } })
8548
8549    screen:expect(noextmarks)
8550  end)
8551
8552  pending('sign_text', function()
8553    -- TODO(altermo): The window signcolumn width is calculated wrongly (when `signcolumn=auto`)
8554    -- This happens in function `win_redraw_signcols` on line containing `buf_meta_total(buf, kMTMetaSignText) > 0`
8555    set_extmark(0, 0, {
8556      sign_text = 'a',
8557      sign_hl_group = 'Comment',
8558    })
8559
8560    api.nvim__ns_set(ns, { wins = { 0 } })
8561
8562    screen:expect {
8563      grid = [[
8564      a 1234^5             |
8565      {2:~                   }|*8
8566                          |
8567    ]],
8568    }
8569
8570    command 'split'
8571    command 'only'
8572
8573    screen:expect(noextmarks)
8574  end)
8575
8576  it('statuscolumn hl group', function()
8577    set_extmark(0, 0, {
8578      number_hl_group = 'comment',
8579    })
8580    set_extmark(0, 0, {
8581      line_hl_group = 'comment',
8582    })
8583
8584    command 'set number'
8585
8586    api.nvim__ns_set(ns, { wins = { win_other } })
8587
8588    screen:expect {
8589      grid = [[
8590      {8:  1 }1234^5           |
8591      {1:~                   }|*8
8592                          |
8593    ]],
8594    }
8595
8596    api.nvim__ns_set(ns, { wins = { 0 } })
8597
8598    screen:expect {
8599      grid = [[
8600      {18:  1 1234^5           }|
8601      {1:~                   }|*8
8602                          |
8603    ]],
8604    }
8605
8606    command 'split'
8607    command 'only'
8608
8609    screen:expect {
8610      grid = [[
8611      {8:  1 }1234^5           |
8612      {1:~                   }|*8
8613                          |
8614    ]],
8615    }
8616  end)
8617
8618  it('spell', function()
8619    api.nvim_buf_set_lines(0, 0, -1, true, { 'aa' })
8620
8621    set_extmark(0, 0, {
8622      spell = true,
8623      end_col = 2,
8624    })
8625
8626    command 'set spelloptions=noplainbuffer'
8627    command 'set spell'
8628    command 'syntax off'
8629
8630    screen:expect({ unchanged = true })
8631
8632    api.nvim__ns_set(ns, { wins = { win_other } })
8633
8634    screen:expect {
8635      grid = [[
8636      a^a                  |
8637      {1:~                   }|*8
8638                          |
8639    ]],
8640    }
8641
8642    api.nvim__ns_set(ns, { wins = { 0 } })
8643
8644    screen:expect {
8645      grid = [[
8646      {100:a^a}                  |
8647      {1:~                   }|*8
8648                          |
8649    ]],
8650    }
8651
8652    command 'split'
8653    command 'only'
8654
8655    screen:expect {
8656      grid = [[
8657      a^a                  |
8658      {1:~                   }|*8
8659                          |
8660    ]],
8661    }
8662  end)
8663
8664  it('url', function()
8665    set_extmark(0, 0, {
8666      end_col = 3,
8667      url = url,
8668    })
8669
8670    api.nvim__ns_set(ns, { wins = { 0 } })
8671
8672    screen:expect {
8673      grid = [[
8674      {101:123}4^5               |
8675      {1:~                   }|*8
8676                          |
8677    ]],
8678    }
8679
8680    command 'split'
8681    command 'only'
8682
8683    screen:expect(noextmarks)
8684  end)
8685
8686  it('change namespace scope', function()
8687    set_extmark(0, 0, {
8688      hl_group = 'Comment',
8689      end_col = 3,
8690    })
8691
8692    api.nvim__ns_set(ns, { wins = { 0 } })
8693    eq({ wins = { api.nvim_get_current_win() } }, api.nvim__ns_get(ns))
8694
8695    screen:expect {
8696      grid = [[
8697      {18:123}4^5               |
8698      {1:~                   }|*8
8699                          |
8700    ]],
8701    }
8702
8703    command 'split'
8704    command 'only'
8705
8706    screen:expect(noextmarks)
8707
8708    api.nvim__ns_set(ns, { wins = { 0 } })
8709    eq({ wins = { api.nvim_get_current_win() } }, api.nvim__ns_get(ns))
8710
8711    screen:expect {
8712      grid = [[
8713      {18:123}4^5               |
8714      {1:~                   }|*8
8715                          |
8716    ]],
8717    }
8718
8719    local win_new = api.nvim_open_win(0, false, {
8720      col = 0,
8721      row = 0,
8722      width = 20,
8723      height = 10,
8724      relative = 'win',
8725      style = 'minimal',
8726      hide = true,
8727    })
8728
8729    api.nvim__ns_set(ns, { wins = { win_new } })
8730    eq({ wins = { win_new } }, api.nvim__ns_get(ns))
8731
8732    screen:expect(noextmarks)
8733  end)
8734
8735  it('namespace get works', function()
8736    eq({ wins = {} }, api.nvim__ns_get(ns))
8737
8738    api.nvim__ns_set(ns, { wins = { 0 } })
8739
8740    eq({ wins = { api.nvim_get_current_win() } }, api.nvim__ns_get(ns))
8741
8742    api.nvim__ns_set(ns, { wins = {} })
8743
8744    eq({ wins = {} }, api.nvim__ns_get(ns))
8745  end)
8746
8747  it('remove window from namespace scope when deleted', function()
8748    api.nvim__ns_set(ns, { wins = { 0 } })
8749
8750    eq({ wins = { api.nvim_get_current_win() } }, api.nvim__ns_get(ns))
8751
8752    command 'split'
8753    command 'only'
8754
8755    eq({ wins = {} }, api.nvim__ns_get(ns))
8756  end)
8757end)