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
524
525
526
527
528
529
530
531
532
|
#!/usr/bin/env -S nvim -l
-- Generator for src/nvim/eval.lua
--- @class vim.api.metadata
--- @field name string
--- @field parameters {[1]:string,[2]:string}[]
--- @field return_type string
--- @field deprecated_since integer
--- @field eval boolean
--- @field fast boolean
--- @field handler_id integer
--- @field impl_name string
--- @field lua boolean
--- @field method boolean
--- @field remote boolean
--- @field since integer
local LUA_META_HEADER = {
'--- @meta',
'-- THIS FILE IS GENERATED',
'-- DO NOT EDIT',
"error('Cannot require a meta file')",
}
local LUA_API_META_HEADER = {
'--- @meta',
'-- THIS FILE IS GENERATED',
'-- DO NOT EDIT',
"error('Cannot require a meta file')",
'',
'vim.api = {}',
}
local LUA_KEYWORDS = {
['and'] = true,
['end'] = true,
['function'] = true,
['or'] = true,
['if'] = true,
['while'] = true,
['repeat'] = true,
}
local API_TYPES = {
Window = 'integer',
Tabpage = 'integer',
Buffer = 'integer',
Boolean = 'boolean',
Object = 'any',
Integer = 'integer',
String = 'string',
Array = 'any[]',
LuaRef = 'function',
Dictionary = 'table<string,any>',
Float = 'number',
void = '',
}
--- Convert an API type to Lua
--- @param t string
--- @return string
local function api_type(t)
if t:match('^ArrayOf%(([z-aA-Z]+), %d+%') then
print(t:match('^ArrayOf%(([z-aA-Z]+), %d+%'))
end
local as0 = t:match('^ArrayOf%((.*)%)')
if as0 then
local as = vim.split(as0, ', ', { plain = true })
return api_type(as[1]) .. '[]'
end
local d = t:match('^Dict%((.*)%)')
if d then
return 'vim.api.keyset.' .. d
end
local d0 = t:match('^DictionaryOf%((.*)%)')
if d0 then
return 'table<string,' .. api_type(d0) .. '>'
end
return API_TYPES[t] or t
end
--- @param f string
--- @param params {[1]:string,[2]:string}[]|true
local function render_fun_sig(f, params)
local param_str --- @type string
if params == true then
param_str = '...'
else
param_str = table.concat(
vim.tbl_map(
--- @param v {[1]:string,[2]:string}
function(v)
return v[1]
end,
params
),
', '
)
end
if LUA_KEYWORDS[f] then
return string.format("vim.fn['%s'] = function(%s) end", f, param_str)
else
return string.format('function vim.fn.%s(%s) end', f, param_str)
end
end
--- Uniquify names
--- Fix any names that are lua keywords
--- @param params {[1]:string,[2]:string,[3]:string}[]
--- @return {[1]:string,[2]:string,[3]:string}[]
local function process_params(params)
local seen = {} --- @type table<string,true>
local sfx = 1
for _, p in ipairs(params) do
if LUA_KEYWORDS[p[1]] then
p[1] = p[1] .. '_'
end
if seen[p[1]] then
p[1] = p[1] .. sfx
sfx = sfx + 1
else
seen[p[1]] = true
end
end
return params
end
--- @class vim.gen_vim_doc_fun
--- @field signature string
--- @field doc string[]
--- @field parameters_doc table<string,string>
--- @field return string[]
--- @field seealso string[]
--- @field annotations string[]
--- @return table<string, vim.EvalFn>
local function get_api_funcs()
local mpack_f = assert(io.open('build/api_metadata.mpack', 'rb'))
local metadata = vim.mpack.decode(mpack_f:read('*all')) --[[@as vim.api.metadata[] ]]
local ret = {} --- @type table<string, vim.EvalFn>
local doc_mpack_f = assert(io.open('runtime/doc/api.mpack', 'rb'))
local doc_metadata = vim.mpack.decode(doc_mpack_f:read('*all')) --[[@as table<string,vim.gen_vim_doc_fun>]]
for _, fun in ipairs(metadata) do
local fdoc = doc_metadata[fun.name]
local params = {} --- @type {[1]:string,[2]:string}[]
for _, p in ipairs(fun.parameters) do
local ptype, pname = p[1], p[2]
params[#params + 1] = {
pname,
api_type(ptype),
fdoc and fdoc.parameters_doc[pname] or nil,
}
end
local r = {
signature = 'NA',
name = fun.name,
params = params,
returns = api_type(fun.return_type),
deprecated = fun.deprecated_since ~= nil,
}
if fdoc then
if #fdoc.doc > 0 then
r.desc = table.concat(fdoc.doc, '\n')
end
r.return_desc = (fdoc['return'] or {})[1]
end
ret[fun.name] = r
end
return ret
end
--- Convert vimdoc references to markdown literals
--- Convert vimdoc codeblocks to markdown codeblocks
--- @param x string
--- @return string
local function norm_text(x)
return (
x:gsub('|([^ ]+)|', '`%1`')
:gsub('>lua', '\n```lua')
:gsub('>vim', '\n```vim')
:gsub('\n<$', '\n```')
:gsub('\n<\n', '\n```\n')
)
end
--- @param _f string
--- @param fun vim.EvalFn
--- @param write fun(line: string)
local function render_api_fun(_f, fun, write)
if not vim.startswith(fun.name, 'nvim_') then
return
end
write('')
if vim.startswith(fun.name, 'nvim__') then
write('--- @private')
end
if fun.deprecated then
write('--- @deprecated')
end
local desc = fun.desc
if desc then
for _, l in ipairs(vim.split(norm_text(desc), '\n', { plain = true })) do
write('--- ' .. l)
end
write('---')
end
local param_names = {} --- @type string[]
local params = process_params(fun.params)
for _, p in ipairs(params) do
param_names[#param_names + 1] = p[1]
local pdesc = p[3]
if pdesc then
local pdesc_a = vim.split(norm_text(pdesc), '\n', { plain = true })
write('--- @param ' .. p[1] .. ' ' .. p[2] .. ' ' .. pdesc_a[1])
for i = 2, #pdesc_a do
if not pdesc_a[i] then
break
end
write('--- ' .. pdesc_a[i])
end
else
write('--- @param ' .. p[1] .. ' ' .. p[2])
end
end
if fun.returns ~= '' then
if fun.returns_desc then
write('--- @return ' .. fun.returns .. ' : ' .. fun.returns_desc)
else
write('--- @return ' .. fun.returns)
end
end
local param_str = table.concat(param_names, ', ')
write(string.format('function vim.api.%s(%s) end', fun.name, param_str))
end
--- @return table<string, vim.EvalFn>
local function get_api_keysets()
local mpack_f = assert(io.open('build/api_metadata.mpack', 'rb'))
--- @diagnostic disable-next-line:no-unknown
local metadata = assert(vim.mpack.decode(mpack_f:read('*all')))
local ret = {} --- @type table<string, vim.EvalFn>
--- @type {[1]: string, [2]: {[1]: string, [2]: string}[] }[]
local keysets = metadata.keysets
for _, keyset in ipairs(keysets) do
local kname = keyset[1]
local kdef = keyset[2]
for _, field in ipairs(kdef) do
field[2] = api_type(field[2])
end
ret[kname] = {
signature = 'NA',
name = kname,
params = kdef,
}
end
return ret
end
--- @param _f string
--- @param fun vim.EvalFn
--- @param write fun(line: string)
local function render_api_keyset(_f, fun, write)
write('')
write('--- @class vim.api.keyset.' .. fun.name)
for _, p in ipairs(fun.params) do
write('--- @field ' .. p[1] .. ' ' .. p[2])
end
end
--- @return table<string, vim.EvalFn>
local function get_eval_funcs()
return require('src/nvim/eval').funcs
end
--- @param f string
--- @param fun vim.EvalFn
--- @param write fun(line: string)
local function render_vimfn(f, fun, write)
if fun.lua == false then
return
end
local funname = fun.name or f
local params = process_params(fun.params)
if fun.signature then
write('')
if fun.deprecated then
write('--- @deprecated')
end
local desc = fun.desc
if desc then
--- @type string
desc = desc:gsub('\n%s*\n%s*$', '\n')
for _, l in ipairs(vim.split(desc, '\n', { plain = true })) do
l = l:gsub('^ ', ''):gsub('\t', ' '):gsub('@', '\\@')
write('--- ' .. l)
end
end
local req_args = type(fun.args) == 'table' and fun.args[1] or fun.args or 0
for i, param in ipairs(params) do
local pname, ptype = param[1], param[2]
local optional = (pname ~= '...' and i > req_args) and '?' or ''
write(string.format('--- @param %s%s %s', pname, optional, ptype))
end
if fun.returns ~= false then
write('--- @return ' .. (fun.returns or 'any'))
end
write(render_fun_sig(funname, params))
return
end
print('no doc for', funname)
end
--- @type table<string,true>
local rendered_tags = {}
--- @param name string
--- @param fun vim.EvalFn
--- @param write fun(line: string)
local function render_sig_and_tag(name, fun, write)
local tags = { '*' .. name .. '()*' }
if fun.tags then
for _, t in ipairs(fun.tags) do
tags[#tags + 1] = '*' .. t .. '*'
end
end
local tag = table.concat(tags, ' ')
local siglen = #fun.signature
local conceal_offset = 2*(#tags - 1)
local tag_pad_len = math.max(1, 80 - #tag + conceal_offset)
if siglen + #tag > 80 then
write(string.rep(' ', tag_pad_len) .. tag)
write(fun.signature)
else
write(string.format('%s%s%s', fun.signature, string.rep(' ', tag_pad_len - siglen), tag))
end
end
--- @param f string
--- @param fun vim.EvalFn
--- @param write fun(line: string)
local function render_eval_doc(f, fun, write)
if fun.deprecated then
return
end
if not fun.signature then
return
end
local desc = fun.desc
if not desc then
write(fun.signature)
return
end
local name = fun.name or f
if rendered_tags[name] then
write(fun.signature)
else
render_sig_and_tag(name, fun, write)
rendered_tags[name] = true
end
desc = vim.trim(desc)
local desc_l = vim.split(desc, '\n', { plain = true })
for _, l in ipairs(desc_l) do
l = l:gsub('^ ', '')
if vim.startswith(l, '<') and not l:match('^<[A-Z][A-Z]') then
write('<\t\t' .. l:sub(2))
else
write('\t\t' .. l)
end
end
if #desc_l > 0 and not desc_l[#desc_l]:match('^<?$') then
write('')
end
end
--- @class nvim.gen_eval_files.elem
--- @field path string
--- @field funcs fun(): table<string, vim.EvalFn>
--- @field render fun(f:string,fun:vim.EvalFn,write:fun(line:string))
--- @field header? string[]
--- @field footer? string[]
--- @type nvim.gen_eval_files.elem[]
local CONFIG = {
{
path = 'runtime/lua/vim/_meta/vimfn.lua',
header = LUA_META_HEADER,
funcs = get_eval_funcs,
render = render_vimfn,
},
{
path = 'runtime/lua/vim/_meta/api.lua',
header = LUA_API_META_HEADER,
funcs = get_api_funcs,
render = render_api_fun,
},
{
path = 'runtime/lua/vim/_meta/api_keysets.lua',
header = LUA_META_HEADER,
funcs = get_api_keysets,
render = render_api_keyset,
},
{
path = 'runtime/doc/builtin.txt',
funcs = get_eval_funcs,
render = render_eval_doc,
header = {
'*builtin.txt* Nvim',
'',
'',
'\t\t VIM REFERENCE MANUAL\t by Bram Moolenaar',
'',
'',
'Builtin functions\t\t*vimscript-functions* *builtin-functions*',
'',
'For functions grouped by what they are used for see |function-list|.',
'',
'\t\t\t\t Type |gO| to see the table of contents.',
'==============================================================================',
'1. Details *builtin-function-details*',
'',
},
footer = {
'==============================================================================',
'2. Matching a pattern in a String *string-match*',
'',
'This is common between several functions. A regexp pattern as explained at',
'|pattern| is normally used to find a match in the buffer lines. When a',
'pattern is used to find a match in a String, almost everything works in the',
'same way. The difference is that a String is handled like it is one line.',
'When it contains a "\\n" character, this is not seen as a line break for the',
'pattern. It can be matched with a "\\n" in the pattern, or with ".". Example:',
'>vim',
'\tlet a = "aaaa\\nxxxx"',
'\techo matchstr(a, "..\\n..")',
'\t" aa',
'\t" xx',
'\techo matchstr(a, "a.x")',
'\t" a',
'\t" x',
'',
'Don\'t forget that "^" will only match at the first character of the String and',
'"$" at the last character of the string. They don\'t match after or before a',
'"\\n".',
'',
' vim:tw=78:ts=8:noet:ft=help:norl:',
},
},
}
--- @param elem nvim.gen_eval_files.elem
local function render(elem)
local o = assert(io.open(elem.path, 'w'))
--- @param l string
local function write(l)
local l1 = l:gsub('%s+$', '')
o:write(l1)
o:write('\n')
end
for _, l in ipairs(elem.header or {}) do
write(l)
end
local funcs = elem.funcs()
--- @type string[]
local fnames = vim.tbl_keys(funcs)
table.sort(fnames)
for _, f in ipairs(fnames) do
elem.render(f, funcs[f], write)
end
for _, l in ipairs(elem.footer or {}) do
write(l)
end
o:close()
end
local function main()
for _, c in ipairs(CONFIG) do
render(c)
end
end
main()
|