aboutsummaryrefslogtreecommitdiff
path: root/test/functional/ex_cmds/help_spec.lua
blob: 23cc11d1a46eab1b4a1eaaeee9f0c2c3b01040c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local describe, it, before_each, after_each, finally =
  t.describe, t.it, t.before_each, t.after_each, t.finally
local clear = n.clear
local eval = n.eval
local command = n.command
local eq = t.eq
local fn = n.fn
local api = n.api
local mkdir = t.mkdir
local rmdir = n.rmdir
local write_file = t.write_file

local cursor = n.api.nvim_win_set_cursor

local function buf_word()
  local word = n.fn.expand('<cWORD>')
  local bufname = n.fn.fnamemodify(n.fn.bufname('%'), ':t')
  return { word, bufname }
end

local function open_helptag()
  -- n.exec [[:normal! K]]
  n.exec [[:help!]]
  local rv = buf_word()
  if n.fn.winnr('$') > 1 then
    n.command('close')
  end
  return rv
end

local function set_lines(text)
  n.exec_lua(
    [[
    vim.cmd'%delete _'
    vim.api.nvim_paste(vim.text.indent(-1, ...), false, -1)
  ]],
    text
  )
end

describe(':help', function()
  before_each(clear)

  it('{subject}', function()
    n.command('helptags ++t $VIMRUNTIME/doc')
    local function check_tag(cmd, tag)
      local cmd_ok = t.pcall(n.command, cmd)
      local found = n.api.nvim_get_current_line():find(tag, 1, true)
      local errmsg = (not cmd_ok and 'command failed') or (not found and 'tag not found') or '?'
      assert(
        cmd_ok and found,
        string.format('Expected `:%s` to jump to tag `%s`, but %s', cmd, tag, errmsg)
      )
      n.command('helpclose')
    end

    check_tag('help', '*help.txt*')
    check_tag('help |', '*bar*')
    check_tag('help "*', '*quotestar*')
    check_tag('help ch??khealth', '*:checkhealth*')

    check_tag([[help \\star]], [[*/\star*]])
    check_tag('help /*', [[*/\star*]])
    check_tag('help ?', '*?*')
    check_tag('help ??', '*??*')
    check_tag('help expr-!=?', '*expr-!=?*')

    check_tag('help /<cr>', '*/<CR>*')
    check_tag([[help %(\\)]], [[*/\%(\)*]])
    check_tag('help %^', [[/\%^]])
    check_tag('help /_^G', '/_CTRL-G')
    check_tag([[help \0]], [[\0]])

    check_tag('help !', '*!*')
    check_tag('help #{}', '*#{}*')
    check_tag('help %:8', '*%:8*')
    check_tag('help &', '*&*')
    check_tag([[help '']], [[*''*]])
    check_tag([[help '(]], [[*'(*]])
    check_tag([[help '0]], [[*'0*]])
    check_tag([[help 'ac']], [[*'ac'*]])
    check_tag([[help '{]], [[*'{*]])
    check_tag('help )', '*)*')
    check_tag('help +', '*+*')

    check_tag('help +opt', '*++opt*')
    check_tag('help --', '*--*')
    check_tag('help -?', '*-?*')
    check_tag('help .', '*.*')
    check_tag('help :', '*:*')
    check_tag([[help :'}]], [[*:'}*]])
    check_tag('help :,', '*:,*')
    check_tag('help :<abuf>', '*:<abuf>*')
    check_tag([[help :\|]], [[*:\bar*]])
    check_tag([[help :\\|]], [[*:\bar*]])
    check_tag('help _', '*_*')
    check_tag('help `', '*`*')
    check_tag('help `(', '*`(*')
    check_tag([[help `:ls`.]], [[*:ls*]])

    check_tag('help [', '*[*')
    check_tag('help [#', '*[#*')
    check_tag([[help [']], [[*['*]])
    check_tag('help [(', '*[(*')
    check_tag('help [++opt]', '*[++opt]*')
    check_tag('help [:tab:]', '*[:tab:]*')
    check_tag('help [count]', '*[count]*')
    check_tag('help :[range]', '*:[range]*')
    check_tag('help [<space>', '[<Space>')
    check_tag('help ]_^D', ']_CTRL-D')
    check_tag('help :set^=', '*:set^=*')

    check_tag([[help $HOME]], [[*$HOME*]])

    check_tag('help <C-pagedown>', '*CTRL-<PageDown>*')
    check_tag('help ^A', '*CTRL-A*')
    check_tag('help ^W_+', '*CTRL-W_+*')
    check_tag('help ^W<up>', '*CTRL-W_<Up>*')
    check_tag('help ^W>', '*CTRL-W_>*')
    check_tag('help ^W^]', '*CTRL-W_CTRL-]*')
    check_tag('help ^W^', '*CTRL-W_^*')
    check_tag('help ^W|', '*CTRL-W_bar*')
    check_tag('help ^Wg<tab>', '*CTRL-W_g<Tab>*')
    check_tag('help ^]', '*CTRL-]*')
    check_tag('help ^{char}', 'CTRL-{char}')
    check_tag('help [^L', '[_CTRL-L')
    check_tag('help <C-', '*<C-*')
    check_tag('help <S-CR>', '*<S-CR>*')
    check_tag('help <<', '*<<*')
    check_tag('help <>', '*<>*')
    check_tag([[help i^x^y]], '*i_CTRL-X_CTRL-Y*')
    check_tag([[help CTRL-\_CTRL-N]], [[*CTRL-\_CTRL-N*]])
    check_tag([[help i_CTRL-U-default]], [[*i_CTRL-U-default*]])
    check_tag([[help expr-=~?]], [[*expr-=~?*]])
    check_tag([[help map-CTRL-C]], '*map-CTRL-C*')
    check_tag([=[help telnet-CTRL-]]=], '*telnet-CTRL-]*')
    check_tag([[help c_CTRL-SHIFT-V]], '*c_CTRL-SHIFT-V*')
    check_tag([[help i_CTRL-SHIFT-V]], '*i_CTRL-SHIFT-V*')
    check_tag([[help c_CTRL-SHIFT-Q]], '*c_CTRL-SHIFT-Q*')

    check_tag([[exe "help i\<C-\>\<C-G>"]], [[*i_CTRL-\_CTRL-G*]])
    check_tag([[exe "help \<C-V>"]], '*CTRL-V*')
    check_tag([[exe "help! arglistid([{winnr})"]], '*arglistid()*')
    check_tag([[exe "help! 'autoindent'."]], [[*'autoindent'*]])

    check_tag('exusage', '*:index*')
    check_tag('viusage', '*normal-index*')

    -- Test cases for removed exceptions
    check_tag('help /\\(\\)', '*/\\(\\)*')
    check_tag('help :s\\=', '*:s\\=*')
    check_tag([[help expr-']], [[*expr-'*]])
    check_tag('help expr-barbar', '*expr-barbar*')
    check_tag([[help s/\9]], [[*s/\9*]])
    check_tag([[help s/\U]], [[*s/\U*]])
    check_tag([[help s/\~]], [[*s/\~*]])
    check_tag([[help \|]], [[*/\bar*]])
  end)

  it('":help!" (bang + no args) guesses the best tag near cursor', function()
    n.command('helptags ++t $VIMRUNTIME/doc')
    -- n.command('enew')
    -- n.command('set filetype=help')
    -- n.command [[set keywordprg=:help]]

    -- Failure modes:
    set_lines ''
    cursor(0, { 1, 1 })
    t.matches('E349: No identifier under cursor', t.pcall_err(n.exec, [[:help!]]))

    set_lines 'xxxxxxxxx'
    cursor(0, { 1, 4 })
    t.matches('E149: No help for xxxxxxxxx', t.pcall_err(n.exec, [[:help!]]))

    -- Success:

    set_lines 'some plain text'
    cursor(0, { 1, 5 }) -- on 'plain'
    eq({ '*ft-plaintex-syntax*', 'syntax.txt' }, open_helptag())

    set_lines ':help command'
    cursor(0, { 1, 4 })
    eq({ '*:help*', 'helphelp.txt' }, open_helptag())

    set_lines ' :help command'
    cursor(0, { 1, 5 })
    eq({ '*:command*', 'map.txt' }, open_helptag())

    set_lines 'v:version name'
    cursor(0, { 1, 5 })
    eq({ '*v:version*', 'vvars.txt' }, open_helptag())
    cursor(0, { 1, 2 })
    eq({ '*v:version*', 'vvars.txt' }, open_helptag())

    set_lines "See 'option' for more."
    cursor(0, { 1, 6 }) -- on 'option'
    eq({ "*'option'*", 'helphelp.txt' }, open_helptag())

    set_lines ':command-nargs'
    cursor(0, { 1, 7 }) -- on 'nargs'
    eq({ '*:command-nargs*', 'map.txt' }, open_helptag())

    set_lines '|("vim.lsp.foldtext()")|'
    cursor(0, { 1, 10 })
    eq({ '*vim.lsp.foldtext()*', 'lsp.txt' }, open_helptag())

    set_lines 'nvim_buf_detach_event[{buf}]'
    cursor(0, { 1, 10 })
    eq({ '*nvim_buf_detach_event*', 'api.txt' }, open_helptag())

    set_lines '{buf}'
    cursor(0, { 1, 1 })
    eq({ '*:buf*', 'windows.txt' }, open_helptag())

    set_lines '(`vim.lsp.ClientConfig`)'
    cursor(0, { 1, 1 })
    eq({ '*vim.lsp.ClientConfig*', 'lsp.txt' }, open_helptag())

    set_lines "vim.lsp.enable('clangd')"
    cursor(0, { 1, 3 })
    eq({ '*vim.lsp.enable()*', 'lsp.txt' }, open_helptag())

    set_lines "vim.lsp.enable('clangd')"
    cursor(0, { 1, 6 })
    eq({ '*vim.lsp.enable()*', 'lsp.txt' }, open_helptag())

    set_lines "vim.lsp.enable('clangd')"
    cursor(0, { 1, 9 })
    eq({ '*vim.lsp.enable()*', 'lsp.txt' }, open_helptag())

    set_lines 'assert(vim.lsp.get_client_by_id(client_id))'
    cursor(0, { 1, 12 })
    eq({ '*vim.lsp.get_client_by_id()*', 'lsp.txt' }, open_helptag())

    set_lines "vim.api.nvim_create_autocmd('LspAttach', {"
    cursor(0, { 1, 7 })
    eq({ '*nvim_create_autocmd()*', 'api.txt' }, open_helptag())

    -- Falls back to <cword> when all trimming fails.
    set_lines "'@lsp.type.function'"
    cursor(0, { 1, 2 }) -- on 'lsp'
    eq({ '*lsp*', 'lsp.txt' }, open_helptag())
    set_lines "'@lsp.type.function'"
    cursor(0, { 1, 14 }) -- on 'function'
    eq({ '*:function*', 'userfunc.txt' }, open_helptag())

    set_lines '  • `@lsp.type.<type>.<ft>` for the type'
    cursor(0, { 1, 6 }) -- on backtick '`' (byte 6, after 2 spaces + 3-byte '•' + space)
    eq({ '*lsp*', 'lsp.txt' }, open_helptag())

    set_lines [[
    - `root_dir` usages akin to >lua
       root_dir  = require'lspconfig.util'.root_pattern(...)
    <
    require'lspconfig.util'.root_pattern(...)
    ]]
    cursor(0, { 2, 17 }) -- on "require"
    eq({ '*require()*', 'luaref.txt' }, open_helptag())

    set_lines '`:lsp restart`. You'
    cursor(0, { 1, 6 }) -- on "restart"
    eq({ '*:restart*', 'gui.txt' }, open_helptag())

    --
    -- Test with actual helpfiles. This affects getcompletion(…,'help') ...
    --

    n.command(':help lua')
    n.feed('gg/package.searchpath<cr>')
    eq({ "vim.cmd.edit(package.searchpath('jit.p',", 'lua.txt' }, buf_word())
    --               ^ cursor on "package"
    n.command(':help!')
    eq({ '*packages*', 'pack.txt' }, buf_word())

    n.command(':help lsp')
    n.feed('gg/type.<lt>type><cr>')
    eq({ '`@lsp.type.<type>.<ft>`', 'lsp.txt' }, buf_word())
    --          ^ cursor on "type"
    n.command(':help!')
    eq({ '*type()*', 'vimfn.txt' }, buf_word())
    n.feed('<c-o>f<lt>')
    eq({ '`@lsp.type.<type>.<ft>`', 'lsp.txt' }, buf_word())
    --               ^ cursor on "<"
    n.command(':help!')

    n.command(':help lsp')
    n.feed('gg/codelens.run()|<cr>')
    eq({ '|vim.lsp.codelens.run()|', 'lsp.txt' }, buf_word())
    --             ^ cursor on "codelens"
    n.command(':help!')
    eq({ '*vim.lsp.codelens.run()*', 'lsp.txt' }, buf_word())
  end)

  it('K resolves generic type names in help', function()
    command('helptags ++t $VIMRUNTIME/doc')
    for _, ty in ipairs({
      'vim.async.Task<R>',
      'vim.async.Task<R...>',
      'vim.async.Task<R>[]',
    }) do
      command('help lua-async')
      command('setlocal keywordprg=:help!')
      assert(fn.search('\\V(`' .. ty .. '`)', 'W') > 0)
      command('normal! fTK')
      eq({ '*vim.async.Task*', 'lua-async.txt' }, buf_word())
    end
  end)

  it('K resolves nullable type names in help', function()
    command('helptags ++t $VIMRUNTIME/doc')
    for _, ref in ipairs({
      { 'lsp', 'vim.lsp.Client', '?' },
      { 'diagnostic', 'vim.Diagnostic', '?' },
      { 'diagnostic', 'vim.Diagnostic', '[]?' },
    }) do
      command('help ' .. ref[1])
      command('setlocal keywordprg=:help!')
      assert(fn.search('\\V(`' .. ref[2] .. ref[3] .. '`)', 'W') > 0)
      command('normal! 2lK')
      eq({ '*' .. ref[2] .. '*', ref[1] .. '.txt' }, buf_word())
    end
  end)

  it('window closed makes cursor return to a valid win/buf #9773', function()
    n.add_builddir_to_rtp()
    command('help help')
    eq(1001, fn.win_getid())
    command('quit')
    eq(1000, fn.win_getid())

    command('autocmd WinNew * wincmd p')

    command('help help')
    -- Window 1002 is opened, but the autocmd switches back to 1000 and
    -- creates the help buffer there instead.
    eq(1000, fn.win_getid())
    command('quit')
    -- Before #9773, Nvim would crash on quitting the help window.
    eq(1002, fn.win_getid())
  end)

  it('multibyte help tags work #23975', function()
    mkdir('Xhelptags')
    finally(function()
      rmdir('Xhelptags')
    end)
    mkdir('Xhelptags/doc')
    write_file('Xhelptags/doc/Xhelptags.txt', '*…*')
    command('helptags Xhelptags/doc')
    command('set rtp+=Xhelptags')
    command('help …')
    eq('*…*', api.nvim_get_current_line())
  end)
end)

describe(':helptags', function()
  before_each(function()
    for _, sfx in ipairs({ '', '2' }) do
      fn.mkdir(('Xhelptags%s/doc'):format(sfx), 'p')
      for _, tag in ipairs({ 'Xa', 'Xb' }) do
        write_file(('Xhelptags%s/doc/%s%s.txt'):format(sfx, tag, sfx), ('*%s%s*'):format(tag, sfx))
      end
    end

    clear()
    command('set rtp+=Xhelptags,Xhelptags2')
  end)

  after_each(function()
    rmdir('Xhelptags')
    rmdir('Xhelptags2')
  end)

  it('requires an argument', function()
    local msg = t.pcall_err(command, 'helptags')
    eq(true, msg:find('E471') ~= nil)
  end)

  it('{dir}', function()
    -- Helpfiles in sub-directories are found and named relative to {dir}.
    fn.mkdir('Xhelptags/doc/sub', 'p')
    -- A "|" in a tag would break |links|, and "*Xd" is not closed, so neither is a tag.
    write_file('Xhelptags/doc/sub/Xc.txt', '*Xc*\n*X|c*\n*Xd\n')
    -- CRLF helpfile: "\r" must not confuse the parser into finding tags in an example.
    write_file('Xhelptags/doc/Xe.txt', '*Xe*\r\n>\r\n\t+-----+\r\n\t|/* a.c */  |/* b.c */  |\r\n')

    for _, dir in ipairs({
      'Xhelptags/doc',
      './Xhelptags/doc',
      fn.fnamemodify('Xhelptags/doc', ':p'),
    }) do
      command('helptags ' .. fn.fnameescape(dir))
      eq({
        'Xa\tXa.txt\t/*Xa*',
        'Xb\tXb.txt\t/*Xb*',
        'Xc\tsub/Xc.txt\t/*Xc*',
        'Xe\tXe.txt\t/*Xe*',
      }, fn.readfile('Xhelptags/doc/tags'))
    end

    command('help Xa')
    eq('*Xa*', api.nvim_get_current_line())

    command('help Xc')
    eq('*Xc*', api.nvim_get_current_line())
  end)

  it('ignores tags in examples and resumes after unindented text', function()
    for _, newline in ipairs({ '\n', '\r\n' }) do
      write_file(
        'Xhelptags/doc/Xa.txt',
        table.concat({
          '*Xa* *Xz*',
          'Some ordinary prose.',
          'prefix*invalid* *invalid*suffix *bad|tag* **',
          '>lua',
          '',
          '  *example*',
          'End of example.',
          '',
          '  *Xc*',
          'More ordinary prose.',
          '*Xd*', -- No final newline.
        }, newline),
        true
      )
      command('helptags Xhelptags/doc')
      eq({
        'Xa\tXa.txt\t/*Xa*',
        'Xb\tXb.txt\t/*Xb*',
        'Xc\tXa.txt\t/*Xc*',
        'Xd\tXa.txt\t/*Xd*',
        'Xz\tXa.txt\t/*Xz*',
      }, fn.readfile('Xhelptags/doc/tags'))
    end