refactor(gen_events): sort enums case-insensitively (#32811)

This actually only affects the order in which Cmdline* and Cmdwin*
autocommands are listed, and it appears that the names of Cmdwin* were
changed to CmdWin* in 8ed2dbf6e2 without
explanation.

Also, remove the final NULL element from the names table.
This commit is contained in:
zeertzjq
2025-03-10 08:27:30 +08:00
committed by GitHub
parent b90f649ca2
commit c53e00889d
2 changed files with 16 additions and 6 deletions

View File

@@ -8,7 +8,9 @@ local aliases = auevents.aliases
--- @type string[] --- @type string[]
local names = vim.tbl_keys(vim.tbl_extend('error', events, aliases)) local names = vim.tbl_keys(vim.tbl_extend('error', events, aliases))
table.sort(names) table.sort(names, function(a, b)
return a:lower() < b:lower()
end)
local enum_tgt = assert(io.open(fileio_enum_file, 'w')) local enum_tgt = assert(io.open(fileio_enum_file, 'w'))
local names_tgt = assert(io.open(names_file, 'w')) local names_tgt = assert(io.open(names_file, 'w'))
@@ -22,7 +24,7 @@ static const struct event_name {
size_t len; size_t len;
char *name; char *name;
int event; int event;
} event_names[] = {]]) } event_names[NUM_EVENTS] = {]])
for i, name in ipairs(names) do for i, name in ipairs(names) do
enum_tgt:write(('\n EVENT_%s = %u,'):format(name:upper(), i - 1)) enum_tgt:write(('\n EVENT_%s = %u,'):format(name:upper(), i - 1))
@@ -45,11 +47,10 @@ enum_tgt:write(('\n NUM_EVENTS = %u,'):format(#names))
enum_tgt:write('\n} event_T;\n') enum_tgt:write('\n} event_T;\n')
enum_tgt:close() enum_tgt:close()
names_tgt:write('\n [NUM_EVENTS] = {0, NULL, (event_T)0},\n};\n') names_tgt:write('\n};\n')
names_tgt:write('\nstatic AutoCmdVec autocmds[NUM_EVENTS] = { 0 };\n') names_tgt:write('\nstatic AutoCmdVec autocmds[NUM_EVENTS] = { 0 };\n')
local hashorder = vim.tbl_map(string.lower, names) local hashorder = vim.tbl_map(string.lower, names)
table.sort(hashorder)
local hashfun local hashfun
hashorder, hashfun = hashy.hashy_hash('event_name2nr', hashorder, function(idx) hashorder, hashfun = hashy.hashy_hash('event_name2nr', hashorder, function(idx)
return 'event_names[event_hash[' .. idx .. ']].name' return 'event_names[event_hash[' .. idx .. ']].name'

View File

@@ -2267,7 +2267,7 @@ char *set_context_in_autocmd(expand_T *xp, char *arg, bool doautocmd)
return NULL; return NULL;
} }
// Function given to ExpandGeneric() to obtain the list of event names. /// Function given to ExpandGeneric() to obtain the list of event names.
char *expand_get_event_name(expand_T *xp, int idx) char *expand_get_event_name(expand_T *xp, int idx)
{ {
(void)xp; // xp is a required parameter to be used with ExpandGeneric (void)xp; // xp is a required parameter to be used with ExpandGeneric
@@ -2283,14 +2283,23 @@ char *expand_get_event_name(expand_T *xp, int idx)
return name; return name;
} }
int i = idx - next_augroup_id;
if (i < 0 || i >= NUM_EVENTS) {
return NULL;
}
// List event names // List event names
return event_names[idx - next_augroup_id].name; return event_names[i].name;
} }
/// Function given to ExpandGeneric() to obtain the list of event names. Don't /// Function given to ExpandGeneric() to obtain the list of event names. Don't
/// include groups. /// include groups.
char *get_event_name_no_group(expand_T *xp FUNC_ATTR_UNUSED, int idx, bool win) char *get_event_name_no_group(expand_T *xp FUNC_ATTR_UNUSED, int idx, bool win)
{ {
if (idx < 0 || idx >= NUM_EVENTS) {
return NULL;
}
if (!win) { if (!win) {
return event_names[idx].name; return event_names[idx].name;
} }