robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
76.2 KiB2386 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local request = n.request
7local eq = t.eq
8local ok = t.ok
9local pcall_err = t.pcall_err
10local insert = n.insert
11local feed = n.feed
12local clear = n.clear
13local command = n.command
14local exec = n.exec
15local exec_lua = n.exec_lua
16local api = n.api
17local fn = n.fn
18local assert_alive = n.assert_alive
19
20local function expect(contents)
21  return eq(contents, n.curbuf_contents())
22end
23
24local function set_extmark(ns_id, id, line, col, opts)
25  if opts == nil then
26    opts = {}
27  end
28  if id ~= nil and id ~= 0 then
29    opts.id = id
30  end
31  return api.nvim_buf_set_extmark(0, ns_id, line, col, opts)
32end
33
34local function get_extmarks(ns_id, start, end_, opts)
35  if opts == nil then
36    opts = {}
37  end
38  return api.nvim_buf_get_extmarks(0, ns_id, start, end_, opts)
39end
40
41local function get_extmark_by_id(ns_id, id, opts)
42  if opts == nil then
43    opts = {}
44  end
45  return api.nvim_buf_get_extmark_by_id(0, ns_id, id, opts)
46end
47
48local function check_undo_redo(ns, mark, sr, sc, er, ec) --s = start, e = end
49  local rv = get_extmark_by_id(ns, mark)
50  eq({ er, ec }, rv)
51  feed('u')
52  rv = get_extmark_by_id(ns, mark)
53  eq({ sr, sc }, rv)
54  feed('<c-r>')
55  rv = get_extmark_by_id(ns, mark)
56  eq({ er, ec }, rv)
57end
58
59local function batch_set(ns_id, positions)
60  local ids = {}
61  for _, pos in ipairs(positions) do
62    table.insert(ids, set_extmark(ns_id, 0, pos[1], pos[2]))
63  end
64  return ids
65end
66
67local function batch_check(ns_id, ids, positions)
68  local actual, expected = {}, {}
69  for i, id in ipairs(ids) do
70    expected[id] = positions[i]
71  end
72  for _, mark in pairs(get_extmarks(ns_id, 0, -1, {})) do
73    actual[mark[1]] = { mark[2], mark[3] }
74  end
75  eq(expected, actual)
76end
77
78local function batch_check_undo_redo(ns_id, ids, before, after)
79  batch_check(ns_id, ids, after)
80  feed('u')
81  batch_check(ns_id, ids, before)
82  feed('<c-r>')
83  batch_check(ns_id, ids, after)
84end
85
86describe('API/extmarks', function()
87  local screen
88  local marks, positions, init_text, row, col
89  local ns, ns2 ---@type integer, integer
90
91  before_each(function()
92    -- Initialize some namespaces and insert 12345 into a buffer
93    marks = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
94    positions = { { 0, 0 }, { 0, 2 }, { 0, 3 } }
95
96    init_text = '12345'
97    row = 0
98    col = 2
99
100    clear()
101
102    insert(init_text)
103    ns = request('nvim_create_namespace', 'my-fancy-plugin')
104    ns2 = request('nvim_create_namespace', 'my-fancy-plugin2')
105  end)
106
107  it('validation', function()
108    eq(
109      "Invalid 'end_col': expected Integer, got Array",
110      pcall_err(set_extmark, ns, marks[2], 0, 0, { end_col = {}, end_row = 1 })
111    )
112    eq(
113      "Invalid 'end_row': expected Integer, got Array",
114      pcall_err(set_extmark, ns, marks[2], 0, 0, { end_col = 1, end_row = {} })
115    )
116    eq(
117      "Invalid 'virt_text_pos': expected String, got Integer",
118      pcall_err(set_extmark, ns, marks[2], 0, 0, { virt_text_pos = 0 })
119    )
120    eq(
121      "Invalid 'virt_text_pos': 'foo'",
122      pcall_err(set_extmark, ns, marks[2], 0, 0, { virt_text_pos = 'foo' })
123    )
124    eq(
125      "Invalid 'hl_mode': expected String, got Integer",
126      pcall_err(set_extmark, ns, marks[2], 0, 0, { hl_mode = 0 })
127    )
128    eq("Invalid 'hl_mode': 'foo'", pcall_err(set_extmark, ns, marks[2], 0, 0, { hl_mode = 'foo' }))
129    eq(
130      "Invalid 'virt_lines_overflow': 'foo'",
131      pcall_err(set_extmark, ns, marks[2], 0, 0, { virt_lines_overflow = 'foo' })
132    )
133    eq(
134      "Invalid 'id': expected Integer, got Array",
135      pcall_err(set_extmark, ns, {}, 0, 0, { end_col = 1, end_row = 1 })
136    )
137    eq(
138      'Invalid mark position: expected 2 Integer items',
139      pcall_err(get_extmarks, ns, {}, { -1, -1 })
140    )
141    eq(
142      'Invalid mark position: expected mark id Integer or 2-item Array',
143      pcall_err(get_extmarks, ns, true, { -1, -1 })
144    )
145    -- No memory leak with virt_text, virt_lines, sign_text
146    eq(
147      "Invalid 'right_gravity': expected boolean",
148      pcall_err(set_extmark, ns, marks[2], 0, 0, {
149        virt_text = { { 'foo', 'Normal' } },
150        virt_lines = { { { 'bar', 'Normal' } } },
151        sign_text = 'a',
152        right_gravity = 'baz',
153      })
154    )
155  end)
156
157  it('can end extranges past final newline using end_col=0', function()
158    set_extmark(ns, marks[1], 0, 0, {
159      end_col = 0,
160      end_row = 1,
161    })
162    eq(
163      "Invalid 'end_col': out of range",
164      pcall_err(set_extmark, ns, marks[2], 0, 0, { end_col = 1, end_row = 1 })
165    )
166  end)
167
168  it('can end extranges past final newline when strict=false', function()
169    local id = set_extmark(ns, marks[1], 0, 0, {
170      end_col = 1,
171      end_row = 1,
172      strict = false,
173    })
174    ok(id > 0, 'id > 0', id)
175  end)
176
177  it('can end extranges past final column when strict=false', function()
178    local id = set_extmark(ns, marks[1], 0, 0, {
179      end_col = 6,
180      end_row = 0,
181      strict = false,
182    })
183    ok(id > 0, 'id > 0', id)
184  end)
185
186  it('end_col=-1 means "end of line" when strict=false', function()
187    local function _test(strict)
188      return set_extmark(ns, marks[1], 0, 0, {
189        end_col = -1,
190        end_row = 0,
191        strict = strict,
192      })
193    end
194
195    -- strict=false
196    local id = _test(false)
197    ok(id > 0, 'id > 0', id)
198
199    -- strict=true
200    eq("Invalid 'end_col': out of range", pcall_err(_test, true))
201  end)
202
203  it('adds, updates  and deletes marks', function()
204    local rv = set_extmark(ns, marks[1], positions[1][1], positions[1][2])
205    eq(marks[1], rv)
206    rv = get_extmark_by_id(ns, marks[1])
207    eq({ positions[1][1], positions[1][2] }, rv)
208    -- Test adding a second mark on same row works
209    rv = set_extmark(ns, marks[2], positions[2][1], positions[2][2])
210    eq(marks[2], rv)
211
212    -- Test an update, (same pos)
213    rv = set_extmark(ns, marks[1], positions[1][1], positions[1][2])
214    eq(marks[1], rv)
215    rv = get_extmark_by_id(ns, marks[2])
216    eq({ positions[2][1], positions[2][2] }, rv)
217    -- Test an update, (new pos)
218    row = positions[1][1]
219    col = positions[1][2] + 1
220    rv = set_extmark(ns, marks[1], row, col)
221    eq(marks[1], rv)
222    rv = get_extmark_by_id(ns, marks[1])
223    eq({ row, col }, rv)
224
225    -- remove the test marks
226    eq(true, api.nvim_buf_del_extmark(0, ns, marks[1]))
227    eq(false, api.nvim_buf_del_extmark(0, ns, marks[1]))
228    eq(true, api.nvim_buf_del_extmark(0, ns, marks[2]))
229    eq(false, api.nvim_buf_del_extmark(0, ns, marks[3]))
230    eq(false, api.nvim_buf_del_extmark(0, ns, 1000))
231  end)
232
233  it('can clear a specific namespace range', function()
234    set_extmark(ns, 1, 0, 1)
235    set_extmark(ns2, 1, 0, 1)
236    -- force a new undo buffer
237    feed('o<esc>')
238    api.nvim_buf_clear_namespace(0, ns2, 0, -1)
239    eq({ { 1, 0, 1 } }, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
240    eq({}, get_extmarks(ns2, { 0, 0 }, { -1, -1 }))
241    feed('u')
242    eq({ { 1, 0, 1 } }, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
243    eq({}, get_extmarks(ns2, { 0, 0 }, { -1, -1 }))
244    feed('<c-r>')
245    eq({ { 1, 0, 1 } }, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
246    eq({}, get_extmarks(ns2, { 0, 0 }, { -1, -1 }))
247  end)
248
249  it('can clear a namespace range using 0,-1', function()
250    set_extmark(ns, 1, 0, 1)
251    set_extmark(ns2, 1, 0, 1)
252    -- force a new undo buffer
253    feed('o<esc>')
254    api.nvim_buf_clear_namespace(0, -1, 0, -1)
255    eq({}, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
256    eq({}, get_extmarks(ns2, { 0, 0 }, { -1, -1 }))
257    feed('u')
258    eq({}, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
259    eq({}, get_extmarks(ns2, { 0, 0 }, { -1, -1 }))
260    feed('<c-r>')
261    eq({}, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
262    eq({}, get_extmarks(ns2, { 0, 0 }, { -1, -1 }))
263  end)
264
265  it('can undo with extmarks (#25147)', function()
266    feed('itest<esc>')
267    set_extmark(ns, 1, 0, 0)
268    set_extmark(ns, 2, 1, 0)
269    eq({ { 1, 0, 0 }, { 2, 1, 0 } }, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
270    feed('dd')
271    eq({ { 1, 1, 0 }, { 2, 1, 0 } }, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
272    api.nvim_buf_clear_namespace(0, ns, 0, -1)
273    eq({}, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
274    set_extmark(ns, 1, 0, 0, { right_gravity = false })
275    set_extmark(ns, 2, 1, 0, { right_gravity = false })
276    eq({ { 1, 0, 0 }, { 2, 1, 0 } }, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
277    feed('u')
278    eq({ { 1, 0, 0 }, { 2, 0, 0 } }, get_extmarks(ns, { 0, 0 }, { -1, -1 }))
279    api.nvim_buf_clear_namespace(0, ns, 0, -1)
280  end)
281
282  it('querying for information and ranges', function()
283    --marks = {1, 2, 3}
284    --positions = {{0, 0,}, {0, 2}, {0, 3}}
285    -- add some more marks
286    for i, m in ipairs(marks) do
287      if positions[i] ~= nil then
288        local rv = set_extmark(ns, m, positions[i][1], positions[i][2])
289        eq(m, rv)
290      end
291    end
292
293    -- {0, 0} and {-1, -1} work as extreme values
294    eq({ { 1, 0, 0 } }, get_extmarks(ns, { 0, 0 }, { 0, 0 }))
295    eq({}, get_extmarks(ns, { -1, -1 }, { -1, -1 }))
296    local rv = get_extmarks(ns, { 0, 0 }, { -1, -1 })
297    for i, m in ipairs(marks) do
298      if positions[i] ~= nil then
299        eq({ m, positions[i][1], positions[i][2] }, rv[i])
300      end
301    end
302
303    -- 0 and -1 works as short hand extreme values
304    eq({ { 1, 0, 0 } }, get_extmarks(ns, 0, 0))
305    eq({}, get_extmarks(ns, -1, -1))
306    rv = get_extmarks(ns, 0, -1)
307    for i, m in ipairs(marks) do
308      if positions[i] ~= nil then
309        eq({ m, positions[i][1], positions[i][2] }, rv[i])
310      end
311    end
312
313    -- next with mark id
314    rv = get_extmarks(ns, marks[1], { -1, -1 }, { limit = 1 })
315    eq({ { marks[1], positions[1][1], positions[1][2] } }, rv)
316    rv = get_extmarks(ns, marks[2], { -1, -1 }, { limit = 1 })
317    eq({ { marks[2], positions[2][1], positions[2][2] } }, rv)
318    -- next with positional when mark exists at position
319    rv = get_extmarks(ns, positions[1], { -1, -1 }, { limit = 1 })
320    eq({ { marks[1], positions[1][1], positions[1][2] } }, rv)
321    -- next with positional index (no mark at position)
322    rv = get_extmarks(ns, { positions[1][1], positions[1][2] + 1 }, { -1, -1 }, { limit = 1 })
323    eq({ { marks[2], positions[2][1], positions[2][2] } }, rv)
324    -- next with Extremity index
325    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 }, { limit = 1 })
326    eq({ { marks[1], positions[1][1], positions[1][2] } }, rv)
327
328    -- nextrange with mark id
329    rv = get_extmarks(ns, marks[1], marks[3])
330    eq({ marks[1], positions[1][1], positions[1][2] }, rv[1])
331    eq({ marks[2], positions[2][1], positions[2][2] }, rv[2])
332    -- nextrange with `limit`
333    rv = get_extmarks(ns, marks[1], marks[3], { limit = 2 })
334    eq(2, #rv)
335    -- nextrange with positional when mark exists at position
336    rv = get_extmarks(ns, positions[1], positions[3])
337    eq({ marks[1], positions[1][1], positions[1][2] }, rv[1])
338    eq({ marks[2], positions[2][1], positions[2][2] }, rv[2])
339    rv = get_extmarks(ns, positions[2], positions[3])
340    eq(2, #rv)
341    -- nextrange with positional index (no mark at position)
342    local lower = { positions[1][1], positions[2][2] - 1 }
343    local upper = { positions[2][1], positions[3][2] - 1 }
344    rv = get_extmarks(ns, lower, upper)
345    eq({ { marks[2], positions[2][1], positions[2][2] } }, rv)
346    lower = { positions[3][1], positions[3][2] + 1 }
347    upper = { positions[3][1], positions[3][2] + 2 }
348    rv = get_extmarks(ns, lower, upper)
349    eq({}, rv)
350    -- nextrange with extremity index
351    lower = { positions[2][1], positions[2][2] + 1 }
352    upper = { -1, -1 }
353    rv = get_extmarks(ns, lower, upper)
354    eq({ { marks[3], positions[3][1], positions[3][2] } }, rv)
355
356    -- reverse with mark id
357    rv = get_extmarks(ns, marks[3], { 0, 0 }, { limit = 1 })
358    eq({ { marks[3], positions[3][1], positions[3][2] } }, rv)
359    rv = get_extmarks(ns, marks[2], { 0, 0 }, { limit = 1 })
360    eq({ { marks[2], positions[2][1], positions[2][2] } }, rv)
361    -- reverse with positional when mark exists at position
362    rv = get_extmarks(ns, positions[3], { 0, 0 }, { limit = 1 })
363    eq({ { marks[3], positions[3][1], positions[3][2] } }, rv)
364    -- reverse with positional index (no mark at position)
365    rv = get_extmarks(ns, { positions[1][1], positions[1][2] + 1 }, { 0, 0 }, { limit = 1 })
366    eq({ { marks[1], positions[1][1], positions[1][2] } }, rv)
367    -- reverse with Extremity index
368    rv = get_extmarks(ns, { -1, -1 }, { 0, 0 }, { limit = 1 })
369    eq({ { marks[3], positions[3][1], positions[3][2] } }, rv)
370
371    -- reverse with mark id
372    rv = get_extmarks(ns, marks[3], marks[1])
373    eq({ marks[3], positions[3][1], positions[3][2] }, rv[1])
374    eq({ marks[2], positions[2][1], positions[2][2] }, rv[2])
375    eq({ marks[1], positions[1][1], positions[1][2] }, rv[3])
376    -- reverse with limit
377    rv = get_extmarks(ns, marks[3], marks[1], { limit = 2 })
378    eq(2, #rv)
379    -- reverse with positional when mark exists at position
380    rv = get_extmarks(ns, positions[3], positions[1])
381    eq({
382      { marks[3], positions[3][1], positions[3][2] },
383      { marks[2], positions[2][1], positions[2][2] },
384      { marks[1], positions[1][1], positions[1][2] },
385    }, rv)
386    rv = get_extmarks(ns, positions[2], positions[1])
387    eq(2, #rv)
388    -- reverse with positional index (no mark at position)
389    lower = { positions[2][1], positions[2][2] + 1 }
390    upper = { positions[3][1], positions[3][2] + 1 }
391    rv = get_extmarks(ns, upper, lower)
392    eq({ { marks[3], positions[3][1], positions[3][2] } }, rv)
393    lower = { positions[3][1], positions[3][2] + 1 }
394    upper = { positions[3][1], positions[3][2] + 2 }
395    rv = get_extmarks(ns, upper, lower)
396    eq({}, rv)
397    -- reverse with extremity index
398    lower = { 0, 0 }
399    upper = { positions[2][1], positions[2][2] - 1 }
400    rv = get_extmarks(ns, upper, lower)
401    eq({ { marks[1], positions[1][1], positions[1][2] } }, rv)
402  end)
403
404  it('querying for information with limit', function()
405    -- add some more marks
406    for i, m in ipairs(marks) do
407      if positions[i] ~= nil then
408        local rv = set_extmark(ns, m, positions[i][1], positions[i][2])
409        eq(m, rv)
410      end
411    end
412
413    local rv = get_extmarks(ns, { 0, 0 }, { -1, -1 }, { limit = 1 })
414    eq(1, #rv)
415    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 }, { limit = 2 })
416    eq(2, #rv)
417    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 }, { limit = 3 })
418    eq(3, #rv)
419
420    -- now in reverse
421    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 }, { limit = 1 })
422    eq(1, #rv)
423    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 }, { limit = 2 })
424    eq(2, #rv)
425    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 }, { limit = 3 })
426    eq(3, #rv)
427  end)
428
429  it('get_marks works when mark col > upper col', function()
430    feed('A<cr>12345<esc>')
431    feed('A<cr>12345<esc>')
432    set_extmark(ns, 10, 0, 2) -- this shouldn't be found
433    set_extmark(ns, 11, 2, 1) -- this shouldn't be found
434    set_extmark(ns, marks[1], 0, 4) -- check col > our upper bound
435    set_extmark(ns, marks[2], 1, 1) -- check col < lower bound
436    set_extmark(ns, marks[3], 2, 0) -- check is inclusive
437    eq(
438      { { marks[1], 0, 4 }, { marks[2], 1, 1 }, { marks[3], 2, 0 } },
439      get_extmarks(ns, { 0, 3 }, { 2, 0 })
440    )
441  end)
442
443  it('get_marks works in reverse when mark col < lower col', function()
444    feed('A<cr>12345<esc>')
445    feed('A<cr>12345<esc>')
446    set_extmark(ns, 10, 0, 1) -- this shouldn't be found
447    set_extmark(ns, 11, 2, 4) -- this shouldn't be found
448    set_extmark(ns, marks[1], 2, 1) -- check col < our lower bound
449    set_extmark(ns, marks[2], 1, 4) -- check col > upper bound
450    set_extmark(ns, marks[3], 0, 2) -- check is inclusive
451    local rv = get_extmarks(ns, { 2, 3 }, { 0, 2 })
452    eq({ { marks[1], 2, 1 }, { marks[2], 1, 4 }, { marks[3], 0, 2 } }, rv)
453    -- doesn't include paired marks whose start pos > lower bound twice
454    -- and returns mark overlapping start pos but not end pos
455    local m1 = set_extmark(ns, nil, 0, 0, { end_row = 1, end_col = 4 })
456    local m2 = set_extmark(ns, nil, 0, 0, { end_row = 1, end_col = 2 })
457    local m3 = set_extmark(ns, nil, 1, 0, { end_row = 1, end_col = 4 })
458    local m4 = set_extmark(ns, nil, 1, 2, { end_row = 1, end_col = 4 })
459    rv = get_extmarks(ns, { 1, 3 }, { 1, 2 }, { overlap = true })
460    eq({ { m4, 1, 2 }, { m3, 1, 0 }, { m2, 0, 0 }, { m1, 0, 0 } }, rv)
461  end)
462
463  it('get_marks limit=0 returns nothing', function()
464    set_extmark(ns, marks[1], positions[1][1], positions[1][2])
465    local rv = get_extmarks(ns, { -1, -1 }, { -1, -1 }, { limit = 0 })
466    eq({}, rv)
467  end)
468
469  it('marks move with line insertations', function()
470    set_extmark(ns, marks[1], 0, 0)
471    feed('yyP')
472    check_undo_redo(ns, marks[1], 0, 0, 1, 0)
473  end)
474
475  it('marks move with multiline insertations', function()
476    feed('a<cr>22<cr>33<esc>')
477    set_extmark(ns, marks[1], 1, 1)
478    feed('ggVGyP')
479    check_undo_redo(ns, marks[1], 1, 1, 4, 1)
480  end)
481
482  it('marks move with line join', function()
483    -- do_join in ops.c
484    feed('a<cr>222<esc>')
485    set_extmark(ns, marks[1], 1, 0)
486    feed('ggJ')
487    check_undo_redo(ns, marks[1], 1, 0, 0, 6)
488  end)
489
490  it('join works when no marks are present', function()
491    screen = Screen.new(15, 10)
492    feed('a<cr>1<esc>')
493    feed('kJ')
494    -- This shouldn't seg fault
495    screen:expect([[
496      12345^ 1        |
497      {1:~              }|*8
498                     |
499    ]])
500  end)
501
502  it('marks move with multiline join', function()
503    -- do_join in ops.c
504    feed('a<cr>222<cr>333<cr>444<esc>')
505    set_extmark(ns, marks[1], 3, 0)
506    feed('2GVGJ')
507    check_undo_redo(ns, marks[1], 3, 0, 1, 8)
508  end)
509
510  it('marks move with line deletes', function()
511    feed('a<cr>222<cr>333<cr>444<esc>')
512    set_extmark(ns, marks[1], 2, 1)
513    feed('ggjdd')
514    check_undo_redo(ns, marks[1], 2, 1, 1, 1)
515  end)
516
517  it('marks move with multiline deletes', function()
518    feed('a<cr>222<cr>333<cr>444<esc>')
519    set_extmark(ns, marks[1], 3, 0)
520    feed('gg2dd')
521    check_undo_redo(ns, marks[1], 3, 0, 1, 0)
522    -- regression test, undoing multiline delete when mark is on row 1
523    feed('ugg3dd')
524    check_undo_redo(ns, marks[1], 3, 0, 0, 0)
525  end)
526
527  it('marks move with open line', function()
528    -- open_line in change.c
529    -- testing marks below are also moved
530    feed('yyP')
531    set_extmark(ns, marks[1], 0, 4)
532    set_extmark(ns, marks[2], 1, 4)
533    feed('1G<s-o><esc>')
534    check_undo_redo(ns, marks[1], 0, 4, 1, 4)
535    check_undo_redo(ns, marks[2], 1, 4, 2, 4)
536    feed('2Go<esc>')
537    check_undo_redo(ns, marks[1], 1, 4, 1, 4)
538    check_undo_redo(ns, marks[2], 2, 4, 3, 4)
539  end)
540
541  it('marks move with char inserts', function()
542    -- insertchar in insert.c (the ins_str branch)
543    screen = Screen.new(15, 10)
544    set_extmark(ns, marks[1], 0, 3)
545    feed('0')
546    insert('abc')
547    screen:expect([[
548      ab^c12345       |
549      {1:~              }|*8
550                     |
551    ]])
552    local rv = get_extmark_by_id(ns, marks[1])
553    eq({ 0, 6 }, rv)
554    check_undo_redo(ns, marks[1], 0, 3, 0, 6)
555  end)
556
557  -- gravity right as definted in tk library
558  it('marks have gravity right', function()
559    -- insertchar in insert.c (the ins_str branch)
560    set_extmark(ns, marks[1], 0, 2)
561    feed('03l')
562    insert('X')
563    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
564
565    -- check multibyte chars
566    feed('03l<esc>')
567    insert('~~')
568    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
569  end)
570
571  it('we can insert multibyte chars', function()
572    -- insertchar in insert.c
573    feed('a<cr>12345<esc>')
574    set_extmark(ns, marks[1], 1, 2)
575    -- Insert a fullwidth (two col) tilde, NICE
576    feed('0i~<esc>')
577    check_undo_redo(ns, marks[1], 1, 2, 1, 5)
578  end)
579
580  it('marks move with blockwise inserts', function()
581    -- op_insert in ops.c
582    feed('a<cr>12345<esc>')
583    set_extmark(ns, marks[1], 1, 2)
584    feed('0<c-v>lkI9<esc>')
585    check_undo_redo(ns, marks[1], 1, 2, 1, 3)
586  end)
587
588  it('marks move with line splits (using enter)', function()
589    -- open_line in change.c
590    -- testing marks below are also moved
591    feed('yyP')
592    set_extmark(ns, marks[1], 0, 4)
593    set_extmark(ns, marks[2], 1, 4)
594    feed('1Gla<cr><esc>')
595    check_undo_redo(ns, marks[1], 0, 4, 1, 2)
596    check_undo_redo(ns, marks[2], 1, 4, 2, 4)
597  end)
598
599  it('marks at last line move on insert new line', function()
600    -- open_line in change.c
601    set_extmark(ns, marks[1], 0, 4)
602    feed('0i<cr><esc>')
603    check_undo_redo(ns, marks[1], 0, 4, 1, 4)
604  end)
605
606  it('yet again marks move with line splits', function()
607    -- the first test above wasn't catching all errors..
608    feed('A67890<esc>')
609    set_extmark(ns, marks[1], 0, 4)
610    feed('04li<cr><esc>')
611    check_undo_redo(ns, marks[1], 0, 4, 1, 0)
612  end)
613
614  it('and one last time line splits...', function()
615    set_extmark(ns, marks[1], 0, 1)
616    set_extmark(ns, marks[2], 0, 2)
617    feed('02li<cr><esc>')
618    check_undo_redo(ns, marks[1], 0, 1, 0, 1)
619    check_undo_redo(ns, marks[2], 0, 2, 1, 0)
620  end)
621
622  it('multiple marks move with mark splits', function()
623    set_extmark(ns, marks[1], 0, 1)
624    set_extmark(ns, marks[2], 0, 3)
625    feed('0li<cr><esc>')
626    check_undo_redo(ns, marks[1], 0, 1, 1, 0)
627    check_undo_redo(ns, marks[2], 0, 3, 1, 2)
628  end)
629
630  it('deleting right before a mark works', function()
631    -- op_delete in ops.c
632    set_extmark(ns, marks[1], 0, 2)
633    feed('0lx')
634    check_undo_redo(ns, marks[1], 0, 2, 0, 1)
635  end)
636
637  it('deleting right after a mark works', function()
638    -- op_delete in ops.c
639    set_extmark(ns, marks[1], 0, 2)
640    feed('02lx')
641    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
642  end)
643
644  it('marks move with char deletes', function()
645    -- op_delete in ops.c
646    set_extmark(ns, marks[1], 0, 2)
647    feed('02dl')
648    check_undo_redo(ns, marks[1], 0, 2, 0, 0)
649    -- from the other side (nothing should happen)
650    feed('$x')
651    check_undo_redo(ns, marks[1], 0, 0, 0, 0)
652  end)
653
654  it('marks move with char deletes over a range', function()
655    -- op_delete in ops.c
656    set_extmark(ns, marks[1], 0, 2)
657    set_extmark(ns, marks[2], 0, 3)
658    feed('0l3dl<esc>')
659    check_undo_redo(ns, marks[1], 0, 2, 0, 1)
660    check_undo_redo(ns, marks[2], 0, 3, 0, 1)
661    -- delete 1, nothing should happen to our marks
662    feed('u')
663    feed('$x')
664    check_undo_redo(ns, marks[2], 0, 3, 0, 3)
665  end)
666
667  it('deleting marks at end of line works', function()
668    set_extmark(ns, marks[1], 0, 4)
669    feed('$x')
670    check_undo_redo(ns, marks[1], 0, 4, 0, 4)
671    -- check the copy happened correctly on delete at eol
672    feed('$x')
673    check_undo_redo(ns, marks[1], 0, 4, 0, 3)
674    feed('u')
675    check_undo_redo(ns, marks[1], 0, 4, 0, 4)
676  end)
677
678  it('marks move with blockwise deletes', function()
679    -- op_delete in ops.c
680    feed('a<cr>12345<esc>')
681    set_extmark(ns, marks[1], 1, 4)
682    feed('h<c-v>hhkd')
683    check_undo_redo(ns, marks[1], 1, 4, 1, 1)
684  end)
685
686  it('marks move with blockwise deletes over a range', function()
687    -- op_delete in ops.c
688    feed('a<cr>12345<esc>')
689    set_extmark(ns, marks[1], 0, 1)
690    set_extmark(ns, marks[2], 0, 3)
691    set_extmark(ns, marks[3], 1, 2)
692    feed('0<c-v>k3lx')
693    check_undo_redo(ns, marks[1], 0, 1, 0, 0)
694    check_undo_redo(ns, marks[2], 0, 3, 0, 0)
695    check_undo_redo(ns, marks[3], 1, 2, 1, 0)
696    -- delete 1, nothing should happen to our marks
697    feed('u')
698    feed('$<c-v>jx')
699    check_undo_redo(ns, marks[2], 0, 3, 0, 3)
700    check_undo_redo(ns, marks[3], 1, 2, 1, 2)
701  end)
702
703  it('works with char deletes over multilines', function()
704    feed('a<cr>12345<cr>test-me<esc>')
705    set_extmark(ns, marks[1], 2, 5)
706    feed('gg')
707    feed('dv?-m?<cr>')
708    check_undo_redo(ns, marks[1], 2, 5, 0, 0)
709  end)
710
711  it('marks outside of deleted range move with visual char deletes', function()
712    -- op_delete in ops.c
713    set_extmark(ns, marks[1], 0, 3)
714    feed('0vx<esc>')
715    check_undo_redo(ns, marks[1], 0, 3, 0, 2)
716
717    feed('u')
718    feed('0vlx<esc>')
719    check_undo_redo(ns, marks[1], 0, 3, 0, 1)
720
721    feed('u')
722    feed('0v2lx<esc>')
723    check_undo_redo(ns, marks[1], 0, 3, 0, 0)
724
725    -- from the other side (nothing should happen)
726    feed('$vx')
727    check_undo_redo(ns, marks[1], 0, 0, 0, 0)
728  end)
729
730  it('marks outside of deleted range move with char deletes', function()
731    -- op_delete in ops.c
732    set_extmark(ns, marks[1], 0, 3)
733    feed('0x<esc>')
734    check_undo_redo(ns, marks[1], 0, 3, 0, 2)
735
736    feed('u')
737    feed('02x<esc>')
738    check_undo_redo(ns, marks[1], 0, 3, 0, 1)
739
740    feed('u')
741    feed('0v3lx<esc>')
742    check_undo_redo(ns, marks[1], 0, 3, 0, 0)
743
744    -- from the other side (nothing should happen)
745    feed('u')
746    feed('$vx')
747    check_undo_redo(ns, marks[1], 0, 3, 0, 3)
748  end)
749
750  it('marks move with P(backward) paste', function()
751    -- do_put in ops.c
752    feed('0iabc<esc>')
753    set_extmark(ns, marks[1], 0, 7)
754    feed('0veyP')
755    check_undo_redo(ns, marks[1], 0, 7, 0, 15)
756  end)
757
758  it('marks move with p(forward) paste', function()
759    -- do_put in ops.c
760    feed('0iabc<esc>')
761    set_extmark(ns, marks[1], 0, 7)
762    feed('0veyp')
763    check_undo_redo(ns, marks[1], 0, 7, 0, 15)
764  end)
765
766  it('marks move with blockwise P(backward) paste', function()
767    -- do_put in ops.c
768    feed('a<cr>12345<esc>')
769    set_extmark(ns, marks[1], 1, 4)
770    feed('<c-v>hhkyP<esc>')
771    check_undo_redo(ns, marks[1], 1, 4, 1, 7)
772  end)
773
774  it('marks move with blockwise p(forward) paste', function()
775    -- do_put in ops.c
776    feed('a<cr>12345<esc>')
777    set_extmark(ns, marks[1], 1, 4)
778    feed('<c-v>hhkyp<esc>')
779    check_undo_redo(ns, marks[1], 1, 4, 1, 7)
780  end)
781
782  describe('multiline regions', function()
783    before_each(function()
784      feed('dd')
785      -- Achtung: code has been spiced with some unicode,
786      -- to make life more interesting.
787      -- luacheck whines about TABs inside strings for whatever reason.
788      -- luacheck: push ignore 621
789      insert([[
790        static int nlua_rpcrequest(lua_State *lstate)
791        {
792          Ïf (!nlua_is_deferred_safe(lstate)) {
793        	// strictly not allowed
794            Яetörn luaL_error(lstate, e_fast_api_disabled, "rpcrequest");
795          }
796          return nlua_rpc(lstate, true);
797        }]])
798      -- luacheck: pop
799    end)
800
801    it('delete', function()
802      local pos1 = {
803        { 2, 4 },
804        { 2, 12 },
805        { 2, 13 },
806        { 2, 14 },
807        { 2, 25 },
808        { 4, 8 },
809        { 4, 10 },
810        { 4, 20 },
811        { 5, 3 },
812        { 6, 10 },
813      }
814      local ids = batch_set(ns, pos1)
815      batch_check(ns, ids, pos1)
816      feed('3Gfiv2+ftd')
817      batch_check_undo_redo(ns, ids, pos1, {
818        { 2, 4 },
819        { 2, 12 },
820        { 2, 13 },
821        { 2, 13 },
822        { 2, 13 },
823        { 2, 13 },
824        { 2, 15 },
825        { 2, 25 },
826        { 3, 3 },
827        { 4, 10 },
828      })
829    end)
830
831    it('can get overlapping extmarks', function()
832      set_extmark(ns, 1, 0, 0, { end_row = 5, end_col = 0 })
833      set_extmark(ns, 2, 2, 5, { end_row = 2, end_col = 30 })
834      set_extmark(ns, 3, 0, 5, { end_row = 2, end_col = 10 })
835      set_extmark(ns, 4, 0, 0, { end_row = 1, end_col = 0 })
836      eq({ { 2, 2, 5 } }, get_extmarks(ns, { 2, 0 }, { 2, -1 }, { overlap = false }))
837      eq(
838        { { 1, 0, 0 }, { 3, 0, 5 }, { 2, 2, 5 } },
839        get_extmarks(ns, { 2, 0 }, { 2, -1 }, { overlap = true })
840      )
841    end)
842
843    it('limits overlap results', function()
844      set_extmark(ns, 1, 0, 0, { end_row = 5, end_col = 0 })
845      set_extmark(ns, 2, 2, 5, { end_row = 2, end_col = 30 })
846      set_extmark(ns, 3, 0, 5, { end_row = 2, end_col = 10 })
847      set_extmark(ns, 4, 0, 0, { end_row = 1, end_col = 0 })
848      local rv = get_extmarks(ns, { 2, 0 }, { 2, -1 }, { overlap = true, limit = 1 })
849      eq(1, #rv)
850    end)
851  end)
852
853  it('replace works', function()
854    set_extmark(ns, marks[1], 0, 2)
855    feed('0r2')
856    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
857  end)
858
859  it('blockwise replace works', function()
860    feed('a<cr>12345<esc>')
861    set_extmark(ns, marks[1], 0, 2)
862    feed('0<c-v>llkr1<esc>')
863    check_undo_redo(ns, marks[1], 0, 2, 0, 3)
864  end)
865
866  it('shift line', function()
867    -- shift_line in ops.c
868    feed(':set shiftwidth=4<cr><esc>')
869    set_extmark(ns, marks[1], 0, 2)
870    feed('0>>')
871    check_undo_redo(ns, marks[1], 0, 2, 0, 6)
872    expect('    12345')
873
874    feed('>>')
875    -- this is counter-intuitive. But what happens
876    -- is that 4 spaces gets extended to one tab (== 8 spaces)
877    check_undo_redo(ns, marks[1], 0, 6, 0, 3)
878    expect('\t12345')
879
880    feed('<LT><LT>') -- have to escape, same as <<
881    check_undo_redo(ns, marks[1], 0, 3, 0, 6)
882  end)
883
884  it('blockwise shift', function()
885    -- shift_block in ops.c
886    feed(':set shiftwidth=4<cr><esc>')
887    feed('a<cr>12345<esc>')
888    set_extmark(ns, marks[1], 1, 2)
889    feed('0<c-v>k>')
890    check_undo_redo(ns, marks[1], 1, 2, 1, 6)
891    -- "gg0": the cursor after undo/redo depends on |restore-undo-cursor|.
892    feed('gg0<c-v>j>')
893    expect('\t12345\n\t12345')
894    check_undo_redo(ns, marks[1], 1, 6, 1, 3)
895
896    feed('gg0<c-v>j<LT>')
897    check_undo_redo(ns, marks[1], 1, 3, 1, 6)
898  end)
899
900  it('tab works with expandtab', function()
901    -- ins_tab in insert.c
902    feed(':set expandtab<cr><esc>')
903    feed(':set shiftwidth=2<cr><esc>')
904    set_extmark(ns, marks[1], 0, 2)
905    feed('0i<tab><tab><esc>')
906    check_undo_redo(ns, marks[1], 0, 2, 0, 6)
907  end)
908
909  it('tabs work', function()
910    -- ins_tab in insert.c
911    feed(':set noexpandtab<cr><esc>')
912    feed(':set shiftwidth=2<cr><esc>')
913    feed(':set softtabstop=2<cr><esc>')
914    feed(':set tabstop=8<cr><esc>')
915    set_extmark(ns, marks[1], 0, 2)
916    feed('0i<tab><esc>')
917    check_undo_redo(ns, marks[1], 0, 2, 0, 4)
918    feed('0iX<tab><esc>')
919    check_undo_redo(ns, marks[1], 0, 4, 0, 6)
920  end)
921
922  it('marks move when using :move', function()
923    set_extmark(ns, marks[1], 0, 0)
924    feed('A<cr>2<esc>:1move 2<cr><esc>')
925    check_undo_redo(ns, marks[1], 0, 0, 1, 0)
926    -- test codepath when moving lines up
927    feed(':2move 0<cr><esc>')
928    check_undo_redo(ns, marks[1], 1, 0, 0, 0)
929  end)
930
931  it('marks move when using :move part 2', function()
932    -- make sure we didn't get lucky with the math...
933    feed('A<cr>2<cr>3<cr>4<cr>5<cr>6<esc>')
934    set_extmark(ns, marks[1], 1, 0)
935    feed(':2,3move 5<cr><esc>')
936    check_undo_redo(ns, marks[1], 1, 0, 3, 0)
937    -- test codepath when moving lines up
938    feed(':4,5move 1<cr><esc>')
939    check_undo_redo(ns, marks[1], 3, 0, 1, 0)
940  end)
941
942  it('undo and redo of set and unset marks', function()
943    -- Force a new undo head
944    feed('o<esc>')
945    set_extmark(ns, marks[1], 0, 1)
946    feed('o<esc>')
947    set_extmark(ns, marks[2], 0, -1)
948    set_extmark(ns, marks[3], 0, -1)
949
950    feed('u')
951    local rv = get_extmarks(ns, { 0, 0 }, { -1, -1 })
952    eq(3, #rv)
953
954    feed('<c-r>')
955    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 })
956    eq(3, #rv)
957
958    -- Test updates
959    feed('o<esc>')
960    set_extmark(ns, marks[1], positions[1][1], positions[1][2])
961    rv = get_extmarks(ns, marks[1], marks[1], { limit = 1 })
962    eq(1, #rv)
963    feed('u')
964    feed('<c-r>')
965    -- old value is NOT kept in history
966    check_undo_redo(
967      ns,
968      marks[1],
969      positions[1][1],
970      positions[1][2],
971      positions[1][1],
972      positions[1][2]
973    )
974
975    -- Test unset
976    feed('o<esc>')
977    api.nvim_buf_del_extmark(0, ns, marks[3])
978    feed('u')
979    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 })
980    -- undo does NOT restore deleted marks
981    eq(2, #rv)
982    feed('<c-r>')
983    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 })
984    eq(2, #rv)
985  end)
986
987  it('undo and redo of a mark created during an edit #30331', function()
988    local function range(id)
989      local m = get_extmark_by_id(ns, id, { details = true })
990      return { m[1], m[2], m[3].end_row, m[3].end_col, m[3].invalid }
991    end
992    -- Create the marks over text the edit added, while the undo block is still open.
993    feed('ggdGifoobar')
994    set_extmark(ns, marks[1], 0, 3, { end_col = 6 })
995    set_extmark(ns, marks[2], 0, 3, { end_col = 6, invalidate = true })
996    feed('<esc>')
997    eq({ 0, 3, 0, 6 }, range(marks[1]))
998    feed('u')
999    -- The text is gone, so the range collapses; the invalidating mark also goes invalid.
1000    eq({ 0, 0, 0, 0 }, range(marks[1]))
1001    eq({ 0, 0, 0, 0, true }, range(marks[2]))
1002    feed('<c-r>')
1003    -- Redo re-applies the position each side of the range was set to, and revives the
1004    -- invalidated mark; replaying the splices alone leaves the range collapsed.
1005    eq({ 0, 3, 0, 6 }, range(marks[1]))
1006    eq({ 0, 3, 0, 6 }, range(marks[2]))
1007  end)
1008
1009  it('undo and redo of a mark explicitly moved during an edit', function()
1010    feed('ggdGiabcdef<esc>')
1011    set_extmark(ns, marks[1], 0, 4)
1012    -- Insert at col 0; while the undo block is still open, explicitly
1013    -- move the mark.
1014    feed('0i!!')
1015    set_extmark(ns, marks[1], 0, 1)
1016    feed('<esc>')
1017    eq({ 0, 1 }, get_extmark_by_id(ns, marks[1]))
1018    feed('u')
1019    -- Back where it was before the edit (the recorded set-time position,
1020    -- shifted back by the splice reversal).
1021    eq({ 0, 4 }, get_extmark_by_id(ns, marks[1]))
1022    feed('<c-r>')
1023    -- Redo restores the explicit position: splice adjustment alone would
1024    -- leave the mark at col 6.
1025    eq({ 0, 1 }, get_extmark_by_id(ns, marks[1]))
1026  end)
1027
1028  it('undo and redo of marks deleted during edits', function()
1029    -- test extmark_adjust
1030    feed('A<cr>12345<esc>')
1031    set_extmark(ns, marks[1], 1, 2)
1032    feed('dd')
1033    check_undo_redo(ns, marks[1], 1, 2, 1, 0)
1034  end)
1035
1036  it('namespaces work properly', function()
1037    local rv = set_extmark(ns, marks[1], positions[1][1], positions[1][2])
1038    eq(1, rv)
1039    rv = set_extmark(ns2, marks[1], positions[1][1], positions[1][2])
1040    eq(1, rv)
1041    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 })
1042    eq(1, #rv)
1043    rv = get_extmarks(ns2, { 0, 0 }, { -1, -1 })
1044    eq(1, #rv)
1045
1046    -- Set more marks for testing the ranges
1047    set_extmark(ns, marks[2], positions[2][1], positions[2][2])
1048    set_extmark(ns, marks[3], positions[3][1], positions[3][2])
1049    set_extmark(ns2, marks[2], positions[2][1], positions[2][2])
1050    set_extmark(ns2, marks[3], positions[3][1], positions[3][2])
1051
1052    -- get_next (limit set)
1053    rv = get_extmarks(ns, { 0, 0 }, positions[2], { limit = 1 })
1054    eq(1, #rv)
1055    rv = get_extmarks(ns2, { 0, 0 }, positions[2], { limit = 1 })
1056    eq(1, #rv)
1057    -- reverse (limit set)
1058    rv = get_extmarks(ns, positions[1], { 0, 0 }, { limit = 1 })
1059    eq(1, #rv)
1060    rv = get_extmarks(ns2, positions[1], { 0, 0 }, { limit = 1 })
1061    eq(1, #rv)
1062
1063    -- get_next (no limit)
1064    rv = get_extmarks(ns, positions[1], positions[2])
1065    eq(2, #rv)
1066    rv = get_extmarks(ns2, positions[1], positions[2])
1067    eq(2, #rv)
1068    -- reverse (no limit)
1069    rv = get_extmarks(ns, positions[2], positions[1])
1070    eq(2, #rv)
1071    rv = get_extmarks(ns2, positions[2], positions[1])
1072    eq(2, #rv)
1073
1074    api.nvim_buf_del_extmark(0, ns, marks[1])
1075    rv = get_extmarks(ns, { 0, 0 }, { -1, -1 })
1076    eq(2, #rv)
1077    api.nvim_buf_del_extmark(0, ns2, marks[1])
1078    rv = get_extmarks(ns2, { 0, 0 }, { -1, -1 })
1079    eq(2, #rv)
1080  end)
1081
1082  it('mark set can create unique identifiers', function()
1083    -- create mark with id 1
1084    eq(1, set_extmark(ns, 1, positions[1][1], positions[1][2]))
1085    -- ask for unique id, it should be the next one, i e 2
1086    eq(2, set_extmark(ns, 0, positions[1][1], positions[1][2]))
1087    eq(3, set_extmark(ns, 3, positions[2][1], positions[2][2]))
1088    eq(4, set_extmark(ns, 0, positions[1][1], positions[1][2]))
1089
1090    -- mixing manual and allocated id:s are not recommended, but it should
1091    -- do something reasonable
1092    eq(6, set_extmark(ns, 6, positions[2][1], positions[2][2]))
1093    eq(7, set_extmark(ns, 0, positions[1][1], positions[1][2]))
1094    eq(8, set_extmark(ns, 0, positions[1][1], positions[1][2]))
1095  end)
1096
1097  it('auto indenting with enter works', function()
1098    -- op_reindent in ops.c
1099    feed(':set cindent<cr><esc>')
1100    feed(':set autoindent<cr><esc>')
1101    feed(':set shiftwidth=2<cr><esc>')
1102    feed('0iint <esc>A {1M1<esc>b<esc>')
1103    -- Set the mark on the M, should move..
1104    set_extmark(ns, marks[1], 0, 12)
1105    -- Set the mark before the cursor, should stay there
1106    set_extmark(ns, marks[2], 0, 10)
1107    feed('i<cr><esc>')
1108    local rv = get_extmark_by_id(ns, marks[1])
1109    eq({ 1, 3 }, rv)
1110    rv = get_extmark_by_id(ns, marks[2])
1111    eq({ 0, 10 }, rv)
1112    check_undo_redo(ns, marks[1], 0, 12, 1, 3)
1113  end)
1114
1115  it('auto indenting entire line works', function()
1116    feed(':set cindent<cr><esc>')
1117    feed(':set autoindent<cr><esc>')
1118    feed(':set shiftwidth=2<cr><esc>')
1119    -- <c-f> will force an indent of 2
1120    feed('0iint <esc>A {<cr><esc>0i1M1<esc>')
1121    set_extmark(ns, marks[1], 1, 1)
1122    feed('0i<c-f><esc>')
1123    local rv = get_extmark_by_id(ns, marks[1])
1124    eq({ 1, 3 }, rv)
1125    check_undo_redo(ns, marks[1], 1, 1, 1, 3)
1126    -- now check when cursor at eol
1127    feed('uA<c-f><esc>')
1128    rv = get_extmark_by_id(ns, marks[1])
1129    eq({ 1, 3 }, rv)
1130  end)
1131
1132  it('removing auto indenting with <C-D> works', function()
1133    feed(':set cindent<cr><esc>')
1134    feed(':set autoindent<cr><esc>')
1135    feed(':set shiftwidth=2<cr><esc>')
1136    feed('0i<tab><esc>')
1137    set_extmark(ns, marks[1], 0, 3)
1138    feed('bi<c-d><esc>')
1139    local rv = get_extmark_by_id(ns, marks[1])
1140    eq({ 0, 1 }, rv)
1141    check_undo_redo(ns, marks[1], 0, 3, 0, 1)
1142    -- check when cursor at eol
1143    feed('uA<c-d><esc>')
1144    rv = get_extmark_by_id(ns, marks[1])
1145    eq({ 0, 1 }, rv)
1146  end)
1147
1148  it('indenting multiple lines with = works', function()
1149    feed(':set cindent<cr><esc>')
1150    feed(':set autoindent<cr><esc>')
1151    feed(':set shiftwidth=2<cr><esc>')
1152    feed('0iint <esc>A {<cr><bs>1M1<cr><bs>2M2<esc>')
1153    set_extmark(ns, marks[1], 1, 1)
1154    set_extmark(ns, marks[2], 2, 1)
1155    feed('=gg')
1156    check_undo_redo(ns, marks[1], 1, 1, 1, 3)
1157    check_undo_redo(ns, marks[2], 2, 1, 2, 5)
1158  end)
1159
1160  it('substitutes by deleting inside the replace matches', function()
1161    -- do_sub in ex_cmds.c
1162    set_extmark(ns, marks[1], 0, 2)
1163    set_extmark(ns, marks[2], 0, 3)
1164    feed(':s/34/xx<cr>')
1165    check_undo_redo(ns, marks[1], 0, 2, 0, 4)
1166    check_undo_redo(ns, marks[2], 0, 3, 0, 4)
1167  end)
1168
1169  it('substitutes when insert text > deleted', function()
1170    -- do_sub in ex_cmds.c
1171    set_extmark(ns, marks[1], 0, 2)
1172    set_extmark(ns, marks[2], 0, 3)
1173    feed(':s/34/xxx<cr>')
1174    check_undo_redo(ns, marks[1], 0, 2, 0, 5)
1175    check_undo_redo(ns, marks[2], 0, 3, 0, 5)
1176  end)
1177
1178  it('substitutes when marks around eol', function()
1179    -- do_sub in ex_cmds.c
1180    set_extmark(ns, marks[1], 0, 4)
1181    set_extmark(ns, marks[2], 0, 5)
1182    feed(':s/5/xxx<cr>')
1183    check_undo_redo(ns, marks[1], 0, 4, 0, 7)
1184    check_undo_redo(ns, marks[2], 0, 5, 0, 7)
1185  end)
1186
1187  it('substitutes over range insert text > deleted', function()
1188    -- do_sub in ex_cmds.c
1189    feed('A<cr>x34xx<esc>')
1190    feed('A<cr>xxx34<esc>')
1191    set_extmark(ns, marks[1], 0, 2)
1192    set_extmark(ns, marks[2], 1, 1)
1193    set_extmark(ns, marks[3], 2, 4)
1194    feed(':1,3s/34/xxx<cr><esc>')
1195    check_undo_redo(ns, marks[1], 0, 2, 0, 5)
1196    check_undo_redo(ns, marks[2], 1, 1, 1, 4)
1197    check_undo_redo(ns, marks[3], 2, 4, 2, 6)
1198  end)
1199
1200  it('substitutes multiple matches in a line', function()
1201    -- do_sub in ex_cmds.c
1202    feed('ddi3x3x3<esc>')
1203    set_extmark(ns, marks[1], 0, 0)
1204    set_extmark(ns, marks[2], 0, 2)
1205    set_extmark(ns, marks[3], 0, 4)
1206    feed(':s/3/yy/g<cr><esc>')
1207    check_undo_redo(ns, marks[1], 0, 0, 0, 2)
1208    check_undo_redo(ns, marks[2], 0, 2, 0, 5)
1209    check_undo_redo(ns, marks[3], 0, 4, 0, 8)
1210  end)
1211
1212  it('substitutes over multiple lines with newline in pattern', function()
1213    feed('A<cr>67890<cr>xx<esc>')
1214    set_extmark(ns, marks[1], 0, 3)
1215    set_extmark(ns, marks[2], 0, 4)
1216    set_extmark(ns, marks[3], 1, 0)
1217    set_extmark(ns, marks[4], 1, 5)
1218    set_extmark(ns, marks[5], 2, 0)
1219    feed([[:1,2s:5\n:5 <cr>]])
1220    check_undo_redo(ns, marks[1], 0, 3, 0, 3)
1221    check_undo_redo(ns, marks[2], 0, 4, 0, 6)
1222    check_undo_redo(ns, marks[3], 1, 0, 0, 6)
1223    check_undo_redo(ns, marks[4], 1, 5, 0, 11)
1224    check_undo_redo(ns, marks[5], 2, 0, 1, 0)
1225  end)
1226
1227  it('inserting', function()
1228    feed('A<cr>67890<cr>xx<esc>')
1229    set_extmark(ns, marks[1], 0, 3)
1230    set_extmark(ns, marks[2], 0, 4)
1231    set_extmark(ns, marks[3], 1, 0)
1232    set_extmark(ns, marks[4], 1, 5)
1233    set_extmark(ns, marks[5], 2, 0)
1234    set_extmark(ns, marks[6], 1, 2)
1235    feed([[:1,2s:5\n67:X<cr>]])
1236    check_undo_redo(ns, marks[1], 0, 3, 0, 3)
1237    check_undo_redo(ns, marks[2], 0, 4, 0, 5)
1238    check_undo_redo(ns, marks[3], 1, 0, 0, 5)
1239    check_undo_redo(ns, marks[4], 1, 5, 0, 8)
1240    check_undo_redo(ns, marks[5], 2, 0, 1, 0)
1241    check_undo_redo(ns, marks[6], 1, 2, 0, 5)
1242  end)
1243
1244  it('substitutes with multiple newlines in pattern', function()
1245    feed('A<cr>67890<cr>xx<esc>')
1246    set_extmark(ns, marks[1], 0, 4)
1247    set_extmark(ns, marks[2], 0, 5)
1248    set_extmark(ns, marks[3], 1, 0)
1249    set_extmark(ns, marks[4], 1, 5)
1250    set_extmark(ns, marks[5], 2, 0)
1251    feed([[:1,2s:\n.*\n:X<cr>]])
1252    check_undo_redo(ns, marks[1], 0, 4, 0, 4)
1253    check_undo_redo(ns, marks[2], 0, 5, 0, 6)
1254    check_undo_redo(ns, marks[3], 1, 0, 0, 6)
1255    check_undo_redo(ns, marks[4], 1, 5, 0, 6)
1256    check_undo_redo(ns, marks[5], 2, 0, 0, 6)
1257  end)
1258
1259  it('substitutes over multiple lines with replace in substitution', function()
1260    feed('A<cr>67890<cr>xx<esc>')
1261    set_extmark(ns, marks[1], 0, 1)
1262    set_extmark(ns, marks[2], 0, 2)
1263    set_extmark(ns, marks[3], 0, 4)
1264    set_extmark(ns, marks[4], 1, 0)
1265    set_extmark(ns, marks[5], 2, 0)
1266    feed([[:1,2s:3:\r<cr>]])
1267    check_undo_redo(ns, marks[1], 0, 1, 0, 1)
1268    check_undo_redo(ns, marks[2], 0, 2, 1, 0)
1269    check_undo_redo(ns, marks[3], 0, 4, 1, 1)
1270    check_undo_redo(ns, marks[4], 1, 0, 2, 0)
1271    check_undo_redo(ns, marks[5], 2, 0, 3, 0)
1272    feed('u')
1273    feed([[:1,2s:3:\rxx<cr>]])
1274    eq({ 1, 3 }, get_extmark_by_id(ns, marks[3]))
1275  end)
1276
1277  it('substitutes over multiple lines with replace in substitution', function()
1278    feed('A<cr>x3<cr>xx<esc>')
1279    set_extmark(ns, marks[1], 1, 0)
1280    set_extmark(ns, marks[2], 1, 1)
1281    set_extmark(ns, marks[3], 1, 2)
1282    feed([[:2,2s:3:\r<cr>]])
1283    check_undo_redo(ns, marks[1], 1, 0, 1, 0)
1284    check_undo_redo(ns, marks[2], 1, 1, 2, 0)
1285    check_undo_redo(ns, marks[3], 1, 2, 2, 0)
1286  end)
1287
1288  it('substitutes over multiple lines with replace in substitution', function()
1289    feed('A<cr>x3<cr>xx<esc>')
1290    set_extmark(ns, marks[1], 0, 1)
1291    set_extmark(ns, marks[2], 0, 2)
1292    set_extmark(ns, marks[3], 0, 4)
1293    set_extmark(ns, marks[4], 1, 1)
1294    set_extmark(ns, marks[5], 2, 0)
1295    feed([[:1,2s:3:\r<cr>]])
1296    check_undo_redo(ns, marks[1], 0, 1, 0, 1)
1297    check_undo_redo(ns, marks[2], 0, 2, 1, 0)
1298    check_undo_redo(ns, marks[3], 0, 4, 1, 1)
1299    check_undo_redo(ns, marks[4], 1, 1, 3, 0)
1300    check_undo_redo(ns, marks[5], 2, 0, 4, 0)
1301    feed('u')
1302    feed([[:1,2s:3:\rxx<cr>]])
1303    check_undo_redo(ns, marks[3], 0, 4, 1, 3)
1304  end)
1305
1306  it('substitutes with newline in match and sub, delta is 0', function()
1307    feed('A<cr>67890<cr>xx<esc>')
1308    set_extmark(ns, marks[1], 0, 3)
1309    set_extmark(ns, marks[2], 0, 4)
1310    set_extmark(ns, marks[3], 0, 5)
1311    set_extmark(ns, marks[4], 1, 0)
1312    set_extmark(ns, marks[5], 1, 5)
1313    set_extmark(ns, marks[6], 2, 0)
1314    feed([[:1,1s:5\n:\r<cr>]])
1315    check_undo_redo(ns, marks[1], 0, 3, 0, 3)
1316    check_undo_redo(ns, marks[2], 0, 4, 1, 0)
1317    check_undo_redo(ns, marks[3], 0, 5, 1, 0)
1318    check_undo_redo(ns, marks[4], 1, 0, 1, 0)
1319    check_undo_redo(ns, marks[5], 1, 5, 1, 5)
1320    check_undo_redo(ns, marks[6], 2, 0, 2, 0)
1321  end)
1322
1323  it('substitutes with newline in match and sub, delta > 0', function()
1324    feed('A<cr>67890<cr>xx<esc>')
1325    set_extmark(ns, marks[1], 0, 3)
1326    set_extmark(ns, marks[2], 0, 4)
1327    set_extmark(ns, marks[3], 0, 5)
1328    set_extmark(ns, marks[4], 1, 0)
1329    set_extmark(ns, marks[5], 1, 5)
1330    set_extmark(ns, marks[6], 2, 0)
1331    feed([[:1,1s:5\n:\r\r<cr>]])
1332    check_undo_redo(ns, marks[1], 0, 3, 0, 3)
1333    check_undo_redo(ns, marks[2], 0, 4, 2, 0)
1334    check_undo_redo(ns, marks[3], 0, 5, 2, 0)
1335    check_undo_redo(ns, marks[4], 1, 0, 2, 0)
1336    check_undo_redo(ns, marks[5], 1, 5, 2, 5)
1337    check_undo_redo(ns, marks[6], 2, 0, 3, 0)
1338  end)
1339
1340  it('substitutes with newline in match and sub, delta < 0', function()
1341    feed('A<cr>67890<cr>xx<cr>xx<esc>')
1342    set_extmark(ns, marks[1], 0, 3)
1343    set_extmark(ns, marks[2], 0, 4)
1344    set_extmark(ns, marks[3], 0, 5)
1345    set_extmark(ns, marks[4], 1, 0)
1346    set_extmark(ns, marks[5], 1, 5)
1347    set_extmark(ns, marks[6], 2, 1)
1348    set_extmark(ns, marks[7], 3, 0)
1349    feed([[:1,2s:5\n.*\n:\r<cr>]])
1350    check_undo_redo(ns, marks[1], 0, 3, 0, 3)
1351    check_undo_redo(ns, marks[2], 0, 4, 1, 0)
1352    check_undo_redo(ns, marks[3], 0, 5, 1, 0)
1353    check_undo_redo(ns, marks[4], 1, 0, 1, 0)
1354    check_undo_redo(ns, marks[5], 1, 5, 1, 0)
1355    check_undo_redo(ns, marks[6], 2, 1, 1, 1)
1356    check_undo_redo(ns, marks[7], 3, 0, 2, 0)
1357  end)
1358
1359  it('substitutes with backrefs, newline inserted into sub', function()
1360    feed('A<cr>67890<cr>xx<cr>xx<esc>')
1361    set_extmark(ns, marks[1], 0, 3)
1362    set_extmark(ns, marks[2], 0, 4)
1363    set_extmark(ns, marks[3], 0, 5)
1364    set_extmark(ns, marks[4], 1, 0)
1365    set_extmark(ns, marks[5], 1, 5)
1366    set_extmark(ns, marks[6], 2, 0)
1367    feed([[:1,1s:5\(\n\):\0\1<cr>]])
1368    check_undo_redo(ns, marks[1], 0, 3, 0, 3)
1369    check_undo_redo(ns, marks[2], 0, 4, 2, 0)
1370    check_undo_redo(ns, marks[3], 0, 5, 2, 0)
1371    check_undo_redo(ns, marks[4], 1, 0, 2, 0)
1372    check_undo_redo(ns, marks[5], 1, 5, 2, 5)
1373    check_undo_redo(ns, marks[6], 2, 0, 3, 0)
1374  end)
1375
1376  it('substitutes a ^', function()
1377    set_extmark(ns, marks[1], 0, 0)
1378    set_extmark(ns, marks[2], 0, 1)
1379    feed([[:s:^:x<cr>]])
1380    check_undo_redo(ns, marks[1], 0, 0, 0, 1)
1381    check_undo_redo(ns, marks[2], 0, 1, 0, 2)
1382  end)
1383
1384  it('using <c-a> without increase in order of magnitude', function()
1385    -- do_addsub in ops.c
1386    feed('ddiabc998xxx<esc>Tc')
1387    set_extmark(ns, marks[1], 0, 2)
1388    set_extmark(ns, marks[2], 0, 3)
1389    set_extmark(ns, marks[3], 0, 5)
1390    set_extmark(ns, marks[4], 0, 6)
1391    set_extmark(ns, marks[5], 0, 7)
1392    feed('<c-a>')
1393    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
1394    check_undo_redo(ns, marks[2], 0, 3, 0, 6)
1395    check_undo_redo(ns, marks[3], 0, 5, 0, 6)
1396    check_undo_redo(ns, marks[4], 0, 6, 0, 6)
1397    check_undo_redo(ns, marks[5], 0, 7, 0, 7)
1398  end)
1399
1400  it('using <c-a> when increase in order of magnitude', function()
1401    -- do_addsub in ops.c
1402    feed('ddiabc999xxx<esc>Tc')
1403    set_extmark(ns, marks[1], 0, 2)
1404    set_extmark(ns, marks[2], 0, 3)
1405    set_extmark(ns, marks[3], 0, 5)
1406    set_extmark(ns, marks[4], 0, 6)
1407    set_extmark(ns, marks[5], 0, 7)
1408    feed('<c-a>')
1409    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
1410    check_undo_redo(ns, marks[2], 0, 3, 0, 7)
1411    check_undo_redo(ns, marks[3], 0, 5, 0, 7)
1412    check_undo_redo(ns, marks[4], 0, 6, 0, 7)
1413    check_undo_redo(ns, marks[5], 0, 7, 0, 8)
1414  end)
1415
1416  it('using <c-a> when negative and without decrease in order of magnitude', function()
1417    feed('ddiabc-999xxx<esc>T-')
1418    set_extmark(ns, marks[1], 0, 2)
1419    set_extmark(ns, marks[2], 0, 3)
1420    set_extmark(ns, marks[3], 0, 6)
1421    set_extmark(ns, marks[4], 0, 7)
1422    set_extmark(ns, marks[5], 0, 8)
1423    feed('<c-a>')
1424    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
1425    check_undo_redo(ns, marks[2], 0, 3, 0, 7)
1426    check_undo_redo(ns, marks[3], 0, 6, 0, 7)
1427    check_undo_redo(ns, marks[4], 0, 7, 0, 7)
1428    check_undo_redo(ns, marks[5], 0, 8, 0, 8)
1429  end)
1430
1431  it('using <c-a> when negative and decrease in order of magnitude', function()
1432    feed('ddiabc-1000xxx<esc>T-')
1433    set_extmark(ns, marks[1], 0, 2)
1434    set_extmark(ns, marks[2], 0, 3)
1435    set_extmark(ns, marks[3], 0, 7)
1436    set_extmark(ns, marks[4], 0, 8)
1437    set_extmark(ns, marks[5], 0, 9)
1438    feed('<c-a>')
1439    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
1440    check_undo_redo(ns, marks[2], 0, 3, 0, 7)
1441    check_undo_redo(ns, marks[3], 0, 7, 0, 7)
1442    check_undo_redo(ns, marks[4], 0, 8, 0, 7)
1443    check_undo_redo(ns, marks[5], 0, 9, 0, 8)
1444  end)
1445
1446  it('using <c-x> without decrease in order of magnitude', function()
1447    -- do_addsub in ops.c
1448    feed('ddiabc999xxx<esc>Tc')
1449    set_extmark(ns, marks[1], 0, 2)
1450    set_extmark(ns, marks[2], 0, 3)
1451    set_extmark(ns, marks[3], 0, 5)
1452    set_extmark(ns, marks[4], 0, 6)
1453    set_extmark(ns, marks[5], 0, 7)
1454    feed('<c-x>')
1455    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
1456    check_undo_redo(ns, marks[2], 0, 3, 0, 6)
1457    check_undo_redo(ns, marks[3], 0, 5, 0, 6)
1458    check_undo_redo(ns, marks[4], 0, 6, 0, 6)
1459    check_undo_redo(ns, marks[5], 0, 7, 0, 7)
1460  end)
1461
1462  it('using <c-x> when decrease in order of magnitude', function()
1463    -- do_addsub in ops.c
1464    feed('ddiabc1000xxx<esc>Tc')
1465    set_extmark(ns, marks[1], 0, 2)
1466    set_extmark(ns, marks[2], 0, 3)
1467    set_extmark(ns, marks[3], 0, 6)
1468    set_extmark(ns, marks[4], 0, 7)
1469    set_extmark(ns, marks[5], 0, 8)
1470    feed('<c-x>')
1471    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
1472    check_undo_redo(ns, marks[2], 0, 3, 0, 6)
1473    check_undo_redo(ns, marks[3], 0, 6, 0, 6)
1474    check_undo_redo(ns, marks[4], 0, 7, 0, 6)
1475    check_undo_redo(ns, marks[5], 0, 8, 0, 7)
1476  end)
1477
1478  it('using <c-x> when negative and without increase in order of magnitude', function()
1479    feed('ddiabc-998xxx<esc>T-')
1480    set_extmark(ns, marks[1], 0, 2)
1481    set_extmark(ns, marks[2], 0, 3)
1482    set_extmark(ns, marks[3], 0, 6)
1483    set_extmark(ns, marks[4], 0, 7)
1484    set_extmark(ns, marks[5], 0, 8)
1485    feed('<c-x>')
1486    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
1487    check_undo_redo(ns, marks[2], 0, 3, 0, 7)
1488    check_undo_redo(ns, marks[3], 0, 6, 0, 7)
1489    check_undo_redo(ns, marks[4], 0, 7, 0, 7)
1490    check_undo_redo(ns, marks[5], 0, 8, 0, 8)
1491  end)
1492
1493  it('using <c-x> when negative and increase in order of magnitude', function()
1494    feed('ddiabc-999xxx<esc>T-')
1495    set_extmark(ns, marks[1], 0, 2)
1496    set_extmark(ns, marks[2], 0, 3)
1497    set_extmark(ns, marks[3], 0, 6)
1498    set_extmark(ns, marks[4], 0, 7)
1499    set_extmark(ns, marks[5], 0, 8)
1500    feed('<c-x>')
1501    check_undo_redo(ns, marks[1], 0, 2, 0, 2)
1502    check_undo_redo(ns, marks[2], 0, 3, 0, 8)
1503    check_undo_redo(ns, marks[3], 0, 6, 0, 8)
1504    check_undo_redo(ns, marks[4], 0, 7, 0, 8)
1505    check_undo_redo(ns, marks[5], 0, 8, 0, 9)
1506  end)
1507
1508  it('throws consistent error codes', function()
1509    local ns_invalid = ns2 + 1 ---@type integer
1510    local err = string.format("Invalid 'ns_id': %d", ns_invalid)
1511    eq(err, pcall_err(set_extmark, ns_invalid, marks[1], positions[1][1], positions[1][2]))
1512    eq(err, pcall_err(api.nvim_buf_del_extmark, 0, ns_invalid, marks[1]))
1513    eq(err, pcall_err(get_extmarks, ns_invalid, positions[1], positions[2]))
1514    eq(err, pcall_err(get_extmark_by_id, ns_invalid, marks[1]))
1515  end)
1516
1517  it('when col = line-length, set the mark on eol', function()
1518    set_extmark(ns, marks[1], 0, -1)
1519    local rv = get_extmark_by_id(ns, marks[1])
1520    eq({ 0, init_text:len() }, rv)
1521    -- Test another
1522    set_extmark(ns, marks[1], 0, -1)
1523    rv = get_extmark_by_id(ns, marks[1])
1524    eq({ 0, init_text:len() }, rv)
1525  end)
1526
1527  it('when col = line-length, set the mark on eol', function()
1528    local invalid_col = init_text:len() + 1
1529    eq("Invalid 'col': out of range", pcall_err(set_extmark, ns, marks[1], 0, invalid_col))
1530  end)
1531
1532  it('fails when line > line_count', function()
1533    local invalid_col = init_text:len() + 1
1534    local invalid_lnum = 3
1535    eq(
1536      "Invalid 'line': out of range",
1537      pcall_err(set_extmark, ns, marks[1], invalid_lnum, invalid_col)
1538    )
1539    eq({}, get_extmark_by_id(ns, marks[1]))
1540  end)
1541
1542  it('bug from check_col in extmark_set', function()
1543    -- This bug was caused by extmark_set always using check_col. check_col
1544    -- always uses the current buffer. This wasn't working during undo so we
1545    -- now use check_col and check_lnum only when they are required.
1546    feed('A<cr>67890<cr>xx<esc>')
1547    feed('A<cr>12345<cr>67890<cr>xx<esc>')
1548    set_extmark(ns, marks[1], 3, 4)
1549    feed([[:1,5s:5\n:5 <cr>]])
1550    check_undo_redo(ns, marks[1], 3, 4, 2, 6)
1551  end)
1552
1553  it('in read-only buffer', function()
1554    command('view! runtime/doc/help.txt')
1555    eq(true, api.nvim_get_option_value('ro', {}))
1556    local id = set_extmark(ns, 0, 0, 2)
1557    eq({ { id, 0, 2 } }, get_extmarks(ns, 0, -1))
1558  end)
1559
1560  it('can set a mark to other buffer', function()
1561    local buf = request('nvim_create_buf', 0, 1)
1562    request('nvim_buf_set_lines', buf, 0, -1, 1, { '', '' })
1563    local id = api.nvim_buf_set_extmark(buf, ns, 1, 0, {})
1564    eq({ { id, 1, 0 } }, api.nvim_buf_get_extmarks(buf, ns, 0, -1, {}))
1565  end)
1566
1567  it('does not crash with append/delete/undo sequence', function()
1568    exec([[
1569      let ns = nvim_create_namespace('myplugin')
1570      call nvim_buf_set_extmark(0, ns, 0, 0, {})
1571      call append(0, '')
1572      %delete
1573      undo]])
1574    assert_alive()
1575  end)
1576
1577  it('works with left and right gravity', function()
1578    -- right gravity should move with inserted text, while
1579    -- left gravity should stay in place.
1580    api.nvim_buf_set_extmark(0, ns, 0, 5, { right_gravity = false })
1581    api.nvim_buf_set_extmark(0, ns, 0, 5, { right_gravity = true })
1582    feed([[Aasdfasdf]])
1583
1584    eq({ { 1, 0, 5 }, { 2, 0, 13 } }, api.nvim_buf_get_extmarks(0, ns, 0, -1, {}))
1585
1586    -- but both move when text is inserted before
1587    feed([[<esc>Iasdf<esc>]])
1588    -- eq({}, api.nvim_buf_get_lines(0, 0, -1, true))
1589    eq({ { 1, 0, 9 }, { 2, 0, 17 } }, api.nvim_buf_get_extmarks(0, ns, 0, -1, {}))
1590
1591    -- clear text
1592    api.nvim_buf_set_text(0, 0, 0, 0, 17, {})
1593
1594    -- handles set_text correctly as well
1595    eq({ { 1, 0, 0 }, { 2, 0, 0 } }, api.nvim_buf_get_extmarks(0, ns, 0, -1, {}))
1596    api.nvim_buf_set_text(0, 0, 0, 0, 0, { 'asdfasdf' })
1597    eq({ { 1, 0, 0 }, { 2, 0, 8 } }, api.nvim_buf_get_extmarks(0, ns, 0, -1, {}))
1598
1599    feed('u')
1600    -- handles pasting
1601    exec([[let @a='asdfasdf']])
1602    feed([["ap]])
1603    eq({ { 1, 0, 0 }, { 2, 0, 8 } }, api.nvim_buf_get_extmarks(0, ns, 0, -1, {}))
1604  end)
1605
1606  it('can accept "end_row" or "end_line" #16548', function()
1607    set_extmark(ns, marks[1], 0, 0, {
1608      end_col = 0,
1609      end_line = 1,
1610    })
1611    eq({
1612      {
1613        1,
1614        0,
1615        0,
1616        {
1617          ns_id = ns,
1618          end_col = 0,
1619          end_row = 1,
1620          right_gravity = true,
1621          end_right_gravity = false,
1622        },
1623      },
1624    }, get_extmarks(ns, 0, -1, { details = true }))
1625  end)
1626
1627  it('in prompt buffer', function()
1628    feed('dd')
1629    set_extmark(ns, marks[1], 0, 0, {})
1630    api.nvim_set_option_value('buftype', 'prompt', {})
1631    feed('i<esc>')
1632    eq({ { marks[1], 0, 2 } }, get_extmarks(ns, 0, -1))
1633    fn.prompt_setprompt('', 'foo > ')
1634    eq({ { marks[1], 0, 6 } }, get_extmarks(ns, 0, -1))
1635    feed('ihello')
1636    eq({ { marks[1], 0, 11 } }, get_extmarks(ns, 0, -1))
1637
1638    local function get_extmark_range(id)
1639      local rv = get_extmark_by_id(ns, id, { details = true })
1640      return rv[3].invalid and 'invalid' or { rv[1], rv[2], rv[3].end_row, rv[3].end_col }
1641    end
1642
1643    set_extmark(ns, marks[2], 0, 0, { invalidate = true, end_col = 6 })
1644    set_extmark(ns, marks[3], 0, 6, { invalidate = true, end_col = 11 })
1645    set_extmark(ns, marks[4], 0, 0, { invalidate = true, end_col = 11 })
1646    set_extmark(ns, marks[5], 0, 0, { invalidate = true, end_row = 1 })
1647    fn.prompt_setprompt('', 'floob > ')
1648    eq({ 0, 13 }, get_extmark_range(marks[1]))
1649    eq('invalid', get_extmark_range(marks[2])) -- extmark spanning old prompt invalidated
1650    eq({ 0, 8, 0, 13 }, get_extmark_range(marks[3]))
1651    eq({ 0, 8, 0, 13 }, get_extmark_range(marks[4]))
1652    eq({ 0, 8, 1, 0 }, get_extmark_range(marks[5]))
1653
1654    set_extmark(ns, marks[2], 0, 0, { invalidate = true, end_col = 8 })
1655    set_extmark(ns, marks[3], 0, 8, { invalidate = true, end_col = 13 })
1656    set_extmark(ns, marks[4], 0, 0, { invalidate = true, end_col = 13 })
1657    set_extmark(ns, marks[5], 0, 0, { invalidate = true, end_row = 1 })
1658    -- Do this in the same event.
1659    exec_lua(function()
1660      vim.fn.setpos("':", { 0, 1, 999, 0 })
1661      vim.fn.prompt_setprompt('', 'discard > ')
1662    end)
1663    eq({ 0, 10 }, get_extmark_range(marks[1]))
1664    eq('invalid', get_extmark_range(marks[2])) -- all spans on line invalidated
1665    eq('invalid', get_extmark_range(marks[3]))
1666    eq('invalid', get_extmark_range(marks[4]))
1667    eq({ 0, 10, 1, 0 }, get_extmark_range(marks[5]))
1668
1669    feed('hello')
1670    eq({ 0, 15 }, get_extmark_range(marks[1]))
1671    eq({ 0, 15, 1, 0 }, get_extmark_range(marks[5]))
1672    -- init_prompt uses correct range for inserted_bytes when fixing empty prompt.
1673    fn.setline('.', { '', 'last line' })
1674    eq({ 'discard > ', 'last line' }, api.nvim_buf_get_lines(0, 0, -1, true))
1675    eq({ 0, 10 }, get_extmark_range(marks[1]))
1676    eq({ 0, 10, 1, 0 }, get_extmark_range(marks[5]))
1677  end)
1678
1679  it('can get details', function()
1680    set_extmark(ns, marks[1], 0, 0, {
1681      conceal = 'c',
1682      conceal_lines = '',
1683      cursorline_hl_group = 'Statement',
1684      end_col = 0,
1685      end_right_gravity = true,
1686      end_row = 1,
1687      hl_eol = true,
1688      hl_group = 'String',
1689      hl_mode = 'blend',
1690      line_hl_group = 'Statement',
1691      number_hl_group = 'Statement',
1692      priority = 0,
1693      right_gravity = false,
1694      sign_hl_group = 'Statement',
1695      sign_text = '>>',
1696      spell = true,
1697      virt_lines = {
1698        { { 'lines', 'Macro' }, { '???' }, { ';;;', '' } },
1699        { { 'stack', { 'Type', 'Search' } }, { '!!!' } },
1700      },
1701      virt_lines_above = true,
1702      virt_lines_leftcol = true,
1703      virt_text = { { 'text', 'Macro' }, { '???' }, { 'stack', { 'Type', 'Search' } } },
1704      virt_text_hide = true,
1705      virt_text_pos = 'right_align',
1706    })
1707    eq({
1708      0,
1709      0,
1710      {
1711        conceal = 'c',
1712        conceal_lines = '',
1713        cursorline_hl_group = 'Statement',
1714        end_col = 0,
1715        end_right_gravity = true,
1716        end_row = 1,
1717        hl_eol = true,
1718        hl_group = 'String',
1719        hl_mode = 'blend',
1720        line_hl_group = 'Statement',
1721        ns_id = ns,
1722        number_hl_group = 'Statement',
1723        priority = 0,
1724        right_gravity = false,
1725        sign_hl_group = 'Statement',
1726        sign_text = '>>',
1727        spell = true,
1728        virt_lines = {
1729          { { 'lines', 'Macro' }, { '???' }, { ';;;', '' } },
1730          { { 'stack', { 'Type', 'Search' } }, { '!!!' } },
1731        },
1732        virt_lines_above = true,
1733        virt_lines_leftcol = true,
1734        virt_lines_overflow = 'trunc',
1735        virt_text = { { 'text', 'Macro' }, { '???' }, { 'stack', { 'Type', 'Search' } } },
1736        virt_text_repeat_linebreak = false,
1737        virt_text_hide = true,
1738        virt_text_pos = 'right_align',
1739      },
1740    }, get_extmark_by_id(ns, marks[1], { details = true }))
1741
1742    set_extmark(ns, marks[2], 0, 0, {
1743      priority = 0,
1744      virt_text = { { '', 'Macro' }, { '', { 'Type', 'Search' } }, { '' } },
1745      virt_text_repeat_linebreak = true,
1746      virt_text_win_col = 1,
1747    })
1748    eq({
1749      0,
1750      0,
1751      {
1752        ns_id = ns,
1753        right_gravity = true,
1754        priority = 0,
1755        virt_text = { { '', 'Macro' }, { '', { 'Type', 'Search' } }, { '' } },
1756        virt_text_repeat_linebreak = true,
1757        virt_text_hide = false,
1758        virt_text_pos = 'win_col',
1759        virt_text_win_col = 1,
1760      },
1761    }, get_extmark_by_id(ns, marks[2], { details = true }))
1762
1763    set_extmark(ns, marks[3], 0, 0, {
1764      priority = 0,
1765      ui_watched = true,
1766      virt_lines = { { { '', 'Macro' }, { '' }, { '', '' } } },
1767      virt_lines_overflow = 'scroll',
1768    })
1769    eq({
1770      0,
1771      0,
1772      {
1773        ns_id = ns,
1774        right_gravity = true,
1775        ui_watched = true,
1776        priority = 0,
1777        virt_lines = { { { '', 'Macro' }, { '' }, { '', '' } } },
1778        virt_lines_above = false,
1779        virt_lines_leftcol = false,
1780        virt_lines_overflow = 'scroll',
1781      },
1782    }, get_extmark_by_id(ns, marks[3], { details = true }))
1783
1784    -- verify 'wrap' is returned through get_extmark_by_id to validate flag comparison
1785    set_extmark(ns, marks[4], 0, 0, {
1786      priority = 0,
1787      ui_watched = true,
1788      virt_lines = { { { '', 'Macro' }, { '' }, { '', '' } } },
1789      virt_lines_overflow = 'wrap',
1790    })
1791    eq({
1792      0,
1793      0,
1794      {
1795        ns_id = ns,
1796        right_gravity = true,
1797        ui_watched = true,
1798        priority = 0,
1799        virt_lines = { { { '', 'Macro' }, { '' }, { '', '' } } },
1800        virt_lines_above = false,
1801        virt_lines_leftcol = false,
1802        virt_lines_overflow = 'wrap',
1803      },
1804    }, get_extmark_by_id(ns, marks[4], { details = true }))
1805
1806    -- verify 'auto' is returned through get_extmark_by_id to validate flag comparison
1807    set_extmark(ns, marks[5], 0, 0, {
1808      priority = 0,
1809      ui_watched = true,
1810      virt_lines = { { { '', 'Macro' }, { '' }, { '', '' } } },
1811      virt_lines_overflow = 'auto',
1812    })
1813    eq({
1814      0,
1815      0,
1816      {
1817        ns_id = ns,
1818        right_gravity = true,
1819        ui_watched = true,
1820        priority = 0,
1821        virt_lines = { { { '', 'Macro' }, { '' }, { '', '' } } },
1822        virt_lines_above = false,
1823        virt_lines_leftcol = false,
1824        virt_lines_overflow = 'auto',
1825      },
1826    }, get_extmark_by_id(ns, marks[5], { details = true }))
1827
1828    set_extmark(ns, marks[4], 0, 0, { cursorline_hl_group = 'Statement' })
1829    eq({
1830      0,
1831      0,
1832      {
1833        ns_id = ns,
1834        cursorline_hl_group = 'Statement',
1835        priority = 4096,
1836        right_gravity = true,
1837      },
1838    }, get_extmark_by_id(ns, marks[4], { details = true }))
1839
1840    set_extmark(ns, marks[5], 0, 0, {
1841      end_col = 1,
1842      conceal = 'a',
1843      spell = true,
1844    })
1845    eq({
1846      0,
1847      0,
1848      {
1849        conceal = 'a',
1850        end_col = 1,
1851        end_right_gravity = false,
1852        end_row = 0,
1853        ns_id = ns,
1854        right_gravity = true,
1855        spell = true,
1856      },
1857    }, get_extmark_by_id(ns, marks[5], { details = true }))
1858
1859    set_extmark(ns, marks[6], 0, 0, {
1860      end_col = 1,
1861      spell = false,
1862    })
1863    eq({
1864      0,
1865      0,
1866      {
1867        end_col = 1,
1868        end_right_gravity = false,
1869        end_row = 0,
1870        ns_id = ns,
1871        right_gravity = true,
1872        spell = false,
1873      },
1874    }, get_extmark_by_id(ns, marks[6], { details = true }))
1875
1876    api.nvim_buf_clear_namespace(0, ns, 0, -1)
1877    -- legacy sign mark includes sign name
1878    command('sign define sign1 text=s1 texthl=Title linehl=LineNR numhl=Normal culhl=CursorLine')
1879    command('sign place 1 name=sign1 line=1')
1880    eq({
1881      {
1882        1,
1883        0,
1884        0,
1885        {
1886          cursorline_hl_group = 'CursorLine',
1887          invalidate = true,
1888          line_hl_group = 'LineNr',
1889          ns_id = 0,
1890          number_hl_group = 'Normal',
1891          priority = 10,
1892          right_gravity = true,
1893          sign_hl_group = 'Title',
1894          sign_name = 'sign1',
1895          sign_text = 's1',
1896          undo_restore = false,
1897        },
1898      },
1899    }, get_extmarks(-1, 0, -1, { details = true }))
1900  end)
1901
1902  it('can get marks from anonymous namespaces', function()
1903    ns = request('nvim_create_namespace', '')
1904    ns2 = request('nvim_create_namespace', '')
1905    set_extmark(ns, 1, 0, 0, {})
1906    set_extmark(ns2, 2, 1, 0, {})
1907    eq({
1908      { 1, 0, 0, { ns_id = ns, right_gravity = true } },
1909      { 2, 1, 0, { ns_id = ns2, right_gravity = true } },
1910    }, get_extmarks(-1, 0, -1, { details = true }))
1911  end)
1912
1913  it('can filter by extmark properties', function()
1914    set_extmark(ns, 1, 0, 0, {})
1915    set_extmark(ns, 2, 0, 0, { hl_group = 'Normal' })
1916    set_extmark(ns, 3, 0, 0, { sign_text = '>>' })
1917    set_extmark(ns, 4, 0, 0, { virt_text = { { 'text', 'Normal' } } })
1918    set_extmark(ns, 5, 0, 0, { virt_lines = { { { 'line', 'Normal' } } } })
1919    eq(5, #get_extmarks(-1, 0, -1, {}))
1920    eq({ { 2, 0, 0 } }, get_extmarks(-1, 0, -1, { type = 'highlight' }))
1921    eq({ { 3, 0, 0 } }, get_extmarks(-1, 0, -1, { type = 'sign' }))
1922    eq({ { 4, 0, 0 } }, get_extmarks(-1, 0, -1, { type = 'virt_text' }))
1923    eq({ { 5, 0, 0 } }, get_extmarks(-1, 0, -1, { type = 'virt_lines' }))
1924  end)
1925
1926  it('invalidated marks are deleted', function()
1927    screen = Screen.new(40, 6)
1928    feed('dd6iaaa bbb ccc<CR><ESC>gg')
1929    api.nvim_set_option_value('signcolumn', 'auto:3', {})
1930    set_extmark(ns, 1, 0, 0, { invalidate = true, sign_text = 'S1', end_row = 1 })
1931    set_extmark(ns, 2, 1, 0, { invalidate = true, sign_text = 'S2', end_row = 2, end_col = 0 })
1932    set_extmark(ns, 3, 1, 0, { invalidate = true, sign_text = 'S3', end_row = 2, end_col = 1 })
1933    -- mark with invalidate is removed
1934    command('d2')
1935    screen:expect([[
1936      {7:S3}^aaa bbb ccc                           |
1937      {7:  }aaa bbb ccc                           |*3
1938      {7:  }                                      |
1939                                              |
1940    ]])
1941    -- mark is restored with undo_restore == true
1942    command('silent undo')
1943    screen:expect([[
1944      {7:S1    }^aaa bbb ccc                       |
1945      {7:S3S2S1}aaa bbb ccc                       |
1946      {7:S3S2  }aaa bbb ccc                       |
1947      {7:      }aaa bbb ccc                       |*2
1948                                              |
1949    ]])
1950    -- decor is not removed twice
1951    command('d3')
1952    api.nvim_buf_del_extmark(0, ns, 1)
1953    api.nvim_buf_del_extmark(0, ns, 3)
1954    command('silent undo')
1955    -- mark is deleted with undo_restore == false
1956    set_extmark(ns, 1, 0, 0, { invalidate = true, undo_restore = false, sign_text = 'S1' })
1957    set_extmark(ns, 2, 1, 0, { invalidate = true, undo_restore = false, sign_text = 'S2' })
1958    command('1d 2')
1959    eq(0, #get_extmarks(-1, 0, -1, {}))
1960    -- mark is not removed when deleting bytes before the range
1961    set_extmark(ns, 3, 0, 4, {
1962      invalidate = true,
1963      hl_group = 'Error',
1964      end_col = 7,
1965      right_gravity = false,
1966    })
1967    feed('dw')
1968    eq(3, get_extmark_by_id(ns, 3, { details = true })[3].end_col)
1969    -- mark is not removed when deleting bytes at the start of the range
1970    feed('x')
1971    eq(2, get_extmark_by_id(ns, 3, { details = true })[3].end_col)
1972    -- mark is not removed when deleting bytes from the end of the range
1973    feed('lx')
1974    eq(1, get_extmark_by_id(ns, 3, { details = true })[3].end_col)
1975    -- mark is not removed when deleting bytes beyond end of the range
1976    feed('x')
1977    eq(1, get_extmark_by_id(ns, 3, { details = true })[3].end_col)
1978    -- mark is removed when all bytes in the range are deleted
1979    feed('hx')
1980    eq(true, get_extmark_by_id(ns, 3, { details = true })[3].invalid)
1981    -- mark is restored with undo_restore == true if pos did not change
1982    command('undo')
1983    eq(nil, get_extmark_by_id(ns, 3, { details = true })[3].invalid)
1984    -- multiline mark is not removed when start of its range is deleted
1985    set_extmark(ns, 4, 1, 4, {
1986      undo_restore = false,
1987      invalidate = true,
1988      hl_group = 'Error',
1989      end_col = 7,
1990      end_row = 3,
1991    })
1992    feed('ddDdd')
1993    eq({ 0, 0 }, get_extmark_by_id(ns, 4, {}))
1994    -- multiline mark is removed when entirety of its range is deleted
1995    feed('vj2ed')
1996    eq({}, get_extmark_by_id(ns, 4, {}))
1997  end)
1998
1999  it('no crash checking invalidated flag of sign pair end key #31856', function()
2000    api.nvim_buf_set_lines(0, 0, 1, false, { '', '' })
2001    api.nvim_set_option_value('signcolumn', 'auto:2', {})
2002    set_extmark(ns, 1, 0, 0, { sign_text = 'S1', invalidate = true, end_row = 0 })
2003    set_extmark(ns, 2, 1, 0, { sign_text = 'S2', end_row = 1 })
2004    command('d')
2005    api.nvim_buf_clear_namespace(0, ns, 0, -1)
2006    n.assert_alive()
2007  end)
2008
2009  it('can set a URL', function()
2010    local url1 = 'https://example.com'
2011    local url2 = 'http://127.0.0.1'
2012    set_extmark(ns, 1, 0, 0, { url = url1, end_col = 3 })
2013    set_extmark(ns, 2, 0, 3, { url = url2, hl_group = 'Search', end_col = 5 })
2014    local extmarks = get_extmarks(ns, 0, -1, { details = true })
2015    eq(2, #extmarks)
2016    eq(url1, extmarks[1][4].url)
2017    eq(url2, extmarks[2][4].url)
2018    eq('Search', extmarks[2][4].hl_group)
2019  end)
2020
2021  it('respects priority', function()
2022    screen = Screen.new(15, 10)
2023
2024    set_extmark(ns, marks[1], 0, 0, {
2025      hl_group = 'Comment',
2026      end_col = 2,
2027      priority = 20,
2028    })
2029
2030    -- Extmark defined after first extmark but has lower priority, first extmark "wins"
2031    set_extmark(ns, marks[2], 0, 0, {
2032      hl_group = 'String',
2033      end_col = 2,
2034      priority = 10,
2035    })
2036
2037    screen:expect {
2038      grid = [[
2039      {1:12}34^5          |
2040      {2:~              }|*8
2041                     |
2042    ]],
2043      attr_ids = {
2044        [1] = { foreground = Screen.colors.Blue1 },
2045        [2] = { foreground = Screen.colors.Blue1, bold = true },
2046      },
2047    }
2048  end)
2049
2050  it('are invalidated when "nofile" buffer is unloaded', function()
2051    local buf = api.nvim_create_buf(false, true)
2052    api.nvim_buf_set_name(buf, 'foo')
2053    api.nvim_buf_set_lines(buf, 0, 0, false, { 'foo', 'bar' })
2054    local id = api.nvim_buf_set_extmark(buf, ns, 1, 0, { invalidate = true })
2055    api.nvim_buf_delete(buf, { unload = true })
2056    local mark = { 0, 0, { invalid = true, invalidate = true, ns_id = ns, right_gravity = true } }
2057    eq(mark, api.nvim_buf_get_extmark_by_id(buf, ns, id, { details = true }))
2058  end)
2059end)
2060
2061describe('Extmarks buffer api with many marks', function()
2062  local ns1
2063  local ns2
2064  local ns_marks = {}
2065  before_each(function()
2066    clear()
2067    ns1 = request('nvim_create_namespace', 'ns1')
2068    ns2 = request('nvim_create_namespace', 'ns2')
2069    ns_marks = { [ns1] = {}, [ns2] = {} }
2070    local lines = {}
2071    for i = 1, 30 do
2072      lines[#lines + 1] = string.rep('x ', i)
2073    end
2074    api.nvim_buf_set_lines(0, 0, -1, true, lines)
2075    local ns = ns1
2076    local q = 0
2077    for i = 0, 29 do
2078      for j = 0, i do
2079        local id = set_extmark(ns, 0, i, j)
2080        eq(nil, ns_marks[ns][id])
2081        ok(id > 0)
2082        ns_marks[ns][id] = { i, j }
2083        ns = ns1 + ns2 - ns
2084        q = q + 1
2085      end
2086    end
2087    eq(233, #ns_marks[ns1])
2088    eq(232, #ns_marks[ns2])
2089  end)
2090
2091  local function get_marks(ns)
2092    local mark_list = get_extmarks(ns, 0, -1)
2093    local marks = {}
2094    for _, mark in ipairs(mark_list) do
2095      local id, row, col = unpack(mark)
2096      eq(nil, marks[id], 'duplicate mark')
2097      marks[id] = { row, col }
2098    end
2099    return marks
2100  end
2101
2102  it('can get marks', function()
2103    eq(ns_marks[ns1], get_marks(ns1))
2104    eq(ns_marks[ns2], get_marks(ns2))
2105  end)
2106
2107  it('can clear all marks in ns', function()
2108    api.nvim_buf_clear_namespace(0, ns1, 0, -1)
2109    eq({}, get_marks(ns1))
2110    eq(ns_marks[ns2], get_marks(ns2))
2111    api.nvim_buf_clear_namespace(0, ns2, 0, -1)
2112    eq({}, get_marks(ns1))
2113    eq({}, get_marks(ns2))
2114  end)
2115
2116  it('can clear line range', function()
2117    api.nvim_buf_clear_namespace(0, ns1, 10, 20)
2118    for id, mark in pairs(ns_marks[ns1]) do
2119      if 10 <= mark[1] and mark[1] < 20 then
2120        ns_marks[ns1][id] = nil
2121      end
2122    end
2123    eq(ns_marks[ns1], get_marks(ns1))
2124    eq(ns_marks[ns2], get_marks(ns2))
2125
2126    api.nvim_buf_clear_namespace(0, ns1, 0, 10)
2127    for id, mark in pairs(ns_marks[ns1]) do
2128      if mark[1] < 10 then
2129        ns_marks[ns1][id] = nil
2130      end
2131    end
2132    eq(ns_marks[ns1], get_marks(ns1))
2133    eq(ns_marks[ns2], get_marks(ns2))
2134
2135    api.nvim_buf_clear_namespace(0, ns1, 20, -1)
2136    for id, mark in pairs(ns_marks[ns1]) do
2137      if mark[1] >= 20 then
2138        ns_marks[ns1][id] = nil
2139      end
2140    end
2141    eq(ns_marks[ns1], get_marks(ns1))
2142    eq(ns_marks[ns2], get_marks(ns2))
2143  end)
2144
2145  it('can delete line', function()
2146    feed('10Gdd')
2147    for _, marks in pairs(ns_marks) do
2148      for id, mark in pairs(marks) do
2149        if mark[1] == 9 then
2150          marks[id] = { 9, 0 }
2151        elseif mark[1] >= 10 then
2152          mark[1] = mark[1] - 1
2153        end
2154      end
2155    end
2156    eq(ns_marks[ns1], get_marks(ns1))
2157    eq(ns_marks[ns2], get_marks(ns2))
2158  end)
2159
2160  it('can delete lines', function()
2161    feed('10G10dd')
2162    for _, marks in pairs(ns_marks) do
2163      for id, mark in pairs(marks) do
2164        if 9 <= mark[1] and mark[1] < 19 then
2165          marks[id] = { 9, 0 }
2166        elseif mark[1] >= 19 then
2167          mark[1] = mark[1] - 10
2168        end
2169      end
2170    end
2171    eq(ns_marks[ns1], get_marks(ns1))
2172    eq(ns_marks[ns2], get_marks(ns2))
2173  end)
2174
2175  it('can wipe buffer', function()
2176    command('bwipe!')
2177    eq({}, get_marks(ns1))
2178    eq({}, get_marks(ns2))
2179  end)
2180end)
2181
2182describe('API/win_extmark', function()
2183  local screen
2184  local marks, line1, line2
2185  local ns
2186
2187  before_each(function()
2188    -- Initialize some namespaces and insert text into a buffer
2189    marks = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
2190
2191    line1 = 'non ui-watched line'
2192    line2 = 'ui-watched line'
2193
2194    clear()
2195
2196    insert(line1)
2197    feed('o<esc>')
2198    insert(line2)
2199    ns = request('nvim_create_namespace', 'extmark-ui')
2200  end)
2201
2202  it('sends and only sends ui-watched marks to ui', function()
2203    screen = Screen.new(20, 4)
2204    -- should send this
2205    set_extmark(ns, marks[1], 1, 0, { ui_watched = true })
2206    -- should not send this
2207    set_extmark(ns, marks[2], 0, 0, { ui_watched = false })
2208    screen:expect({
2209      grid = [[
2210      non ui-watched line |
2211      ui-watched lin^e     |
2212      {1:~                   }|
2213                          |
2214    ]],
2215      extmarks = {
2216        [2] = {
2217          -- positioned at the end of the 2nd line
2218          { 1000, ns, marks[1], 1, 16 },
2219        },
2220      },
2221    })
2222  end)
2223
2224  it('sends multiple ui-watched marks to ui', function()
2225    screen = Screen.new(20, 4)
2226    feed('15A!<Esc>')
2227    -- should send all of these
2228    set_extmark(ns, marks[1], 1, 0, { ui_watched = true, virt_text_pos = 'overlay' })
2229    set_extmark(ns, marks[2], 1, 2, { ui_watched = true, virt_text_pos = 'overlay' })
2230    set_extmark(ns, marks[3], 1, 4, { ui_watched = true, virt_text_pos = 'overlay' })
2231    set_extmark(ns, marks[4], 1, 6, { ui_watched = true, virt_text_pos = 'overlay' })
2232    set_extmark(ns, marks[5], 1, 8, { ui_watched = true })
2233    screen:expect({
2234      grid = [[
2235      non ui-watched line |
2236      ui-watched line!!!!!|
2237      !!!!!!!!!^!          |
2238                          |
2239    ]],
2240      extmarks = {
2241        [2] = {
2242          -- notification from 1st call
2243          { 1000, ns, marks[1], 1, 0 },
2244          -- notifications from 2nd call
2245          { 1000, ns, marks[1], 1, 0 },
2246          { 1000, ns, marks[2], 1, 2 },
2247          -- notifications from 3rd call
2248          { 1000, ns, marks[1], 1, 0 },
2249          { 1000, ns, marks[2], 1, 2 },
2250          { 1000, ns, marks[3], 1, 4 },
2251          -- notifications from 4th call
2252          { 1000, ns, marks[1], 1, 0 },
2253          { 1000, ns, marks[2], 1, 2 },
2254          { 1000, ns, marks[3], 1, 4 },
2255          { 1000, ns, marks[4], 1, 6 },
2256          -- final
2257          --   overlay
2258          { 1000, ns, marks[1], 1, 0 },
2259          { 1000, ns, marks[2], 1, 2 },
2260          { 1000, ns, marks[3], 1, 4 },
2261          { 1000, ns, marks[4], 1, 6 },
2262          --   eol
2263          { 1000, ns, marks[5], 2, 11 },
2264        },
2265      },
2266    })
2267  end)
2268
2269  it('updates ui-watched marks', function()
2270    screen = Screen.new(20, 4)
2271    -- should send this
2272    set_extmark(ns, marks[1], 1, 0, { ui_watched = true })
2273    -- should not send this
2274    set_extmark(ns, marks[2], 0, 0, { ui_watched = false })
2275    -- make some changes
2276    insert(' update')
2277    screen:expect({
2278      grid = [[
2279      non ui-watched line |
2280      ui-watched linupdat^e|
2281      e                   |
2282                          |
2283    ]],
2284      extmarks = {
2285        [2] = {
2286          -- positioned at the end of the 2nd line
2287          { 1000, ns, marks[1], 1, 16 },
2288          -- updated and wrapped to 3rd line
2289          { 1000, ns, marks[1], 2, 2 },
2290        },
2291      },
2292    })
2293    feed('<c-e>')
2294    screen:expect({
2295      grid = [[
2296      ui-watched linupdat^e|
2297      e                   |
2298      {1:~                   }|
2299                          |
2300    ]],
2301      extmarks = {
2302        [2] = {
2303          -- positioned at the end of the 2nd line
2304          { 1000, ns, marks[1], 1, 16 },
2305          -- updated and wrapped to 3rd line
2306          { 1000, ns, marks[1], 2, 2 },
2307          -- scrolled up one line, should be handled by grid scroll
2308        },
2309      },
2310    })
2311  end)
2312
2313  it('sends ui-watched to splits', function()
2314    screen = Screen.new(20, 8, { ext_multigrid = true })
2315    -- should send this
2316    set_extmark(ns, marks[1], 1, 0, { ui_watched = true })
2317    -- should not send this
2318    set_extmark(ns, marks[2], 0, 0, { ui_watched = false })
2319    command('split')
2320    screen:expect({
2321      grid = [[
2322        ## grid 1
2323          [4:--------------------]|*3
2324          {3:[No Name] [+]       }|
2325          [2:--------------------]|*2
2326          {2:[No Name] [+]       }|
2327          [3:--------------------]|
2328        ## grid 2
2329          non ui-watched line |
2330          ui-watched line     |
2331        ## grid 3
2332                              |
2333        ## grid 4
2334          non ui-watched line |
2335          ui-watched lin^e     |
2336          {1:~                   }|
2337    ]],
2338      extmarks = {
2339        [2] = {
2340          -- positioned at the end of the 2nd line
2341          { 1000, ns, marks[1], 1, 16 },
2342          -- updated after split
2343          { 1000, ns, marks[1], 1, 16 },
2344        },
2345        [4] = {
2346          -- only after split
2347          { 1001, ns, marks[1], 1, 16 },
2348        },
2349      },
2350    })
2351    -- make some changes
2352    insert(' update')
2353    screen:expect({
2354      grid = [[
2355        ## grid 1
2356          [4:--------------------]|*3
2357          {3:[No Name] [+]       }|
2358          [2:--------------------]|*2
2359          {2:[No Name] [+]       }|
2360          [3:--------------------]|
2361        ## grid 2
2362          non ui-watched line |
2363          ui-watched linupd{1:@@@}|
2364        ## grid 3
2365                              |
2366        ## grid 4
2367          non ui-watched line |
2368          ui-watched linupdat^e|
2369          e                   |
2370    ]],
2371      extmarks = {
2372        [2] = {
2373          -- positioned at the end of the 2nd line
2374          { 1000, ns, marks[1], 1, 16 },
2375          -- updated after split
2376          { 1000, ns, marks[1], 1, 16 },
2377        },
2378        [4] = {
2379          { 1001, ns, marks[1], 1, 16 },
2380          -- updated
2381          { 1001, ns, marks[1], 2, 2 },
2382        },
2383      },
2384    })
2385  end)
2386end)