robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
16.8 KiB523 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, after_each, finally =
5  t.describe, t.it, t.before_each, t.after_each, t.finally
6local clear = n.clear
7local eval = n.eval
8local command = n.command
9local eq = t.eq
10local fn = n.fn
11local api = n.api
12local mkdir = t.mkdir
13local rmdir = n.rmdir
14local write_file = t.write_file
15
16local cursor = n.api.nvim_win_set_cursor
17
18local function buf_word()
19  local word = n.fn.expand('<cWORD>')
20  local bufname = n.fn.fnamemodify(n.fn.bufname('%'), ':t')
21  return { word, bufname }
22end
23
24local function open_helptag()
25  -- n.exec [[:normal! K]]
26  n.exec [[:help!]]
27  local rv = buf_word()
28  if n.fn.winnr('$') > 1 then
29    n.command('close')
30  end
31  return rv
32end
33
34local function set_lines(text)
35  n.exec_lua(
36    [[
37    vim.cmd'%delete _'
38    vim.api.nvim_paste(vim.text.indent(-1, ...), false, -1)
39  ]],
40    text
41  )
42end
43
44describe(':help', function()
45  before_each(clear)
46
47  it('{subject}', function()
48    n.command('helptags ++t $VIMRUNTIME/doc')
49    local function check_tag(cmd, tag)
50      local cmd_ok = t.pcall(n.command, cmd)
51      local found = n.api.nvim_get_current_line():find(tag, 1, true)
52      local errmsg = (not cmd_ok and 'command failed') or (not found and 'tag not found') or '?'
53      assert(
54        cmd_ok and found,
55        string.format('Expected `:%s` to jump to tag `%s`, but %s', cmd, tag, errmsg)
56      )
57      n.command('helpclose')
58    end
59
60    check_tag('help', '*help.txt*')
61    check_tag('help |', '*bar*')
62    check_tag('help "*', '*quotestar*')
63    check_tag('help ch??khealth', '*:checkhealth*')
64
65    check_tag([[help \\star]], [[*/\star*]])
66    check_tag('help /*', [[*/\star*]])
67    check_tag('help ?', '*?*')
68    check_tag('help ??', '*??*')
69    check_tag('help expr-!=?', '*expr-!=?*')
70
71    check_tag('help /<cr>', '*/<CR>*')
72    check_tag([[help %(\\)]], [[*/\%(\)*]])
73    check_tag('help %^', [[/\%^]])
74    check_tag('help /_^G', '/_CTRL-G')
75    check_tag([[help \0]], [[\0]])
76
77    check_tag('help !', '*!*')
78    check_tag('help #{}', '*#{}*')
79    check_tag('help %:8', '*%:8*')
80    check_tag('help &', '*&*')
81    check_tag([[help '']], [[*''*]])
82    check_tag([[help '(]], [[*'(*]])
83    check_tag([[help '0]], [[*'0*]])
84    check_tag([[help 'ac']], [[*'ac'*]])
85    check_tag([[help '{]], [[*'{*]])
86    check_tag('help )', '*)*')
87    check_tag('help +', '*+*')
88
89    check_tag('help +opt', '*++opt*')
90    check_tag('help --', '*--*')
91    check_tag('help -?', '*-?*')
92    check_tag('help .', '*.*')
93    check_tag('help :', '*:*')
94    check_tag([[help :'}]], [[*:'}*]])
95    check_tag('help :,', '*:,*')
96    check_tag('help :<abuf>', '*:<abuf>*')
97    check_tag([[help :\|]], [[*:\bar*]])
98    check_tag([[help :\\|]], [[*:\bar*]])
99    check_tag('help _', '*_*')
100    check_tag('help `', '*`*')
101    check_tag('help `(', '*`(*')
102    check_tag([[help `:ls`.]], [[*:ls*]])
103
104    check_tag('help [', '*[*')
105    check_tag('help [#', '*[#*')
106    check_tag([[help [']], [[*['*]])
107    check_tag('help [(', '*[(*')
108    check_tag('help [++opt]', '*[++opt]*')
109    check_tag('help [:tab:]', '*[:tab:]*')
110    check_tag('help [count]', '*[count]*')
111    check_tag('help :[range]', '*:[range]*')
112    check_tag('help [<space>', '[<Space>')
113    check_tag('help ]_^D', ']_CTRL-D')
114    check_tag('help :set^=', '*:set^=*')
115
116    check_tag([[help $HOME]], [[*$HOME*]])
117
118    check_tag('help <C-pagedown>', '*CTRL-<PageDown>*')
119    check_tag('help ^A', '*CTRL-A*')
120    check_tag('help ^W_+', '*CTRL-W_+*')
121    check_tag('help ^W<up>', '*CTRL-W_<Up>*')
122    check_tag('help ^W>', '*CTRL-W_>*')
123    check_tag('help ^W^]', '*CTRL-W_CTRL-]*')
124    check_tag('help ^W^', '*CTRL-W_^*')
125    check_tag('help ^W|', '*CTRL-W_bar*')
126    check_tag('help ^Wg<tab>', '*CTRL-W_g<Tab>*')
127    check_tag('help ^]', '*CTRL-]*')
128    check_tag('help ^{char}', 'CTRL-{char}')
129    check_tag('help [^L', '[_CTRL-L')
130    check_tag('help <C-', '*<C-*')
131    check_tag('help <S-CR>', '*<S-CR>*')
132    check_tag('help <<', '*<<*')
133    check_tag('help <>', '*<>*')
134    check_tag([[help i^x^y]], '*i_CTRL-X_CTRL-Y*')
135    check_tag([[help CTRL-\_CTRL-N]], [[*CTRL-\_CTRL-N*]])
136    check_tag([[help i_CTRL-U-default]], [[*i_CTRL-U-default*]])
137    check_tag([[help expr-=~?]], [[*expr-=~?*]])
138    check_tag([[help map-CTRL-C]], '*map-CTRL-C*')
139    check_tag([=[help telnet-CTRL-]]=], '*telnet-CTRL-]*')
140    check_tag([[help c_CTRL-SHIFT-V]], '*c_CTRL-SHIFT-V*')
141    check_tag([[help i_CTRL-SHIFT-V]], '*i_CTRL-SHIFT-V*')
142    check_tag([[help c_CTRL-SHIFT-Q]], '*c_CTRL-SHIFT-Q*')
143
144    check_tag([[exe "help i\<C-\>\<C-G>"]], [[*i_CTRL-\_CTRL-G*]])
145    check_tag([[exe "help \<C-V>"]], '*CTRL-V*')
146    check_tag([[exe "help! arglistid([{winnr})"]], '*arglistid()*')
147    check_tag([[exe "help! 'autoindent'."]], [[*'autoindent'*]])
148
149    check_tag('exusage', '*:index*')
150    check_tag('viusage', '*normal-index*')
151
152    -- Test cases for removed exceptions
153    check_tag('help /\\(\\)', '*/\\(\\)*')
154    check_tag('help :s\\=', '*:s\\=*')
155    check_tag([[help expr-']], [[*expr-'*]])
156    check_tag('help expr-barbar', '*expr-barbar*')
157    check_tag([[help s/\9]], [[*s/\9*]])
158    check_tag([[help s/\U]], [[*s/\U*]])
159    check_tag([[help s/\~]], [[*s/\~*]])
160    check_tag([[help \|]], [[*/\bar*]])
161  end)
162
163  it('":help!" (bang + no args) guesses the best tag near cursor', function()
164    n.command('helptags ++t $VIMRUNTIME/doc')
165    -- n.command('enew')
166    -- n.command('set filetype=help')
167    -- n.command [[set keywordprg=:help]]
168
169    -- Failure modes:
170    set_lines ''
171    cursor(0, { 1, 1 })
172    t.matches('E349: No identifier under cursor', t.pcall_err(n.exec, [[:help!]]))
173
174    set_lines 'xxxxxxxxx'
175    cursor(0, { 1, 4 })
176    t.matches('E149: No help for xxxxxxxxx', t.pcall_err(n.exec, [[:help!]]))
177
178    -- Success:
179
180    set_lines 'some plain text'
181    cursor(0, { 1, 5 }) -- on 'plain'
182    eq({ '*ft-plaintex-syntax*', 'syntax.txt' }, open_helptag())
183
184    set_lines ':help command'
185    cursor(0, { 1, 4 })
186    eq({ '*:help*', 'helphelp.txt' }, open_helptag())
187
188    set_lines ' :help command'
189    cursor(0, { 1, 5 })
190    eq({ '*:command*', 'map.txt' }, open_helptag())
191
192    set_lines 'v:version name'
193    cursor(0, { 1, 5 })
194    eq({ '*v:version*', 'vvars.txt' }, open_helptag())
195    cursor(0, { 1, 2 })
196    eq({ '*v:version*', 'vvars.txt' }, open_helptag())
197
198    set_lines "See 'option' for more."
199    cursor(0, { 1, 6 }) -- on 'option'
200    eq({ "*'option'*", 'helphelp.txt' }, open_helptag())
201
202    set_lines ':command-nargs'
203    cursor(0, { 1, 7 }) -- on 'nargs'
204    eq({ '*:command-nargs*', 'map.txt' }, open_helptag())
205
206    set_lines '|("vim.lsp.foldtext()")|'
207    cursor(0, { 1, 10 })
208    eq({ '*vim.lsp.foldtext()*', 'lsp.txt' }, open_helptag())
209
210    set_lines 'nvim_buf_detach_event[{buf}]'
211    cursor(0, { 1, 10 })
212    eq({ '*nvim_buf_detach_event*', 'api.txt' }, open_helptag())
213
214    set_lines '{buf}'
215    cursor(0, { 1, 1 })
216    eq({ '*:buf*', 'windows.txt' }, open_helptag())
217
218    set_lines '(`vim.lsp.ClientConfig`)'
219    cursor(0, { 1, 1 })
220    eq({ '*vim.lsp.ClientConfig*', 'lsp.txt' }, open_helptag())
221
222    set_lines "vim.lsp.enable('clangd')"
223    cursor(0, { 1, 3 })
224    eq({ '*vim.lsp.enable()*', 'lsp.txt' }, open_helptag())
225
226    set_lines "vim.lsp.enable('clangd')"
227    cursor(0, { 1, 6 })
228    eq({ '*vim.lsp.enable()*', 'lsp.txt' }, open_helptag())
229
230    set_lines "vim.lsp.enable('clangd')"
231    cursor(0, { 1, 9 })
232    eq({ '*vim.lsp.enable()*', 'lsp.txt' }, open_helptag())
233
234    set_lines 'assert(vim.lsp.get_client_by_id(client_id))'
235    cursor(0, { 1, 12 })
236    eq({ '*vim.lsp.get_client_by_id()*', 'lsp.txt' }, open_helptag())
237
238    set_lines "vim.api.nvim_create_autocmd('LspAttach', {"
239    cursor(0, { 1, 7 })
240    eq({ '*nvim_create_autocmd()*', 'api.txt' }, open_helptag())
241
242    -- Falls back to <cword> when all trimming fails.
243    set_lines "'@lsp.type.function'"
244    cursor(0, { 1, 2 }) -- on 'lsp'
245    eq({ '*lsp*', 'lsp.txt' }, open_helptag())
246    set_lines "'@lsp.type.function'"
247    cursor(0, { 1, 14 }) -- on 'function'
248    eq({ '*:function*', 'userfunc.txt' }, open_helptag())
249
250    set_lines '  • `@lsp.type.<type>.<ft>` for the type'
251    cursor(0, { 1, 6 }) -- on backtick '`' (byte 6, after 2 spaces + 3-byte '•' + space)
252    eq({ '*lsp*', 'lsp.txt' }, open_helptag())
253
254    set_lines [[
255    - `root_dir` usages akin to >lua
256       root_dir  = require'lspconfig.util'.root_pattern(...)
257    <
258    require'lspconfig.util'.root_pattern(...)
259    ]]
260    cursor(0, { 2, 17 }) -- on "require"
261    eq({ '*require()*', 'luaref.txt' }, open_helptag())
262
263    set_lines '`:lsp restart`. You'
264    cursor(0, { 1, 6 }) -- on "restart"
265    eq({ '*:restart*', 'gui.txt' }, open_helptag())
266
267    --
268    -- Test with actual helpfiles. This affects getcompletion(…,'help') ...
269    --
270
271    n.command(':help lua')
272    n.feed('gg/package.searchpath<cr>')
273    eq({ "vim.cmd.edit(package.searchpath('jit.p',", 'lua.txt' }, buf_word())
274    --               ^ cursor on "package"
275    n.command(':help!')
276    eq({ '*packages*', 'pack.txt' }, buf_word())
277
278    n.command(':help lsp')
279    n.feed('gg/type.<lt>type><cr>')
280    eq({ '`@lsp.type.<type>.<ft>`', 'lsp.txt' }, buf_word())
281    --          ^ cursor on "type"
282    n.command(':help!')
283    eq({ '*type()*', 'vimfn.txt' }, buf_word())
284    n.feed('<c-o>f<lt>')
285    eq({ '`@lsp.type.<type>.<ft>`', 'lsp.txt' }, buf_word())
286    --               ^ cursor on "<"
287    n.command(':help!')
288
289    n.command(':help lsp')
290    n.feed('gg/codelens.run()|<cr>')
291    eq({ '|vim.lsp.codelens.run()|', 'lsp.txt' }, buf_word())
292    --             ^ cursor on "codelens"
293    n.command(':help!')
294    eq({ '*vim.lsp.codelens.run()*', 'lsp.txt' }, buf_word())
295  end)
296
297  it('K resolves generic type names in help', function()
298    command('helptags ++t $VIMRUNTIME/doc')
299    for _, ty in ipairs({
300      'vim.async.Task<R>',
301      'vim.async.Task<R...>',
302      'vim.async.Task<R>[]',
303    }) do
304      command('help lua-async')
305      command('setlocal keywordprg=:help!')
306      assert(fn.search('\\V(`' .. ty .. '`)', 'W') > 0)
307      command('normal! fTK')
308      eq({ '*vim.async.Task*', 'lua-async.txt' }, buf_word())
309    end
310  end)
311
312  it('K resolves nullable type names in help', function()
313    command('helptags ++t $VIMRUNTIME/doc')
314    for _, ref in ipairs({
315      { 'lsp', 'vim.lsp.Client', '?' },
316      { 'diagnostic', 'vim.Diagnostic', '?' },
317      { 'diagnostic', 'vim.Diagnostic', '[]?' },
318    }) do
319      command('help ' .. ref[1])
320      command('setlocal keywordprg=:help!')
321      assert(fn.search('\\V(`' .. ref[2] .. ref[3] .. '`)', 'W') > 0)
322      command('normal! 2lK')
323      eq({ '*' .. ref[2] .. '*', ref[1] .. '.txt' }, buf_word())
324    end
325  end)
326
327  it('window closed makes cursor return to a valid win/buf #9773', function()
328    n.add_builddir_to_rtp()
329    command('help help')
330    eq(1001, fn.win_getid())
331    command('quit')
332    eq(1000, fn.win_getid())
333
334    command('autocmd WinNew * wincmd p')
335
336    command('help help')
337    -- Window 1002 is opened, but the autocmd switches back to 1000 and
338    -- creates the help buffer there instead.
339    eq(1000, fn.win_getid())
340    command('quit')
341    -- Before #9773, Nvim would crash on quitting the help window.
342    eq(1002, fn.win_getid())
343  end)
344
345  it('multibyte help tags work #23975', function()
346    mkdir('Xhelptags')
347    finally(function()
348      rmdir('Xhelptags')
349    end)
350    mkdir('Xhelptags/doc')
351    write_file('Xhelptags/doc/Xhelptags.txt', '*…*')
352    command('helptags Xhelptags/doc')
353    command('set rtp+=Xhelptags')
354    command('help …')
355    eq('*…*', api.nvim_get_current_line())
356  end)
357end)
358
359describe(':helptags', function()
360  before_each(function()
361    for _, sfx in ipairs({ '', '2' }) do
362      fn.mkdir(('Xhelptags%s/doc'):format(sfx), 'p')
363      for _, tag in ipairs({ 'Xa', 'Xb' }) do
364        write_file(('Xhelptags%s/doc/%s%s.txt'):format(sfx, tag, sfx), ('*%s%s*'):format(tag, sfx))
365      end
366    end
367
368    clear()
369    command('set rtp+=Xhelptags,Xhelptags2')
370  end)
371
372  after_each(function()
373    rmdir('Xhelptags')
374    rmdir('Xhelptags2')
375  end)
376
377  it('requires an argument', function()
378    local msg = t.pcall_err(command, 'helptags')
379    eq(true, msg:find('E471') ~= nil)
380  end)
381
382  it('{dir}', function()
383    -- Helpfiles in sub-directories are found and named relative to {dir}.
384    fn.mkdir('Xhelptags/doc/sub', 'p')
385    -- A "|" in a tag would break |links|, and "*Xd" is not closed, so neither is a tag.
386    write_file('Xhelptags/doc/sub/Xc.txt', '*Xc*\n*X|c*\n*Xd\n')
387    -- CRLF helpfile: "\r" must not confuse the parser into finding tags in an example.
388    write_file('Xhelptags/doc/Xe.txt', '*Xe*\r\n>\r\n\t+-----+\r\n\t|/* a.c */  |/* b.c */  |\r\n')
389
390    for _, dir in ipairs({
391      'Xhelptags/doc',
392      './Xhelptags/doc',
393      fn.fnamemodify('Xhelptags/doc', ':p'),
394    }) do
395      command('helptags ' .. fn.fnameescape(dir))
396      eq({
397        'Xa\tXa.txt\t/*Xa*',
398        'Xb\tXb.txt\t/*Xb*',
399        'Xc\tsub/Xc.txt\t/*Xc*',
400        'Xe\tXe.txt\t/*Xe*',
401      }, fn.readfile('Xhelptags/doc/tags'))
402    end
403
404    command('help Xa')
405    eq('*Xa*', api.nvim_get_current_line())
406
407    command('help Xc')
408    eq('*Xc*', api.nvim_get_current_line())
409  end)
410
411  it('ignores tags in examples and resumes after unindented text', function()
412    for _, newline in ipairs({ '\n', '\r\n' }) do
413      write_file(
414        'Xhelptags/doc/Xa.txt',
415        table.concat({
416          '*Xa* *Xz*',
417          'Some ordinary prose.',
418          'prefix*invalid* *invalid*suffix *bad|tag* **',
419          '>lua',
420          '',
421          '  *example*',
422          'End of example.',
423          '',
424          '  *Xc*',
425          'More ordinary prose.',
426          '*Xd*', -- No final newline.
427        }, newline),
428        true
429      )
430      command('helptags Xhelptags/doc')
431      eq({
432        'Xa\tXa.txt\t/*Xa*',
433        'Xb\tXb.txt\t/*Xb*',
434        'Xc\tXa.txt\t/*Xc*',
435        'Xd\tXa.txt\t/*Xd*',
436        'Xz\tXa.txt\t/*Xz*',
437      }, fn.readfile('Xhelptags/doc/tags'))
438    end
439  end)
440
441  it('overwrites existing tags file', function()
442    write_file('Xhelptags/doc/tags', 'Xstale	Xold.txt	/*Xstale*', nil, true)
443    write_file('Xhelptags/doc/Xa.txt', 'no tags here', nil, true)
444    write_file('Xhelptags/doc/Xb.txt', 'no tags here', nil, true)
445
446    command('helptags Xhelptags/doc')
447    eq({}, eval("readfile('Xhelptags/doc/tags')"))
448  end)
449
450  it('reports E150 E151 E152', function()
451    eq(true, t.pcall_err(command, 'helptags Xhelptags/doc/Xa.txt'):find('E150') ~= nil)
452
453    fn.mkdir('Xhelptags/Xempty', 'p')
454    eq(true, t.pcall_err(command, 'helptags Xhelptags/Xempty'):find('E151') ~= nil)
455
456    -- A directory named "tags" cannot be opened for writing.
457    fn.mkdir('Xhelptags/doc/tags', 'p')
458    eq(true, t.pcall_err(command, 'helptags Xhelptags/doc'):find('E152') ~= nil)
459  end)
460
461  it('ALL', function()
462    command('helptags ALL')
463
464    eq(eval("['Xa	Xa.txt	/*Xa*','Xb	Xb.txt	/*Xb*']"), eval("readfile('Xhelptags/doc/tags')"))
465    eq(eval("['Xa2	Xa2.txt	/*Xa2*','Xb2	Xb2.txt	/*Xb2*']"), eval("readfile('Xhelptags2/doc/tags')"))
466
467    command('help Xa2')
468    eq('*Xa2*', api.nvim_get_current_line())
469  end)
470
471  it('++t', function()
472    write_file('Xhelptags/doc/Xa.nlx', '*Xa*', nil, true)
473
474    command('helptags ++t Xhelptags/doc')
475    eq('help-tags	tags	1', eval("readfile('Xhelptags/doc/tags')[-1]"))
476    -- Each language gets a "help-tags" tag naming its own tags file.
477    eq('help-tags	tags-nl	1', eval("readfile('Xhelptags/doc/tags-nl')[-1]"))
478  end)
479
480  it('generates help-tag tag for VIMRUNTIME', function()
481    command('let $VIMRUNTIME="Xhelptags"')
482    command('helptags Xhelptags/doc')
483    eq('help-tags	tags	1', eval("readfile('Xhelptags/doc/tags')[-1]"))
484  end)
485
486  it('errors on duplicate tags', function()
487    -- duplicate tags in different files
488    write_file('Xhelptags/doc/Xd.txt', '*Xa*', nil, true)
489    local msg = t.pcall_err(command, 'helptags Xhelptags/doc')
490    eq(true, msg:find('E154: Duplicate tag "Xa" in Xd.txt and Xa.txt', 1, true) ~= nil)
491
492    -- tags file should still be generated
493    eq(1, eval("filereadable('Xhelptags/doc/tags')"))
494
495    os.remove('Xhelptags/doc/Xd.txt')
496
497    -- duplicate tags in same file
498    write_file('Xhelptags/doc/Xa.txt', '\n*Xa*', nil, true)
499
500    msg = t.pcall_err(command, 'helptags Xhelptags/doc')
501    eq(true, msg:find('E154: Duplicate tag "Xa" in Xa.txt', 1, true) ~= nil)
502
503    eq(1, eval("filereadable('Xhelptags/doc/tags')"))
504
505    -- Duplicates do not abort the run: other languages are still processed.
506    write_file('Xhelptags/doc/Xa.nlx', '*Xnl*', nil, true)
507
508    msg = t.pcall_err(command, 'helptags Xhelptags/doc')
509    eq(true, msg:find('E154') ~= nil)
510    eq(false, msg:find('E5108') ~= nil)
511    eq(1, eval("filereadable('Xhelptags/doc/tags-nl')"))
512  end)
513
514  it('with translated help files', function()
515    write_file('Xhelptags/doc/Xa.nlx', '*Xa*', nil, true)
516    -- The suffix is matched case-insensitively.
517    write_file('Xhelptags/doc/Xc.FRX', '*Xc*', nil, true)
518
519    command('helptags Xhelptags/doc')
520    eq(eval("['Xa	Xa.nlx	/*Xa*']"), eval("readfile('Xhelptags/doc/tags-nl')"))
521    eq(eval("['Xc	Xc.FRX	/*Xc*']"), eval("readfile('Xhelptags/doc/tags-fr')"))
522  end)
523end)