genvimvim.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. local mpack = require('mpack')
  2. if arg[1] == '--help' then
  3. print('Usage: lua genvimvim.lua src/nvim runtime/syntax/vim/generated.vim')
  4. os.exit(0)
  5. end
  6. local nvimsrcdir = arg[1]
  7. local syntax_file = arg[2]
  8. local funcs_file = arg[3]
  9. package.path = nvimsrcdir .. '/?.lua;' .. package.path
  10. local lld = {}
  11. local syn_fd = 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. local options = require('options')
  22. local auevents = require('auevents')
  23. local ex_cmds = require('ex_cmds')
  24. local function cmd_kw(prev_cmd, cmd)
  25. if not prev_cmd then
  26. return cmd:sub(1, 1) .. '[' .. cmd:sub(2) .. ']'
  27. else
  28. local shift = 1
  29. while cmd:sub(shift, shift) == prev_cmd:sub(shift, shift) do
  30. shift = shift + 1
  31. end
  32. if shift >= #cmd then
  33. return cmd
  34. else
  35. return cmd:sub(1, shift) .. '[' .. cmd:sub(shift + 1) .. ']'
  36. end
  37. end
  38. end
  39. -- Exclude these from the vimCommand keyword list, they are handled specially
  40. -- in syntax/vim.vim (vimAugroupKey, vimAutoCmd). #9327
  41. local function is_autocmd_cmd(cmd)
  42. return (cmd == 'augroup'
  43. or cmd == 'autocmd'
  44. or cmd == 'doautocmd'
  45. or cmd == 'doautoall')
  46. end
  47. local vimcmd_start = 'syn keyword vimCommand contained '
  48. w(vimcmd_start)
  49. local prev_cmd = nil
  50. for _, cmd_desc in ipairs(ex_cmds.cmds) do
  51. if lld.line_length > 850 then
  52. w('\n' .. vimcmd_start)
  53. end
  54. local cmd = cmd_desc.command
  55. if cmd:match('%w') and cmd ~= 'z' and not is_autocmd_cmd(cmd) then
  56. w(' ' .. cmd_kw(prev_cmd, cmd))
  57. end
  58. prev_cmd = cmd
  59. end
  60. local vimopt_start = 'syn keyword vimOption contained '
  61. w('\n\n' .. vimopt_start)
  62. for _, opt_desc in ipairs(options.options) do
  63. if not opt_desc.varname or opt_desc.varname:sub(1, 7) ~= 'p_force' then
  64. if lld.line_length > 850 then
  65. w('\n' .. vimopt_start)
  66. end
  67. w(' ' .. opt_desc.full_name)
  68. if opt_desc.abbreviation then
  69. w(' ' .. opt_desc.abbreviation)
  70. end
  71. if opt_desc.type == 'bool' then
  72. w(' inv' .. opt_desc.full_name)
  73. w(' no' .. opt_desc.full_name)
  74. if opt_desc.abbreviation then
  75. w(' inv' .. opt_desc.abbreviation)
  76. w(' no' .. opt_desc.abbreviation)
  77. end
  78. end
  79. end
  80. end
  81. w('\n\nsyn case ignore')
  82. local vimau_start = 'syn keyword vimAutoEvent contained '
  83. w('\n\n' .. vimau_start)
  84. for _, au in ipairs(auevents.events) do
  85. if not auevents.nvim_specific[au] then
  86. if lld.line_length > 850 then
  87. w('\n' .. vimau_start)
  88. end
  89. w(' ' .. au)
  90. end
  91. end
  92. for au, _ in pairs(auevents.aliases) do
  93. if not auevents.nvim_specific[au] then
  94. if lld.line_length > 850 then
  95. w('\n' .. vimau_start)
  96. end
  97. w(' ' .. au)
  98. end
  99. end
  100. local nvimau_start = 'syn keyword nvimAutoEvent contained '
  101. w('\n\n' .. nvimau_start)
  102. for au, _ in pairs(auevents.nvim_specific) do
  103. if lld.line_length > 850 then
  104. w('\n' .. nvimau_start)
  105. end
  106. w(' ' .. au)
  107. end
  108. w('\n\nsyn case match')
  109. local vimfun_start = 'syn keyword vimFuncName contained '
  110. w('\n\n' .. vimfun_start)
  111. local funcs = mpack.unpack(io.open(funcs_file, 'rb'):read("*all"))
  112. for name, _ in pairs(funcs) do
  113. if name then
  114. if lld.line_length > 850 then
  115. w('\n' .. vimfun_start)
  116. end
  117. w(' ' .. name)
  118. end
  119. end
  120. w('\n')
  121. syn_fd:close()