robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1---@diagnostic disable: no-unknown 2local options_input_file =arg [ 7 ] 3 4--- @module 'nvim.options' 5local options =loadfile ( options_input_file )() 6local options_meta =options . options 7local cstr =options . cstr 8local valid_scopes =options . valid_scopes 9local schema_values =options . schema_values 10-- Limit for a free-string (`str`) dict value; opt_strings_check() rejects anything longer. 11local keyset_str_max =256 12 13-- Object type of a `dict` key's value, for the generated KeySetLink table. String-ish kinds 14-- (enum/str) are stored inline as fixed `char[]` (keyset_charsize); opt_fill() copies to them. 15local keyset_ctype ={ 16num ='kObjectTypeInteger' , 17snum ='kObjectTypeInteger' , 18enum ='kObjectTypeString' , 19str ='kObjectTypeString' , 20} 21-- C field type for `dict` key kinds (except enum/str, which use a fixed-size `char[]`). 22local keyset_ftype ={ 23num ='Integer' , 24snum ='Integer' , 25} 26 27--- Fixed `char[]` size for a string-ish dict key, to avoid allocations. An `enum` fits its longest 28--- value, a `str` is limited to `keyset_str_max`. Returns nil for non-string kinds (num/snum/flag). 29--- @param item vim.option_schema.dictkey 30--- @return integer? 31local function keyset_charsize ( item ) 32local kind =type ( item ) == 'string' and 'flag' or item [ 2 ] 33if kind == 'enum' then 34local n =0 35for _ , val in ipairs ( item [ 3 ]. values ) do 36n =math . max ( n , # val ) 37end 38return n + 1 39elseif kind == 'str' then 40return keyset_str_max 41end 42return nil 43end 44 45--- @param o vim.option_meta 46--- @return string 47local function get_values_var ( o ) 48return ( 'opt_%s_values' ): format ( o . abbreviation or o . full_name ) 49end 50 51--- True if an option is a dict option: a `dict` schema (typed key:value map, e.g. 'diffopt'), as 52--- opposed to a flag/enum/char category. Dict options are stored as their (canonical) ":set" 53--- string; the `dict` schema generates the keyset machinery used to validate and reify it on demand. 54--- @param o vim.option_meta 55--- @return boolean 56local function is_dict_option ( o ) 57return o . schema ~= nil and o . schema . dict ~= nil 58end 59 60--- @param s string 61--- @return string 62local function lowercase_to_titlecase ( s ) 63return table . concat ( vim . tbl_map ( function ( word ) --- @param word string 64return word : sub ( 1 , 1 ): upper () .. word : sub ( 2 ) 65end , vim . split ( s , '[-_]' ))) 66end 67 68--- @param scope string 69--- @param option_name string 70--- @return string 71local function get_scope_option ( scope , option_name ) 72return ( 'k%sOpt%s' ): format ( lowercase_to_titlecase ( scope ), lowercase_to_titlecase ( option_name )) 73end 74 75local redraw_flags ={ 76ui_option ='kOptFlagUIOption' , 77tabline ='kOptFlagRedrTabl' , 78statuslines ='kOptFlagRedrStat' , 79current_window ='kOptFlagRedrWin' , 80current_buffer ='kOptFlagRedrBuf' , 81all_windows ='kOptFlagRedrAll' , 82curswant ='kOptFlagCurswant' , 83highlight_only ='kOptFlagHLOnly' , 84} 85 86local list_flags ={ 87comma ='kOptFlagComma' , 88onecomma ='kOptFlagOneComma' , 89commacolon ='kOptFlagComma|kOptFlagColon' , 90onecommacolon ='kOptFlagOneComma|kOptFlagColon' , 91flags ='kOptFlagFlagList' , 92flagscomma ='kOptFlagComma|kOptFlagFlagList' , 93} 94 95--- @param o vim.option_meta 96--- @return string 97local function get_flags ( o ) 98--- @type string[] 99local flags ={ '0' } 100 101--- @param f string 102local function add_flag ( f ) 103table . insert ( flags , f ) 104end 105 106if o . list then 107add_flag ( list_flags [ o . list ]) 108end 109 110for _ , r_flag in ipairs ( o . redraw or {} ) do 111add_flag ( redraw_flags [ r_flag ]) 112end 113 114if o . expand then 115add_flag ( 'kOptFlagExpand' ) 116if o . expand == 'nodefault' then 117add_flag ( 'kOptFlagNoDefExp' ) 118end 119end 120 121-- Callback option: `type='func'` (funcref/lambda/Lua fn) or `type='expr'` (Vimscript expr). 122if o . type == 'func' then 123add_flag ( 'kOptFlagFunc' ) 124elseif o . type == 'expr' then 125add_flag ( 'kOptFlagExpr' ) 126end 127 128for _ , flag_desc in ipairs ( { 129{ 'deny_duplicates' , 'NoDup' } , 130{ 'gettext' } , 131{ 'modelineexpr' , 'MLE' } , 132{ 'no_mkrc' , 'NoMkrc' } , 133{ 'nodefault' , 'NoDefault' } , 134{ 'noglob' , 'NoGlob' } , 135{ 'normal_dname_chars' , 'NDname' } , 136{ 'normal_fname_chars' , 'NFname' } , 137{ 'pri_mkrc' , 'PriMkrc' } , 138{ 'secure' } , 139} ) do 140local key_name , flag_suffix =flag_desc [ 1 ], flag_desc [ 2 ] 141if o [ key_name ] then 142local def_name ='kOptFlag' .. ( flag_suffix or lowercase_to_titlecase ( key_name )) 143add_flag ( def_name ) 144end 145end 146 147return table . concat ( flags , '|' ) 148end 149 150--- @param opt_type vim.option_type 151--- @return string 152local function opt_type_enum ( opt_type ) 153return ( 'kObjectType%s' ): format (( { 154boolean ='Boolean' , 155number ='Integer' , 156string ='String' , 157func ='String' , 158expr ='String' , 159} )[ opt_type ]) 160end 161 162--- @param scope vim.option_scope 163--- @return string 164local function opt_scope_enum ( scope ) 165return ( 'kOptScope%s' ): format ( lowercase_to_titlecase ( scope )) 166end 167 168--- @param o vim.option_meta 169--- @return string 170local function get_scope_flags ( o ) 171local scope_flags ='0' 172 173for _ , scope in ipairs ( o . scope ) do 174scope_flags =( '%s | (1 << %s)' ): format ( scope_flags , opt_scope_enum ( scope )) 175end 176 177return scope_flags 178end 179 180--- @param o vim.option_meta 181--- @return string 182local function get_scope_idx ( o ) 183--- @type string[] 184local strs ={} 185 186for _ , scope in pairs ( valid_scopes ) do 187local has_scope =vim . tbl_contains ( o . scope , scope ) 188strs [ # strs + 1 ] =( ' [%s] = %s' ): format ( 189opt_scope_enum ( scope ), 190get_scope_option ( scope , has_scope and o . full_name or 'Invalid' ) 191) 192end 193 194return ( '{\n%s\n }' ): format ( table . concat ( strs , ',\n' )) 195end 196 197--- @param s string 198--- @return string 199local function static_cstr_as_string ( s ) 200return ( '{ .data = %s, .size = sizeof(%s) - 1 }' ): format ( s , s ) 201end 202 203--- @param v vim.option_value|function 204--- @return string 205local function get_opt_val ( v ) 206--- @type vim.option_type 207local v_type 208 209if type ( v ) == 'function' then 210v , v_type =v () --[[ @as string, vim.option_type ]] 211 212if v_type == 'string' then 213v =static_cstr_as_string ( v ) 214end 215else 216v_type =type ( v ) --[[ @as vim.option_type ]] 217 218if v_type == 'boolean' then 219v =v and 'true' or 'false' 220elseif v_type == 'number' then 221v =( '%iL' ): format ( v ) 222elseif v_type == 'string' then 223--- @cast v string 224v =static_cstr_as_string ( cstr ( v )) 225end 226end 227 228-- def_val is stored as an API Object. 229local obj_type =( { boolean ='Boolean' , number ='Integer' , string ='String' } )[ v_type ] 230local obj_field =( { boolean ='boolean' , number ='integer' , string ='string' } )[ v_type ] 231return ( '{ .type = kObjectType%s, .data.%s = %s }' ): format ( obj_type , obj_field , v ) 232end 233 234--- @param d vim.option_value|function 235--- @param n string 236--- @return string 237local function get_defaults ( d , n ) 238if d == nil then 239error ( "option '" .. n .. "' should have a default value" ) 240end 241return get_opt_val ( d ) 242end 243 244--- @param i integer 245--- @param o vim.option_meta 246--- @param write fun(...: string) 247local function dump_option ( i , o , write ) 248write ( ' [' , ( '%u' ): format ( i - 1 ) .. ']={' ) 249write ( ' .fullname=' , cstr ( o . full_name )) 250if o . abbreviation then 251write ( ' .shortname=' , cstr ( o . abbreviation )) 252end 253write ( ' .type=' , opt_type_enum ( o . type )) 254write ( ' .flags=' , get_flags ( o )) 255write ( ' .scope_flags=' , get_scope_flags ( o )) 256write ( ' .scope_idx=' , get_scope_idx ( o )) 257-- `chars` (dispatch tables) and `flagchars` don't generate completion values (they self-expand). 258local values =o . schema and schema_values ( o . schema ) or {} 259write ( ' .values=' , ( # values > 0 and get_values_var ( o ) or 'NULL' )) 260write ( ' .values_len=' , ( # values > 0 and # values or '0' )) 261-- A `dict` schema also emits an OptSchemaItem[]; expose it so the structured value conversion can 262-- read typed sub-values. 263write ( 264' .schema=' , 265( is_dict_option ( o ) and ( 'opt_%s_schema' ): format ( o . abbreviation or o . full_name ) or 'NULL' ) 266) 267write ( ' .flags_var=' , ( o . flags_varname and ( '&%s' ): format ( o . flags_varname ) or 'NULL' )) 268if o . enable_if then 269write (( '#if defined(%s)' ): format ( o . enable_if )) 270end 271 272local is_window_local =# o . scope == 1 and o . scope [ 1 ] == 'win' 273 274if is_window_local then 275write ( ' .var=NULL' ) 276elseif o . varname then 277write ( ' .var=&' , o . varname ) 278elseif o . immutable then 279-- Immutable options can directly point to the default value. 280write (( ' .var=&options[%u].def_val.data' ): format ( i - 1 )) 281else 282error ( 'Option must be immutable or have a variable.' ) 283end 284 285write ( ' .immutable=' , ( o . immutable and 'true' or 'false' )) 286write ( ' .opt_validate_cb=' , o . validation_cb or 'NULL' ) 287write ( ' .opt_did_set_cb=' , o . cb or 'NULL' ) 288write ( ' .opt_expand_cb=' , o . expand_cb or 'NULL' ) 289 290if o . enable_if then 291write ( '#else' ) 292-- Hidden option directly points to default value. 293write (( ' .var=&options[%u].def_val.data' ): format ( i - 1 )) 294-- Option is always immutable on the false branch of `enable_if`. 295write ( ' .immutable=true' ) 296write ( '#endif' ) 297end 298 299if not o . defaults then 300write ( ' .def_val={ .type = kObjectTypeNil }' ) 301elseif o . defaults . condition then 302write (( '#if defined(%s)' ): format ( o . defaults . condition )) 303write ( ' .def_val=' , get_defaults ( o . defaults . if_true , o . full_name )) 304-- Check against nil: `if_false=false` is a valid default and must still emit the `#else`. 305if o . defaults . if_false ~= nil then 306write ( '#else' ) 307write ( ' .def_val=' , get_defaults ( o . defaults . if_false , o . full_name )) 308end 309write ( '#endif' ) 310else 311write ( ' .def_val=' , get_defaults ( o . defaults . if_true , o . full_name )) 312end 313 314write ( ' },' ) 315end 316 317--- Emit an option's flag-set enum from its `flagchars` or `flags` schema: 318--- * `flagchars` (e.g. 'formatoptions'): a name->char map -> `enum { kFoWrap = 't', … }` 319--- * `flags` (e.g. 'foldopen', 'virtualedit'): an ordered list of names (bit = 2^i) or of 320--- {name, bit} pairs -> `typedef enum { kOptFdoFlagHor = 0x…, … } OptFdoFlags;`. A pair's 321--- optional 3rd element overrides the C constant's token (e.g. 'NONE' -> `NoneU`). 322--- @param o vim.option_meta 323--- @return string 324local function gen_token_enum ( o ) 325local out ={} --- @type string[] 326local function add ( s ) 327table . insert ( out , s ) 328end 329local opt_name =lowercase_to_titlecase ( o . abbreviation or o . full_name ) 330 331if o . schema . flagchars then 332-- Char flags complete as raw chars, so order is irrelevant; sort by name for a stable enum. 333local names =vim . tbl_keys ( o . schema . flagchars ) --[[ @as string[] ]] 334table . sort ( names ) 335add ( '' ) 336add ( 'enum {' ) 337for _ , name in ipairs ( names ) do 338add ( 339( " k%s%s = '%s'," ): format ( 340opt_name , 341lowercase_to_titlecase ( name ), 342( o . schema . flagchars [ name ]: gsub ( "[\\']" , '\\%0' )) 343) 344) 345end 346add ( '};' ) 347return table . concat ( out , '\n' ) 348end 349 350-- Bitmask flags: collect (C-token, bit) and emit in bit order. 351local flags ={} --- @type {name:string,bit:integer}[] 352for i , entry in ipairs ( o . schema . flags ) do 353if type ( entry ) == 'string' then 354flags [ # flags + 1 ] ={ name =entry , bit =math . pow ( 2 , i - 1 ) } 355else 356local e =entry --[[ @as {[1]:string, [2]:integer, [3]:string?} ]] 357flags [ # flags + 1 ] ={ name =e [ 3 ] or e [ 1 ], bit =e [ 2 ] } 358end 359end 360table . sort ( flags , function ( a , b ) 361return a . bit < b . bit 362end ) 363 364add ( '' ) 365add ( 'typedef enum {' ) 366for _ , f in ipairs ( flags ) do 367-- A "key:" token (e.g. 'messagesopt' "wait:") names its flag without the trailing colon. 368add ( 369( ' kOpt%sFlag%s = 0x%02x,' ): format ( 370opt_name , 371lowercase_to_titlecase (( f . name : gsub ( ':$' , '' ))), 372f . bit 373) 374) 375end 376add (( '} Opt%sFlags;' ): format ( opt_name )) 377return table . concat ( out , '\n' ) 378end 379 380--- @param output_file string 381--- @return table<string,string> options_index Map of option name to option index 382local function gen_enums ( output_file ) 383--- Options for each scope. 384--- @type table<string, vim.option_meta[]> 385local scope_options ={} 386for _ , scope in ipairs ( valid_scopes ) do 387scope_options [ scope ] ={} 388end 389 390local fd =assert ( io . open ( output_file , 'w' )) 391 392--- @param s string 393local function write ( s ) 394fd : write ( s ) 395fd : write ( '\n' ) 396end 397 398-- Generate options enum file 399write ( '// IWYU pragma: private, include "nvim/option_defs.h"' ) 400write ( '' ) 401 402--- Map of option name to option index 403--- @type table<string, string> 404local option_index ={} 405 406-- Generate option index enum and populate the `option_index` and `scope_option` dicts. 407write ( 'typedef enum {' ) 408write ( ' kOptInvalid = -1,' ) 409 410for i , o in ipairs ( options_meta ) do 411local enum_val_name ='kOpt' .. lowercase_to_titlecase ( o . full_name ) 412write (( ' %s = %u,' ): format ( enum_val_name , i - 1 )) 413 414option_index [ o . full_name ] =enum_val_name 415 416if o . abbreviation then 417option_index [ o . abbreviation ] =enum_val_name 418end 419 420local alias =o . alias or {} --[[@as string[] ]] 421for _ , v in ipairs ( alias ) do 422option_index [ v ] =enum_val_name 423end 424 425for _ , scope in ipairs ( o . scope ) do 426table . insert ( scope_options [ scope ], o ) 427end 428end 429 430write ( ' // Option count' ) 431write ( '#define kOptCount ' .. tostring ( # options_meta )) 432write ( '} OptIndex;' ) 433 434-- Generate option index enum for each scope 435for _ , scope in ipairs ( valid_scopes ) do 436write ( '' ) 437 438local scope_name =lowercase_to_titlecase ( scope ) 439write ( 'typedef enum {' ) 440write (( ' %s = -1,' ): format ( get_scope_option ( scope , 'Invalid' ))) 441 442for idx , option in ipairs ( scope_options [ scope ]) do 443write (( ' %s = %u,' ): format ( get_scope_option ( scope , option . full_name ), idx - 1 )) 444end 445 446write (( ' // %s option count' ): format ( scope_name )) 447write (( '#define %s %d' ): format ( get_scope_option ( scope , 'Count' ), # scope_options [ scope ])) 448write (( '} %sOptIndex;' ): format ( scope_name )) 449end 450 451-- Generate reverse lookup from option scope index to option index for each scope. 452for _ , scope in ipairs ( valid_scopes ) do 453write ( '' ) 454write (( 'EXTERN const OptIndex %s_opt_idx[] INIT( = {' ): format ( scope )) 455for _ , option in ipairs ( scope_options [ scope ]) do 456local idx =option_index [ option . full_name ] 457write (( ' [%s] = %s,' ): format ( get_scope_option ( scope , option . full_name ), idx )) 458end 459write ( '});' ) 460end 461 462fd : close () 463 464return option_index 465end 466 467--- @param output_file string 468--- @param option_index table<string,string> 469local function gen_map ( output_file , option_index ) 470-- Generate option index map. 471local hashy =require ( 'gen.hashy' ) 472 473local neworder , hashfun =hashy . hashy_hash ( 474'find_option' , 475vim . tbl_keys ( option_index ), 476function ( idx ) 477return ( 'option_hash_elems[%s].name' ): format ( idx ) 478end 479) 480 481local fd =assert ( io . open ( output_file , 'w' )) 482 483--- @param s string 484local function write ( s ) 485fd : write ( s ) 486fd : write ( '\n' ) 487end 488 489write ( 'static const struct { const char *name; OptIndex opt_idx; } option_hash_elems[] = {' ) 490 491for _ , name in ipairs ( neworder ) do 492assert ( option_index [ name ] ~= nil ) 493write (( ' { .name = "%s", .opt_idx = %s },' ): format ( name , option_index [ name ])) 494end 495 496write ( '};' ) 497write ( '' ) 498write ( 'static ' .. hashfun ) 499 500fd : close () 501end 502 503-- Maps a `dict` key's value-kind to its OptSchemaKind enum. Enum keys carry a values array instead. 504local schema_kinds ={ 505num ='kOptSchemaNum' , 506snum ='kOptSchemaSNum' , 507str ='kOptSchemaStr' , 508} 509 510--- Emit the OptSchemaItem[] that opt_strings_check() validates against, from a `dict` schema. 511--- @param prefix string e.g. "opt_dip" 512--- @param schema vim.option_schema 513--- @return string 514local function gen_opt_schema ( prefix , schema ) 515local out ={} --- @type string[] 516local function add ( s ) 517table . insert ( out , s ) 518end 519 520add ( '' ) 521add (( 'EXTERN const OptSchemaItem %s_schema[] INIT( = {' ): format ( prefix )) 522for _ , item in ipairs ( schema . dict ) do 523if type ( item ) == 'string' then 524add (( ' { "%s", kOptSchemaFlag, NULL },' ): format ( item )) 525elseif item [ 2 ] == 'enum' then 526add (( ' { "%s", kOptSchemaEnum, %s_%s_values },' ): format ( item [ 1 ], prefix , item [ 1 ])) 527else 528local kind =schema_kinds [ item [ 2 ]] or error (( '%s: bad kind %q' ): format ( prefix , item [ 2 ])) 529add (( ' { "%s", %s, NULL },' ): format ( item [ 1 ], kind )) 530end 531end 532add ( ' { NULL, kOptSchemaFlag, NULL },' ) 533add ( '});' ) 534 535return table . concat ( out , '\n' ) 536end 537 538--- Emit the completion values arrays (`opt_<name>_values`, plus `opt_<name>_<key>_values` for each 539--- `dict` enum key). Enum/set/flag tokens complete as-is; `dict` keys complete as "key:". 540--- @param prefix string e.g. "opt_dip" 541--- @param schema vim.option_schema 542--- @return string 543local function gen_schema_values ( prefix , schema ) 544local out ={} --- @type string[] 545local function add ( s ) 546table . insert ( out , s ) 547end 548 549local values =schema_values ( schema ) 550add ( '' ) 551add (( 'EXTERN const char *(%s_values[%s]) INIT( = {' ): format ( prefix , # values + 1 )) 552for _ , v in ipairs ( values ) do 553add (( ' "%s",' ): format ( v )) 554end 555add ( ' NULL' ) 556add ( '});' ) 557 558for _ , item in ipairs ( schema . dict or {} ) do 559if type ( item ) == 'table' and item [ 2 ] == 'enum' then 560local vals =item [ 3 ]. values --[[ @as string[] ]] 561add ( '' ) 562add (( 'EXTERN const char *(%s_%s_values[%s]) INIT( = {' ): format ( prefix , item [ 1 ], # vals + 1 )) 563for _ , v in ipairs ( vals ) do 564add (( ' "%s",' ): format ( v )) 565end 566add ( ' NULL' ) 567add ( '});' ) 568end 569end 570 571return table . concat ( out , '\n' ) 572end 573 574local c_keywords ={ inline =true , default =true , auto =true , register =true } 575 576--- Sanitize a schema key into a C struct field name. C keywords get a trailing '_'. 577--- @param name string 578--- @return string 579local function c_field_name ( name ) 580local f =( name : gsub ( '%-' , '_' )) 581return c_keywords [ f ] and ( f .. '_' ) or f 582end 583 584--- @param output_file string 585local function gen_vars ( output_file ) 586local fd =assert ( io . open ( output_file , 'w' )) 587 588--- @param s string 589local function write ( s ) 590fd : write ( s ) 591fd : write ( '\n' ) 592end 593 594write ( '// IWYU pragma: private, include "nvim/option_vars.h"' ) 595 596-- Generate enums for option flags. 597for _ , o in ipairs ( options_meta ) do 598if o . schema and ( o . schema . flags or o . schema . flagchars ) then 599write ( gen_token_enum ( o )) 600end 601end 602 603-- Generate valid values (for completion) and, for `dict` schemas, the validation schema. 604for _ , option in ipairs ( options_meta ) do 605local values_var =( 'opt_%s' ): format ( option . abbreviation or option . full_name ) 606-- `chars` dispatch tables (gen_chartab) and `flagchars` (gen_token_enum) have no completion 607-- values; every other schema does. 608if option . schema and # schema_values ( option . schema ) > 0 then 609write ( gen_schema_values ( values_var , option . schema )) 610if is_dict_option ( option ) then 611write ( gen_opt_schema ( values_var , option . schema )) 612end 613end 614end 615 616fd : close () 617end 618 619--- Emit the chars_tab[] dispatch tables (fcs_tab/lcs_tab) from options with a `char`/`chars` 620--- schema. 621--- 622--- #include "options_chartab.generated.h" 623--- 624--- @param output_file string 625local function gen_chartab ( output_file ) 626local fd =assert ( io . open ( output_file , 'w' )) 627--- @param s string 628local function write ( s ) 629fd : write ( s ) 630fd : write ( '\n' ) 631end 632 633write ( '// IWYU pragma: private, include "nvim/optionstr.c"' ) 634for _ , o in ipairs ( options_meta ) do 635if o . schema and o . schema . chars then 636local abbr =o . abbreviation or o . full_name 637write ( '' ) 638write (( 'static const struct chars_tab %s_tab[] = {' ): format ( abbr )) 639for _ , item in ipairs ( o . schema . chars ) do 640--- @cast item vim.option_schema.char 641local name =item [ 1 ] 642local opts =item [ 3 ] or {} 643-- opts.field == false means no storage (NULL cp), e.g. 'multispace'; nil defaults to name. 644local cp ='NULL' 645if opts . field ~= false then 646cp =( '&%s_chars.%s' ): format ( abbr , opts . field or name ) 647end 648local def =opts . def and ( '"%s"' ): format ( opts . def ) or 'NULL' 649local fallback =opts . fallback and ( '"%s"' ): format ( opts . fallback ) or 'NULL' 650write (( ' CHARSTAB_ENTRY(%s, "%s", %s, %s),' ): format ( cp , name , def , fallback )) 651end 652write ( '};' ) 653end 654end 655 656fd : close () 657end 658 659--- Emit an option keyset: a `KeyDict_…` struct + `KeySetLink` + perfect-hash get_field, generated 660--- from a `dict` schema. Consumed by `api_dict_to_keydict()`. 661--- @param output_file string 662local function gen_keysets ( output_file ) 663local keyset =require ( 'gen.keyset' ) 664local fd =assert ( io . open ( output_file , 'w' )) 665local function write ( s ) 666fd : write ( s ) 667fd : write ( '\n' ) 668end 669 670write ( '#pragma once' ) 671write ( '' ) 672write ( '#include <stddef.h>' ) 673write ( '' ) 674write ( '#include "nvim/api/private/defs.h"' ) 675write ( '#include "nvim/option_defs.h"' ) 676 677local struct_opts ={} --- @type {enum:string,abbr:string,kd:string}[] 678 679for _ , o in ipairs ( options_meta ) do 680-- A `dict` schema reifies to a keyset; generate it here. 681if is_dict_option ( o ) then 682-- Mirrors the API's KeyDict codegen with an "Opt" prefix: struct OptKeyDict_<abbr>, and 683-- <abbr> as the keyset name for the table/hash/HAS_KEY (e.g. HAS_KEY(v, dip, filler)). 684local abbr =o . abbreviation or o . full_name 685local kd ='OptKeyDict_' .. abbr 686struct_opts [ # struct_opts + 1 ] = 687{ enum ='kOpt' .. lowercase_to_titlecase ( o . full_name ), abbr =abbr , kd =kd } 688local keys ={} --- @type string[] 689local info ={} --- @type table<string,{field:string,ctype:string}> 690for _ , item in ipairs ( o . schema . dict ) do 691local key =type ( item ) == 'string' and item or item [ 1 ] 692local kind =type ( item ) == 'string' and 'flag' or item [ 2 ] 693keys [ # keys + 1 ] =key 694info [ key ] ={ 695field =c_field_name ( key ), 696ctype =keyset_ctype [ kind ] or 'kObjectTypeBoolean' , 697} 698end 699 700-- The schema array (validation grammar) lives in option_vars.generated.h; forward-declare it 701-- so this header stays self-contained. 702write ( '' ) 703write (( 'extern const OptSchemaItem opt_%s_schema[];' ): format ( abbr )) 704write ( '' ) 705write (( "/// '%s'" ): format ( o . full_name )) 706write (( 'typedef struct %s {' ): format ( kd )) 707write (( ' OptionalKeys is_set__%s_;' ): format ( abbr )) 708for _ , item in ipairs ( o . schema . dict ) do 709local key =type ( item ) == 'string' and item or item [ 1 ] 710local kind =type ( item ) == 'string' and 'flag' or item [ 2 ] 711local charsize =keyset_charsize ( item ) 712if charsize then 713write (( ' char %s[%d];' ): format ( c_field_name ( key ), charsize )) 714else 715write (( ' %s %s;' ): format ( keyset_ftype [ kind ] or 'Boolean' , c_field_name ( key ))) 716end 717end 718write (( '} %s;' ): format ( kd )) 719write ( '' ) 720 721local order , hashfun =keyset . hash ( abbr , keys ) 722local entry ={} --- @type table<string, gen.keyset.entry> 723for i , key in ipairs ( order ) do 724write (( '#define KEYSET_OPTIDX_%s__%s %d' ): format ( abbr , c_field_name ( key ), i )) 725entry [ key ] = 726{ field =info [ key ]. field , type =info [ key ]. ctype , opt_index =i , is_hlgroup =false } 727end 728-- `static` (inline): diff.c includes this header for the struct type alone, so a plain `static` 729-- table/hash/get_field would warn as unused. 730keyset . emit ( write , { 731name =abbr , 732get_field =kd .. '_get_field' , 733struct =kd , 734order =order , 735hashfun =hashfun , 736entry =entry , 737static =true , 738} ) 739end 740end 741 742-- Union of all keyset types, for opt_keyset()'s shared storage. 743write ( '' ) 744write ( 'typedef union {' ) 745for _ , s in ipairs ( struct_opts ) do 746write (( ' %s %s;' ): format ( s . kd , s . abbr )) 747end 748write ( '} OptKeyDict;' ) 749 750-- Dispatch from option index to its dict schema (opt_dict_schema); NULL for non-dict options. 751write ( '' ) 752write ( 'static inline const OptDictSchema *opt_dict_schema(OptIndex opt_idx)' ) 753write ( '{' ) 754write ( ' switch (opt_idx) {' ) 755for _ , s in ipairs ( struct_opts ) do 756write (( ' case %s: {' ): format ( s . enum )) 757write ( 758( ' static const OptDictSchema info = { %s_get_field, opt_%s_schema,' ): format ( s . kd , s . abbr ) 759) 760write (( ' sizeof(%s) };' ): format ( s . kd )) 761write ( ' return &info;' ) 762write ( ' }' ) 763end 764write ( ' default:' ) 765write ( ' return NULL;' ) 766write ( ' }' ) 767write ( '}' ) 768 769fd : close () 770end 771 772--- @param output_file string 773local function gen_options ( output_file ) 774local fd =assert ( io . open ( output_file , 'w' )) 775 776--- @param ... string 777local function write ( ... ) 778local s =table . concat ( { ... } , '' ) 779fd : write ( s ) 780if s : match ( '^ %.' ) then 781fd : write ( ',' ) 782end 783fd : write ( '\n' ) 784end 785 786-- Generate options[] array. 787write ( [[ 788#include "nvim/ex_docmd.h" 789#include "nvim/ex_getln.h" 790#include "nvim/insexpand.h" 791#include "nvim/mapping.h" 792#include "nvim/ops.h" 793#include "nvim/option.h" 794#include "nvim/optionstr.h" 795#include "nvim/quickfix.h" 796#include "nvim/runtime.h" 797#include "nvim/tag.h" 798#include "nvim/window.h" 799 800static vimoption_T options[] = {]] ) 801 802for i , o in ipairs ( options_meta ) do 803dump_option ( i , o , write ) 804end 805 806write ( '};' ) 807 808fd : close () 809end 810 811local function main () 812local options_file =arg [ 1 ] 813local options_enum_file =arg [ 2 ] 814local options_map_file =arg [ 3 ] 815local option_vars_file =arg [ 4 ] 816local options_chartab_file =arg [ 5 ] 817local options_keysets_file =arg [ 6 ] 818 819local option_index =gen_enums ( options_enum_file ) 820gen_map ( options_map_file , option_index ) 821gen_vars ( option_vars_file ) 822gen_chartab ( options_chartab_file ) 823gen_keysets ( options_keysets_file ) 824gen_options ( options_file ) 825end 826 827main ()