robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
10.9 KiB443 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear, api = n.clear, n.api
7local eq = t.eq
8local command = n.command
9
10describe('ui/cursor', function()
11  local screen
12
13  before_each(function()
14    clear()
15    screen = Screen.new(25, 5)
16  end)
17
18  it("'guicursor' is published as a UI event", function()
19    local expected_mode_info = {
20      [1] = {
21        blinkoff = 0,
22        blinkon = 0,
23        blinkwait = 0,
24        cell_percentage = 0,
25        cursor_shape = 'block',
26        name = 'normal',
27        hl_id = 0,
28        id_lm = 0,
29        attr = {},
30        attr_lm = {},
31        mouse_shape = 0,
32        short_name = 'n',
33      },
34      [2] = {
35        blinkoff = 0,
36        blinkon = 0,
37        blinkwait = 0,
38        cell_percentage = 0,
39        cursor_shape = 'block',
40        name = 'visual',
41        hl_id = 0,
42        id_lm = 0,
43        attr = {},
44        attr_lm = {},
45        mouse_shape = 0,
46        short_name = 'v',
47      },
48      [3] = {
49        blinkoff = 0,
50        blinkon = 0,
51        blinkwait = 0,
52        cell_percentage = 25,
53        cursor_shape = 'vertical',
54        name = 'insert',
55        hl_id = 0,
56        id_lm = 0,
57        attr = {},
58        attr_lm = {},
59        mouse_shape = 0,
60        short_name = 'i',
61      },
62      [4] = {
63        blinkoff = 0,
64        blinkon = 0,
65        blinkwait = 0,
66        cell_percentage = 20,
67        cursor_shape = 'horizontal',
68        name = 'replace',
69        hl_id = 0,
70        id_lm = 0,
71        attr = {},
72        attr_lm = {},
73        mouse_shape = 0,
74        short_name = 'r',
75      },
76      [5] = {
77        blinkoff = 0,
78        blinkon = 0,
79        blinkwait = 0,
80        cell_percentage = 0,
81        cursor_shape = 'block',
82        name = 'cmdline_normal',
83        hl_id = 0,
84        id_lm = 0,
85        attr = {},
86        attr_lm = {},
87        mouse_shape = 0,
88        short_name = 'c',
89      },
90      [6] = {
91        blinkoff = 0,
92        blinkon = 0,
93        blinkwait = 0,
94        cell_percentage = 25,
95        cursor_shape = 'vertical',
96        name = 'cmdline_insert',
97        hl_id = 0,
98        id_lm = 0,
99        attr = {},
100        attr_lm = {},
101        mouse_shape = 0,
102        short_name = 'ci',
103      },
104      [7] = {
105        blinkoff = 0,
106        blinkon = 0,
107        blinkwait = 0,
108        cell_percentage = 20,
109        cursor_shape = 'horizontal',
110        name = 'cmdline_replace',
111        hl_id = 0,
112        id_lm = 0,
113        attr = {},
114        attr_lm = {},
115        mouse_shape = 0,
116        short_name = 'cr',
117      },
118      [8] = {
119        blinkoff = 0,
120        blinkon = 0,
121        blinkwait = 0,
122        cell_percentage = 20,
123        cursor_shape = 'horizontal',
124        name = 'operator',
125        hl_id = 0,
126        id_lm = 0,
127        attr = {},
128        attr_lm = {},
129        mouse_shape = 0,
130        short_name = 'o',
131      },
132      [9] = {
133        blinkoff = 0,
134        blinkon = 0,
135        blinkwait = 0,
136        cell_percentage = 25,
137        cursor_shape = 'vertical',
138        name = 'visual_select',
139        hl_id = 0,
140        id_lm = 0,
141        attr = {},
142        attr_lm = {},
143        mouse_shape = 0,
144        short_name = 've',
145      },
146      [10] = {
147        name = 'cmdline_hover',
148        mouse_shape = 0,
149        short_name = 'e',
150      },
151      [11] = {
152        name = 'statusline_hover',
153        mouse_shape = 0,
154        short_name = 's',
155      },
156      [12] = {
157        name = 'statusline_drag',
158        mouse_shape = 0,
159        short_name = 'sd',
160      },
161      [13] = {
162        name = 'vsep_hover',
163        mouse_shape = 0,
164        short_name = 'vs',
165      },
166      [14] = {
167        name = 'vsep_drag',
168        mouse_shape = 0,
169        short_name = 'vd',
170      },
171      [15] = {
172        name = 'more',
173        mouse_shape = 0,
174        short_name = 'm',
175      },
176      [16] = {
177        name = 'more_lastline',
178        mouse_shape = 0,
179        short_name = 'ml',
180      },
181      [17] = {
182        blinkoff = 0,
183        blinkon = 0,
184        blinkwait = 0,
185        cell_percentage = 0,
186        cursor_shape = 'block',
187        name = 'showmatch',
188        hl_id = 0,
189        id_lm = 0,
190        attr = {},
191        attr_lm = {},
192        short_name = 'sm',
193      },
194      [18] = {
195        blinkoff = 500,
196        blinkon = 500,
197        blinkwait = 0,
198        cell_percentage = 0,
199        cursor_shape = 'block',
200        name = 'terminal',
201        hl_id = 3,
202        id_lm = 3,
203        attr = { reverse = true },
204        attr_lm = { reverse = true },
205        short_name = 't',
206      },
207    }
208
209    screen:expect(function()
210      -- Default 'guicursor', published on startup.
211      eq(expected_mode_info, screen._mode_info)
212      eq(true, screen._cursor_style_enabled)
213      eq('normal', screen.mode)
214    end)
215
216    -- Event is published ONLY if the cursor style changed.
217    screen._mode_info = nil
218    command("echo 'test'")
219    screen:expect {
220      grid = [[
221      ^                         |
222      {1:~                        }|*3
223      test                     |
224    ]],
225      condition = function()
226        eq(nil, screen._mode_info)
227      end,
228    }
229
230    -- Change the cursor style.
231    n.command('hi Cursor guibg=DarkGray')
232    n.command(
233      'set guicursor=n-v-c:block,i-ci-ve:ver25,r-cr-o:hor20'
234        .. ',a:blinkwait700-blinkoff400-blinkon250-Cursor/lCursor'
235        .. ',sm:block-blinkwait175-blinkoff150-blinkon175'
236    )
237
238    -- Update the expected values.
239    for _, m in ipairs(expected_mode_info) do
240      if m.name == 'showmatch' then
241        if m.blinkon then
242          m.blinkon = 175
243        end
244        if m.blinkoff then
245          m.blinkoff = 150
246        end
247        if m.blinkwait then
248          m.blinkwait = 175
249        end
250      else
251        if m.blinkon then
252          m.blinkon = 250
253        end
254        if m.blinkoff then
255          m.blinkoff = 400
256        end
257        if m.blinkwait then
258          m.blinkwait = 700
259        end
260      end
261      if m.hl_id then
262        m.hl_id = 67
263        m.attr = { background = Screen.colors.DarkGray }
264      end
265      if m.id_lm then
266        m.id_lm = 82
267        m.attr_lm = {}
268      end
269    end
270
271    -- Assert the new expectation.
272    screen:expect(function()
273      for i, v in ipairs(expected_mode_info) do
274        eq(v, screen._mode_info[i])
275      end
276      eq(true, screen._cursor_style_enabled)
277      eq('normal', screen.mode)
278    end)
279
280    -- Change hl groups only, should update the styles
281    n.command('hi Cursor guibg=Red')
282    n.command('hi lCursor guibg=Green')
283
284    -- Update the expected values.
285    for _, m in ipairs(expected_mode_info) do
286      if m.hl_id then
287        m.attr = { background = Screen.colors.Red }
288      end
289      if m.id_lm then
290        m.attr_lm = { background = Screen.colors.Green }
291      end
292    end
293    -- Assert the new expectation.
294    screen:expect(function()
295      eq(expected_mode_info, screen._mode_info)
296      eq(true, screen._cursor_style_enabled)
297      eq('normal', screen.mode)
298    end)
299
300    -- update the highlight again to hide cursor
301    n.command('hi Cursor blend=100')
302
303    for _, m in ipairs(expected_mode_info) do
304      if m.hl_id then
305        m.attr = { background = Screen.colors.Red, blend = 100 }
306      end
307    end
308    screen:expect {
309      grid = [[
310      ^                         |
311      {1:~                        }|*3
312      test                     |
313    ]],
314      condition = function()
315        eq(expected_mode_info, screen._mode_info)
316      end,
317    }
318
319    -- Another cursor style.
320    api.nvim_set_option_value(
321      'guicursor',
322      'n-v-c:ver35-blinkwait171-blinkoff172-blinkon173'
323        .. ',ve:hor35,o:ver50,i-ci:block,r-cr:hor90,sm:ver42',
324      {}
325    )
326    screen:expect(function()
327      local named = {}
328      for _, m in ipairs(screen._mode_info) do
329        named[m.name] = m
330      end
331      eq('vertical', named.normal.cursor_shape)
332      eq(35, named.normal.cell_percentage)
333      eq('horizontal', named.visual_select.cursor_shape)
334      eq(35, named.visual_select.cell_percentage)
335      eq('vertical', named.operator.cursor_shape)
336      eq(50, named.operator.cell_percentage)
337      eq('block', named.insert.cursor_shape)
338      eq('vertical', named.showmatch.cursor_shape)
339      eq(90, named.cmdline_replace.cell_percentage)
340      eq(171, named.normal.blinkwait)
341      eq(172, named.normal.blinkoff)
342      eq(173, named.normal.blinkon)
343      eq(42, named.showmatch.cell_percentage)
344    end)
345
346    -- If there is no setting for guicursor, it becomes the default setting.
347    api.nvim_set_option_value(
348      'guicursor',
349      'n:ver35-blinkwait171-blinkoff172-blinkon173-Cursor/lCursor',
350      {}
351    )
352    screen:expect(function()
353      for _, m in ipairs(screen._mode_info) do
354        if m.name ~= 'normal' then
355          eq('block', m.cursor_shape or 'block')
356          eq(0, m.blinkon or 0)
357          eq(0, m.blinkoff or 0)
358          eq(0, m.blinkwait or 0)
359          eq(0, m.hl_id or 0)
360          eq(0, m.id_lm or 0)
361        end
362      end
363    end)
364  end)
365
366  it("empty 'guicursor' sets cursor_shape=block in all modes", function()
367    api.nvim_set_option_value('guicursor', '', {})
368    screen:expect(function()
369      -- Empty 'guicursor' sets enabled=false.
370      eq(false, screen._cursor_style_enabled)
371      for _, m in ipairs(screen._mode_info) do
372        if m['cursor_shape'] ~= nil then
373          eq('block', m.cursor_shape)
374          eq(0, m.blinkon)
375          eq(0, m.hl_id)
376          eq(0, m.id_lm)
377        end
378      end
379    end)
380  end)
381
382  it("'set all&' reapplies 'guicursor'", function()
383    command('set guicursor=n:ver25')
384    screen:expect(function()
385      eq('vertical', screen._mode_info[1].cursor_shape)
386      eq(25, screen._mode_info[1].cell_percentage)
387    end)
388
389    command('set all&')
390    screen:expect(function()
391      eq('block', screen._mode_info[1].cursor_shape)
392      eq(0, screen._mode_info[1].blinkon)
393      eq(0, screen._mode_info[1].blinkoff)
394      eq(true, screen._cursor_style_enabled)
395    end)
396  end)
397
398  it(':sleep does not hide cursor when sleeping', function()
399    n.feed(':sleep 300m | echo 42')
400    screen:expect([[
401                               |
402      {1:~                        }|*3
403      :sleep 300m | echo 42^    |
404    ]])
405    n.feed('\n')
406    screen:expect({
407      grid = [[
408      ^                         |
409      {1:~                        }|*3
410      :sleep 300m | echo 42    |
411    ]],
412      timeout = 100,
413    })
414    screen:expect([[
415      ^                         |
416      {1:~                        }|*3
417      42                       |
418    ]])
419  end)
420
421  it(':sleep! hides cursor when sleeping', function()
422    n.feed(':sleep! 300m | echo 42')
423    screen:expect([[
424                               |
425      {1:~                        }|*3
426      :sleep! 300m | echo 42^   |
427    ]])
428    n.feed('\n')
429    screen:expect({
430      grid = [[
431                               |
432      {1:~                        }|*3
433      :sleep! 300m | echo 42   |
434    ]],
435      timeout = 100,
436    })
437    screen:expect([[
438      ^                         |
439      {1:~                        }|*3
440      42                       |
441    ]])
442  end)
443end)