aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/generators/gen_vimvim.lua
diff options
context:
space:
mode:
authorbfredl <bjorn.linse@gmail.com>2023-04-21 16:46:35 +0200
committerbfredl <bjorn.linse@gmail.com>2023-04-21 16:46:35 +0200
commit702892270dec627acb3dd585495ee0950f690bde (patch)
tree4f9b15237b83b7924ef5fca212d463f45773e741 /src/nvim/generators/gen_vimvim.lua
parent558e4191f049662065984677a9c7cdbb1518092a (diff)
downloadrneovim-702892270dec627acb3dd585495ee0950f690bde.tar.gz
rneovim-702892270dec627acb3dd585495ee0950f690bde.tar.bz2
rneovim-702892270dec627acb3dd585495ee0950f690bde.zip
refactor(build): move the last generator from scripts/ to src/nvim/generators
This one generates a runtime/ file instead of a source file. But otherwise it works the same like all other generators. It has the same prerequisites (shared and mpack modules, etc), and, importantly, it uses results from the source generators. The odd location makes it easy to overlook when refactoring generators (like I did last time, lol)
Diffstat (limited to 'src/nvim/generators/gen_vimvim.lua')
-rw-r--r--src/nvim/generators/gen_vimvim.lua148
1 files changed, 148 insertions, 0 deletions
diff --git a/src/nvim/generators/gen_vimvim.lua b/src/nvim/generators/gen_vimvim.lua
new file mode 100644
index 0000000000..09a7cab0c6
--- /dev/null
+++ b/src/nvim/generators/gen_vimvim.lua
@@ -0,0 +1,148 @@
+local mpack = vim.mpack
+
+local syntax_file = arg[1]
+local funcs_file = arg[2]
+
+local lld = {}
+local syn_fd = io.open(syntax_file, 'w')
+lld.line_length = 0
+local function w(s)
+ syn_fd:write(s)
+ if s:find('\n') then
+ lld.line_length = #(s:gsub('.*\n', ''))
+ else
+ lld.line_length = lld.line_length + #s
+ end
+end
+
+local options = require('options')
+local auevents = require('auevents')
+local ex_cmds = require('ex_cmds')
+
+local function cmd_kw(prev_cmd, cmd)
+ if not prev_cmd then
+ return cmd:sub(1, 1) .. '[' .. cmd:sub(2) .. ']'
+ else
+ local shift = 1
+ while cmd:sub(shift, shift) == prev_cmd:sub(shift, shift) do
+ shift = shift + 1
+ end
+ if cmd:sub(1, shift) == 'def' then
+ shift = shift + 1
+ end
+ if shift >= #cmd then
+ return cmd
+ else
+ return cmd:sub(1, shift) .. '[' .. cmd:sub(shift + 1) .. ']'
+ end
+ end
+end
+
+-- Exclude these from the vimCommand keyword list, they are handled specially
+-- in syntax/vim.vim (vimAugroupKey, vimAutoCmd, vimGlobal, vimSubst). #9327
+local function is_special_cased_cmd(cmd)
+ return (cmd == 'augroup'
+ or cmd == 'autocmd'
+ or cmd == 'doautocmd'
+ or cmd == 'doautoall'
+ or cmd == 'global'
+ or cmd == 'substitute')
+end
+
+local vimcmd_start = 'syn keyword vimCommand contained '
+w(vimcmd_start)
+local prev_cmd = nil
+for _, cmd_desc in ipairs(ex_cmds.cmds) do
+ if lld.line_length > 850 then
+ w('\n' .. vimcmd_start)
+ end
+ local cmd = cmd_desc.command
+ if cmd:match('%w') and cmd ~= 'z' and not is_special_cased_cmd(cmd) then
+ w(' ' .. cmd_kw(prev_cmd, cmd))
+ end
+ if cmd == 'delete' then
+ -- Add special abbreviations of :delete
+ w(' ' .. cmd_kw('d', 'dl'))
+ w(' ' .. cmd_kw('del', 'dell'))
+ w(' ' .. cmd_kw('dele', 'delel'))
+ w(' ' .. cmd_kw('delet', 'deletl'))
+ w(' ' .. cmd_kw('delete', 'deletel'))
+ w(' ' .. cmd_kw('d', 'dp'))
+ w(' ' .. cmd_kw('de', 'dep'))
+ w(' ' .. cmd_kw('del', 'delp'))
+ w(' ' .. cmd_kw('dele', 'delep'))
+ w(' ' .. cmd_kw('delet', 'deletp'))
+ w(' ' .. cmd_kw('delete', 'deletep'))
+ end
+ prev_cmd = cmd
+end
+
+local vimopt_start = 'syn keyword vimOption contained '
+w('\n\n' .. vimopt_start)
+
+for _, opt_desc in ipairs(options.options) do
+ if not opt_desc.varname or opt_desc.varname:sub(1, 7) ~= 'p_force' then
+ if lld.line_length > 850 then
+ w('\n' .. vimopt_start)
+ end
+ w(' ' .. opt_desc.full_name)
+ if opt_desc.abbreviation then
+ w(' ' .. opt_desc.abbreviation)
+ end
+ if opt_desc.type == 'bool' then
+ w(' inv' .. opt_desc.full_name)
+ w(' no' .. opt_desc.full_name)
+ if opt_desc.abbreviation then
+ w(' inv' .. opt_desc.abbreviation)
+ w(' no' .. opt_desc.abbreviation)
+ end
+ end
+ end
+end
+
+w('\n\nsyn case ignore')
+local vimau_start = 'syn keyword vimAutoEvent contained '
+w('\n\n' .. vimau_start)
+
+for _, au in ipairs(auevents.events) do
+ if not auevents.nvim_specific[au] then
+ if lld.line_length > 850 then
+ w('\n' .. vimau_start)
+ end
+ w(' ' .. au)
+ end
+end
+for au, _ in pairs(auevents.aliases) do
+ if not auevents.nvim_specific[au] then
+ if lld.line_length > 850 then
+ w('\n' .. vimau_start)
+ end
+ w(' ' .. au)
+ end
+end
+
+local nvimau_start = 'syn keyword nvimAutoEvent contained '
+w('\n\n' .. nvimau_start)
+
+for au, _ in vim.spairs(auevents.nvim_specific) do
+ if lld.line_length > 850 then
+ w('\n' .. nvimau_start)
+ end
+ w(' ' .. au)
+end
+
+w('\n\nsyn case match')
+local vimfun_start = 'syn keyword vimFuncName contained '
+w('\n\n' .. vimfun_start)
+local funcs = mpack.decode(io.open(funcs_file, 'rb'):read("*all"))
+for _, name in ipairs(funcs) do
+ if name then
+ if lld.line_length > 850 then
+ w('\n' .. vimfun_start)
+ end
+ w(' ' .. name)
+ end
+end
+
+w('\n')
+syn_fd:close()