neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

gen_vimvim.lua (4829B)


      1 local mpack = vim.mpack
      2 
      3 local syntax_file = arg[1]
      4 local funcs_file = arg[2]
      5 local options_file = arg[3]
      6 local auevents_file = arg[4]
      7 local ex_cmds_file = arg[5]
      8 local vvars_file = arg[6]
      9 
     10 local lld = {}
     11 local syn_fd = assert(io.open(syntax_file, 'w'))
     12 lld.line_length = 0
     13 local function w(s)
     14  syn_fd:write(s)
     15  if s:find('\n') then
     16    lld.line_length = #(s:gsub('.*\n', ''))
     17  else
     18    lld.line_length = lld.line_length + #s
     19  end
     20 end
     21 
     22 local options = loadfile(options_file)()
     23 local auevents = loadfile(auevents_file)()
     24 local ex_cmds = loadfile(ex_cmds_file)()
     25 local vvars = loadfile(vvars_file)()
     26 
     27 local function cmd_kw(prev_cmd, cmd)
     28  if not prev_cmd then
     29    return cmd:sub(1, 1) .. '[' .. cmd:sub(2) .. ']'
     30  else
     31    local shift = 1
     32    while cmd:sub(shift, shift) == prev_cmd:sub(shift, shift) do
     33      shift = shift + 1
     34    end
     35    if cmd:sub(1, shift) == 'def' then
     36      shift = shift + 1
     37    end
     38    if shift >= #cmd then
     39      return cmd
     40    else
     41      return cmd:sub(1, shift) .. '[' .. cmd:sub(shift + 1) .. ']'
     42    end
     43  end
     44 end
     45 
     46 -- Exclude these from the vimCommand keyword list, they are handled specially
     47 -- in syntax/vim.vim (vimAugroupKey, vimAutocmd, vimGlobal, vimSubst). #9327
     48 local function is_special_cased_cmd(cmd)
     49  return (
     50    cmd == 'augroup'
     51    or cmd == 'autocmd'
     52    or cmd == 'doautocmd'
     53    or cmd == 'doautoall'
     54    or cmd == 'global'
     55    or cmd == 'substitute'
     56  )
     57 end
     58 
     59 local vimcmd_start = 'syn keyword vimCommand contained '
     60 local vimcmd_end = ' nextgroup=vimBang'
     61 w(vimcmd_start)
     62 local prev_cmd = nil
     63 for _, cmd_desc in ipairs(ex_cmds.cmds) do
     64  if lld.line_length > 850 then
     65    w(vimcmd_end .. '\n' .. vimcmd_start)
     66  end
     67  local cmd = cmd_desc.command
     68  if cmd:match('%w') and cmd ~= 'z' and not is_special_cased_cmd(cmd) then
     69    w(' ' .. cmd_kw(prev_cmd, cmd))
     70  end
     71  if cmd == 'delete' then
     72    -- Add special abbreviations of :delete
     73    w(' ' .. cmd_kw('d', 'dl'))
     74    w(' ' .. cmd_kw('del', 'dell'))
     75    w(' ' .. cmd_kw('dele', 'delel'))
     76    w(' ' .. cmd_kw('delet', 'deletl'))
     77    w(' ' .. cmd_kw('delete', 'deletel'))
     78    w(' ' .. cmd_kw('d', 'dp'))
     79    w(' ' .. cmd_kw('de', 'dep'))
     80    w(' ' .. cmd_kw('del', 'delp'))
     81    w(' ' .. cmd_kw('dele', 'delep'))
     82    w(' ' .. cmd_kw('delet', 'deletp'))
     83    w(' ' .. cmd_kw('delete', 'deletep'))
     84  end
     85  prev_cmd = cmd
     86 end
     87 w(vimcmd_end .. '\n')
     88 
     89 local vimopt_start = 'syn keyword vimOption contained '
     90 local vimopt_end = ' skipwhite nextgroup=vimSetEqual,vimSetMod'
     91 w('\n' .. vimopt_start)
     92 for _, opt_desc in ipairs(options.options) do
     93  if not opt_desc.immutable then
     94    if lld.line_length > 850 then
     95      w(vimopt_end .. '\n' .. vimopt_start)
     96    end
     97    w(' ' .. opt_desc.full_name)
     98    if opt_desc.abbreviation then
     99      w(' ' .. opt_desc.abbreviation)
    100    end
    101    if opt_desc.type == 'boolean' then
    102      w(' inv' .. opt_desc.full_name)
    103      w(' no' .. opt_desc.full_name)
    104      if opt_desc.abbreviation then
    105        w(' inv' .. opt_desc.abbreviation)
    106        w(' no' .. opt_desc.abbreviation)
    107      end
    108    end
    109  end
    110 end
    111 w(vimopt_end .. '\n')
    112 
    113 local vimoptvar_start = 'syn keyword vimOptionVarName contained '
    114 w('\n' .. vimoptvar_start)
    115 for _, opt_desc in ipairs(options.options) do
    116  if not opt_desc.immutable then
    117    if lld.line_length > 850 then
    118      w('\n' .. vimoptvar_start)
    119    end
    120    w(' ' .. opt_desc.full_name)
    121    if opt_desc.abbreviation then
    122      w(' ' .. opt_desc.abbreviation)
    123    end
    124  end
    125 end
    126 
    127 w('\n\nsyn case ignore')
    128 local vimau_start = 'syn keyword vimAutoEvent contained '
    129 local vimau_end = ' skipwhite nextgroup=vimAutoEventSep,@vimAutocmdPattern'
    130 w('\n\n' .. vimau_start)
    131 for au, _ in vim.spairs(vim.tbl_extend('error', auevents.events, auevents.aliases)) do
    132  -- "User" requires a user defined argument event.
    133  -- (Separately specified in vim.vim).
    134  if au ~= 'User' and not auevents.nvim_specific[au] then
    135    if lld.line_length > 850 then
    136      w(vimau_end .. '\n' .. vimau_start)
    137    end
    138    w(' ' .. au)
    139  end
    140 end
    141 w(vimau_end .. '\n')
    142 
    143 local nvimau_start = 'syn keyword nvimAutoEvent contained '
    144 local nvimau_end = vimau_end
    145 w('\n' .. nvimau_start)
    146 for au, _ in vim.spairs(auevents.nvim_specific) do
    147  if lld.line_length > 850 then
    148    w(nvimau_end .. '\n' .. nvimau_start)
    149  end
    150  w(' ' .. au)
    151 end
    152 w(nvimau_end .. '\n')
    153 
    154 w('\nsyn case match')
    155 local vimfun_start = 'syn keyword vimFuncName contained '
    156 w('\n\n' .. vimfun_start)
    157 local funcs = mpack.decode(io.open(funcs_file, 'rb'):read('*all'))
    158 for _, name in ipairs(funcs) do
    159  if name then
    160    if lld.line_length > 850 then
    161      w('\n' .. vimfun_start)
    162    end
    163    w(' ' .. name)
    164  end
    165 end
    166 
    167 local vimvvar_start = 'syn keyword vimVimVarName contained '
    168 w('\n\n' .. vimvvar_start)
    169 for name, _ in vim.spairs(vvars.vars) do
    170  if lld.line_length > 850 then
    171    w('\n' .. vimvvar_start)
    172  end
    173  w(' ' .. name)
    174 end
    175 
    176 w('\n')
    177 syn_fd:close()