robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40325 from justinmk/cmdwindd5ebae7ac

master
5.2 KiB200 linesraw
1---@diagnostic disable: no-unknown
2-- Will generate files ex_cmds_enum.generated.h with cmdidx_T enum
3-- and ex_cmds_defs.generated.h with main Ex commands definitions.
4
5local enumfname = arg[1] -- '/ex_cmds_enum.generated.h'
6local defsfname = arg[2] -- '/ex_cmds_defs.generated.h'
7local ex_cmds_name = arg[3] -- 'ex_cmds.lua'
8
9local enumfile = assert(io.open(enumfname, 'w'))
10local defsfile = assert(io.open(defsfname, 'w'))
11
12local bit = require 'bit'
13local ex_cmds = loadfile(ex_cmds_name)()
14local defs = ex_cmds.cmds
15local flags = ex_cmds.flags
16
17local byte_a = string.byte('a')
18local byte_z = string.byte('z')
19local a_to_z = byte_z - byte_a + 1
20
21-- Table giving the index of the first command in cmdnames[] to lookup
22-- based on the first letter of a command.
23local cmdidxs1_out = string.format(
24  [[
25static const uint16_t cmdidxs1[%u] = {
26]],
27  a_to_z
28)
29-- Table giving the index of the first command in cmdnames[] to lookup
30-- based on the first 2 letters of a command.
31-- Values in cmdidxs2[c1][c2] are relative to cmdidxs1[c1] so that they
32-- fit in a byte.
33local cmdidxs2_out = string.format(
34  [[
35static const uint8_t cmdidxs2[%u][%u] = {
36  /*           a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y   z */
37]],
38  a_to_z,
39  a_to_z
40)
41
42enumfile:write([[
43// IWYU pragma: private, include "nvim/ex_cmds_defs.h"
44
45typedef enum CMD_index {
46]])
47defsfile:write(string.format(
48  [[
49#include "nvim/arglist.h"
50#include "nvim/autocmd.h"
51#include "nvim/buffer.h"
52#include "nvim/cmdhist.h"
53#include "nvim/debugger.h"
54#include "nvim/diff.h"
55#include "nvim/digraph.h"
56#include "nvim/eval.h"
57#include "nvim/eval/userfunc.h"
58#include "nvim/eval/vars.h"
59#include "nvim/ex_cmds.h"
60#include "nvim/ex_cmds2.h"
61#include "nvim/ex_docmd.h"
62#include "nvim/ex_eval.h"
63#include "nvim/ex_session.h"
64#include "nvim/help.h"
65#include "nvim/indent.h"
66#include "nvim/lua/executor.h"
67#include "nvim/lua/secure.h"
68#include "nvim/mapping.h"
69#include "nvim/mark.h"
70#include "nvim/match.h"
71#include "nvim/menu.h"
72#include "nvim/message.h"
73#include "nvim/ops.h"
74#include "nvim/option.h"
75#include "nvim/os/lang.h"
76#include "nvim/profile.h"
77#include "nvim/quickfix.h"
78#include "nvim/register.h"
79#include "nvim/runtime.h"
80#include "nvim/sign.h"
81#include "nvim/spell.h"
82#include "nvim/spellfile.h"
83#include "nvim/syntax.h"
84#include "nvim/undo.h"
85#include "nvim/usercmd.h"
86#include "nvim/version.h"
87
88static const int command_count = %u;
89static CommandDefinition cmdnames[%u] = {
90]],
91  #defs,
92  #defs
93))
94local cmds, cmdidxs1, cmdidxs2 = {}, {}, {}
95for _, cmd in ipairs(defs) do
96  if bit.band(cmd.flags, flags.RANGE) == flags.RANGE then
97    assert(
98      cmd.addr_type ~= 'ADDR_NONE',
99      string.format('ex_cmds.lua:%s: Using RANGE with ADDR_NONE\n', cmd.command)
100    )
101  else
102    assert(
103      cmd.addr_type == 'ADDR_NONE',
104      string.format('ex_cmds.lua:%s: Missing ADDR_NONE\n', cmd.command)
105    )
106  end
107  if bit.band(cmd.flags, flags.DFLALL) == flags.DFLALL then
108    assert(
109      cmd.addr_type ~= 'ADDR_OTHER' and cmd.addr_type ~= 'ADDR_NONE',
110      string.format('ex_cmds.lua:%s: Missing misplaced DFLALL\n', cmd.command)
111    )
112  end
113  if bit.band(cmd.flags, flags.PREVIEW) == flags.PREVIEW then
114    assert(
115      cmd.preview_func ~= nil,
116      string.format('ex_cmds.lua:%s: Missing preview_func\n', cmd.command)
117    )
118  end
119  if bit.band(cmd.flags, flags.BUFLOCK_OK) == flags.BUFLOCK_OK then
120    assert(
121      bit.band(cmd.flags, flags.LOCK_OK) == flags.LOCK_OK,
122      string.format('ex_cmds.lua:%s: BUFLOCK_OK implies LOCK_OK\n', cmd.command)
123    )
124  end
125  local enumname = cmd.enum or ('CMD_' .. cmd.command)
126  local byte_cmd = cmd.command:sub(1, 1):byte()
127  if byte_a <= byte_cmd and byte_cmd <= byte_z then
128    table.insert(cmds, cmd.command)
129  end
130  local preview_func
131  if cmd.preview_func then
132    preview_func = string.format('&%s', cmd.preview_func)
133  else
134    preview_func = 'NULL'
135  end
136  enumfile:write('  ' .. enumname .. ',\n')
137  defsfile:write(string.format(
138    [[
139  [%s] = {
140    .cmd_name = "%s",
141    .cmd_func = (ex_func_T)&%s,
142    .cmd_preview_func = %s,
143    .cmd_argt = %uL,
144    .cmd_addr_type = %s
145  },
146]],
147    enumname,
148    cmd.command,
149    cmd.func,
150    preview_func,
151    cmd.flags,
152    cmd.addr_type
153  ))
154end
155for i = #cmds, 1, -1 do
156  local cmd = cmds[i]
157  -- First and second characters of the command
158  local c1 = cmd:sub(1, 1)
159  cmdidxs1[c1] = i - 1
160  if cmd:len() >= 2 then
161    local c2 = cmd:sub(2, 2)
162    local byte_c2 = string.byte(c2)
163    if byte_a <= byte_c2 and byte_c2 <= byte_z then
164      if not cmdidxs2[c1] then
165        cmdidxs2[c1] = {}
166      end
167      cmdidxs2[c1][c2] = i - 1
168    end
169  end
170end
171for i = byte_a, byte_z do
172  local c1 = string.char(i)
173  cmdidxs1_out = cmdidxs1_out .. '  /*  ' .. c1 .. '  */ ' .. cmdidxs1[c1] .. ',\n'
174  cmdidxs2_out = cmdidxs2_out .. '  /*  ' .. c1 .. '  */ {'
175  for j = byte_a, byte_z do
176    local c2 = string.char(j)
177    cmdidxs2_out = cmdidxs2_out
178      .. ((cmdidxs2[c1] and cmdidxs2[c1][c2]) and string.format(
179        '%3d',
180        cmdidxs2[c1][c2] - cmdidxs1[c1]
181      ) or '  0')
182      .. ','
183  end
184  cmdidxs2_out = cmdidxs2_out .. ' },\n'
185end
186enumfile:write([[
187  CMD_SIZE,
188  CMD_USER = -1,
189  CMD_USER_BUF = -2
190} cmdidx_T;
191]])
192defsfile:write(string.format(
193  [[
194};
195%s};
196%s};
197]],
198  cmdidxs1_out,
199  cmdidxs2_out
200))