genvimvim.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, vimSubst). #9327
  41. local function is_special_cased_cmd(cmd)
  42. return (cmd == 'augroup'
  43. or cmd == 'autocmd'
  44. or cmd == 'doautocmd'
  45. or cmd == 'doautoall'
  46. or cmd == 'substitute')
  47. end
  48. local vimcmd_start = 'syn keyword vimCommand contained '
  49. w(vimcmd_start)
  50. local prev_cmd = nil
  51. for _, cmd_desc in ipairs(ex_cmds.cmds) do
  52. if lld.line_length > 850 then
  53. w('\n' .. vimcmd_start)
  54. end
  55. local cmd = cmd_desc.command
  56. if cmd:match('%w') and cmd ~= 'z' and not is_special_cased_cmd(cmd) then
  57. w(' ' .. cmd_kw(prev_cmd, cmd))
  58. end
  59. prev_cmd = cmd
  60. end
  61. local vimopt_start = 'syn keyword vimOption contained '
  62. w('\n\n' .. vimopt_start)
  63. for _, opt_desc in ipairs(options.options) do
  64. if not opt_desc.varname or opt_desc.varname:sub(1, 7) ~= 'p_force' then
  65. if lld.line_length > 850 then
  66. w('\n' .. vimopt_start)
  67. end
  68. w(' ' .. opt_desc.full_name)
  69. if opt_desc.abbreviation then
  70. w(' ' .. opt_desc.abbreviation)
  71. end
  72. if opt_desc.type == 'bool' then
  73. w(' inv' .. opt_desc.full_name)
  74. w(' no' .. opt_desc.full_name)
  75. if opt_desc.abbreviation then
  76. w(' inv' .. opt_desc.abbreviation)
  77. w(' no' .. opt_desc.abbreviation)
  78. end
  79. end
  80. end
  81. end
  82. w('\n\nsyn case ignore')
  83. local vimau_start = 'syn keyword vimAutoEvent contained '
  84. w('\n\n' .. vimau_start)
  85. for _, au in ipairs(auevents.events) do
  86. if not auevents.nvim_specific[au] then
  87. if lld.line_length > 850 then
  88. w('\n' .. vimau_start)
  89. end
  90. w(' ' .. au)
  91. end
  92. end
  93. for au, _ in pairs(auevents.aliases) do
  94. if not auevents.nvim_specific[au] then
  95. if lld.line_length > 850 then
  96. w('\n' .. vimau_start)
  97. end
  98. w(' ' .. au)
  99. end
  100. end
  101. local nvimau_start = 'syn keyword nvimAutoEvent contained '
  102. w('\n\n' .. nvimau_start)
  103. for au, _ in pairs(auevents.nvim_specific) do
  104. if lld.line_length > 850 then
  105. w('\n' .. nvimau_start)
  106. end
  107. w(' ' .. au)
  108. end
  109. w('\n\nsyn case match')
  110. local vimfun_start = 'syn keyword vimFuncName contained '
  111. w('\n\n' .. vimfun_start)
  112. local funcs = mpack.unpack(io.open(funcs_file, 'rb'):read("*all"))
  113. for name, _ in pairs(funcs) do
  114. if name then
  115. if lld.line_length > 850 then
  116. w('\n' .. vimfun_start)
  117. end
  118. w(' ' .. name)
  119. end
  120. end
  121. w('\n')
  122. syn_fd:close()