robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#!/usr/bin/env -S nvim -l 2 3-- Generator for various vimdoc and Lua type files 4 5local util =require ( 'gen.util' ) 6local lint =require ( 'gen.lint' ) 7local api_type =require ( 'gen.api_types' ) 8local fmt =string . format 9 10local DEP_API_METADATA =arg [ 1 ] 11local TAGS_FILE =arg [ 2 ] 12local TEXT_WIDTH =78 13 14--- @class vim.api.metadata 15--- @field name string 16--- @field parameters [string,string][] 17--- @field return_type string 18--- @field deprecated_since integer 19--- @field eval boolean 20--- @field fast boolean 21--- @field handler_id integer 22--- @field impl_name string 23--- @field lua boolean 24--- @field method boolean 25--- @field remote boolean 26--- @field since integer 27 28local LUA_API_RETURN_OVERRIDES ={ 29nvim_parse_cmd ='vim.api.keyset.cmd_ret' , 30nvim_win_get_config ='vim.api.keyset.win_config_ret' , 31} 32 33local LUA_META_HEADER ={ 34'--- @meta _' , 35'--' , 36'-- DO NOT EDIT. GENERATED BY: src/gen/gen_eval_files.lua' , 37'--' , 38"error('Cannot require a meta file')" , 39} 40 41local LUA_API_META_HEADER =vim . list_extend ( vim . deepcopy ( LUA_META_HEADER ), { 42'' , 43'--- This file embeds vimdoc as the function descriptions' , 44'--- so ignore any doc related errors.' , 45'--- @diagnostic disable: undefined-doc-name,luadoc-miss-symbol' , 46'' , 47'vim.api = {}' , 48} ) 49 50local LUA_OPTION_META_HEADER =vim . list_extend ( vim . deepcopy ( LUA_META_HEADER ), { 51'' , 52'---@class vim.bo' , 53'---@field [integer] vim.bo' , 54'vim.bo = vim.bo' , 55'' , 56'---@class vim.wo' , 57'---@field [integer] vim.wo' , 58'vim.wo = vim.wo' , 59} ) 60 61local LUA_VVAR_META_HEADER =vim . list_extend ( vim . deepcopy ( LUA_META_HEADER ), { 62'' , 63'--- @class vim.v' , 64'vim.v = ...' , 65} ) 66 67local LUA_KEYWORDS ={ 68[ 'and' ] =true , 69[ 'end' ] =true , 70[ 'function' ] =true , 71[ 'or' ] =true , 72[ 'if' ] =true , 73[ 'while' ] =true , 74[ 'repeat' ] =true , 75[ 'true' ] =true , 76[ 'false' ] =true , 77} 78 79local OPTION_TYPES ={ 80boolean ='boolean' , 81number ='integer' , 82string ='string' , 83} 84 85--- @param s string 86--- @return string 87local function luaescape ( s ) 88if LUA_KEYWORDS [ s ] then 89return s .. '_' 90end 91return s 92end 93 94--- @param x string 95--- @param sep? string 96--- @return string[] 97local function split ( x , sep ) 98return vim . split ( x , sep or '\n' , { plain =true } ) 99end 100 101--- @param f string 102--- @param params [string,string,string?][]|true 103--- @return string 104local function render_fun_sig ( f , params ) 105local param_str --- @type string 106if params == true then 107param_str ='...' 108else 109param_str =table . concat ( 110vim . tbl_map ( 111--- @param v [string,string,string?] 112--- @return string 113function ( v ) 114return luaescape ( v [ 1 ]) 115end , 116params 117), 118', ' 119) 120end 121 122if LUA_KEYWORDS [ f ] then 123return fmt ( "vim.fn['%s'] = function(%s) end" , f , param_str ) 124else 125return fmt ( 'function vim.fn.%s(%s) end' , f , param_str ) 126end 127end 128 129--- Uniquify names 130--- @param params [string,string,string?][] 131--- @return [string,string,string?][] 132local function process_params ( params ) 133local seen ={} --- @type table<string,true> 134local sfx =1 135 136for _ , p in ipairs ( params ) do 137if seen [ p [ 1 ]] then 138p [ 1 ] =p [ 1 ] .. sfx 139sfx =sfx + 1 140else 141seen [ p [ 1 ]] =true 142end 143end 144 145return params 146end 147 148--- @return table<string, vim.EvalFn> 149local function get_api_meta () 150local ret ={} --- @type table<string, vim.EvalFn> 151 152local cdoc_parser =require ( 'gen.cdoc_parser' ) 153 154local f ='src/nvim/api' 155 156local function include ( fun ) 157if not vim . startswith ( fun . name , 'nvim_' ) then 158return false 159end 160if vim . tbl_contains ( fun . attrs or {} , 'lua_only' ) then 161return true 162end 163if vim . tbl_contains ( fun . attrs or {} , 'remote_only' ) then 164return false 165end 166return true 167end 168 169--- @type table<string,nvim.cdoc.parser.fun> 170local functions ={} 171for path , ty in vim . fs . dir ( f ) do 172if ty == 'file' and ( vim . endswith ( path , '.c' ) or vim . endswith ( path , '.h' )) then 173local filename =vim . fs . joinpath ( f , path ) 174local _ , funs =cdoc_parser . parse ( filename ) 175for _ , fn in ipairs ( funs ) do 176if include ( fn ) then 177functions [ fn . name ] =fn 178end 179end 180end 181end 182 183for _ , fun in pairs ( functions ) do 184local deprecated =fun . deprecated_since ~= nil 185 186local notes ={} --- @type string[] 187for _ , note in ipairs ( fun . notes or {} ) do 188notes [ # notes + 1 ] =note . desc 189end 190 191local sees ={} --- @type string[] 192for _ , see in ipairs ( fun . see or {} ) do 193sees [ # sees + 1 ] =see . desc 194end 195 196local params ={} --- @type [string,string,string?][] 197for _ , p in ipairs ( fun . params ) do 198params [ # params + 1 ] ={ 199p . name , 200p . type , 201not deprecated and p . desc or nil , 202} 203end 204 205local r ={ 206signature ='NA' , 207name =fun . name , 208params =params , 209notes =notes , 210see =sees , 211returns =fun . returns [ 1 ]. type , 212deprecated =deprecated , 213} 214 215if not deprecated then 216r . desc =fun . desc 217r . returns_desc =fun . returns [ 1 ]. desc 218end 219 220ret [ fun . name ] =r 221end 222return ret 223end 224 225--- Convert vimdoc references to markdown literals 226--- Convert vimdoc codeblocks to markdown codeblocks 227--- 228--- Ensure code blocks have one empty line before the start fence and after the closing fence. 229--- 230--- @param x string 231--- @param special string? 232--- | 'see-api-meta' Normalize `@see` for API meta docstrings. 233--- @return string 234local function norm_text ( x , special ) 235if special == 'see-api-meta' then 236-- Try to guess a symbol that actually works in @see. 237-- "nvim_xx()" => "vim.api.nvim_xx" 238x =x : gsub ( [=[%|?(nvim_[^.()| ]+)%(?%)?%|?]=] , 'vim.api.%1' ) 239-- TODO: Remove backticks when LuaLS resolves: https://github.com/LuaLS/lua-language-server/issues/2889 240-- "|foo|" => "`:help foo`" 241x =x : gsub ( [=[|([^%s|]+)|]=] , '`:help %1`' ) 242end 243 244return ( 245x : gsub ( '|([^%s|]+)|' , '`%1`' ) 246: gsub ( '\n*>lua' , '\n\n```lua' ) 247: gsub ( '\n*>vim' , '\n\n```vim' ) 248: gsub ( '\n+<$' , '\n```' ) 249: gsub ( '\n+<\n+' , '\n```\n\n' ) 250: gsub ( '%s+>\n+' , '\n```\n' ) 251: gsub ( '\n+<%s+\n?' , '\n```\n' ) 252) 253end 254 255--- @param items string[] 256--- @param conjunction string 257--- @return string 258--- Example: "foo, bar, or baz" 259local function join_with_conjunction ( items , conjunction ) 260if # items == 0 then 261return '' 262elseif # items == 1 then 263return items [ 1 ] 264elseif # items == 2 then 265return items [ 1 ] .. ' ' .. conjunction .. ' ' .. items [ 2 ] 266end 267 268local parts ={} --- @type string[] 269for i =1 , # items - 1 do 270parts [ # parts + 1 ] =items [ i ] 271end 272return table . concat ( parts , ', ' ) .. ', ' .. conjunction .. ' ' .. items [ # items ] 273end 274 275--- @param fun vim.EvalFn 276--- @return string? 277--- Example: "Lua: Prefer |math.abs()|." 278local function see_lua_text ( fun ) 279if not fun . see_lua or fun . see_lua == false then 280return 281end 282 283return 'Lua: Prefer ' .. join_with_conjunction ( fun . see_lua , 'or' ) .. '.' 284end 285 286local VIMDOC_PARAGRAPH_PREFIX ='\t\t' 287local VIMDOC_PARAGRAPH_INDENT =16 -- Two tabs in rendered vimdoc. 288 289--- @param text string 290--- @return string 291--- Example: "\t\tLua: Prefer |math.abs()|." 292local function tab_indent_vimdoc ( text ) 293return VIMDOC_PARAGRAPH_PREFIX 294.. util 295. md_to_vimdoc ( text , 0 , 0 , TEXT_WIDTH - VIMDOC_PARAGRAPH_INDENT ) 296: gsub ( '\n' , '\n' .. VIMDOC_PARAGRAPH_PREFIX ) 297end 298 299--- @param fun vim.EvalFn 300--- @param write fun(line: string) 301--- Example first line: "--- Lua: Prefer |math.abs()|." 302local function render_eval_see_lua_meta ( fun , write ) 303local text =see_lua_text ( fun ) 304if not text then 305return 306end 307 308write ( '--- ' .. text ) 309write ( '---' ) 310end 311 312--- @param fun vim.EvalFn 313--- @param write fun(line: string) 314--- Example first line: "\t\tLua: Prefer |math.abs()|." 315local function render_eval_see_lua_doc ( fun , write ) 316local text =see_lua_text ( fun ) 317if not text then 318return 319end 320 321write ( tab_indent_vimdoc ( text )) 322write ( '' ) 323end 324 325--- Generates LuaLS docstring for an API function. 326--- @param _f string 327--- @param fun vim.EvalFn 328--- @param write fun(line: string) 329local function render_api_meta ( _f , fun , write ) 330write ( '' ) 331 332if fun . deprecated then 333write ( '--- @deprecated' ) 334end 335 336local internal =vim . startswith ( fun . name , 'nvim__' ) 337if internal or ( fun . since and tonumber ( fun . since ) == 0 ) then 338write ( '--- WARNING: This feature is experimental/unstable.' ) 339write ( '---' ) 340end 341 342local desc =fun . desc 343if desc then 344write ( util . prefix_lines ( '--- ' , norm_text ( desc ))) 345end 346 347-- LuaLS doesn't support @note. Render @note items as a markdown list. 348if fun . notes and # fun . notes > 0 then 349write ( '--- Note:' ) 350write ( util . prefix_lines ( '--- ' , table . concat ( fun . notes , '\n' ))) 351write ( '---' ) 352end 353 354for _ , see in ipairs ( fun . see or {} ) do 355write ( util . prefix_lines ( '--- @see ' , norm_text ( see , 'see-api-meta' ))) 356end 357 358local param_names ={} --- @type string[] 359local params =process_params ( fun . params ) 360for _ , p in ipairs ( params ) do 361local pname , ptype , pdesc =luaescape ( p [ 1 ]), p [ 2 ], p [ 3 ] 362param_names [ # param_names + 1 ] =pname 363if pdesc then 364local s ='--- @param ' .. pname .. ' ' .. ptype .. ' ' 365local pdesc_a =split ( vim . trim ( norm_text ( pdesc ))) 366write ( s .. pdesc_a [ 1 ]) 367for i =2 , # pdesc_a do 368if not pdesc_a [ i ] then 369break 370end 371write ( '--- ' .. pdesc_a [ i ]) 372end 373else 374write ( '--- @param ' .. pname .. ' ' .. ptype ) 375end 376end 377 378if fun . returns ~= 'nil' then 379local ret_desc =fun . returns_desc and ' # ' .. fun . returns_desc or '' 380local ret =LUA_API_RETURN_OVERRIDES [ fun . name ] or fun . returns 381write ( util . prefix_lines ( '--- ' , '@return ' .. ret .. ret_desc )) 382end 383local param_str =table . concat ( param_names , ', ' ) 384 385write ( fmt ( 'function vim.api.%s(%s) end' , fun . name , param_str )) 386end 387 388--- @return table<string, vim.EvalFn> 389local function get_api_keysets_meta () 390local mpack_f =assert ( io . open ( DEP_API_METADATA , 'rb' )) 391local metadata =assert ( vim . mpack . decode ( mpack_f : read ( '*all' ))) 392 393local ret ={} --- @type table<string, vim.EvalFn> 394 395--- @type {name: string, keys: string[], types: table<string,string>}[] 396local keysets =metadata . keysets 397local event_type ='vim.api.keyset.events|vim.api.keyset.events[]' 398 399lint . lint_names ( 'src/nvim/api/keysets_defs.h' , nil , keysets ) 400 401for _ , k in ipairs ( keysets ) do 402util . sort_by_key ( k . keys ) 403local params ={} 404for _ , key in ipairs ( k . keys ) do 405local pty =k . types [ key ] or 'any' 406table . insert ( params , { 407key .. '?' , 408k . name : find ( 'autocmd' ) and key == 'event' and event_type or api_type ( pty ), 409} ) 410end 411ret [ k . name ] ={ 412signature ='NA' , 413name =k . name , 414params =params , 415} 416end 417 418return ret 419end 420 421--- Generates LuaLS docstring for an API keyset. 422--- @param _f string 423--- @param fun vim.EvalFn 424--- @param write fun(line: string) 425local function render_api_keyset_meta ( _f , fun , write ) 426if string . sub ( fun . name , 1 , 1 ) == '_' then 427return -- not exported 428elseif fun . name == 'create_autocmd' then 429local events =vim . deepcopy ( require ( 'nvim.auevents' )) 430for event in pairs ( events . aliases ) do 431events . events [ event ] =true 432end 433write ( '' ) 434write ( '--- @alias vim.api.keyset.events' ) 435for event in vim . spairs ( events . events ) do 436write (( "--- |'%s'" ): format ( event )) 437end 438end 439write ( '' ) 440write ( '--- @class vim.api.keyset.' .. fun . name ) 441for _ , p in ipairs ( fun . params ) do 442write ( '--- @field ' .. p [ 1 ] .. ' ' .. p [ 2 ]) 443end 444end 445 446--- @return table<string, vim.EvalFn> 447local function get_eval_meta () 448return require ( 'nvim.eval' ). funcs 449end 450 451--- Group suffix variants for Lua metadata; Vimdoc keeps separate entries. 452--- @return table<string, vim.EvalFn[]> 453local function get_eval_lua_meta () 454local ret ={} --- @type table<string, vim.EvalFn[]> 455-- Sorting puts the base declaration before its __N variants. 456for name , fun in vim . spairs ( get_eval_meta ()) do 457if fun . lua ~= false then 458local base =name : match ( '^(.-)__%d+$' ) or name 459ret [ base ] =ret [ base ] or {} 460table . insert ( ret [ base ], fun ) 461end 462end 463return ret 464end 465 466--- Generates LuaLS docstring for a Vimscript "eval" function. 467--- @param f string 468--- @param funs vim.EvalFn[] 469--- @param write fun(line: string) 470local function render_eval_meta ( f , funs , write ) 471local fun =assert ( funs [ 1 ]) 472 473local funname =fun . name or f 474local params =process_params ( fun . params ) 475 476write ( '' ) 477if fun . deprecated then 478write ( '--- @deprecated' ) 479end 480 481-- Combine descriptions into one docstring: the base first, then its __N variants. 482for _ , variant in ipairs ( funs ) do 483render_eval_see_lua_meta ( variant , write ) 484 485local desc =variant . desc --[[@as string?]] 486 487if desc then 488desc =desc : gsub ( '\n%s*\n%s*$' , '\n' ) 489for _ , l in ipairs ( split ( desc )) do 490l =l : gsub ( '^ ' , '' ): gsub ( '\t' , ' ' ): gsub ( '@' , '\\@' ) 491write ( '--- ' .. l ) 492end 493end 494end 495 496for _ , text in 497ipairs ( vim . fn . reverse ( fun . generics or {} --[[@as string[] ]] )) 498do 499write ( fmt ( '--- @generic %s' , text )) 500end 501 502-- E.g. sign_undefine__1 becomes @overload fun(list?: string[]): (integer[]). 503for i , overload in ipairs ( funs ) do 504if i > 1 then 505local overload_params ={} --- @type string[] 506local req_args =type ( overload . args ) == 'table' and overload . args [ 1 ] or overload . args or 0 507for j , param in ipairs ( process_params ( overload . params )) do 508local pname , ptype =luaescape ( param [ 1 ]), param [ 2 ] 509local optional =( pname ~= '...' and j > req_args ) and '?' or '' 510overload_params [ # overload_params + 1 ] =fmt ( '%s%s: %s' , pname , optional , ptype ) 511end 512local generics =overload . generics and ( '<' .. table . concat ( overload . generics , ', ' ) .. '>' ) 513or '' 514-- Group the return list so a type like (0|-1)[] remains one array return. 515local ret =overload . returns == false and '' or ': (' .. ( overload . returns or 'any' ) .. ')' 516write ( fmt ( '--- @overload fun%s(%s)%s' , generics , table . concat ( overload_params , ', ' ), ret )) 517end 518end 519 520local req_args =type ( fun . args ) == 'table' and fun . args [ 1 ] or fun . args or 0 521 522for i , param in ipairs ( params ) do 523local pname , ptype =luaescape ( param [ 1 ]), param [ 2 ] 524local optional =( pname ~= '...' and i > req_args ) and '?' or '' 525write ( fmt ( '--- @param %s%s %s' , pname , optional , ptype )) 526end 527 528if fun . returns ~= false then 529local ret_desc =fun . returns_desc and ' # ' .. fun . returns_desc or '' 530write ( '--- @return ' .. ( fun . returns or 'any' ) .. ret_desc ) 531end 532 533write ( render_fun_sig ( funname , params )) 534end 535 536--- Generates vimdoc heading for a Vimscript "eval" function signature. 537--- @param name string 538--- @param name_tag boolean 539--- @param fun vim.EvalFn 540--- @param write fun(line: string) 541local function render_sig_and_tag ( name , name_tag , fun , write ) 542if not fun . signature then 543return 544end 545 546local tags =name_tag and { '*' .. name .. '()*' } or {} 547 548if fun . tags then 549for _ , t in ipairs ( fun . tags ) do 550tags [ # tags + 1 ] ='*' .. t .. '*' 551end 552end 553 554if # tags == 0 then 555write ( fun . signature ) 556return 557end 558 559local tag =table . concat ( tags , ' ' ) 560local siglen =# fun . signature 561local conceal_offset =2 * ( # tags - 1 ) 562local tag_pad_len =math . max ( 1 , 80 - # tag + conceal_offset ) 563 564if siglen + # tag > 80 then 565write ( string . rep ( ' ' , tag_pad_len ) .. tag ) 566write ( fun . signature ) 567else 568write ( fmt ( '%s%s%s' , fun . signature , string . rep ( ' ' , tag_pad_len - siglen ), tag )) 569end 570end 571 572--- Generates vimdoc for a Vimscript "eval" function. 573--- @param f string 574--- @param fun vim.EvalFn 575--- @param write fun(line: string) 576local function render_eval_doc ( f , fun , write ) 577if fun . deprecated or not fun . signature then 578return 579end 580 581render_sig_and_tag ( fun . name or f , not f : find ( '__%d+$' ), fun , write ) 582 583render_eval_see_lua_doc ( fun , write ) 584 585if not fun . desc then 586return 587end 588 589local params =process_params ( fun . params ) 590local req_args =type ( fun . args ) == 'table' and fun . args [ 1 ] or fun . args or 0 591 592local desc_l =split ( vim . trim ( fun . desc )) 593for _ , l in ipairs ( desc_l ) do 594l =l : gsub ( '^ ' , '' ) 595if vim . startswith ( l , '<' ) and not l : match ( '^<[^ \t]+>' ) then 596write ( '<\t\t' .. l : sub ( 2 )) 597elseif l : match ( '^>[a-z0-9]*$' ) then 598write ( l ) 599else 600write ( '\t\t' .. l ) 601end 602end 603 604if # desc_l > 0 and not desc_l [ # desc_l ]: match ( '^<?$' ) then 605write ( '' ) 606end 607 608if fun . fast then 609write ( util . md_to_vimdoc ( 'Attributes: ~' , 16 , 16 , TEXT_WIDTH )) 610write ( util . md_to_vimdoc ( '|api-fast|' , 18 , 18 , TEXT_WIDTH )) 611write ( '' ) 612end 613 614if # params > 0 then 615write ( util . md_to_vimdoc ( 'Parameters: ~' , 16 , 16 , TEXT_WIDTH )) 616for i , param in ipairs ( params ) do 617local pname , ptype =param [ 1 ], param [ 2 ] 618local optional =( pname ~= '...' and i > req_args ) and '?' or '' 619local s =fmt ( '- %-14s (`%s%s`)' , fmt ( '{%s}' , pname ), ptype , optional ) 620write ( util . md_to_vimdoc ( s , 16 , 18 , TEXT_WIDTH )) 621end 622write ( '' ) 623end 624 625if fun . returns ~= false then 626write ( util . md_to_vimdoc ( 'Return: ~' , 16 , 16 , TEXT_WIDTH )) 627local ret =fmt ( '(`%s`)' , ( fun . returns or 'any' )) 628ret =ret .. ( fun . returns_desc and ' ' .. fun . returns_desc or '' ) 629ret =util . md_to_vimdoc ( ret , 18 , 18 , TEXT_WIDTH ) 630write ( ret ) 631write ( '' ) 632end 633end 634 635--- @param d vim.option_defaults 636--- @param vimdoc? boolean 637--- @return string 638local function render_option_default ( d , vimdoc ) 639local dt --- @type integer|boolean|string|fun(): string 640if d . if_false ~= nil then 641dt =d . if_false 642else 643dt =d . if_true 644end 645 646if vimdoc then 647if d . doc then 648return d . doc 649end 650if type ( dt ) == 'boolean' then 651return dt and 'on' or 'off' 652end 653end 654 655if dt == '' or dt == nil or type ( dt ) == 'function' then 656dt =d . meta 657end 658 659local v --- @type string 660if not vimdoc then 661v =vim . inspect ( dt ) --[[@as string]] 662else 663v =type ( dt ) == 'string' and '"' .. dt .. '"' or tostring ( dt ) 664end 665 666--- @type table<string, string|false> 667local envvars ={ 668TMPDIR =false , 669VIMRUNTIME =false , 670XDG_CONFIG_HOME =vim . env . HOME .. '/.local/config' , 671XDG_DATA_HOME =vim . env . HOME .. '/.local/share' , 672XDG_STATE_HOME =vim . env . HOME .. '/.local/state' , 673} 674 675for name , default in pairs ( envvars ) do 676local value =vim . env [ name ] or default 677if value then 678v =v : gsub ( vim . pesc ( value ), '$' .. name ) 679end 680end 681 682return v 683end 684 685--- @param _f string 686--- @param opt vim.option_meta 687--- @param write fun(line: string) 688local function render_option_meta ( _f , opt , write ) 689write ( '' ) 690for _ , l in ipairs ( split ( norm_text ( opt . desc ))) do 691write ( '--- ' .. l ) 692end 693 694-- A non-list string option with a fixed value set (e.g. 'ambiwidth', 'tagcase') documents its 695-- exact value union; everything else uses its Lua type. 696local values ={} --- @type string[] 697if opt . type == 'string' and not opt . list and opt . schema then 698for _ , v in ipairs ( require ( 'nvim.options' ). schema_values ( opt . schema )) do 699values [ # values + 1 ] =fmt ( "'%s'" , v ) 700end 701end 702if opt . type == 'func' or opt . type == 'expr' then 703-- "Callback" option ('operatorfunc', 'foldexpr', …). 704write ( '--- @type string|function' ) 705elseif # values > 0 then 706write ( '--- @type ' .. table . concat ( values , '|' )) 707else 708write ( '--- @type ' .. OPTION_TYPES [ opt . type ]) 709end 710 711write ( 712'vim.o.' 713.. opt . full_name 714.. ' = ' 715.. render_option_default ( opt . defaults --[[@as vim.option_defaults]] ) 716) 717if opt . abbreviation then 718write ( 'vim.o.' .. opt . abbreviation .. ' = vim.o.' .. opt . full_name ) 719end 720 721for _ , s in pairs { 722{ 'wo' , 'win' } , 723{ 'bo' , 'buf' } , 724{ 'go' , 'global' } , 725} do 726local id , scope =s [ 1 ], s [ 2 ] 727if vim . list_contains ( opt . scope , scope ) or ( id == 'go' and # opt . scope > 1 ) then 728local pfx ='vim.' .. id .. '.' 729write ( pfx .. opt . full_name .. ' = vim.o.' .. opt . full_name ) 730if opt . abbreviation then 731write ( pfx .. opt . abbreviation .. ' = ' .. pfx .. opt . full_name ) 732end 733end 734end 735end 736 737--- @param _f string 738--- @param opt vim.option_meta 739--- @param write fun(line: string) 740local function render_vvar_meta ( _f , opt , write ) 741write ( '' ) 742 743local desc =split ( norm_text ( opt . desc )) 744while desc [ # desc ]: match ( '^%s*$' ) do 745desc [ # desc ] =nil 746end 747 748for _ , l in ipairs ( desc ) do 749write ( '--- ' .. l ) 750end 751 752write ( '--- @type ' .. ( opt . type or 'any' )) 753 754if LUA_KEYWORDS [ opt . full_name ] then 755write ( "vim.v['" .. opt . full_name .. "'] = ..." ) 756else 757write ( 'vim.v.' .. opt . full_name .. ' = ...' ) 758end 759end 760 761--- Generates vimdoc for option scope flags. 762--- @param o vim.option_meta 763--- @return string 764local function option_scope_doc ( o ) 765local m ={ 766global ='global' , 767buf ='local to buffer' , 768win ='local to window' , 769tab ='local to tabpage' , 770} 771 772local r --- @type string 773if # o . scope == 1 then 774r =m [ o . scope [ 1 ]] 775else 776assert ( o . scope [ 1 ] == 'global' ) 777r ='global or ' .. m [ o . scope [ 2 ]] .. ( o . scope [ 2 ] ~= 'tab' and ' |global-local|' or '' ) 778end 779 780if 781vim . list_contains ( { 782'bufhidden' , 783'buftype' , 784'filetype' , 785'modified' , 786'previewwindow' , 787'readonly' , 788'scroll' , 789'syntax' , 790'winfixheight' , 791'winfixwidth' , 792'winpinned' , 793} , o . full_name ) 794then 795r =r .. ' |local-noglobal|' 796end 797 798return r 799end 800 801--- Generates vimdoc for option "attributes" (e.g. modeline restriction). 802-- @param o vim.option_meta 803-- @return string? 804local function option_attrs_doc ( o ) 805local attrs ={} --- @type string[] 806if o . secure then 807attrs [ # attrs + 1 ] ='Disallowed in |modeline|. |no-modeline-option|' 808end 809if # attrs == 0 then 810return nil 811end 812return table . concat ( attrs , '\n\t\t\t' ) 813end 814 815--- @param x string 816local function dedent ( x ) 817return ( vim . text . indent ( 0 , ( x : gsub ( '\n%s-([\n]?)$' , '\n%1' )))) 818end 819 820--- @return table<string,vim.option_meta> 821local function get_option_meta () 822local opts =require ( 'nvim.options' ). options 823local optinfo =vim . api . nvim_get_all_options_info () 824local ret ={} --- @type table<string,vim.option_meta> 825for _ , o in ipairs ( opts ) do 826local is_window_option =# o . scope == 1 and o . scope [ 1 ] == 'win' 827local is_option_hidden =o . immutable and not o . varname and not is_window_option 828if not is_option_hidden and o . desc then 829if o . full_name == 'cmdheight' then 830table . insert ( o . scope , 'tab' ) 831end 832local r =vim . deepcopy ( o ) --[[@as vim.option_meta]] 833r . desc =o . desc : gsub ( '^ ' , '' ): gsub ( '\n ' , '\n' ) 834if o . full_name == 'eventignorewin' then 835local events =require ( 'nvim.auevents' ). events 836local tags_file =assert ( io . open ( TAGS_FILE )) 837local tags_text =tags_file : read ( '*a' ) 838tags_file : close () 839local map_fn =function ( event_name , is_window_local ) 840if is_window_local then 841return nil -- Don't include in the list of events outside window context. 842end 843local tag_pat =fmt ( '\n%s\t([^\t]+)\t' , event_name ) 844local link_text =fmt ( '|%s|' , event_name ) 845local tags_match =tags_text : match ( tag_pat ) --- @type string? 846return tags_match and tags_match ~= 'deprecated.txt' and link_text or nil 847end 848local extra_desc =vim . iter ( vim . spairs ( events )): map ( map_fn ): join ( ',\n\t' ) 849r . desc =r . desc : gsub ( '<PLACEHOLDER>' , extra_desc ) 850end 851r . defaults =r . defaults or {} 852if r . defaults . meta == nil then 853r . defaults . meta =optinfo [ o . full_name ]. default 854end 855ret [ o . full_name ] =r 856end 857end 858return ret 859end 860 861--- @return table<string,vim.option_meta> 862local function get_vvar_meta () 863local info =require ( 'nvim.vvars' ). vars 864local ret ={} --- @type table<string,vim.option_meta> 865for name , o in pairs ( info ) do 866o . desc =dedent ( o . desc ) 867o . full_name =name 868ret [ name ] =o 869end 870return ret 871end 872 873--- @param opt vim.option_meta 874--- @return string[] 875local function build_option_tags ( opt ) 876--- @type string[] 877local tags ={ opt . full_name } 878 879tags [ # tags + 1 ] =opt . abbreviation 880if opt . type == 'boolean' then 881for i =1 , # tags do 882tags [ # tags + 1 ] ='no' .. tags [ i ] 883end 884end 885 886for i , t in ipairs ( tags ) do 887tags [ i ] ="'" .. t .. "'" 888end 889 890for _ , t in ipairs ( opt . tags or {} ) do 891tags [ # tags + 1 ] =t 892end 893 894for i , t in ipairs ( tags ) do 895tags [ i ] ='*' .. t .. '*' 896end 897 898return tags 899end 900 901--- @param _f string 902--- @param opt vim.option_meta 903--- @param write fun(line: string) 904local function render_option_doc ( _f , opt , write ) 905local tags =build_option_tags ( opt ) 906local tag_str =table . concat ( tags , ' ' ) 907local conceal_offset =2 * ( # tags - 1 ) 908local tag_pad =string . rep ( '\t' , math . ceil (( 64 - # tag_str + conceal_offset ) / 8 )) 909-- local pad = string.rep(' ', 80 - #tag_str + conceal_offset) 910write ( tag_pad .. tag_str ) 911 912local name_str --- @type string 913if opt . abbreviation then 914name_str =fmt ( "'%s' '%s'" , opt . full_name , opt . abbreviation ) 915else 916name_str =fmt ( "'%s'" , opt . full_name ) 917end 918 919-- Callback (func/expr) options are labeled "string" in the help, their ":set" form. 920local otype =( opt . type == 'func' or opt . type == 'expr' ) and 'string' or opt . type 921if opt . defaults . doc or opt . defaults . if_true ~= nil or opt . defaults . meta ~= nil then 922local v =render_option_default ( opt . defaults --[[@as vim.option_defaults]] , true ) 923local pad =string . rep ( '\t' , math . max ( 1 , math . ceil (( 24 - # name_str ) / 8 ))) 924if opt . defaults . doc then 925local deflen =# fmt ( '%s%s%s (' , name_str , pad , otype ) 926--- @type string 927v =v : gsub ( '\n' , '\n' .. string . rep ( ' ' , deflen - 2 )) 928end 929write ( fmt ( '%s%s%s\t(default %s)' , name_str , pad , otype , v )) 930else 931write ( fmt ( '%s\t%s' , name_str , otype )) 932end 933 934write ( '\t\t\t' .. option_scope_doc ( opt )) 935local attrs =option_attrs_doc ( opt ) 936if attrs then 937write ( '\t\t\t' .. attrs ) 938end 939for _ , l in ipairs ( split ( opt . desc )) do 940if l == '<' or l : match ( '^<%s' ) then 941write ( l ) 942else 943write ( '\t' .. l : gsub ( '\\<' , '<' )) 944end 945end 946end 947 948--- @param _f string 949--- @param vvar vim.option_meta 950--- @param write fun(line: string) 951local function render_vvar_doc ( _f , vvar , write ) 952local name =vvar . full_name 953 954local tags ={ 'v:' .. name , name .. '-variable' } 955if vvar . tags then 956vim . list_extend ( tags , vvar . tags ) 957end 958 959for i , t in ipairs ( tags ) do 960tags [ i ] ='*' .. t .. '*' 961end 962 963local tag_str =table . concat ( tags , ' ' ) 964local conceal_offset =2 * ( # tags - 1 ) 965 966local tag_pad =string . rep ( '\t' , math . ceil (( 64 - # tag_str + conceal_offset ) / 8 )) 967write ( tag_pad .. tag_str ) 968 969local desc =split ( vvar . desc ) 970 971if ( # desc == 1 or # desc == 2 and desc [ 2 ]: match ( '^%s*$' )) and # name < 10 then 972-- single line 973write ( 'v:' .. name .. '\t' .. desc [ 1 ]: gsub ( '^%s*' , '' )) 974write ( '' ) 975else 976write ( 'v:' .. name ) 977for _ , l in ipairs ( desc ) do 978if l == '<' or l : match ( '^<%s' ) then 979write ( l ) 980else 981write ( '\t\t' .. l : gsub ( '\\<' , '<' )) 982end 983end 984end 985end 986 987--- @class nvim.gen_eval_files.elem 988--- @field path string 989--- @field from? string Skip lines in path until this pattern is reached. 990--- @field funcs fun(): table<string, table> 991--- @field render fun(f:string,obj:table,write:fun(line:string)) 992--- @field header? string[] 993--- @field footer? string[] 994 995--- @type nvim.gen_eval_files.elem[] 996local CONFIG ={ 997{ 998path ='runtime/lua/vim/_meta/vimfn.gen.lua' , 999header =LUA_META_HEADER , 1000funcs =get_eval_lua_meta , 1001render =render_eval_meta , 1002} , 1003{ 1004path ='runtime/lua/vim/_meta/api.gen.lua' , 1005header =LUA_API_META_HEADER , 1006funcs =get_api_meta , 1007render =render_api_meta , 1008} , 1009{ 1010path ='runtime/lua/vim/_meta/api_keysets.gen.lua' , 1011header =LUA_META_HEADER , 1012funcs =get_api_keysets_meta , 1013render =render_api_keyset_meta , 1014} , 1015{ 1016path ='runtime/doc/vimfn.txt' , 1017funcs =get_eval_meta , 1018render =render_eval_doc , 1019header ={ 1020'*vimfn.txt* Nvim' , 1021'' , 1022'' , 1023'\t\t NVIM REFERENCE MANUAL' , 1024'' , 1025'' , 1026'Vimscript functions\t\t\t*vimscript-functions* *builtin.txt*' , 1027'' , 1028'For functions grouped by what they are used for see |function-list|.' , 1029'' , 1030'\t\t\t\t Type |gO| to see the table of contents.' , 1031'==============================================================================' , 1032'1. Details *vimscript-functions-details*' , 1033'' , 1034} , 1035footer ={ 1036'==============================================================================' , 1037'2. Matching a pattern in a String *string-match*' , 1038'' , 1039'This is common between several functions. A regexp pattern as explained at' , 1040'|pattern| is normally used to find a match in the buffer lines. When a' , 1041'pattern is used to find a match in a String, almost everything works in the' , 1042'same way. The difference is that a String is handled like it is one line.' , 1043'When it contains a "\\n" character, this is not seen as a line break for the' , 1044'pattern. It can be matched with a "\\n" in the pattern, or with ".". Example:' , 1045'>vim' , 1046'\tlet a = "aaaa\\nxxxx"' , 1047'\techo matchstr(a, "..\\n..")' , 1048'\t" aa' , 1049'\t" xx' , 1050'\techo matchstr(a, "a.x")' , 1051'\t" a' , 1052'\t" x' , 1053'' , 1054'Don\'t forget that "^" will only match at the first character of the String and' , 1055'"$" at the last character of the string. They don\'t match after or before a' , 1056'"\\n".' , 1057'' , 1058' vim:tw=78:ts=8:noet:ft=help:norl:' , 1059} , 1060} , 1061{ 1062path ='runtime/lua/vim/_meta/options.gen.lua' , 1063header =LUA_OPTION_META_HEADER , 1064funcs =get_option_meta , 1065render =render_option_meta , 1066} , 1067{ 1068path ='runtime/doc/options.txt' , 1069header ={ '' } , 1070from ='A jump table for the options with a short description can be found at |Q_op|.' , 1071footer ={ 1072' vim:tw=78:ts=8:noet:ft=help:norl:' , 1073} , 1074funcs =get_option_meta , 1075render =render_option_doc , 1076} , 1077{ 1078path ='runtime/lua/vim/_meta/vvars.gen.lua' , 1079header =LUA_VVAR_META_HEADER , 1080funcs =get_vvar_meta , 1081render =render_vvar_meta , 1082} , 1083{ 1084path ='runtime/doc/vvars.txt' , 1085header ={ '' } , 1086from ='Type |gO| to see the table of contents.' , 1087footer ={ 1088' vim:tw=78:ts=8:noet:ft=help:norl:' , 1089} , 1090funcs =get_vvar_meta , 1091render =render_vvar_doc , 1092} , 1093} 1094 1095--- @param elem nvim.gen_eval_files.elem 1096local function render ( elem ) 1097( 'Rendering ' .. elem . path ) 1098local from_lines ={} --- @type string[] 1099local from =elem . from 1100if from then 1101for line in io . lines ( elem . path ) do 1102from_lines [ # from_lines + 1 ] =line 1103if line : match ( from ) then 1104break 1105end 1106end 1107end 1108 1109local o =assert ( io . open ( elem . path , 'w' )) 1110 1111--- @param l string 1112local function write ( l ) 1113local l1 =l : gsub ( '%s+$' , '' ) 1114o : write ( l1 ) 1115o : write ( '\n' ) 1116end 1117 1118for _ , l in ipairs ( from_lines ) do 1119write ( l ) 1120end 1121 1122for _ , l in ipairs ( elem . header or {} ) do 1123write ( l ) 1124end 1125 1126local funcs =elem . funcs () 1127 1128--- @type string[] 1129local fnames =vim . tbl_keys ( funcs ) 1130table . sort ( fnames ) 1131 1132for _ , f in ipairs ( fnames ) do 1133elem . render ( f , funcs [ f ], write ) 1134end 1135 1136for _ , l in ipairs ( elem . footer or {} ) do 1137write ( l ) 1138end 1139 1140o : close () 1141end 1142 1143local function main () 1144for _ , c in ipairs ( CONFIG ) do 1145render ( c ) 1146end 1147end 1148 1149main ()