robin/laufey

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

robinmerge: 8c77b5be50c50ab119

master
14.4 KiB360 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 exec_lua = n.exec_lua
7local eq = t.eq
8local eval = n.eval
9local command = n.command
10local clear = n.clear
11local api = n.api
12
13describe('vim.hl.range', function()
14  local screen
15
16  before_each(function()
17    clear()
18    screen = Screen.new(60, 6)
19    screen:add_extra_attr_ids({
20      [100] = { foreground = Screen.colors.Blue, background = Screen.colors.Yellow, bold = true },
21    })
22    api.nvim_set_option_value('list', true, {})
23    api.nvim_set_option_value('listchars', 'eol:$', {})
24    api.nvim_buf_set_lines(0, 0, -1, true, {
25      'asdfghjkl',
26      '«口=口»',
27      'qwertyuiop',
28      '口口=口口',
29      'zxcvbnm',
30    })
31    screen:expect([[
32      ^asdfghjkl{1:$}                                                  |
33      «口=口»{1:$}                                                    |
34      qwertyuiop{1:$}                                                 |
35      口口=口口{1:$}                                                  |
36      zxcvbnm{1:$}                                                    |
37                                                                  |
38    ]])
39  end)
40
41  it('works with charwise selection', function()
42    exec_lua(function()
43      local ns = vim.api.nvim_create_namespace('')
44      vim.hl.range(0, ns, 'Search', { 1, 5 }, { 3, 10 })
45    end)
46    screen:expect([[
47      ^asdfghjkl{1:$}                                                  |
48      «口{10:=口»}{100:$}                                                    |
49      {10:qwertyuiop}{100:$}                                                 |
50      {10:口口=口}口{1:$}                                                  |
51      zxcvbnm{1:$}                                                    |
52                                                                  |
53    ]])
54  end)
55
56  it('works with linewise selection', function()
57    exec_lua(function()
58      local ns = vim.api.nvim_create_namespace('')
59      vim.hl.range(0, ns, 'Search', { 0, 0 }, { 4, 0 }, { regtype = 'V' })
60    end)
61    screen:expect([[
62      {10:^asdfghjkl}{100:$}                                                  |
63      {10:«口=口»}{100:$}                                                    |
64      {10:qwertyuiop}{100:$}                                                 |
65      {10:口口=口口}{100:$}                                                  |
66      {10:zxcvbnm}{100:$}                                                    |
67                                                                  |
68    ]])
69  end)
70
71  it('works with blockwise selection', function()
72    exec_lua(function()
73      local ns = vim.api.nvim_create_namespace('')
74      vim.hl.range(0, ns, 'Search', { 0, 0 }, { 4, 4 }, { regtype = '\022' })
75    end)
76    screen:expect([[
77      {10:^asdf}ghjkl{1:$}                                                  |
78      {10:«口=}口»{1:$}                                                    |
79      {10:qwer}tyuiop{1:$}                                                 |
80      {10:口口}=口口{1:$}                                                  |
81      {10:zxcv}bnm{1:$}                                                    |
82                                                                  |
83    ]])
84  end)
85
86  it('works with blockwise selection with width', function()
87    exec_lua(function()
88      local ns = vim.api.nvim_create_namespace('')
89      vim.hl.range(0, ns, 'Search', { 0, 4 }, { 4, 7 }, { regtype = '\0226' })
90    end)
91    screen:expect([[
92      ^asdf{10:ghjkl}{1:$}                                                  |
93      «口={10:口»}{1:$}                                                    |
94      qwer{10:tyuiop}{1:$}                                                 |
95      口口{10:=口口}{1:$}                                                  |
96      zxcv{10:bnm}{1:$}                                                    |
97                                                                  |
98    ]])
99  end)
100
101  it('can use -1 or v:maxcol to indicate end of line', function()
102    exec_lua(function()
103      local ns = vim.api.nvim_create_namespace('')
104      vim.hl.range(0, ns, 'Search', { 0, 4 }, { 1, -1 }, {})
105      vim.hl.range(0, ns, 'Search', { 2, 6 }, { 3, vim.v.maxcol }, {})
106    end)
107    screen:expect([[
108      ^asdf{10:ghjkl}{100:$}                                                  |
109      {10:«口=口»}{100:$}                                                    |
110      qwerty{10:uiop}{100:$}                                                 |
111      {10:口口=口口}{100:$}                                                  |
112      zxcvbnm{1:$}                                                    |
113                                                                  |
114    ]])
115  end)
116
117  it('removes highlight after `timeout`', function()
118    local timeout = 300
119    exec_lua(function()
120      local ns = vim.api.nvim_create_namespace('')
121      vim.hl.range(0, ns, 'Search', { 0, 0 }, { 4, 0 }, { timeout = timeout })
122    end)
123    screen:expect({
124      grid = [[
125      {10:^asdfghjkl}{100:$}                                                  |
126      {10:«口=口»}{100:$}                                                    |
127      {10:qwertyuiop}{100:$}                                                 |
128      {10:口口=口口}{1:$}                                                  |
129      zxcvbnm{1:$}                                                    |
130                                                                  |
131    ]],
132      timeout = timeout / 3,
133    })
134    -- The deferred `range_hl_clear` works even if a different buffer was switched-to.
135    exec_lua(function()
136      local orig = vim.api.nvim_get_current_buf()
137      vim.api.nvim_set_current_buf(vim.api.nvim_create_buf(false, true))
138      vim.wait(timeout * 2)
139      vim.api.nvim_set_current_buf(orig)
140    end)
141    screen:expect([[
142      ^asdfghjkl{1:$}                                                  |
143      «口=口»{1:$}                                                    |
144      qwertyuiop{1:$}                                                 |
145      口口=口口{1:$}                                                  |
146      zxcvbnm{1:$}                                                    |
147                                                                  |
148    ]])
149  end)
150
151  it('shows multiple highlights with different timeouts simultaneously', function()
152    local timeout1 = 300
153    local timeout2 = 600
154    exec_lua(function()
155      local ns = vim.api.nvim_create_namespace('')
156      vim.hl.range(0, ns, 'Search', { 0, 0 }, { 4, 0 }, { timeout = timeout1 })
157      vim.hl.range(0, ns, 'Search', { 2, 6 }, { 3, 5 }, { timeout = timeout2 })
158    end)
159    screen:expect({
160      grid = [[
161        {10:^asdfghjkl}{100:$}                                                  |
162        {10:«口=口»}{100:$}                                                    |
163        {10:qwertyuiop}{100:$}                                                 |
164        {10:口口=口口}{1:$}                                                  |
165        zxcvbnm{1:$}                                                    |
166                                                                    |
167      ]],
168      timeout = timeout1 / 3,
169    })
170    screen:expect({
171      grid = [[
172        ^asdfghjkl{1:$}                                                  |
173        «口=口»{1:$}                                                    |
174        qwerty{10:uiop}{100:$}                                                 |
175        {10:口口}=口口{1:$}                                                  |
176        zxcvbnm{1:$}                                                    |
177                                                                    |
178      ]],
179      timeout = timeout1 + ((timeout2 - timeout1) / 3),
180    })
181    screen:expect([[
182      ^asdfghjkl{1:$}                                                  |
183      «口=口»{1:$}                                                    |
184      qwertyuiop{1:$}                                                 |
185      口口=口口{1:$}                                                  |
186      zxcvbnm{1:$}                                                    |
187                                                                  |
188    ]])
189  end)
190
191  it('allows cancelling a highlight before timeout', function()
192    exec_lua(function()
193      local timeout = 3000
194      local range_timer
195      local range_hl_clear
196      local ns = vim.api.nvim_create_namespace('')
197      range_timer, range_hl_clear = vim.hl.range(
198        0,
199        ns,
200        'Search',
201        { 0, 0 },
202        { 4, 0 },
203        { timeout = timeout }
204      )
205      if range_timer and not range_timer:is_closing() then
206        range_timer:close()
207        assert(range_hl_clear)
208        range_hl_clear()
209        range_hl_clear() -- Exercise redundant call
210      end
211    end)
212    screen:expect({
213      grid = [[
214        ^asdfghjkl{1:$}                                                  |
215        «口=口»{1:$}                                                    |
216        qwertyuiop{1:$}                                                 |
217        口口=口口{1:$}                                                  |
218        zxcvbnm{1:$}                                                    |
219                                                                    |
220      ]],
221      unchanged = true,
222    })
223  end)
224end)
225
226describe('vim.hl.hl_op', function()
227  before_each(function()
228    clear()
229  end)
230
231  it('no errors even if buffer is wiped before timeout', function()
232    command('new')
233    n.feed('ifoo<esc>') -- set '[, ']
234    exec_lua(function()
235      vim.hl.hl_op({
236        timeout = 10,
237        on_macro = true,
238        event = { operator = 'y', regtype = 'v' },
239      })
240      vim.cmd('bwipeout!')
241    end)
242    vim.uv.sleep(10)
243    n.feed('<cr>') -- avoid hang if error message exists
244    eq('', eval('v:errmsg'))
245  end)
246
247  it('does not close timer twice', function()
248    exec_lua(function()
249      vim.hl.hl_op({ timeout = 10, on_macro = true, event = { operator = 'y' } })
250      vim.uv.sleep(10)
251      vim.schedule(function()
252        vim.hl.hl_op({ timeout = 0, on_macro = true, event = { operator = 'y' } })
253      end)
254    end)
255    eq('', eval('v:errmsg'))
256  end)
257
258  it('does not show in another window', function()
259    command('vsplit')
260    exec_lua(function()
261      vim.api.nvim_buf_set_mark(0, '[', 1, 1, {})
262      vim.api.nvim_buf_set_mark(0, ']', 1, 1, {})
263      vim.hl.hl_op({ timeout = math.huge, on_macro = true, event = { operator = 'y' } })
264    end)
265    local ns = api.nvim_create_namespace('nvim.hl.events')
266    local win = api.nvim_get_current_win()
267    eq({ win }, api.nvim__ns_get(ns).wins)
268    command('wincmd w')
269    eq({ win }, api.nvim__ns_get(ns).wins)
270    -- Use a new vim.hl.hl_op() call to cancel the previous timer
271    exec_lua(function()
272      vim.hl.hl_op({ timeout = 0, on_macro = true, event = { operator = 'y' } })
273    end)
274  end)
275
276  it('removes old highlight if new one is created before old timeout', function()
277    command('vnew')
278    exec_lua(function()
279      vim.api.nvim_buf_set_mark(0, '[', 1, 1, {})
280      vim.api.nvim_buf_set_mark(0, ']', 1, 1, {})
281      vim.hl.hl_op({ timeout = math.huge, on_macro = true, event = { operator = 'y' } })
282    end)
283    local ns = api.nvim_create_namespace('nvim.hl.events')
284    eq(api.nvim_get_current_win(), api.nvim__ns_get(ns).wins[1])
285    command('wincmd w')
286    exec_lua(function()
287      vim.api.nvim_buf_set_mark(0, '[', 1, 1, {})
288      vim.api.nvim_buf_set_mark(0, ']', 1, 1, {})
289      vim.hl.hl_op({ timeout = math.huge, on_macro = true, event = { operator = 'y' } })
290    end)
291    local win = api.nvim_get_current_win()
292    eq({ win }, api.nvim__ns_get(ns).wins)
293    command('wincmd w')
294    eq({ win }, api.nvim__ns_get(ns).wins)
295    -- Use a new vim.hl.hl_op() call to cancel the previous timer
296    exec_lua(function()
297      vim.hl.hl_op({ timeout = 0, on_macro = true, event = { operator = 'y' } })
298    end)
299  end)
300
301  it('highlights last character with exclusive motion', function()
302    local screen = Screen.new(60, 4)
303    screen:add_extra_attr_ids({
304      [100] = { foreground = Screen.colors.Blue, background = Screen.colors.Yellow, bold = true },
305    })
306    command('autocmd TextYankPost * lua vim.hl.hl_op{timeout=100000}')
307    api.nvim_buf_set_lines(0, 0, -1, true, {
308      [[foo(bar) 'baz']],
309      [[foo(bar) 'baz']],
310    })
311    n.feed('yw')
312    screen:expect([[
313      {2:^foo}(bar) 'baz'                                              |
314      foo(bar) 'baz'                                              |
315      {1:~                                                           }|
316                                                                  |
317    ]])
318    n.feed("yi'")
319    screen:expect([[
320      foo(bar) '{2:^baz}'                                              |
321      foo(bar) 'baz'                                              |
322      {1:~                                                           }|
323                                                                  |
324    ]])
325    n.feed('yvj')
326    screen:expect([[
327      foo(bar) '{2:^baz'}                                              |
328      {2:foo(bar) '}baz'                                              |
329      {1:~                                                           }|
330                                                                  |
331    ]])
332    -- Use a new vim.hl.hl_op() call to cancel the previous timer
333    exec_lua(function()
334      vim.hl.hl_op({ timeout = 0, on_macro = true, event = { operator = 'y' } })
335    end)
336  end)
337
338  it('highlights TextPutPost', function()
339    local screen = Screen.new(60, 4)
340    screen:add_extra_attr_ids({
341      [100] = { foreground = Screen.colors.Blue, background = Screen.colors.Yellow, bold = true },
342    })
343    command('autocmd TextPutPost * lua vim.hl.hl_op{timeout=100000}')
344    api.nvim_buf_set_lines(0, 0, -1, true, {
345      [[foo(bar) '1']],
346      [[foo(bar) '2']],
347    })
348    n.feed('ggyyp')
349    screen:expect([[
350      foo(bar) '1'                                                |
351      {2:^foo(bar) '1'}                                                |
352      foo(bar) '2'                                                |
353                                                                  |
354    ]])
355    -- Use a new vim.hl.hl_op() call to cancel the previous timer
356    exec_lua(function()
357      vim.hl.hl_op({ timeout = 0, on_macro = true, event = { operator = 'p' } })
358    end)
359  end)
360end)