robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#!/usr/bin/env -S nvim -l 2--- Generates Nvim :help docs from Lua/C docstrings. 3--- 4--- Usage: 5--- make doc 6--- 7--- The generated :help text for each function is formatted as follows: 8--- - Max width of 78 columns (`TEXT_WIDTH`). 9--- - Indent with spaces (not tabs). 10--- - Indent of 4 columns for body text (`INDENTATION`). 11--- - Function signature and helptag (right-aligned) on the same line. 12--- - Signature and helptag must have a minimum of 8 spaces between them. 13--- - If the signature is too long, it is placed on the line after the helptag. 14--- Signature wraps with subsequent lines indented to the open parenthesis. 15--- - Subsection bodies are indented an additional 4 spaces. 16--- - Body consists of function description, parameters, return description, and 17--- C declaration (`INCLUDE_C_DECL`). 18--- - Parameters are omitted for the `void` and `Error *` types, or if the 19--- parameter is marked as [out]. 20--- - Each function documentation is separated by a single line. 21 22local luacats_parser =require ( 'gen.luacats_parser' ) 23local cdoc_parser =require ( 'gen.cdoc_parser' ) 24local util =require ( 'gen.util' ) 25local lint =require ( 'gen.lint' ) 26 27local fmt =string . format 28 29local wrap =util . wrap 30local md_to_vimdoc =util . md_to_vimdoc 31local align_tags =util . align_tags 32 33local TEXT_WIDTH =78 34local INDENTATION =4 35 36--- @class (exact) nvim.gen_vimdoc.Config 37--- 38--- Generated documentation target, e.g. api.txt 39--- @field filename string 40--- 41--- Ordered filenames or groups of filenames to merge into one section. 42--- For a group, the first filename determines the section name and help tag. 43--- @field section_order (string|string[])[] 44--- 45--- List of files/directories for doxygen to read, relative to `base_dir`. 46--- @field files string[] 47--- 48--- Section name overrides. Key: filename (e.g., vim.c) 49--- @field section_name? table<string,string> 50--- 51--- @field fn_name_pat? string 52--- 53--- @field fn_xform? fun(fun: nvim.luacats.parser.fun) 54--- 55--- @field brief_xform? fun(brief: string): string 56--- 57--- For generated section names. 58--- @field section_fmt fun(name: string): string 59--- 60--- @field helptag_fmt fun(name: string): string|string[] 61--- 62--- Per-function helptag. 63--- @field fn_helptag_fmt? fun(fun: nvim.gen_vimdoc.HelptagTarget): string 64 65---@alias nvim.gen_vimdoc.HelptagTarget 66---| nvim.luacats.parser.fun 67---| nvim.luacats.parser.field 68---| nvim.luacats.parser.param 69 70local function contains ( t , xs ) 71return vim . tbl_contains ( xs , t ) 72end 73 74--- True if the `.` class member should render like a module function. 75--- @param fun nvim.gen_vimdoc.HelptagTarget 76--- @return boolean 77local function is_module_fun ( fun ) 78return fun . classvar ~= nil 79and fun . member_sep == '.' 80and fun . modvar ~= nil 81and fun . module ~= nil 82and fun . classvar == fun . modvar 83end 84 85--- @type {level:integer, prerelease:boolean}? 86local nvim_api_info_ 87 88--- @return {level: integer, prerelease:boolean} 89local function nvim_api_info () 90if not nvim_api_info_ then 91--- @type integer?, boolean? 92local level , prerelease 93for l in io . lines ( 'CMakeLists.txt' ) do 94--- @cast l string 95if level and prerelease then 96break 97end 98local m1 =l : match ( '^set%(NVIM_API_LEVEL%s+(%d+)%)' ) 99if m1 then 100level =tonumber ( m1 ) --[[@as integer]] 101end 102local m2 =l : match ( '^set%(NVIM_API_PRERELEASE%s+(%w+)%)' ) 103if m2 then 104prerelease =m2 == 'true' 105end 106end 107nvim_api_info_ ={ level =assert ( level ), prerelease =assert ( prerelease ) } 108end 109 110return nvim_api_info_ 111end 112 113--- @param fun nvim.gen_vimdoc.HelptagTarget 114--- @return string 115local function fn_helptag_fmt_common ( fun ) 116local fn_sfx =fun . table and '' or '()' 117if is_module_fun ( fun ) then 118return fmt ( '%s.%s%s' , fun . module , fun . name , fn_sfx ) 119end 120if fun . classvar then 121return fmt ( '%s:%s%s' , fun . classvar , fun . name , fn_sfx ) 122end 123if fun . module then 124return fmt ( '%s.%s%s' , fun . module , fun . name , fn_sfx ) 125end 126return fun . name .. fn_sfx 127end 128 129--- @type table<string,nvim.gen_vimdoc.Config> 130local config ={ 131api ={ 132filename ='api.txt' , 133section_order ={ 134-- Sections at the top, in a specific order: 135'events.c' , 136'vim.c' , 137'vimscript.c' , 138 139-- Sections in alphanumeric order: 140'autocmd.c' , 141'buffer.c' , 142'command.c' , 143'extmark.c' , 144'options.c' , 145'tabpage.c' , 146'ui.c' , 147'win_config.c' , 148'window.c' , 149} , 150fn_name_pat ='nvim_.*' , 151files ={ 'src/nvim/api' } , 152section_name ={ 153[ 'vim.c' ] ='Global' , 154} , 155section_fmt =function ( name ) 156if name == 'Events' then 157return 'Global Events' 158end 159 160return name .. ' Functions' 161end , 162helptag_fmt =function ( name ) 163return fmt ( 'api-%s' , name : lower ()) 164end , 165fn_helptag_fmt =function ( fun ) 166local name =fun . name 167if vim . endswith ( name , '_event' ) then 168return name 169end 170return fn_helptag_fmt_common ( fun ) 171end , 172} , 173lua ={ 174filename ='lua.txt' , 175section_order ={ 176-- Sections at the top, in a specific order: 177'builtin.lua' , 178'options.lua' , 179{ 'editor.lua' , 'inspect.lua' , 'shared.lua' } , 180'_inspector.lua' , 181 182-- Sections in alphanumeric order: 183'base64.lua' , 184'filetype.lua' , 185'fs.lua' , 186'glob.lua' , 187'hl.lua' , 188'iter.lua' , 189'json.lua' , 190'keymap.lua' , 191'loader.lua' , 192'log.lua' , 193'lpeg.lua' , 194'mpack.lua' , 195'net.lua' , 196'pos.lua' , 197'range.lua' , 198're.lua' , 199'regex.lua' , 200'secure.lua' , 201'snippet.lua' , 202'spell.lua' , 203'system.lua' , 204'text.lua' , 205'ui.lua' , 206'img.lua' , -- ui/img.lua 207'uri.lua' , 208'version.lua' , 209 210-- Sections at the end, in a specific order: 211'ui2.lua' , 212} , 213files ={ 214'runtime/lua/vim/_core/editor.lua' , 215'runtime/lua/vim/_core/options.lua' , 216'runtime/lua/vim/_core/shared.lua' , 217'runtime/lua/vim/_core/system.lua' , 218'runtime/lua/vim/_core/ui2.lua' , 219'runtime/lua/vim/_inspector.lua' , 220'runtime/lua/vim/_meta/base64.lua' , 221'runtime/lua/vim/_meta/builtin.lua' , 222'runtime/lua/vim/_meta/json.lua' , 223'runtime/lua/vim/_meta/lpeg.lua' , 224'runtime/lua/vim/_meta/mpack.lua' , 225'runtime/lua/vim/_meta/re.lua' , 226'runtime/lua/vim/_meta/regex.lua' , 227'runtime/lua/vim/_meta/spell.lua' , 228'runtime/lua/vim/filetype.lua' , 229'runtime/lua/vim/fs.lua' , 230'runtime/lua/vim/glob.lua' , 231'runtime/lua/vim/hl.lua' , 232'runtime/lua/vim/inspect.lua' , 233'runtime/lua/vim/iter.lua' , 234'runtime/lua/vim/keymap.lua' , 235'runtime/lua/vim/loader.lua' , 236'runtime/lua/vim/log.lua' , 237'runtime/lua/vim/net.lua' , 238'runtime/lua/vim/pos.lua' , 239'runtime/lua/vim/range.lua' , 240'runtime/lua/vim/secure.lua' , 241'runtime/lua/vim/snippet.lua' , 242'runtime/lua/vim/text.lua' , 243'runtime/lua/vim/ui.lua' , 244'runtime/lua/vim/ui/img.lua' , 245'runtime/lua/vim/uri.lua' , 246'runtime/lua/vim/version.lua' , 247} , 248fn_xform =function ( fun ) 249if contains ( fun . module , { 'vim.uri' , 'vim._core.shared' , 'vim._core.editor' } ) then 250fun . module ='vim' 251end 252 253if fun . module == 'vim' and fun . name == 'cmd' then 254fun . table =nil 255end 256 257-- Render the callable version module as ordinary module functions. 258if fun . class == 'vim.VersionModule' then 259fun . classvar =nil 260end 261 262if fun . classvar or vim . startswith ( fun . name , 'vim.' ) or fun . module == 'vim.iter' then 263return 264end 265 266fun . name =fmt ( '%s.%s' , fun . module , fun . name ) 267end , 268section_name ={ 269[ '_inspector.lua' ] ='inspector' , 270[ 'img.lua' ] ='ui.img' , 271[ 'ui2.lua' ] ='ui2' , 272} , 273section_fmt =function ( name ) 274name =name : lower () 275if name == 'editor' then 276return 'Lua module: vim' 277elseif name == 'system' then 278return 'Lua module: vim.system' 279elseif name == 'options' then 280return 'LUA-VIMSCRIPT BRIDGE' 281elseif name == 'builtin' then 282return 'VIM' 283elseif name == 'ui2' then 284return 'UI2' 285end 286return 'Lua module: vim.' .. name 287end , 288helptag_fmt =function ( name ) 289if name == 'Editor' then 290return 'lua-vim' 291elseif name == 'System' then 292return 'lua-vim-system' 293elseif name == 'Options' then 294return 'lua-vimscript' 295elseif name == 'ui2' then 296return 'ui2' 297end 298return 'vim.' .. name : lower () 299end , 300fn_helptag_fmt =function ( fun ) 301local name =fun . name 302 303if vim . startswith ( name , 'vim.' ) then 304local fn_sfx =fun . table and '' or '()' 305return name .. fn_sfx 306elseif fun . classvar == 'Option' then 307return fmt ( 'vim.opt:%s()' , name ) 308end 309 310return fn_helptag_fmt_common ( fun ) 311end , 312} , 313lsp ={ 314filename ='lsp.txt' , 315section_order ={ 316-- Sections at the top, in a specific order: 317{ 'lsp.lua' , 'tagfunc.lua' } , 318 319-- Sections in alphanumeric order: 320'buf.lua' , 321'client.lua' , 322'codelens.lua' , 323'completion.lua' , 324'diagnostic.lua' , 325'document_color.lua' , 326'folding_range.lua' , 327'handlers.lua' , 328'inlay_hint.lua' , 329'inline_completion.lua' , 330'linked_editing_range.lua' , 331'log.lua' , 332'on_type_formatting.lua' , 333'rpc.lua' , 334'semantic_tokens.lua' , 335 336-- Sections at the end, in a specific order: 337'util.lua' , 338'protocol.lua' , 339} , 340files ={ 341'runtime/lua/vim/lsp' , 342'runtime/lua/vim/lsp.lua' , 343} , 344fn_xform =function ( fun ) 345fun . name =fun . name : gsub ( 'result%.' , '' ) 346if fun . module == 'vim.lsp' and fun . name == 'config' then 347fun . table =nil 348end 349if fun . module == 'vim.lsp.protocol' then 350fun . classvar =nil 351end 352end , 353section_fmt =function ( name ) 354if name : lower () == 'lsp' then 355return 'Lua module: vim.lsp' 356end 357return 'Lua module: vim.lsp.' .. name : lower () 358end , 359helptag_fmt =function ( name ) 360if name : lower () == 'lsp' then 361return 'lsp-core' 362end 363return fmt ( 'lsp-%s' , name : lower ()) 364end , 365} , 366async ={ 367filename ='lua-async.txt' , 368section_order ={ 369{ 'async.lua' , '_core.lua' , '_semaphore.lua' } , 370} , 371files ={ 372'runtime/lua/vim/async.lua' , 373'runtime/lua/vim/async/_core.lua' , 374'runtime/lua/vim/async/_semaphore.lua' , 375} , 376section_fmt =function () 377return 'Lua module: vim.async' 378end , 379helptag_fmt =function () 380return { 'lua-async' , 'vim.async' } 381end , 382fn_xform =function ( fun ) 383if fun . classvar == 'M' then 384fun . classvar =nil 385end 386end , 387} , 388diagnostic ={ 389filename ='diagnostic.txt' , 390section_order ={ 391'diagnostic.lua' , 392} , 393files ={ 'runtime/lua/vim/diagnostic.lua' } , 394section_fmt =function () 395return 'Lua module: vim.diagnostic' 396end , 397helptag_fmt =function () 398return 'diagnostic-api' 399end , 400} , 401treesitter ={ 402filename ='treesitter.txt' , 403section_order ={ 404-- Sections at the top, in a specific order: 405'tstree.lua' , 406'tsnode.lua' , 407'treesitter.lua' , 408 409-- Sections in alphanumeric order: 410'dev.lua' , 411'highlighter.lua' , 412'language.lua' , 413'languagetree.lua' , 414{ 'query.lua' , 'tsquery.lua' } , 415} , 416files ={ 417'runtime/lua/vim/treesitter/_meta/' , 418'runtime/lua/vim/treesitter.lua' , 419'runtime/lua/vim/treesitter/' , 420} , 421section_fmt =function ( name ) 422if name : lower () == 'treesitter' then 423return 'Lua module: vim.treesitter' 424elseif name : lower () == 'tstree' then 425return 'TREESITTER TREES' 426elseif name : lower () == 'tsnode' then 427return 'TREESITTER NODES' 428end 429return 'Lua module: vim.treesitter.' .. name : lower () 430end , 431helptag_fmt =function ( name ) 432if name : lower () == 'treesitter' then 433return 'lua-treesitter-core' 434elseif name : lower () == 'query' then 435return 'lua-treesitter-query' 436elseif name : lower () == 'tstree' then 437return { 'treesitter-tree' , 'TSTree' } 438elseif name : lower () == 'tsnode' then 439return { 'treesitter-node' , 'TSNode' } 440end 441return 'treesitter-' .. name : lower () 442end , 443} , 444health ={ 445filename ='health.txt' , 446files ={ 447'runtime/lua/vim/health.lua' , 448} , 449section_order ={ 450'health.lua' , 451} , 452section_fmt =function ( _name ) 453return 'Checkhealth' 454end , 455helptag_fmt =function () 456return { 'vim.health' , 'health' } 457end , 458} , 459pack ={ 460filename ='pack.txt' , 461files ={ 'runtime/lua/vim/pack.lua' } , 462section_order ={ 'pack.lua' } , 463section_fmt =function ( _name ) 464return 'Plugin manager' 465end , 466helptag_fmt =function () 467return { 'vim.pack' } 468end , 469} , 470plugins ={ 471filename ='plugins.txt' , 472section_order ={ 473'difftool.lua' , 474'editorconfig.lua' , 475'spellfile.lua' , 476'tohtml.lua' , 477'undotree.lua' , 478} , 479files ={ 480'runtime/lua/editorconfig.lua' , 481'runtime/lua/nvim/spellfile.lua' , 482'runtime/pack/dist/opt/nvim.tohtml/lua/tohtml.lua' , 483'runtime/pack/dist/opt/nvim.undotree/lua/undotree.lua' , 484'runtime/pack/dist/opt/nvim.difftool/lua/difftool.lua' , 485} , 486fn_xform =function ( fun ) 487if fun . module == 'editorconfig' then 488-- Example: "editorconfig.properties.root()" => "editorconfig.root" 489fun . table =true 490fun . name =vim . split ( fun . name , '.' , { plain =true } )[ 2 ] or fun . name 491end 492if vim . startswith ( fun . module , 'nvim.' ) then 493fun . module =fun . module : sub ( # 'nvim.' + 1 ) 494end 495end , 496section_fmt =function ( name ) 497return 'Builtin plugin: ' .. name : lower () 498end , 499helptag_fmt =function ( name ) 500name =name : lower () 501if vim . tbl_contains ( { 'spellfile' , 'tohtml' , 'undotree' } , name ) then 502name =( 'package-%s' ): format ( name ) 503end 504return name 505end , 506} , 507tui ={ 508filename ='tui.txt' , 509section_order ={ 'tui.lua' } , 510files ={ 'runtime/lua/vim/_meta/tui.lua' } , 511section_fmt =function ( _name ) 512return 'Terminfo override' 513end , 514helptag_fmt =function () 515return { 'tui-termdefs' } 516end , 517brief_xform =function ( brief ) 518local terminfo =require ( 'src.gen.terminfo' ). fields 519local booleans =terminfo . bools 520local numbers =terminfo . ints 521local strings =terminfo . strings 522 523local strings_ext =vim . tbl_map ( function ( v ) 524return v [ 1 ] 525end , terminfo . strings_ext ) 526 527local unshifted_keys =vim . tbl_map ( function ( v ) 528return 'key_' .. v [ 1 ] 529end , ( vim . tbl_filter ( function ( v ) 530return not v [ 2 ] 531end , terminfo . termkeys ))) 532 533local shifted_keys =vim . tbl_map ( function ( v ) 534return 'key_' .. v [ 1 ] 535end , ( vim . tbl_filter ( function ( v ) 536return v [ 2 ] 537end , terminfo . termkeys ))) 538 539local f_keys ={ 'key_f1' , '...' , 'key_f' .. terminfo . func_key_max } 540 541local table_rows ={ { 'Field' , 'Type' } } 542local col1_max_width =0 543--- @param defs string[] 544--- @param type string 545--- @param omit_padding_row nil | boolean 546--- @param skip_sort nil | boolean 547local function add_defs ( defs , type , omit_padding_row , skip_sort ) 548if not skip_sort then 549table . sort ( defs ) 550end 551for _ , def in ipairs ( defs ) do 552table . insert ( table_rows , { def , type } ) 553col1_max_width =math . max ( col1_max_width , # def ) 554end 555if not omit_padding_row then 556table . insert ( table_rows , { '' , '' } ) 557end 558end 559 560add_defs ( booleans , 'bool' ) 561add_defs ( numbers , 'int' ) 562add_defs ( strings , 'string' ) 563add_defs ( strings_ext , 'string' ) 564add_defs ( unshifted_keys , 'string' ) 565add_defs ( shifted_keys , 'string[]' ) 566add_defs ( f_keys , 'string' , true , true ) 567 568-- Convert all the table rows into strings by calculating the padding 569-- needed between them 570local table_str_rows ={} 571local min_padding =3 -- padding between table columns 572local max_width =0 -- max width of col1 + padding + col2 573local indent =' ' 574for _ , row in ipairs ( table_rows ) do 575if # row [ 1 ] == 0 then 576table . insert ( table_str_rows , '' ) 577else 578local padding =string . rep ( ' ' , col1_max_width + min_padding - # row [ 1 ]) 579local val =table . concat ( row , padding ) 580max_width =math . max ( max_width , # val ) 581table . insert ( table_str_rows , indent .. val ) 582end 583end 584 585-- Add table header 586table . insert ( table_str_rows , 2 , indent .. string . rep ( '-' , max_width )) 587return ( brief : gsub ( 'NVIM_TERMDEFS_TABLE' , table . concat ( table_str_rows , '\n' ))) 588end , 589} , 590} 591 592--- @param ty string 593--- @param generics table<string,string> 594--- @return string 595local function replace_generics ( ty , generics ) 596if ty : sub ( - 2 ) == '[]' then 597local ty0 =ty : sub ( 1 , - 3 ) 598if generics [ ty0 ] then 599return generics [ ty0 ] .. '[]' 600end 601elseif ty : sub ( - 1 ) == '?' then 602local ty0 =ty : sub ( 1 , - 2 ) 603if generics [ ty0 ] then 604return generics [ ty0 ] .. '?' 605end 606end 607 608return generics [ ty ] or ty 609end 610 611--- @param name string 612local function fmt_field_name ( name ) 613local name0 , opt =name : match ( '^([^?]*)(%??)$' ) 614return fmt ( '{%s}%s' , name0 , opt ) 615end 616 617--- @param ty string 618--- @param generics? table<string,string> 619--- @param default? string 620local function render_type ( ty , generics , default ) 621ty =ty : gsub ( 'vim%.lsp%.protocol%.Method.[%w.]+' , 'string' ) 622 623if generics then 624ty =replace_generics ( ty , generics ) 625end 626ty =ty : gsub ( '%s*|%s*nil' , '?' ) 627ty =ty : gsub ( 'nil%s*|%s*(.*)' , '%1?' ) 628ty =ty : gsub ( '%s*|%s*' , '|' ) 629if default then 630return fmt ( '(`%s`, default: %s)' , ty , default ) 631end 632return fmt ( '(`%s`)' , ty ) 633end 634 635--- @param p nvim.luacats.parser.param|nvim.luacats.parser.field 636local function should_render_field_or_param ( p ) 637return not p . nodoc 638and not p . access 639and not contains ( p . name , { '_' , 'self' } ) 640and not vim . startswith ( p . name , '_' ) 641end 642 643--- Gets a field's description and its "(default: …)" value, if any (see `lsp/client.lua` for 644--- examples). 645--- 646--- @param desc? string 647--- @return string?, string? 648local function get_default ( desc ) 649if not desc then 650return 651end 652 653local default =desc : match ( '\n%s*%([dD]efault: ([^)]+)%)' ) 654if default then 655desc =desc : gsub ( '\n%s*%([dD]efault: [^)]+%)' , '' ) 656end 657 658return desc , default 659end 660 661--- @param ty string 662--- @param classes? table<string,nvim.luacats.parser.class> 663--- @return nvim.luacats.parser.class? 664local function get_class ( ty , classes ) 665if not classes then 666return 667end 668 669local cty =ty : gsub ( '%s*|%s*nil' , '?' ): gsub ( '?$' , '' ): gsub ( '%[%]$' , '' ): gsub ( '%b<>$' , '' ) 670 671return classes [ cty ] 672end 673 674--- Recursively resolves `@inlinedoc` classes: replaces the type with "table" and expands class 675--- fields into the object's description. 676--- 677--- @param obj nvim.luacats.parser.param|nvim.luacats.parser.return|nvim.luacats.parser.field 678--- @param classes? table<string,nvim.luacats.parser.class> 679local function inline_type ( obj , classes ) 680local ty =obj . type 681if not ty then 682return 683end 684 685local cls =get_class ( ty , classes ) 686 687if not cls or cls . nodoc or not cls . inlinedoc then 688return 689end 690 691local ty_isopt =( ty : match ( '%?$' ) or ty : match ( '%s*|%s*nil' )) ~= nil 692local ty_islist =( ty : match ( '%[%]$' )) ~= nil 693ty =ty_isopt and 'table?' or ty_islist and 'table[]' or 'table' 694 695local desc =obj . desc or '' 696if cls . desc then 697desc =desc .. cls . desc 698elseif desc == '' then 699if ty_islist then 700desc =desc .. 'A list of objects with the following fields:' 701elseif cls . parent then 702desc =desc .. fmt ( 'Extends |%s| with the additional fields:' , cls . parent ) 703else 704desc =desc .. 'A table with the following fields:' 705end 706end 707 708util . sort_by_key ( cls . fields , 'name' ) 709 710local desc_append ={} 711for _ , f in ipairs ( cls . fields ) do 712if not f . access then 713inline_type ( f , classes ) 714local fdesc , default =get_default ( f . desc ) 715if fdesc then 716-- Indent nested list items from recursive inlining. 717fdesc =fdesc : gsub ( '\n(%- {)' , '\n %1' ) 718end 719local fty =render_type ( f . type , nil , default ) 720local fnm =fmt_field_name ( f . name ) 721table . insert ( desc_append , table . concat ( { '-' , fnm , fty , fdesc } , ' ' )) 722end 723end 724 725desc =desc .. '\n' .. table . concat ( desc_append , '\n' ) 726obj . type =ty 727obj . desc =desc 728end 729 730--- @param xs (nvim.luacats.parser.param|nvim.luacats.parser.field)[] 731--- @param generics? table<string,string> 732--- @param classes? table<string,nvim.luacats.parser.class> 733--- @param cfg nvim.gen_vimdoc.Config 734local function render_fields_or_params ( xs , generics , classes , cfg ) 735local ret ={} --- @type string[] 736 737xs =vim . tbl_filter ( should_render_field_or_param , xs ) 738 739local indent =0 740for _ , p in ipairs ( xs ) do 741if p . type or p . desc then 742indent =math . max ( indent , # p . name + 3 ) 743end 744end 745 746for _ , p in ipairs ( xs ) do 747local pdesc , default =get_default ( p . desc ) 748p . desc =pdesc 749 750inline_type ( p , classes ) 751local nm , ty =p . name , p . type 752 753local desc =p . classvar and fmt ( 'See |%s|.' , cfg . fn_helptag_fmt ( p )) or p . desc 754 755local fnm =p . kind == 'operator' and fmt ( 'op(%s)' , nm ) or fmt_field_name ( nm ) 756local pnm =fmt ( ' • %-' .. indent .. 's' , fnm ) 757 758if ty then 759local pty =render_type ( ty , generics , default ) 760 761if desc then 762table . insert ( ret , pnm ) 763if # pty > TEXT_WIDTH - indent then 764vim . list_extend ( ret , { ' ' , pty , '\n' } ) 765table . insert ( ret , md_to_vimdoc ( desc , 9 + indent , 9 + indent , TEXT_WIDTH , true )) 766else 767desc =fmt ( '%s %s' , pty , desc ) 768table . insert ( ret , md_to_vimdoc ( desc , 1 , 9 + indent , TEXT_WIDTH , true )) 769end 770else 771table . insert ( ret , fmt ( '%s %s\n' , pnm , pty )) 772end 773else 774if desc then 775table . insert ( ret , pnm ) 776table . insert ( ret , md_to_vimdoc ( desc , 1 , 9 + indent , TEXT_WIDTH , true )) 777end 778end 779end 780 781return table . concat ( ret ) 782end 783 784--- @param class nvim.luacats.parser.class 785--- @param classes table<string,nvim.luacats.parser.class> 786--- @param hidden_fields? table<string,table<string,true>> 787--- @param cfg nvim.gen_vimdoc.Config 788local function render_class ( class , classes , hidden_fields , cfg ) 789if class . access or class . nodoc or class . inlinedoc then 790return 791end 792 793local ret ={} --- @type string[] 794 795table . insert ( ret , fmt ( '*%s*\n' , class . name )) 796 797if class . parent then 798local txt =fmt ( 'Extends: |%s|' , class . parent ) 799table . insert ( ret , md_to_vimdoc ( txt , INDENTATION , INDENTATION , TEXT_WIDTH )) 800table . insert ( ret , '\n' ) 801end 802 803if class . desc then 804table . insert ( ret , md_to_vimdoc ( class . desc , INDENTATION , INDENTATION , TEXT_WIDTH )) 805end 806 807local class_hidden =hidden_fields and hidden_fields [ class . name ] 808local fields =class . fields 809if class_hidden then 810fields =vim . tbl_filter ( function ( field ) 811return not class_hidden [ field . name ] 812end , fields ) 813end 814 815util . sort_by_key ( fields , 'name' ) 816 817local fields_txt =render_fields_or_params ( fields , nil , classes , cfg ) 818if not fields_txt : match ( '^%s*$' ) then 819table . insert ( ret , '\n Fields: ~\n' ) 820table . insert ( ret , fields_txt ) 821end 822table . insert ( ret , '\n' ) 823 824return table . concat ( ret ) 825end 826 827--- @param classes table<string,nvim.luacats.parser.class> 828--- @param funs nvim.luacats.parser.fun[] 829--- @param cfg nvim.gen_vimdoc.Config 830local function render_classes ( classes , funs , cfg ) 831local ret ={} --- @type string[] 832-- Hide `.` members of returned class-modules from class Fields; 833-- they render as module functions. 834local hidden_fields ={} --- @type table<string,table<string,true>> 835for _ , fun in ipairs ( funs ) do 836if is_module_fun ( fun ) and fun . class then 837hidden_fields [ fun . class ] =hidden_fields [ fun . class ] or {} 838hidden_fields [ fun . class ][ fun . name ] =true 839end 840end 841 842for _ , class in vim . spairs ( classes ) do 843ret [ # ret + 1 ] =render_class ( class , classes , hidden_fields , cfg ) 844end 845 846return table . concat ( ret ) 847end 848 849--- @param fun nvim.luacats.parser.fun 850--- @param cfg nvim.gen_vimdoc.Config 851local function render_fun_header ( fun , cfg ) 852local ret ={} --- @type string[] 853 854local args ={} --- @type string[] 855for _ , p in ipairs ( fun . params or {} ) do 856if p . name ~= 'self' then 857args [ # args + 1 ] =fmt_field_name ( p . name ) 858end 859end 860 861local nm =fun . name 862if fun . classvar and not is_module_fun ( fun ) then 863nm =fmt ( '%s:%s' , fun . classvar , nm ) 864end 865if nm == 'vim.bo' then 866nm ='vim.bo[{bufnr}]' 867end 868if nm == 'vim.wo' then 869nm ='vim.wo[{winid}][{bufnr}]' 870end 871 872local proto =fun . table and nm or nm .. '(' .. table . concat ( args , ', ' ) .. ')' 873 874local tag ='*' .. cfg . fn_helptag_fmt ( fun ) .. '*' 875 876if # proto + # tag > TEXT_WIDTH - 8 then 877table . insert ( ret , fmt ( '%78s\n' , tag )) 878local name , pargs =proto : match ( '([^(]+%()(.*)' ) 879if name then 880table . insert ( ret , name ) 881table . insert ( ret , wrap ( pargs , 0 , # name , TEXT_WIDTH )) 882else 883table . insert ( ret , proto ) 884end 885else 886local pad =TEXT_WIDTH - # proto - # tag 887table . insert ( ret , proto .. string . rep ( ' ' , pad ) .. tag ) 888end 889 890return table . concat ( ret ) 891end 892 893--- @param returns nvim.luacats.parser.return[] 894--- @param generics? table<string,string> 895--- @param classes? table<string,nvim.luacats.parser.class> 896--- @return string? 897local function render_returns ( returns , generics , classes ) 898local ret ={} --- @type string[] 899 900if # returns == 1 and returns [ 1 ]. type == 'nil' then 901return 902end 903 904if # returns > 1 then 905table . insert ( ret , ' Return (multiple): ~\n' ) 906elseif # returns == 1 and next ( returns [ 1 ]) then 907table . insert ( ret , ' Return: ~\n' ) 908end 909 910for _ , p in ipairs ( returns ) do 911inline_type ( p , classes ) 912local rnm , ty , desc =p . name , p . type , p . desc 913 914local blk ={} --- @type string[] 915if ty then 916blk [ # blk + 1 ] =render_type ( ty , generics ) 917end 918blk [ # blk + 1 ] =rnm 919blk [ # blk + 1 ] =desc 920 921ret [ # ret + 1 ] =md_to_vimdoc ( table . concat ( blk , ' ' ), 8 , 8 , TEXT_WIDTH , true ) 922end 923 924return table . concat ( ret ) 925end 926 927--- @param fun nvim.luacats.parser.fun 928--- @param classes table<string,nvim.luacats.parser.class> 929--- @param cfg nvim.gen_vimdoc.Config 930local function render_fun ( fun , classes , cfg ) 931if fun . access or fun . deprecated or fun . nodoc then 932return 933end 934 935if cfg . fn_name_pat and not fun . name : match ( cfg . fn_name_pat ) then 936return 937end 938 939if not fun . name then 940error (( 'fun.name is nil, check fn_xform(). fun: %s' ): format ( vim . inspect ( fun ))) 941end 942 943if vim . startswith ( fun . name , '_' ) or fun . name : find ( '[:.]_' ) then 944return 945end 946 947local internal =vim . startswith ( fun . name , 'nvim__' ) 948local ret ={} --- @type string[] 949 950table . insert ( ret , render_fun_header ( fun , cfg )) 951table . insert ( ret , '\n' ) 952 953if internal or fun . since then 954local since =assert ( tonumber ( fun . since or ( internal and 0 )), 'invalid @since on ' .. fun . name ) 955local nvim_api =false and nvim_api_info () 956_ =nvim_api -- Disable prerelease "WARNING" doc, in preparation for for upcoming release. 957 958if 959internal or since == 0 --[[or (nvim_api.prerelease and since == nvim_api.level)]] 960then 961-- Experimental = (since==0 or current prerelease) 962local s ='WARNING: This feature is experimental/unstable.' 963table . insert ( ret , md_to_vimdoc ( s , INDENTATION , INDENTATION , TEXT_WIDTH )) 964table . insert ( ret , '\n' ) 965end 966if since > 0 then 967local v =assert ( util . version_level [ since ], 'invalid @since on ' .. fun . name ) 968fun . attrs =fun . attrs or {} 969table . insert ( fun . attrs , fmt ( 'Since: %s' , v )) 970end 971end 972 973if fun . desc then 974table . insert ( ret , md_to_vimdoc ( fun . desc , INDENTATION , INDENTATION , TEXT_WIDTH )) 975end 976 977if fun . notes then 978table . insert ( ret , '\n Note: ~\n' ) 979for _ , p in ipairs ( fun . notes ) do 980table . insert ( ret , ' • ' .. md_to_vimdoc ( p . desc , 0 , 8 , TEXT_WIDTH , true )) 981end 982end 983 984if fun . attrs then 985table . insert ( ret , '\n Attributes: ~\n' ) 986for _ , attr in ipairs ( fun . attrs ) do 987local attr_str =( { 988textlock ='not allowed when |textlock| is active' , 989fast ='|api-fast|' , 990remote_only ='|RPC| only' , 991lua_only ='Lua |vim.api| only' , 992} )[ attr ] or attr 993table . insert ( ret , fmt ( ' %s\n' , attr_str )) 994end 995end 996 997if fun . params and # fun . params > 0 then 998local param_txt =render_fields_or_params ( fun . params , fun . generics , classes , cfg ) 999if not param_txt : match ( '^%s*$' ) then 1000table . insert ( ret , '\n Parameters: ~\n' ) 1001ret [ # ret + 1 ] =param_txt 1002end 1003end 1004 1005if fun . overloads then 1006table . insert ( ret , '\n Overloads: ~\n' ) 1007for _ , p in ipairs ( fun . overloads ) do 1008table . insert ( ret , fmt ( ' • `%s`\n' , p )) 1009end 1010end 1011 1012if fun . returns then 1013local txt =render_returns ( fun . returns , fun . generics , classes ) 1014if txt and not txt : match ( '^%s*$' ) then 1015table . insert ( ret , '\n' ) 1016ret [ # ret + 1 ] =txt 1017end 1018end 1019 1020if fun . see then 1021table . insert ( ret , '\n See also: ~\n' ) 1022for _ , p in ipairs ( fun . see ) do 1023table . insert ( ret , ' • ' .. md_to_vimdoc ( p . desc , 0 , 8 , TEXT_WIDTH , true )) 1024end 1025end 1026 1027table . insert ( ret , '\n' ) 1028return table . concat ( ret ) 1029end 1030 1031--- @param briefs string[] 1032--- @param cfg nvim.gen_vimdoc.Config 1033local function render_briefs ( briefs , cfg ) 1034local ret ={} --- @type string[] 1035 1036for _ , b in ipairs ( briefs ) do 1037if cfg . brief_xform then 1038b =cfg . brief_xform ( b ) 1039end 1040ret [ # ret + 1 ] =b 1041end 1042 1043return ret 1044end 1045 1046--- @param funs nvim.luacats.parser.fun[] 1047--- @param classes table<string,nvim.luacats.parser.class> 1048--- @param cfg nvim.gen_vimdoc.Config 1049local function render_funs ( funs , classes , cfg ) 1050local ret ={} --- @type string[] 1051 1052for _ , f in ipairs ( funs ) do 1053if cfg . fn_xform then 1054cfg . fn_xform ( f ) 1055end 1056ret [ # ret + 1 ] =render_fun ( f , classes , cfg ) 1057end 1058 1059-- Sort via prototype. Experimental API functions ("nvim__") sort last. 1060table . sort ( ret , function ( a , b ) 1061local a1 =( '\n' .. a ): match ( '\n[a-zA-Z_][^\n]+\n' ) 1062local b1 =( '\n' .. b ): match ( '\n[a-zA-Z_][^\n]+\n' ) 1063 1064local a1__ =a1 : find ( '^%s*nvim__' ) and 1 or 0 1065local b1__ =b1 : find ( '^%s*nvim__' ) and 1 or 0 1066if a1__ ~= b1__ then 1067return a1__ < b1__ 1068end 1069 1070return a1 : lower () < b1 : lower () 1071end ) 1072 1073return table . concat ( ret ) 1074end 1075 1076--- @return string 1077local function get_script_path () 1078local str =debug . getinfo ( 2 , 'S' ). source : gsub ( '^@' , '' ) 1079return str : match ( '(.*[/\\])' ) or './' 1080end 1081 1082local script_path =get_script_path () 1083local base_dir =vim . fs . dirname ( vim . fs . dirname ( vim . fs . dirname ( script_path ))) 1084 1085local function delete_lines_below ( doc_file , tokenstr ) 1086local lines ={} --- @type string[] 1087local found =false 1088for line in io . lines ( doc_file ) do 1089if line : find ( vim . pesc ( tokenstr )) then 1090found =true 1091break 1092end 1093lines [ # lines + 1 ] =line 1094end 1095if not found then 1096error ( fmt ( 'not found: %s in %s' , tokenstr , doc_file )) 1097end 1098lines [ # lines ] =nil 1099local fp =assert ( io . open ( doc_file , 'w' )) 1100fp : write ( table . concat ( lines , '\n' )) 1101fp : write ( '\n' ) 1102fp : close () 1103end 1104 1105--- @param x string 1106local function mktitle ( x ) 1107if x == 'ui' then 1108return 'UI' 1109end 1110return x : sub ( 1 , 1 ): upper () .. x : sub ( 2 ) 1111end 1112 1113--- @class nvim.gen_vimdoc.Section 1114--- @field name string 1115--- @field title string 1116--- @field help_tag string 1117--- @field funs_txt string 1118--- @field classes_txt string 1119--- @field briefs string[] 1120 1121--- @param filename string 1122--- @param cfg nvim.gen_vimdoc.Config 1123--- @param briefs string[] 1124--- @param funs_txt string 1125--- @param classes_txt string 1126--- @return nvim.gen_vimdoc.Section? 1127local function make_section ( filename , cfg , briefs , funs_txt , classes_txt ) 1128-- filename: e.g., 'autocmd.c' 1129-- name: e.g. 'autocmd' 1130local name =filename : match ( '(.*)%.[a-z]+' ) 1131 1132-- Formatted (this is what's going to be written in the vimdoc) 1133-- e.g., "Autocmd Functions" 1134local sectname =cfg . section_name and cfg . section_name [ filename ] or mktitle ( name ) 1135 1136-- section tag: e.g., "*api-autocmd*" 1137local help_labels =cfg . helptag_fmt ( sectname ) 1138if type ( help_labels ) == 'table' then 1139help_labels =table . concat ( help_labels , '* *' ) 1140end 1141local help_tags ='*' .. help_labels .. '*' 1142 1143if funs_txt == '' and classes_txt == '' and # briefs == 0 then 1144return 1145end 1146 1147return { 1148name =sectname , 1149title =cfg . section_fmt ( sectname ), 1150help_tag =help_tags , 1151funs_txt =funs_txt , 1152classes_txt =classes_txt , 1153briefs =briefs , 1154} 1155end 1156 1157--- @param section nvim.gen_vimdoc.Section 1158local function render_section ( section ) 1159local doc ={} --- @type string[] 1160 1161if not section . title then 1162error (( 'section.title is nil, check section_fmt(). section: %s' ): format ( vim . inspect ( section ))) 1163end 1164 1165doc [ # doc + 1 ] =string . rep ( '=' , TEXT_WIDTH ) 1166.. '\n' 1167.. align_tags ( TEXT_WIDTH )( section . title .. ' ' .. section . help_tag ) 1168 1169if next ( section . briefs ) then 1170local briefs_txt ={} --- @type string[] 1171for _ , b in ipairs ( section . briefs ) do 1172briefs_txt [ # briefs_txt + 1 ] =md_to_vimdoc ( b , 0 , 0 , TEXT_WIDTH ) 1173end 1174 1175local sdoc =table . concat ( briefs_txt , '\n' ) 1176if sdoc : find ( '[^%s]' ) then 1177doc [ # doc + 1 ] =sdoc 1178end 1179end 1180 1181if section . classes_txt ~= '' then 1182table . insert ( doc , ( section . classes_txt : gsub ( '\n+$' , '\n' ))) 1183end 1184 1185if section . funs_txt ~= '' then 1186table . insert ( doc , section . funs_txt ) 1187end 1188 1189return table . concat ( doc , '\n\n' ) 1190end 1191 1192local parsers ={ 1193lua =luacats_parser . parse , 1194c =cdoc_parser . parse , 1195h =cdoc_parser . parse , 1196} 1197 1198--- @param files string[] 1199local function expand_files ( files ) 1200for k , f in pairs ( files ) do 1201if vim . fn . isdirectory ( f ) == 1 then 1202table . remove ( files , k ) 1203for path , ty in vim . fs . dir ( f ) do 1204if ty == 'file' then 1205table . insert ( files , vim . fs . joinpath ( f , path )) 1206end 1207end 1208end 1209end 1210end 1211 1212--- @param classes table<string,nvim.luacats.parser.class> 1213--- @return string? 1214local function find_module_class ( classes , modvar ) 1215for nm , cls in pairs ( classes ) do 1216local _ , field =next ( cls . fields or {} ) 1217if cls . desc and field and field . classvar == modvar then 1218return nm 1219end 1220end 1221end 1222 1223--- Add callable exports and inherited module functions under their public names. 1224--- E.g. `M.semaphore = require('vim.async._semaphore')` in vim.async exposes 1225--- that module's returned `new_semaphore` function as `vim.async.semaphore()`. 1226--- A callable table uses its module name: vim.inspect's `__call` => `vim.inspect()`. 1227--- @param modules table<string,nvim.luacats.parser.module> 1228local function expand_module_docs ( modules ) 1229local exports ={} --- @type table<nvim.luacats.parser.module,string[]> 1230for _ , module in pairs ( modules ) do 1231local documented ={} --- @type table<string,true> 1232for _ , fun in ipairs ( module . funs ) do 1233if fun . modvar == module . modvar and ( not fun . classvar or fun . classvar == module . modvar ) then 1234documented [ fun . name ] =true 1235end 1236end 1237for name , imported in pairs ( module . imports ) do 1238local public_name =module . name .. '.' .. name 1239if not documented [ name ] and not ( '.' .. public_name ): find ( '%._' ) then 1240exports [ imported ] =exports [ imported ] or {} 1241table . insert ( exports [ imported ], public_name ) 1242end 1243end 1244end 1245 1246for name , module in pairs ( modules ) do 1247-- E.g. new_semaphore becomes vim.async.semaphore through a require() binding. 1248-- Otherwise, use the public module path (vim.inspect for its __call). 1249if module . callable and ( exports [ module ] or not ( '.' .. name ): find ( '%._' )) then 1250for _ , public_name in ipairs ( exports [ module ] or { name } ) do 1251local fun =vim . deepcopy ( module . callable ) 1252fun . module , fun . name =public_name : match ( '^(.*)%.([^.]+)$' ) 1253fun . name =fun . name or public_name 1254module . funs [ # module . funs + 1 ] =fun 1255end 1256end 1257end 1258 1259-- `@class vim.async: vim.async._core` makes `vim.async._core.run()` => `vim.async.run()`. 1260-- Copy inherited functions under the public module name, keeping nearer declarations. 1261local hidden ={} --- @type table<nvim.luacats.parser.fun,true> 1262for _ , module in pairs ( modules ) do 1263if module . class and not ( '.' .. module . name ): find ( '%._' ) then 1264local names ={} --- @type table<string,true> 1265local seen ={} --- @type table<nvim.luacats.parser.module,true> 1266local source =module 1267while source and not seen [ source ] do 1268seen [ source ] =true 1269for _ , fun in ipairs ( source . funs ) do 1270if fun . class == source . class . name and fun . member_sep == '.' then 1271if 1272source ~= module 1273and not names [ fun . name ] 1274and not fun . nodoc 1275and not fun . access 1276and fun . name : sub ( 1 , 1 ) ~= '_' 1277then 1278local inherited =vim . deepcopy ( fun ) 1279inherited . module , inherited . class =module . name , module . class . name 1280inherited . modvar , inherited . classvar =module . modvar , module . modvar 1281module . funs [ # module . funs + 1 ] =inherited 1282end 1283if source ~= module and ( '.' .. source . name ): find ( '%._' ) then 1284hidden [ fun ] =true 1285end 1286-- Include functions already inherited by the source module. 1287names [ fun . name ] =true 1288end 1289end 1290-- A nearer declaration shadows ancestors, including non-function fields. 1291for _ , field in ipairs ( source . class . fields ) do 1292names [ field . name ] =true 1293end 1294for name in pairs ( source . assignments ) do 1295names [ name ] =true 1296end 1297source =source . parent 1298end 1299end 1300end 1301-- Delay hiding until all modules have inherited the original documentation. 1302for fun in pairs ( hidden ) do 1303fun . nodoc =true 1304end 1305end 1306 1307--- @param cfg nvim.gen_vimdoc.Config 1308local function gen_target ( cfg ) 1309cfg . fn_helptag_fmt =cfg . fn_helptag_fmt or fn_helptag_fmt_common 1310( 'Target:' , cfg . filename ) 1311local sections ={} --- @type nvim.gen_vimdoc.Section[] 1312 1313expand_files ( cfg . files ) 1314 1315--- @type table<string,[table<string,nvim.luacats.parser.class>, nvim.luacats.parser.fun[], string[]]> 1316local file_results ={} 1317 1318--- @type table<string,nvim.luacats.parser.class> 1319local all_classes ={} 1320 1321--- @type table<string,nvim.luacats.parser.module> 1322local modules ={} 1323 1324--- First pass so we can collect all classes and module exports. 1325for _ , f in vim . spairs ( cfg . files ) do 1326local ext =f : match ( '%.([^.]+)$' ) 1327local parser =parsers [ ext ] 1328if parser then 1329local classes , funs , briefs , _ , module =parser ( f ) 1330file_results [ f ] ={ classes , funs , briefs } 1331all_classes =vim . tbl_extend ( 'error' , all_classes , classes ) 1332if module then 1333modules [ module . name ] =module 1334end 1335end 1336end 1337 1338luacats_parser . resolve_modules ( modules ) 1339expand_module_docs ( modules ) 1340 1341--- @type table<string,[table<string,nvim.luacats.parser.class>, nvim.luacats.parser.fun[], string[]]> 1342local files_by_name ={} 1343 1344for f , r in vim . spairs ( file_results ) do 1345local classes , funs , briefs =r [ 1 ], r [ 2 ], r [ 3 ] 1346 1347if not f : find ( 'ui_events%.in%.h$' ) then -- TODO(justinmk): also lint UI events. 1348lint . lint_names ( f , funs , nil , classes ) 1349lint . lint_quasi_keysets ( f , funs ) 1350end 1351 1352local mod_cls_nm =find_module_class ( classes , 'M' ) 1353if mod_cls_nm then 1354local mod_cls =classes [ mod_cls_nm ] 1355classes [ mod_cls_nm ] =nil 1356-- If the module documentation is present, add it to the briefs 1357-- so it appears at the top of the section. 1358briefs [ # briefs + 1 ] =mod_cls . desc 1359end 1360 1361( ' Processing file:' , f ) 1362 1363-- FIXME: Using basenames will confuse `_meta/protocol.lua` with `protocol.lua` 1364files_by_name [ vim . fs . basename ( f )] =r 1365end 1366 1367for _ , entry in ipairs ( cfg . section_order ) do 1368local files =type ( entry ) == 'table' and entry or { entry } 1369local classes ={} --- @type table<string,nvim.luacats.parser.class> 1370local funs ={} --- @type nvim.luacats.parser.fun[] 1371local briefs ={} --- @type string[] 1372for _ , f in ipairs ( files ) do 1373local r =files_by_name [ f ] 1374if r then 1375classes =vim . tbl_extend ( 'error' , classes , r [ 1 ]) 1376vim . list_extend ( funs , r [ 2 ]) 1377vim . list_extend ( briefs , r [ 3 ]) 1378end 1379end 1380sections [ # sections + 1 ] =make_section ( 1381files [ 1 ], 1382cfg , 1383render_briefs ( briefs , cfg ), 1384render_funs ( funs , all_classes , cfg ), 1385render_classes ( classes , funs , cfg ) 1386) 1387end 1388 1389local first_section_tag =sections [ 1 ]. help_tag 1390local docs ={} --- @type string[] 1391for _ , section in ipairs ( sections ) do 1392( fmt ( " Rendering section: '%s'" , section . title )) 1393docs [ # docs + 1 ] =render_section ( section ) 1394end 1395 1396table . insert ( 1397docs , 1398fmt ( ' vim:tw=78:ts=8:sw=%d:sts=%d:et:ft=help:norl:\n' , INDENTATION , INDENTATION ) 1399) 1400 1401local doc_file =vim . fs . joinpath ( base_dir , 'runtime' , 'doc' , cfg . filename ) 1402 1403if vim . uv . fs_stat ( doc_file ) then 1404delete_lines_below ( doc_file , first_section_tag ) 1405end 1406 1407local fp =assert ( io . open ( doc_file , 'a' )) 1408fp : write ( table . concat ( docs , '\n' )) 1409fp : close () 1410end 1411 1412local function run () 1413for _ , cfg in vim . spairs ( config ) do 1414gen_target ( cfg ) 1415end 1416end 1417 1418run ()