gen_eval_files.lua 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. #!/usr/bin/env -S nvim -l
  2. -- Generator for various vimdoc and Lua type files
  3. local util = require('scripts.util')
  4. local fmt = string.format
  5. local DEP_API_METADATA = 'build/funcs_metadata.mpack'
  6. local TEXT_WIDTH = 78
  7. --- @class vim.api.metadata
  8. --- @field name string
  9. --- @field parameters [string,string][]
  10. --- @field return_type string
  11. --- @field deprecated_since integer
  12. --- @field eval boolean
  13. --- @field fast boolean
  14. --- @field handler_id integer
  15. --- @field impl_name string
  16. --- @field lua boolean
  17. --- @field method boolean
  18. --- @field remote boolean
  19. --- @field since integer
  20. local LUA_API_RETURN_OVERRIDES = {
  21. nvim_buf_get_command = 'table<string,vim.api.keyset.command_info>',
  22. nvim_buf_get_extmark_by_id = 'vim.api.keyset.get_extmark_item_by_id',
  23. nvim_buf_get_extmarks = 'vim.api.keyset.get_extmark_item[]',
  24. nvim_buf_get_keymap = 'vim.api.keyset.keymap[]',
  25. nvim_get_autocmds = 'vim.api.keyset.get_autocmds.ret[]',
  26. nvim_get_color_map = 'table<string,integer>',
  27. nvim_get_command = 'table<string,vim.api.keyset.command_info>',
  28. nvim_get_keymap = 'vim.api.keyset.keymap[]',
  29. nvim_get_mark = 'vim.api.keyset.get_mark',
  30. -- Can also return table<string,vim.api.keyset.get_hl_info>, however we need to
  31. -- pick one to get some benefit.
  32. -- REVISIT lewrus01 (26/01/24): we can maybe add
  33. -- @overload fun(ns: integer, {}): table<string,vim.api.keyset.get_hl_info>
  34. nvim_get_hl = 'vim.api.keyset.get_hl_info',
  35. nvim_get_mode = 'vim.api.keyset.get_mode',
  36. nvim_get_namespaces = 'table<string,integer>',
  37. nvim_get_option_info = 'vim.api.keyset.get_option_info',
  38. nvim_get_option_info2 = 'vim.api.keyset.get_option_info',
  39. nvim_parse_cmd = 'vim.api.keyset.parse_cmd',
  40. nvim_win_get_config = 'vim.api.keyset.win_config',
  41. }
  42. local LUA_META_HEADER = {
  43. '--- @meta _',
  44. '-- THIS FILE IS GENERATED',
  45. '-- DO NOT EDIT',
  46. "error('Cannot require a meta file')",
  47. }
  48. local LUA_API_META_HEADER = {
  49. '--- @meta _',
  50. '-- THIS FILE IS GENERATED',
  51. '-- DO NOT EDIT',
  52. "error('Cannot require a meta file')",
  53. '',
  54. 'vim.api = {}',
  55. }
  56. local LUA_OPTION_META_HEADER = {
  57. '--- @meta _',
  58. '-- THIS FILE IS GENERATED',
  59. '-- DO NOT EDIT',
  60. "error('Cannot require a meta file')",
  61. '',
  62. '---@class vim.bo',
  63. '---@field [integer] vim.bo',
  64. 'vim.bo = vim.bo',
  65. '',
  66. '---@class vim.wo',
  67. '---@field [integer] vim.wo',
  68. 'vim.wo = vim.wo',
  69. }
  70. local LUA_VVAR_META_HEADER = {
  71. '--- @meta _',
  72. '-- THIS FILE IS GENERATED',
  73. '-- DO NOT EDIT',
  74. "error('Cannot require a meta file')",
  75. '',
  76. '--- @class vim.v',
  77. 'vim.v = ...',
  78. }
  79. local LUA_KEYWORDS = {
  80. ['and'] = true,
  81. ['end'] = true,
  82. ['function'] = true,
  83. ['or'] = true,
  84. ['if'] = true,
  85. ['while'] = true,
  86. ['repeat'] = true,
  87. ['true'] = true,
  88. ['false'] = true,
  89. }
  90. local OPTION_TYPES = {
  91. boolean = 'boolean',
  92. number = 'integer',
  93. string = 'string',
  94. }
  95. local API_TYPES = {
  96. Window = 'integer',
  97. Tabpage = 'integer',
  98. Buffer = 'integer',
  99. Boolean = 'boolean',
  100. Object = 'any',
  101. Integer = 'integer',
  102. String = 'string',
  103. Array = 'any[]',
  104. LuaRef = 'function',
  105. Dict = 'table<string,any>',
  106. Float = 'number',
  107. HLGroupID = 'number|string',
  108. void = '',
  109. }
  110. --- @param x string
  111. --- @param sep? string
  112. --- @return string[]
  113. local function split(x, sep)
  114. return vim.split(x, sep or '\n', { plain = true })
  115. end
  116. --- Convert an API type to Lua
  117. --- @param t string
  118. --- @return string
  119. local function api_type(t)
  120. local as0 = t:match('^ArrayOf%((.*)%)')
  121. if as0 then
  122. local as = split(as0, ', ')
  123. return api_type(as[1]) .. '[]'
  124. end
  125. local d = t:match('^Dict%((.*)%)')
  126. if d then
  127. return 'vim.api.keyset.' .. d
  128. end
  129. local d0 = t:match('^DictOf%((.*)%)')
  130. if d0 then
  131. return 'table<string,' .. api_type(d0) .. '>'
  132. end
  133. return API_TYPES[t] or t
  134. end
  135. --- @param f string
  136. --- @param params [string,string][]|true
  137. --- @return string
  138. local function render_fun_sig(f, params)
  139. local param_str --- @type string
  140. if params == true then
  141. param_str = '...'
  142. else
  143. param_str = table.concat(
  144. vim.tbl_map(
  145. --- @param v [string,string]
  146. --- @return string
  147. function(v)
  148. return v[1]
  149. end,
  150. params
  151. ),
  152. ', '
  153. )
  154. end
  155. if LUA_KEYWORDS[f] then
  156. return fmt("vim.fn['%s'] = function(%s) end", f, param_str)
  157. else
  158. return fmt('function vim.fn.%s(%s) end', f, param_str)
  159. end
  160. end
  161. --- Uniquify names
  162. --- Fix any names that are lua keywords
  163. --- @param params [string,string,string][]
  164. --- @return [string,string,string][]
  165. local function process_params(params)
  166. local seen = {} --- @type table<string,true>
  167. local sfx = 1
  168. for _, p in ipairs(params) do
  169. if LUA_KEYWORDS[p[1]] then
  170. p[1] = p[1] .. '_'
  171. end
  172. if seen[p[1]] then
  173. p[1] = p[1] .. sfx
  174. sfx = sfx + 1
  175. else
  176. seen[p[1]] = true
  177. end
  178. end
  179. return params
  180. end
  181. --- @return table<string, vim.EvalFn>
  182. local function get_api_meta()
  183. local ret = {} --- @type table<string, vim.EvalFn>
  184. local cdoc_parser = require('scripts.cdoc_parser')
  185. local f = 'src/nvim/api'
  186. local function include(fun)
  187. if not vim.startswith(fun.name, 'nvim_') then
  188. return false
  189. end
  190. if vim.tbl_contains(fun.attrs or {}, 'lua_only') then
  191. return true
  192. end
  193. if vim.tbl_contains(fun.attrs or {}, 'remote_only') then
  194. return false
  195. end
  196. return true
  197. end
  198. --- @type table<string,nvim.cdoc.parser.fun>
  199. local functions = {}
  200. for path, ty in vim.fs.dir(f) do
  201. if ty == 'file' then
  202. local filename = vim.fs.joinpath(f, path)
  203. local _, funs = cdoc_parser.parse(filename)
  204. for _, fn in ipairs(funs) do
  205. if include(fn) then
  206. functions[fn.name] = fn
  207. end
  208. end
  209. end
  210. end
  211. for _, fun in pairs(functions) do
  212. local deprecated = fun.deprecated_since ~= nil
  213. local notes = {} --- @type string[]
  214. for _, note in ipairs(fun.notes or {}) do
  215. notes[#notes + 1] = note.desc
  216. end
  217. local sees = {} --- @type string[]
  218. for _, see in ipairs(fun.see or {}) do
  219. sees[#sees + 1] = see.desc
  220. end
  221. local params = {} --- @type [string,string][]
  222. for _, p in ipairs(fun.params) do
  223. params[#params + 1] = {
  224. p.name,
  225. api_type(p.type),
  226. not deprecated and p.desc or nil,
  227. }
  228. end
  229. local r = {
  230. signature = 'NA',
  231. name = fun.name,
  232. params = params,
  233. notes = notes,
  234. see = sees,
  235. returns = api_type(fun.returns[1].type),
  236. deprecated = deprecated,
  237. }
  238. if not deprecated then
  239. r.desc = fun.desc
  240. r.returns_desc = fun.returns[1].desc
  241. end
  242. ret[fun.name] = r
  243. end
  244. return ret
  245. end
  246. --- Convert vimdoc references to markdown literals
  247. --- Convert vimdoc codeblocks to markdown codeblocks
  248. ---
  249. --- Ensure code blocks have one empty line before the start fence and after the closing fence.
  250. ---
  251. --- @param x string
  252. --- @param special string?
  253. --- | 'see-api-meta' Normalize `@see` for API meta docstrings.
  254. --- @return string
  255. local function norm_text(x, special)
  256. if special == 'see-api-meta' then
  257. -- Try to guess a symbol that actually works in @see.
  258. -- "nvim_xx()" => "vim.api.nvim_xx"
  259. x = x:gsub([=[%|?(nvim_[^.()| ]+)%(?%)?%|?]=], 'vim.api.%1')
  260. -- TODO: Remove backticks when LuaLS resolves: https://github.com/LuaLS/lua-language-server/issues/2889
  261. -- "|foo|" => "`:help foo`"
  262. x = x:gsub([=[|([^ ]+)|]=], '`:help %1`')
  263. end
  264. return (
  265. x:gsub('|([^ ]+)|', '`%1`')
  266. :gsub('\n*>lua', '\n\n```lua')
  267. :gsub('\n*>vim', '\n\n```vim')
  268. :gsub('\n+<$', '\n```')
  269. :gsub('\n+<\n+', '\n```\n\n')
  270. :gsub('%s+>\n+', '\n```\n')
  271. :gsub('\n+<%s+\n?', '\n```\n')
  272. )
  273. end
  274. --- Generates LuaLS docstring for an API function.
  275. --- @param _f string
  276. --- @param fun vim.EvalFn
  277. --- @param write fun(line: string)
  278. local function render_api_meta(_f, fun, write)
  279. write('')
  280. if vim.startswith(fun.name, 'nvim__') then
  281. write('--- @private')
  282. end
  283. if fun.deprecated then
  284. write('--- @deprecated')
  285. end
  286. local desc = fun.desc
  287. if desc then
  288. write(util.prefix_lines('--- ', norm_text(desc)))
  289. end
  290. -- LuaLS doesn't support @note. Render @note items as a markdown list.
  291. if fun.notes and #fun.notes > 0 then
  292. write('--- Note:')
  293. write(util.prefix_lines('--- ', table.concat(fun.notes, '\n')))
  294. write('---')
  295. end
  296. for _, see in ipairs(fun.see or {}) do
  297. write(util.prefix_lines('--- @see ', norm_text(see, 'see-api-meta')))
  298. end
  299. local param_names = {} --- @type string[]
  300. local params = process_params(fun.params)
  301. for _, p in ipairs(params) do
  302. param_names[#param_names + 1] = p[1]
  303. local pdesc = p[3]
  304. if pdesc then
  305. local s = '--- @param ' .. p[1] .. ' ' .. p[2] .. ' '
  306. local pdesc_a = split(vim.trim(norm_text(pdesc)))
  307. write(s .. pdesc_a[1])
  308. for i = 2, #pdesc_a do
  309. if not pdesc_a[i] then
  310. break
  311. end
  312. write('--- ' .. pdesc_a[i])
  313. end
  314. else
  315. write('--- @param ' .. p[1] .. ' ' .. p[2])
  316. end
  317. end
  318. if fun.returns ~= '' then
  319. local ret_desc = fun.returns_desc and ' # ' .. fun.returns_desc or ''
  320. local ret = LUA_API_RETURN_OVERRIDES[fun.name] or fun.returns
  321. write(util.prefix_lines('--- ', '@return ' .. ret .. ret_desc))
  322. end
  323. local param_str = table.concat(param_names, ', ')
  324. write(fmt('function vim.api.%s(%s) end', fun.name, param_str))
  325. end
  326. --- @return table<string, vim.EvalFn>
  327. local function get_api_keysets_meta()
  328. local mpack_f = assert(io.open(DEP_API_METADATA, 'rb'))
  329. local metadata = assert(vim.mpack.decode(mpack_f:read('*all')))
  330. local ret = {} --- @type table<string, vim.EvalFn>
  331. --- @type {name: string, keys: string[], types: table<string,string>}[]
  332. local keysets = metadata.keysets
  333. for _, k in ipairs(keysets) do
  334. local params = {}
  335. for _, key in ipairs(k.keys) do
  336. table.insert(params, { key .. '?', api_type(k.types[key] or 'any') })
  337. end
  338. ret[k.name] = {
  339. signature = 'NA',
  340. name = k.name,
  341. params = params,
  342. }
  343. end
  344. return ret
  345. end
  346. --- Generates LuaLS docstring for an API keyset.
  347. --- @param _f string
  348. --- @param fun vim.EvalFn
  349. --- @param write fun(line: string)
  350. local function render_api_keyset_meta(_f, fun, write)
  351. if string.sub(fun.name, 1, 1) == '_' then
  352. return -- not exported
  353. end
  354. write('')
  355. write('--- @class vim.api.keyset.' .. fun.name)
  356. for _, p in ipairs(fun.params) do
  357. write('--- @field ' .. p[1] .. ' ' .. p[2])
  358. end
  359. end
  360. --- @return table<string, vim.EvalFn>
  361. local function get_eval_meta()
  362. return require('src/nvim/eval').funcs
  363. end
  364. --- Generates LuaLS docstring for a Vimscript "eval" function.
  365. --- @param f string
  366. --- @param fun vim.EvalFn
  367. --- @param write fun(line: string)
  368. local function render_eval_meta(f, fun, write)
  369. if fun.lua == false then
  370. return
  371. end
  372. local funname = fun.name or f
  373. local params = process_params(fun.params)
  374. write('')
  375. if fun.deprecated then
  376. write('--- @deprecated')
  377. end
  378. local desc = fun.desc
  379. if desc then
  380. --- @type string
  381. desc = desc:gsub('\n%s*\n%s*$', '\n')
  382. for _, l in ipairs(split(desc)) do
  383. l = l:gsub('^ ', ''):gsub('\t', ' '):gsub('@', '\\@')
  384. write('--- ' .. l)
  385. end
  386. end
  387. local req_args = type(fun.args) == 'table' and fun.args[1] or fun.args or 0
  388. for i, param in ipairs(params) do
  389. local pname, ptype = param[1], param[2]
  390. local optional = (pname ~= '...' and i > req_args) and '?' or ''
  391. write(fmt('--- @param %s%s %s', pname, optional, ptype))
  392. end
  393. if fun.returns ~= false then
  394. local ret_desc = fun.returns_desc and ' # ' .. fun.returns_desc or ''
  395. write('--- @return ' .. (fun.returns or 'any') .. ret_desc)
  396. end
  397. write(render_fun_sig(funname, params))
  398. end
  399. --- Generates vimdoc heading for a Vimscript "eval" function signature.
  400. --- @param name string
  401. --- @param name_tag boolean
  402. --- @param fun vim.EvalFn
  403. --- @param write fun(line: string)
  404. local function render_sig_and_tag(name, name_tag, fun, write)
  405. if not fun.signature then
  406. return
  407. end
  408. local tags = name_tag and { '*' .. name .. '()*' } or {}
  409. if fun.tags then
  410. for _, t in ipairs(fun.tags) do
  411. tags[#tags + 1] = '*' .. t .. '*'
  412. end
  413. end
  414. if #tags == 0 then
  415. write(fun.signature)
  416. return
  417. end
  418. local tag = table.concat(tags, ' ')
  419. local siglen = #fun.signature
  420. local conceal_offset = 2 * (#tags - 1)
  421. local tag_pad_len = math.max(1, 80 - #tag + conceal_offset)
  422. if siglen + #tag > 80 then
  423. write(string.rep(' ', tag_pad_len) .. tag)
  424. write(fun.signature)
  425. else
  426. write(fmt('%s%s%s', fun.signature, string.rep(' ', tag_pad_len - siglen), tag))
  427. end
  428. end
  429. --- Generates vimdoc for a Vimscript "eval" function.
  430. --- @param f string
  431. --- @param fun vim.EvalFn
  432. --- @param write fun(line: string)
  433. local function render_eval_doc(f, fun, write)
  434. if fun.deprecated or not fun.signature then
  435. return
  436. end
  437. render_sig_and_tag(fun.name or f, not f:find('__%d+$'), fun, write)
  438. if not fun.desc then
  439. return
  440. end
  441. local params = process_params(fun.params)
  442. local req_args = type(fun.args) == 'table' and fun.args[1] or fun.args or 0
  443. local desc_l = split(vim.trim(fun.desc))
  444. for _, l in ipairs(desc_l) do
  445. l = l:gsub('^ ', '')
  446. if vim.startswith(l, '<') and not l:match('^<[^ \t]+>') then
  447. write('<\t\t' .. l:sub(2))
  448. elseif l:match('^>[a-z0-9]*$') then
  449. write(l)
  450. else
  451. write('\t\t' .. l)
  452. end
  453. end
  454. if #desc_l > 0 and not desc_l[#desc_l]:match('^<?$') then
  455. write('')
  456. end
  457. if #params > 0 then
  458. write(util.md_to_vimdoc('Parameters: ~', 16, 16, TEXT_WIDTH))
  459. for i, param in ipairs(params) do
  460. local pname, ptype = param[1], param[2]
  461. local optional = (pname ~= '...' and i > req_args) and '?' or ''
  462. local s = fmt('- %-14s (`%s%s`)', fmt('{%s}', pname), ptype, optional)
  463. write(util.md_to_vimdoc(s, 16, 18, TEXT_WIDTH))
  464. end
  465. write('')
  466. end
  467. if fun.returns ~= false then
  468. write(util.md_to_vimdoc('Return: ~', 16, 16, TEXT_WIDTH))
  469. local ret = ('(`%s`)'):format((fun.returns or 'any'))
  470. ret = ret .. (fun.returns_desc and ' ' .. fun.returns_desc or '')
  471. ret = util.md_to_vimdoc(ret, 18, 18, TEXT_WIDTH)
  472. write(ret)
  473. write('')
  474. end
  475. end
  476. --- @param d vim.option_defaults
  477. --- @param vimdoc? boolean
  478. --- @return string
  479. local function render_option_default(d, vimdoc)
  480. local dt --- @type integer|boolean|string|fun(): string
  481. if d.if_false ~= nil then
  482. dt = d.if_false
  483. else
  484. dt = d.if_true
  485. end
  486. if vimdoc then
  487. if d.doc then
  488. return d.doc
  489. end
  490. if type(dt) == 'boolean' then
  491. return dt and 'on' or 'off'
  492. end
  493. end
  494. if dt == '' or dt == nil or type(dt) == 'function' then
  495. dt = d.meta
  496. end
  497. local v --- @type string
  498. if not vimdoc then
  499. v = vim.inspect(dt) --[[@as string]]
  500. else
  501. v = type(dt) == 'string' and '"' .. dt .. '"' or tostring(dt)
  502. end
  503. --- @type table<string, string|false>
  504. local envvars = {
  505. TMPDIR = false,
  506. VIMRUNTIME = false,
  507. XDG_CONFIG_HOME = vim.env.HOME .. '/.local/config',
  508. XDG_DATA_HOME = vim.env.HOME .. '/.local/share',
  509. XDG_STATE_HOME = vim.env.HOME .. '/.local/state',
  510. }
  511. for name, default in pairs(envvars) do
  512. local value = vim.env[name] or default
  513. if value then
  514. v = v:gsub(vim.pesc(value), '$' .. name)
  515. end
  516. end
  517. return v
  518. end
  519. --- @param _f string
  520. --- @param opt vim.option_meta
  521. --- @param write fun(line: string)
  522. local function render_option_meta(_f, opt, write)
  523. write('')
  524. for _, l in ipairs(split(norm_text(opt.desc))) do
  525. write('--- ' .. l)
  526. end
  527. write('--- @type ' .. OPTION_TYPES[opt.type])
  528. write('vim.o.' .. opt.full_name .. ' = ' .. render_option_default(opt.defaults))
  529. if opt.abbreviation then
  530. write('vim.o.' .. opt.abbreviation .. ' = vim.o.' .. opt.full_name)
  531. end
  532. for _, s in pairs {
  533. { 'wo', 'win' },
  534. { 'bo', 'buf' },
  535. { 'go', 'global' },
  536. } do
  537. local id, scope = s[1], s[2]
  538. if vim.list_contains(opt.scope, scope) or (id == 'go' and #opt.scope > 1) then
  539. local pfx = 'vim.' .. id .. '.'
  540. write(pfx .. opt.full_name .. ' = vim.o.' .. opt.full_name)
  541. if opt.abbreviation then
  542. write(pfx .. opt.abbreviation .. ' = ' .. pfx .. opt.full_name)
  543. end
  544. end
  545. end
  546. end
  547. --- @param _f string
  548. --- @param opt vim.option_meta
  549. --- @param write fun(line: string)
  550. local function render_vvar_meta(_f, opt, write)
  551. write('')
  552. local desc = split(norm_text(opt.desc))
  553. while desc[#desc]:match('^%s*$') do
  554. desc[#desc] = nil
  555. end
  556. for _, l in ipairs(desc) do
  557. write('--- ' .. l)
  558. end
  559. write('--- @type ' .. (opt.type or 'any'))
  560. if LUA_KEYWORDS[opt.full_name] then
  561. write("vim.v['" .. opt.full_name .. "'] = ...")
  562. else
  563. write('vim.v.' .. opt.full_name .. ' = ...')
  564. end
  565. end
  566. --- @param s string[]
  567. --- @return string
  568. local function scope_to_doc(s)
  569. local m = {
  570. global = 'global',
  571. buf = 'local to buffer',
  572. win = 'local to window',
  573. tab = 'local to tab page',
  574. }
  575. if #s == 1 then
  576. return m[s[1]]
  577. end
  578. assert(s[1] == 'global')
  579. return 'global or ' .. m[s[2]] .. (s[2] ~= 'tab' and ' |global-local|' or '')
  580. end
  581. -- @param o vim.option_meta
  582. -- @return string
  583. local function scope_more_doc(o)
  584. if
  585. vim.list_contains({
  586. 'bufhidden',
  587. 'buftype',
  588. 'filetype',
  589. 'modified',
  590. 'previewwindow',
  591. 'readonly',
  592. 'scroll',
  593. 'syntax',
  594. 'winfixheight',
  595. 'winfixwidth',
  596. }, o.full_name)
  597. then
  598. return ' |local-noglobal|'
  599. end
  600. return ''
  601. end
  602. --- @param x string
  603. --- @return string
  604. local function dedent(x)
  605. local xs = split(x)
  606. local leading_ws = xs[1]:match('^%s*') --[[@as string]]
  607. local leading_ws_pat = '^' .. leading_ws
  608. for i in ipairs(xs) do
  609. local strip_pat = xs[i]:match(leading_ws_pat) and leading_ws_pat or '^%s*'
  610. xs[i] = xs[i]:gsub(strip_pat, '')
  611. end
  612. return table.concat(xs, '\n')
  613. end
  614. --- @return table<string,vim.option_meta>
  615. local function get_option_meta()
  616. local opts = require('src/nvim/options').options
  617. local optinfo = vim.api.nvim_get_all_options_info()
  618. local ret = {} --- @type table<string,vim.option_meta>
  619. for _, o in ipairs(opts) do
  620. local is_window_option = #o.scope == 1 and o.scope[1] == 'win'
  621. local is_option_hidden = o.immutable and not o.varname and not is_window_option
  622. if not is_option_hidden and o.desc then
  623. if o.full_name == 'cmdheight' then
  624. table.insert(o.scope, 'tab')
  625. end
  626. local r = vim.deepcopy(o) --[[@as vim.option_meta]]
  627. r.desc = o.desc:gsub('^ ', ''):gsub('\n ', '\n')
  628. r.defaults = r.defaults or {}
  629. if r.defaults.meta == nil then
  630. r.defaults.meta = optinfo[o.full_name].default
  631. end
  632. ret[o.full_name] = r
  633. end
  634. end
  635. return ret
  636. end
  637. --- @return table<string,vim.option_meta>
  638. local function get_vvar_meta()
  639. local info = require('src/nvim/vvars').vars
  640. local ret = {} --- @type table<string,vim.option_meta>
  641. for name, o in pairs(info) do
  642. o.desc = dedent(o.desc)
  643. o.full_name = name
  644. ret[name] = o
  645. end
  646. return ret
  647. end
  648. --- @param opt vim.option_meta
  649. --- @return string[]
  650. local function build_option_tags(opt)
  651. --- @type string[]
  652. local tags = { opt.full_name }
  653. tags[#tags + 1] = opt.abbreviation
  654. if opt.type == 'boolean' then
  655. for i = 1, #tags do
  656. tags[#tags + 1] = 'no' .. tags[i]
  657. end
  658. end
  659. for i, t in ipairs(tags) do
  660. tags[i] = "'" .. t .. "'"
  661. end
  662. for _, t in ipairs(opt.tags or {}) do
  663. tags[#tags + 1] = t
  664. end
  665. for i, t in ipairs(tags) do
  666. tags[i] = '*' .. t .. '*'
  667. end
  668. return tags
  669. end
  670. --- @param _f string
  671. --- @param opt vim.option_meta
  672. --- @param write fun(line: string)
  673. local function render_option_doc(_f, opt, write)
  674. local tags = build_option_tags(opt)
  675. local tag_str = table.concat(tags, ' ')
  676. local conceal_offset = 2 * (#tags - 1)
  677. local tag_pad = string.rep('\t', math.ceil((64 - #tag_str + conceal_offset) / 8))
  678. -- local pad = string.rep(' ', 80 - #tag_str + conceal_offset)
  679. write(tag_pad .. tag_str)
  680. local name_str --- @type string
  681. if opt.abbreviation then
  682. name_str = fmt("'%s' '%s'", opt.full_name, opt.abbreviation)
  683. else
  684. name_str = fmt("'%s'", opt.full_name)
  685. end
  686. local otype = opt.type == 'boolean' and 'boolean' or opt.type
  687. if opt.defaults.doc or opt.defaults.if_true ~= nil or opt.defaults.meta ~= nil then
  688. local v = render_option_default(opt.defaults, true)
  689. local pad = string.rep('\t', math.max(1, math.ceil((24 - #name_str) / 8)))
  690. if opt.defaults.doc then
  691. local deflen = #fmt('%s%s%s (', name_str, pad, otype)
  692. --- @type string
  693. v = v:gsub('\n', '\n' .. string.rep(' ', deflen - 2))
  694. end
  695. write(fmt('%s%s%s\t(default %s)', name_str, pad, otype, v))
  696. else
  697. write(fmt('%s\t%s', name_str, otype))
  698. end
  699. write('\t\t\t' .. scope_to_doc(opt.scope) .. scope_more_doc(opt))
  700. for _, l in ipairs(split(opt.desc)) do
  701. if l == '<' or l:match('^<%s') then
  702. write(l)
  703. else
  704. write('\t' .. l:gsub('\\<', '<'))
  705. end
  706. end
  707. end
  708. --- @param _f string
  709. --- @param vvar vim.option_meta
  710. --- @param write fun(line: string)
  711. local function render_vvar_doc(_f, vvar, write)
  712. local name = vvar.full_name
  713. local tags = { 'v:' .. name, name .. '-variable' }
  714. if vvar.tags then
  715. vim.list_extend(tags, vvar.tags)
  716. end
  717. for i, t in ipairs(tags) do
  718. tags[i] = '*' .. t .. '*'
  719. end
  720. local tag_str = table.concat(tags, ' ')
  721. local conceal_offset = 2 * (#tags - 1)
  722. local tag_pad = string.rep('\t', math.ceil((64 - #tag_str + conceal_offset) / 8))
  723. write(tag_pad .. tag_str)
  724. local desc = split(vvar.desc)
  725. if (#desc == 1 or #desc == 2 and desc[2]:match('^%s*$')) and #name < 10 then
  726. -- single line
  727. write('v:' .. name .. '\t' .. desc[1]:gsub('^%s*', ''))
  728. write('')
  729. else
  730. write('v:' .. name)
  731. for _, l in ipairs(desc) do
  732. if l == '<' or l:match('^<%s') then
  733. write(l)
  734. else
  735. write('\t\t' .. l:gsub('\\<', '<'))
  736. end
  737. end
  738. end
  739. end
  740. --- @class nvim.gen_eval_files.elem
  741. --- @field path string
  742. --- @field from? string Skip lines in path until this pattern is reached.
  743. --- @field funcs fun(): table<string, table>
  744. --- @field render fun(f:string,obj:table,write:fun(line:string))
  745. --- @field header? string[]
  746. --- @field footer? string[]
  747. --- @type nvim.gen_eval_files.elem[]
  748. local CONFIG = {
  749. {
  750. path = 'runtime/lua/vim/_meta/vimfn.lua',
  751. header = LUA_META_HEADER,
  752. funcs = get_eval_meta,
  753. render = render_eval_meta,
  754. },
  755. {
  756. path = 'runtime/lua/vim/_meta/api.lua',
  757. header = LUA_API_META_HEADER,
  758. funcs = get_api_meta,
  759. render = render_api_meta,
  760. },
  761. {
  762. path = 'runtime/lua/vim/_meta/api_keysets.lua',
  763. header = LUA_META_HEADER,
  764. funcs = get_api_keysets_meta,
  765. render = render_api_keyset_meta,
  766. },
  767. {
  768. path = 'runtime/doc/builtin.txt',
  769. funcs = get_eval_meta,
  770. render = render_eval_doc,
  771. header = {
  772. '*builtin.txt* Nvim',
  773. '',
  774. '',
  775. '\t\t NVIM REFERENCE MANUAL',
  776. '',
  777. '',
  778. 'Builtin functions\t\t*vimscript-functions* *builtin-functions*',
  779. '',
  780. 'For functions grouped by what they are used for see |function-list|.',
  781. '',
  782. '\t\t\t\t Type |gO| to see the table of contents.',
  783. '==============================================================================',
  784. '1. Details *builtin-function-details*',
  785. '',
  786. },
  787. footer = {
  788. '==============================================================================',
  789. '2. Matching a pattern in a String *string-match*',
  790. '',
  791. 'This is common between several functions. A regexp pattern as explained at',
  792. '|pattern| is normally used to find a match in the buffer lines. When a',
  793. 'pattern is used to find a match in a String, almost everything works in the',
  794. 'same way. The difference is that a String is handled like it is one line.',
  795. 'When it contains a "\\n" character, this is not seen as a line break for the',
  796. 'pattern. It can be matched with a "\\n" in the pattern, or with ".". Example:',
  797. '>vim',
  798. '\tlet a = "aaaa\\nxxxx"',
  799. '\techo matchstr(a, "..\\n..")',
  800. '\t" aa',
  801. '\t" xx',
  802. '\techo matchstr(a, "a.x")',
  803. '\t" a',
  804. '\t" x',
  805. '',
  806. 'Don\'t forget that "^" will only match at the first character of the String and',
  807. '"$" at the last character of the string. They don\'t match after or before a',
  808. '"\\n".',
  809. '',
  810. ' vim:tw=78:ts=8:noet:ft=help:norl:',
  811. },
  812. },
  813. {
  814. path = 'runtime/lua/vim/_meta/options.lua',
  815. header = LUA_OPTION_META_HEADER,
  816. funcs = get_option_meta,
  817. render = render_option_meta,
  818. },
  819. {
  820. path = 'runtime/doc/options.txt',
  821. header = { '' },
  822. from = 'A jump table for the options with a short description can be found at |Q_op|.',
  823. footer = {
  824. ' vim:tw=78:ts=8:noet:ft=help:norl:',
  825. },
  826. funcs = get_option_meta,
  827. render = render_option_doc,
  828. },
  829. {
  830. path = 'runtime/lua/vim/_meta/vvars.lua',
  831. header = LUA_VVAR_META_HEADER,
  832. funcs = get_vvar_meta,
  833. render = render_vvar_meta,
  834. },
  835. {
  836. path = 'runtime/doc/vvars.txt',
  837. header = { '' },
  838. from = 'Type |gO| to see the table of contents.',
  839. footer = {
  840. ' vim:tw=78:ts=8:noet:ft=help:norl:',
  841. },
  842. funcs = get_vvar_meta,
  843. render = render_vvar_doc,
  844. },
  845. }
  846. --- @param elem nvim.gen_eval_files.elem
  847. local function render(elem)
  848. print('Rendering ' .. elem.path)
  849. local from_lines = {} --- @type string[]
  850. local from = elem.from
  851. if from then
  852. for line in io.lines(elem.path) do
  853. from_lines[#from_lines + 1] = line
  854. if line:match(from) then
  855. break
  856. end
  857. end
  858. end
  859. local o = assert(io.open(elem.path, 'w'))
  860. --- @param l string
  861. local function write(l)
  862. local l1 = l:gsub('%s+$', '')
  863. o:write(l1)
  864. o:write('\n')
  865. end
  866. for _, l in ipairs(from_lines) do
  867. write(l)
  868. end
  869. for _, l in ipairs(elem.header or {}) do
  870. write(l)
  871. end
  872. local funcs = elem.funcs()
  873. --- @type string[]
  874. local fnames = vim.tbl_keys(funcs)
  875. table.sort(fnames)
  876. for _, f in ipairs(fnames) do
  877. elem.render(f, funcs[f], write)
  878. end
  879. for _, l in ipairs(elem.footer or {}) do
  880. write(l)
  881. end
  882. o:close()
  883. end
  884. local function main()
  885. for _, c in ipairs(CONFIG) do
  886. render(c)
  887. end
  888. end
  889. main()