neovim

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

vimpatch.lua (4319B)


      1 --  Updates version.c list of applied Vim patches.
      2 --
      3 --  Usage:
      4 --    VIM_SOURCE_DIR=~/neovim/.vim-src/ nvim -l ./scripts/vimpatch.lua
      5 
      6 local nvim = vim.api
      7 
      8 local function systemlist(...)
      9  local rv = nvim.nvim_call_function('systemlist', ...)
     10  local err = nvim.nvim_get_vvar('shell_error')
     11  local args_str = nvim.nvim_call_function('string', ...)
     12  if 0 ~= err then
     13    error('command failed: ' .. args_str)
     14  end
     15  return rv
     16 end
     17 
     18 local function vimpatch_sh_list_tokens()
     19  return systemlist({ { 'bash', '-c', 'scripts/vim-patch.sh -M' } })
     20 end
     21 
     22 -- Generate the data,lines to update src/nvim/version.c.
     23 -- - `vim_versions[]`
     24 -- - `Versions[]`
     25 -- - `num_patches[]`
     26 -- - `included_patchsets[]`
     27 local function gen_version_c_lines()
     28  -- List of version sets where each set contains:
     29  -- 1. major_minor_version (int)
     30  -- 2. major_minor_version (string)
     31  -- 3. set of merged patch numbers
     32  local merged_version_list = {}
     33  for _, token in ipairs(vimpatch_sh_list_tokens()) do
     34    local major_version, minor_version, patch_num = string.match(token, '^(%d+).(%d+).(%d+)$')
     35    local n = tonumber(patch_num)
     36    if n then
     37      local major_minor_version = major_version * 100 + minor_version
     38      local len = #merged_version_list
     39      if len == 0 or merged_version_list[len][1] ~= major_minor_version then
     40        local vstr = '"' .. major_version .. '.' .. minor_version .. '"'
     41        table.insert(merged_version_list, { major_minor_version, vstr, { n } })
     42      else
     43        table.insert(merged_version_list[len][3], n)
     44      end
     45    end
     46  end
     47 
     48  local major_vim_versions = {}
     49  local major_vim_versions_str = {}
     50  local num_patches = {}
     51  local patch_lines = {}
     52  for _, version_set in ipairs(merged_version_list) do
     53    local major_minor_version, major_minor_version_str, patch_set = unpack(version_set)
     54    table.insert(major_vim_versions, major_minor_version)
     55    table.insert(major_vim_versions_str, major_minor_version_str)
     56    table.insert(num_patches, #patch_set)
     57    table.insert(patch_lines, '  (const int[]) {  // ' .. major_minor_version)
     58 
     59    local patchset_set = {}
     60    for i = #patch_set, 1, -1 do
     61      local patch = patch_set[i]
     62      local next_patch = patch_set[i - 1]
     63      local patch_diff = patch - (next_patch or 0)
     64      table.insert(patchset_set, patch)
     65 
     66      -- guard against last patch or `make formatc`
     67      if #patchset_set > 15 or i == 1 or patch_diff > 1 then
     68        table.insert(patch_lines, '    ' .. table.concat(patchset_set, ', ') .. ',')
     69        patchset_set = {}
     70      end
     71      if i == 1 and patch > 0 then
     72        local line = '    // 0'
     73        if patch > 1 then
     74          line = line .. '-' .. (patch - 1)
     75        end
     76        table.insert(patch_lines, line)
     77      elseif patch_diff > 1 then
     78        local line = '    // ' .. (next_patch + 1)
     79        if patch_diff > 2 then
     80          line = line .. '-' .. (patch - 1)
     81        end
     82        table.insert(patch_lines, line)
     83      end
     84    end
     85 
     86    table.insert(patch_lines, '  },')
     87  end
     88 
     89  return major_vim_versions, major_vim_versions_str, num_patches, patch_lines
     90 end
     91 
     92 local function patch_version_c()
     93  local major_vim_versions, major_vim_versions_str, num_patches, patch_lines = gen_version_c_lines()
     94 
     95  nvim.nvim_command('silent noswapfile noautocmd edit src/nvim/version.c')
     96  nvim.nvim_command([[/^char \*Versions]])
     97  -- Replace the line.
     98  nvim.nvim_call_function('setline', {
     99    nvim.nvim_eval('line(".")'),
    100    'char *Versions[] = { ' .. table.concat(major_vim_versions_str, ', ') .. ' };',
    101  })
    102  nvim.nvim_command([[/^static const int vim_versions]])
    103  -- Replace the line.
    104  nvim.nvim_call_function('setline', {
    105    nvim.nvim_eval('line(".")'),
    106    'static const int vim_versions[] = { ' .. table.concat(major_vim_versions, ', ') .. ' };',
    107  })
    108  nvim.nvim_command([[/^static const int num_patches]])
    109  -- Replace the line.
    110  nvim.nvim_call_function('setline', {
    111    nvim.nvim_eval('line(".")'),
    112    'static const int num_patches[] = { ' .. table.concat(num_patches, ', ') .. ' };',
    113  })
    114  nvim.nvim_command([[/^static const int \*included_patchsets]])
    115  -- Delete the existing lines.
    116  nvim.nvim_command('silent normal! j0d/};\rk')
    117  -- Insert the lines.
    118  nvim.nvim_call_function('append', {
    119    nvim.nvim_eval('line(".")'),
    120    patch_lines,
    121  })
    122  nvim.nvim_command('silent write')
    123 end
    124 
    125 patch_version_c()