robin/laufey

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

Barrett Ruthfix(messages): `inputlist()` offers the mouse when it cannot be used (#41280)fc3f71fb7d

master
16.8 KiB545 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 eq = t.eq
7local pcall_err = t.pcall_err
8local feed = n.feed
9local api = n.api
10local clear = n.clear
11local source = n.source
12local command = n.command
13local async_meths = n.async_meths
14local NIL = vim.NIL
15
16local screen
17
18before_each(function()
19  clear()
20  screen = Screen.new(25, 5)
21  source([[
22    hi Test ctermfg=Red guifg=Red term=bold
23    function CustomCompl(...)
24      return 'TEST'
25    endfunction
26    function CustomListCompl(...)
27      return ['FOO']
28    endfunction
29
30    highlight RBP1 guibg=Red
31    highlight RBP2 guibg=Yellow
32    highlight RBP3 guibg=Green
33    highlight RBP4 guibg=Blue
34    let g:NUM_LVLS = 4
35    function Redraw()
36      redraw!
37      return ''
38    endfunction
39    cnoremap <expr> {REDRAW} Redraw()
40    function RainBowParens(cmdline)
41      let ret = []
42      let i = 0
43      let lvl = 0
44      while i < len(a:cmdline)
45        if a:cmdline[i] is# '('
46          call add(ret, [i, i + 1, 'RBP' . ((lvl % g:NUM_LVLS) + 1)])
47          let lvl += 1
48        elseif a:cmdline[i] is# ')'
49          let lvl -= 1
50          call add(ret, [i, i + 1, 'RBP' . ((lvl % g:NUM_LVLS) + 1)])
51        endif
52        let i += 1
53      endwhile
54      return ret
55    endfunction
56  ]])
57  screen:set_default_attr_ids({
58    EOB = { bold = true, foreground = Screen.colors.Blue1 },
59    T = { foreground = Screen.colors.Red },
60    RBP1 = { background = Screen.colors.Red },
61    RBP2 = { background = Screen.colors.Yellow },
62    RBP3 = { background = Screen.colors.Green },
63    RBP4 = { background = Screen.colors.Blue },
64    SEP = { bold = true, reverse = true },
65    CONFIRM = { bold = true, foreground = Screen.colors.SeaGreen4 },
66  })
67end)
68
69describe('input()', function()
70  it('works with multiline prompts', function()
71    feed([[:call input("Test\nFoo")<CR>]])
72    screen:expect([[
73                               |
74      {EOB:~                        }|
75      {SEP:                         }|
76      Test                     |
77      Foo^                      |
78    ]])
79  end)
80  it('works with multiline prompts and :echohl', function()
81    feed([[:echohl Test | call input("Test\nFoo")<CR>]])
82    screen:expect([[
83                               |
84      {EOB:~                        }|
85      {SEP:                         }|
86      {T:Test}                     |
87      {T:Foo}^                      |
88    ]])
89    command('redraw!')
90    screen:expect([[
91                               |
92      {EOB:~                        }|*3
93      {T:Foo}^                      |
94    ]])
95  end)
96  it('allows unequal numeric arguments when using multiple args', function()
97    command('echohl Test')
98    feed([[:call input(1, 2)<CR>]])
99    screen:expect([[
100                               |
101      {EOB:~                        }|*3
102      {T:1}2^                       |
103    ]])
104    feed('<BS>')
105    screen:expect([[
106                               |
107      {EOB:~                        }|*3
108      {T:1}^                        |
109    ]])
110  end)
111  it('allows unequal numeric values when using {opts} dict', function()
112    command('echohl Test')
113    api.nvim_set_var('opts', { prompt = 1, default = 2, cancelreturn = 3 })
114    feed([[:echo input(opts)<CR>]])
115    screen:expect([[
116                               |
117      {EOB:~                        }|*3
118      {T:1}2^                       |
119    ]])
120    feed('<BS>')
121    screen:expect([[
122                               |
123      {EOB:~                        }|*3
124      {T:1}^                        |
125    ]])
126    feed('<Esc>')
127    screen:expect([[
128      ^                         |
129      {EOB:~                        }|*3
130      {T:3}                        |
131    ]])
132  end)
133  it('works with redraw', function()
134    command('echohl Test')
135    api.nvim_set_var('opts', { prompt = 'Foo>', default = 'Bar' })
136    feed([[:echo inputdialog(opts)<CR>]])
137    screen:expect([[
138                               |
139      {EOB:~                        }|*3
140      {T:Foo>}Bar^                  |
141    ]])
142    command('mode')
143    screen:expect {
144      grid = [[
145                               |
146      {EOB:~                        }|*3
147      {T:Foo>}Bar^                  |
148    ]],
149      reset = true,
150    }
151    feed('<BS>')
152    screen:expect([[
153                               |
154      {EOB:~                        }|*3
155      {T:Foo>}Ba^                   |
156    ]])
157    command('mode')
158    screen:expect {
159      grid = [[
160                               |
161      {EOB:~                        }|*3
162      {T:Foo>}Ba^                   |
163    ]],
164      reset = true,
165    }
166  end)
167  it('allows omitting everything with dict argument', function()
168    command('echohl Test')
169    feed([[:call input({})<CR>]])
170    screen:expect([[
171                               |
172      {EOB:~                        }|*3
173      ^                         |
174    ]])
175  end)
176  it('supports completion', function()
177    feed(':let var = input("", "", "custom,CustomCompl")<CR>')
178    feed('<Tab><CR>')
179    eq('TEST', api.nvim_get_var('var'))
180
181    feed(':let var = input({"completion": "customlist,CustomListCompl"})<CR>')
182    feed('<Tab><CR>')
183    eq('FOO', api.nvim_get_var('var'))
184  end)
185  it('supports cancelreturn', function()
186    feed(':let var = input({"cancelreturn": "BAR"})<CR>')
187    feed('<Esc>')
188    eq('BAR', api.nvim_get_var('var'))
189    feed(':let var = input({"cancelreturn": []})<CR>')
190    feed('<Esc>')
191    eq({}, api.nvim_get_var('var'))
192    feed(':let var = input({"cancelreturn": v:false})<CR>')
193    feed('<Esc>')
194    eq(false, api.nvim_get_var('var'))
195    feed(':let var = input({"cancelreturn": v:null})<CR>')
196    feed('<Esc>')
197    eq(NIL, api.nvim_get_var('var'))
198  end)
199  it('supports default string', function()
200    feed(':let var = input("", "DEF1")<CR>')
201    feed('<CR>')
202    eq('DEF1', api.nvim_get_var('var'))
203
204    feed(':let var = input({"default": "DEF2"})<CR>')
205    feed('<CR>')
206    eq('DEF2', api.nvim_get_var('var'))
207  end)
208  it('errors out on invalid inputs', function()
209    eq('Vim(call):E730: Using a List as a String', pcall_err(command, 'call input([])'))
210    eq('Vim(call):E730: Using a List as a String', pcall_err(command, 'call input("", [])'))
211    eq('Vim(call):E730: Using a List as a String', pcall_err(command, 'call input("", "", [])'))
212    eq('Vim(call):E730: Using a List as a String', pcall_err(command, 'call input({"prompt": []})'))
213    eq(
214      'Vim(call):E730: Using a List as a String',
215      pcall_err(command, 'call input({"default": []})')
216    )
217    eq(
218      'Vim(call):E730: Using a List as a String',
219      pcall_err(command, 'call input({"completion": []})')
220    )
221    eq(
222      'Vim(call):E5050: {opts} must be the only argument',
223      pcall_err(command, 'call input({}, "default")')
224    )
225    eq(
226      'Vim(call):E118: Too many arguments for function: input',
227      pcall_err(command, 'call input("prompt> ", "default", "file", "extra")')
228    )
229  end)
230  it('supports highlighting', function()
231    command('nnoremap <expr> X input({"highlight": "RainBowParens"})[-1]')
232    feed([[X]])
233    feed('(())')
234    screen:expect([[
235                               |
236      {EOB:~                        }|*3
237      {RBP1:(}{RBP2:()}{RBP1:)}^                     |
238    ]])
239  end)
240  it('is not hidden by :silent', function()
241    feed([[:silent call input('Foo: ')<CR>]])
242    screen:expect([[
243                               |
244      {EOB:~                        }|
245      {SEP:                         }|
246      Foo: ^                    |
247                               |
248    ]])
249    feed('Bar')
250    screen:expect([[
251                               |
252      {EOB:~                        }|
253      {SEP:                         }|
254      Foo: Bar^                 |
255                               |
256    ]])
257    feed('<CR>')
258  end)
259end)
260describe('inputdialog()', function()
261  it('works with multiline prompts', function()
262    feed([[:call inputdialog("Test\nFoo")<CR>]])
263    screen:expect([[
264                               |
265      {EOB:~                        }|
266      {SEP:                         }|
267      Test                     |
268      Foo^                      |
269    ]])
270  end)
271  it('works with multiline prompts and :echohl', function()
272    feed([[:echohl Test | call inputdialog("Test\nFoo")<CR>]])
273    screen:expect([[
274                               |
275      {EOB:~                        }|
276      {SEP:                         }|
277      {T:Test}                     |
278      {T:Foo}^                      |
279    ]])
280    command('redraw!')
281    screen:expect([[
282                               |
283      {EOB:~                        }|*3
284      {T:Foo}^                      |
285    ]])
286  end)
287  it('allows unequal numeric arguments when using multiple args', function()
288    command('echohl Test')
289    feed([[:call inputdialog(1, 2)<CR>]])
290    screen:expect([[
291                               |
292      {EOB:~                        }|*3
293      {T:1}2^                       |
294    ]])
295    feed('<BS>')
296    screen:expect([[
297                               |
298      {EOB:~                        }|*3
299      {T:1}^                        |
300    ]])
301  end)
302  it('allows unequal numeric values when using {opts} dict', function()
303    command('echohl Test')
304    api.nvim_set_var('opts', { prompt = 1, default = 2, cancelreturn = 3 })
305    feed([[:echo input(opts)<CR>]])
306    screen:expect([[
307                               |
308      {EOB:~                        }|*3
309      {T:1}2^                       |
310    ]])
311    feed('<BS>')
312    screen:expect([[
313                               |
314      {EOB:~                        }|*3
315      {T:1}^                        |
316    ]])
317    feed('<Esc>')
318    screen:expect([[
319      ^                         |
320      {EOB:~                        }|*3
321      {T:3}                        |
322    ]])
323  end)
324  it('works with redraw', function()
325    command('echohl Test')
326    api.nvim_set_var('opts', { prompt = 'Foo>', default = 'Bar' })
327    feed([[:echo input(opts)<CR>]])
328    screen:expect([[
329                               |
330      {EOB:~                        }|*3
331      {T:Foo>}Bar^                  |
332    ]])
333    command('mode')
334    screen:expect {
335      grid = [[
336                               |
337      {EOB:~                        }|*3
338      {T:Foo>}Bar^                  |
339    ]],
340      reset = true,
341    }
342    feed('<BS>')
343    screen:expect([[
344                               |
345      {EOB:~                        }|*3
346      {T:Foo>}Ba^                   |
347    ]])
348    command('mode')
349    screen:expect {
350      grid = [[
351                               |
352      {EOB:~                        }|*3
353      {T:Foo>}Ba^                   |
354    ]],
355      reset = true,
356    }
357  end)
358  it('allows omitting everything with dict argument', function()
359    command('echohl Test')
360    feed(':echo inputdialog({})<CR>')
361    screen:expect([[
362                               |
363      {EOB:~                        }|*3
364      ^                         |
365    ]])
366  end)
367  it('supports completion', function()
368    feed(':let var = inputdialog({"completion": "customlist,CustomListCompl"})<CR>')
369    feed('<Tab><CR>')
370    eq('FOO', api.nvim_get_var('var'))
371  end)
372  it('supports cancelreturn', function()
373    feed(':let var = inputdialog("", "", "CR1")<CR>')
374    feed('<Esc>')
375    eq('CR1', api.nvim_get_var('var'))
376
377    feed(':let var = inputdialog({"cancelreturn": "BAR"})<CR>')
378    feed('<Esc>')
379    eq('BAR', api.nvim_get_var('var'))
380  end)
381  it('supports default string', function()
382    feed(':let var = inputdialog("", "DEF1")<CR>')
383    feed('<CR>')
384    eq('DEF1', api.nvim_get_var('var'))
385
386    feed(':let var = inputdialog({"default": "DEF2"})<CR>')
387    feed('<CR>')
388    eq('DEF2', api.nvim_get_var('var'))
389  end)
390  it('errors out on invalid inputs', function()
391    eq('Vim(call):E730: Using a List as a String', pcall_err(command, 'call inputdialog([])'))
392    eq('Vim(call):E730: Using a List as a String', pcall_err(command, 'call inputdialog("", [])'))
393    eq(
394      'Vim(call):E730: Using a List as a String',
395      pcall_err(command, 'call inputdialog("", "", [])')
396    )
397    eq(
398      'Vim(call):E730: Using a List as a String',
399      pcall_err(command, 'call inputdialog({"prompt": []})')
400    )
401    eq(
402      'Vim(call):E730: Using a List as a String',
403      pcall_err(command, 'call inputdialog({"default": []})')
404    )
405    eq(
406      'Vim(call):E730: Using a List as a String',
407      pcall_err(command, 'call inputdialog({"completion": []})')
408    )
409    eq(
410      'Vim(call):E5050: {opts} must be the only argument',
411      pcall_err(command, 'call inputdialog({}, "default")')
412    )
413    eq(
414      'Vim(call):E118: Too many arguments for function: inputdialog',
415      pcall_err(command, 'call inputdialog("prompt> ", "default", "file", "extra")')
416    )
417  end)
418  it('supports highlighting', function()
419    command('nnoremap <expr> X inputdialog({"highlight": "RainBowParens"})[-1]')
420    feed([[X]])
421    feed('(())')
422    screen:expect([[
423                               |
424      {EOB:~                        }|*3
425      {RBP1:(}{RBP2:()}{RBP1:)}^                     |
426    ]])
427  end)
428end)
429
430describe('inputlist()', function()
431  it("only offers the mouse when 'mouse' applies to cmdline mode", function()
432    screen:try_resize(75, 5)
433    feed(':call inputlist(["foo", "bar"])<CR>')
434    screen:expect({ any = vim.pesc('Type number and <Enter> (q or empty cancels): ') })
435    feed('q')
436    command('set mouse=a')
437    feed(':call inputlist(["foo", "bar"])<CR>')
438    screen:expect({ any = vim.pesc('Type number and <Enter> or click with the mouse') })
439    feed('q')
440  end)
441end)
442
443describe('confirm()', function()
444  it('works', function()
445    api.nvim_set_option_value('more', false, {}) -- Avoid hit-enter prompt
446    api.nvim_set_option_value('laststatus', 2, {})
447    -- screen:expect() calls are needed to avoid feeding input too early
448    screen:expect({ any = '%[No Name%]' })
449
450    async_meths.nvim_command([[let a = confirm('Press O to proceed')]])
451    screen:expect({ any = '{CONFIRM:.+: }' })
452    feed('o')
453    screen:expect({ any = '%[No Name%]' })
454    eq(1, api.nvim_get_var('a'))
455
456    async_meths.nvim_command([[let a = 'Are you sure?'->confirm("&Yes\n&No")]])
457    screen:expect({ any = '{CONFIRM:.+: }' })
458    feed('y')
459    screen:expect({ any = '%[No Name%]' })
460    eq(1, api.nvim_get_var('a'))
461
462    async_meths.nvim_command([[let a = confirm('Are you sure?', "&Yes\n&No")]])
463    screen:expect({ any = '{CONFIRM:.+: }' })
464    feed('n')
465    screen:expect({ any = '%[No Name%]' })
466    eq(2, api.nvim_get_var('a'))
467
468    -- Not possible to match Vim's CTRL-C test here as CTRL-C always sets got_int in Nvim.
469
470    -- confirm() should return 0 when pressing ESC.
471    async_meths.nvim_command([[let a = confirm('Are you sure?', "&Yes\n&No")]])
472    screen:expect({ any = '{CONFIRM:.+: }' })
473    feed('<Esc>')
474    screen:expect({ any = '%[No Name%]' })
475    eq(0, api.nvim_get_var('a'))
476
477    -- Default choice is returned when pressing <CR>.
478    async_meths.nvim_command([[let a = confirm('Are you sure?', "&Yes\n&No")]])
479    screen:expect({ any = '{CONFIRM:.+: }' })
480    feed('<CR>')
481    screen:expect({ any = '%[No Name%]' })
482    eq(1, api.nvim_get_var('a'))
483
484    async_meths.nvim_command([[let a = confirm('Are you sure?', "&Yes\n&No", 2)]])
485    screen:expect({ any = '{CONFIRM:.+: }' })
486    feed('<CR>')
487    screen:expect({ any = '%[No Name%]' })
488    eq(2, api.nvim_get_var('a'))
489
490    async_meths.nvim_command([[let a = confirm('Are you sure?', "&Yes\n&No", 0)]])
491    screen:expect({ any = '{CONFIRM:.+: }' })
492    feed('<CR>')
493    screen:expect({ any = '%[No Name%]' })
494    eq(0, api.nvim_get_var('a'))
495
496    -- Test with the {type} 4th argument
497    for _, type in ipairs({ 'Error', 'Question', 'Info', 'Warning', 'Generic' }) do
498      async_meths.nvim_command(
499        ([[let a = confirm('Are you sure?', "&Yes\n&No", 1, '%s')]]):format(type)
500      )
501      screen:expect({ any = '{CONFIRM:.+: }' })
502      feed('y')
503      screen:expect({ any = '%[No Name%]' })
504      eq(1, api.nvim_get_var('a'))
505    end
506  end)
507
508  it('shows dialog even if :silent #8788', function()
509    command("autocmd BufNewFile * call confirm('test')")
510
511    local function check_and_clear(edit_line)
512      screen:expect([[
513                                 |
514        {SEP:                         }|
515        ]] .. edit_line .. [[
516        {CONFIRM:test}                     |
517        {CONFIRM:[O]k: }^                   |
518      ]])
519      feed('<cr>')
520      command('redraw')
521      command('bdelete!')
522    end
523
524    -- With shortmess-=F
525    command('set shortmess-=F')
526    feed(':edit foo<cr>')
527    check_and_clear('"foo" [New]              |\n')
528
529    -- With shortmess+=F
530    command('set shortmess+=F')
531    feed(':edit foo<cr>')
532    check_and_clear(':edit foo                |\n')
533
534    -- With :silent
535    feed(':silent edit foo<cr>')
536    check_and_clear(':silent edit foo         |\n')
537
538    -- With API (via eval/Vimscript) call and shortmess+=F
539    feed(':call nvim_command("edit x")<cr>')
540    check_and_clear(':call nvim_command("edit |\n')
541
542    async_meths.nvim_command('edit x')
543    check_and_clear('                         |\n')
544  end)
545end)