feat(keysets): teach Union and LuaRefOf

This commit is contained in:
Lewis Russell 2024-11-24 10:40:56 +00:00 committed by Lewis Russell
parent 99e7323aa3
commit 8d55cc218c
11 changed files with 258 additions and 109 deletions

View File

@ -1012,7 +1012,7 @@ function vim.api.nvim_create_namespace(name) end
--- ```
---
--- @param name string Name of the new user command. Must begin with an uppercase letter.
--- @param command any Replacement command to execute when this user command is executed. When called
--- @param command string|fun(args: vim.api.keyset.create_user_command.command_args) Replacement command to execute when this user command is executed. When called
--- from Lua, the command can also be a Lua function. The function is called with a
--- single table argument that contains the following keys:
--- - name: (string) Command name

View File

@ -4,11 +4,11 @@
error('Cannot require a meta file')
--- @class vim.api.keyset.buf_attach
--- @field on_lines? function
--- @field on_bytes? function
--- @field on_changedtick? function
--- @field on_detach? function
--- @field on_reload? function
--- @field on_lines? fun(_: "lines", bufnr: integer, changedtick: integer, first: integer, last_old: integer, last_new: integer, byte_count: integer, deleted_codepoints?: integer, deleted_codeunits?: integer): boolean?
--- @field on_bytes? fun(_: "bytes", bufnr: integer, changedtick: integer, start_row: integer, start_col: integer, start_byte: integer, old_end_row: integer, old_end_col: integer, old_end_byte: integer, new_end_row: integer, new_end_col: integer, new_end_byte: integer): boolean?
--- @field on_changedtick? fun(_: "changedtick", bufnr: integer, changedtick: integer)
--- @field on_detach? fun(_: "detach", bufnr: integer)
--- @field on_reload? fun(_: "reload", bufnr: integer)
--- @field utf_sizes? boolean
--- @field preview? boolean
@ -18,9 +18,9 @@ error('Cannot require a meta file')
--- @class vim.api.keyset.clear_autocmds
--- @field buffer? integer
--- @field event? any
--- @field group? any
--- @field pattern? any
--- @field event? string|string[]
--- @field group? integer|string
--- @field pattern? string|string[]
--- @class vim.api.keyset.cmd
--- @field cmd? string
@ -28,12 +28,12 @@ error('Cannot require a meta file')
--- @field count? integer
--- @field reg? string
--- @field bang? boolean
--- @field args? any[]
--- @field args? string[]
--- @field magic? table<string,any>
--- @field mods? table<string,any>
--- @field nargs? any
--- @field addr? any
--- @field nextcmd? any
--- @field nargs? integer|string
--- @field addr? string
--- @field nextcmd? string
--- @class vim.api.keyset.cmd_magic
--- @field file? boolean
@ -72,20 +72,20 @@ error('Cannot require a meta file')
--- @field info? string
--- @class vim.api.keyset.context
--- @field types? any[]
--- @field types? string[]
--- @class vim.api.keyset.create_augroup
--- @field clear? any
--- @field clear? boolean
--- @class vim.api.keyset.create_autocmd
--- @field buffer? integer
--- @field callback? any
--- @field callback? string|(fun(args: vim.api.keyset.create_autocmd.callback_args): boolean?)
--- @field command? string
--- @field desc? string
--- @field group? any
--- @field group? integer|string
--- @field nested? boolean
--- @field once? boolean
--- @field pattern? any
--- @field pattern? string|string[]
--- @class vim.api.keyset.echo_opts
--- @field verbose? boolean
@ -103,19 +103,19 @@ error('Cannot require a meta file')
--- @class vim.api.keyset.exec_autocmds
--- @field buffer? integer
--- @field group? any
--- @field group? integer|string
--- @field modeline? boolean
--- @field pattern? any
--- @field pattern? string|string[]
--- @field data? any
--- @class vim.api.keyset.exec_opts
--- @field output? boolean
--- @class vim.api.keyset.get_autocmds
--- @field event? any
--- @field group? any
--- @field pattern? any
--- @field buffer? any
--- @field event? string|string[]
--- @field group? integer|string
--- @field pattern? string|string[]
--- @field buffer? integer|integer[]
--- @class vim.api.keyset.get_commands
--- @field builtin? boolean
@ -154,17 +154,17 @@ error('Cannot require a meta file')
--- @field altfont? boolean
--- @field nocombine? boolean
--- @field default? boolean
--- @field cterm? any
--- @field foreground? any
--- @field fg? any
--- @field background? any
--- @field bg? any
--- @field ctermfg? any
--- @field ctermbg? any
--- @field special? any
--- @field sp? any
--- @field link? any
--- @field global_link? any
--- @field cterm? integer|string
--- @field foreground? integer|string
--- @field fg? integer|string
--- @field background? integer|string
--- @field bg? integer|string
--- @field ctermfg? integer|string
--- @field ctermbg? integer|string
--- @field special? integer|string
--- @field sp? integer|string
--- @field link? integer|string
--- @field global_link? integer|string
--- @field fallback? boolean
--- @field blend? integer
--- @field fg_indexed? boolean
@ -201,7 +201,7 @@ error('Cannot require a meta file')
--- @field wins? any[]
--- @class vim.api.keyset.open_term
--- @field on_input? function
--- @field on_input? fun(_: "input", term: integer, bufnr: integer, data: any)
--- @field force_crlf? boolean
--- @class vim.api.keyset.option
@ -227,20 +227,20 @@ error('Cannot require a meta file')
--- @field do_source? boolean
--- @class vim.api.keyset.set_decoration_provider
--- @field on_start? function
--- @field on_buf? function
--- @field on_win? function
--- @field on_line? function
--- @field on_end? function
--- @field _on_hl_def? function
--- @field _on_spell_nav? function
--- @field on_start? fun(_: "start", tick: integer)
--- @field on_buf? fun(_: "buf", bufnr: integer, tick: integer)
--- @field on_win? fun(_: "win", winid: integer, bufnr: integer, toprow: integer, botrow: integer)
--- @field on_line? fun(_: "line", winid: integer, bufnr: integer, row: integer)
--- @field on_end? fun(_: "end", tick: integer)
--- @field _on_hl_def? fun(_: "hl_def")
--- @field _on_spell_nav? fun(_: "spell_nav")
--- @class vim.api.keyset.set_extmark
--- @field id? integer
--- @field end_line? integer
--- @field end_row? integer
--- @field end_col? integer
--- @field hl_group? number|string
--- @field hl_group? integer|string
--- @field virt_text? any[]
--- @field virt_text_pos? string
--- @field virt_text_win_col? integer
@ -258,10 +258,10 @@ error('Cannot require a meta file')
--- @field virt_lines_leftcol? boolean
--- @field strict? boolean
--- @field sign_text? string
--- @field sign_hl_group? number|string
--- @field number_hl_group? number|string
--- @field line_hl_group? number|string
--- @field cursorline_hl_group? number|string
--- @field sign_hl_group? integer|string
--- @field number_hl_group? integer|string
--- @field line_hl_group? integer|string
--- @field cursorline_hl_group? integer|string
--- @field conceal? string
--- @field spell? boolean
--- @field ui_watched? boolean
@ -292,7 +292,7 @@ error('Cannot require a meta file')
--- @field relative? string
--- @field split? string
--- @field win? integer
--- @field bufpos? any[]
--- @field bufpos? integer[]
--- @field external? boolean
--- @field focusable? boolean
--- @field mouse? boolean
@ -315,12 +315,12 @@ error('Cannot require a meta file')
--- @field end_vcol? integer
--- @class vim.api.keyset.xdl_diff
--- @field on_hunk? function
--- @field on_hunk? fun(start_a: integer, count_a: integer, start_b: integer, count_b: integer): integer?
--- @field result_type? string
--- @field algorithm? string
--- @field ctxlen? integer
--- @field interhunkctxlen? integer
--- @field linematch? any
--- @field linematch? boolean|integer
--- @field ignore_whitespace? boolean
--- @field ignore_whitespace_change? boolean
--- @field ignore_whitespace_change_at_eol? boolean

View File

@ -73,6 +73,51 @@ error('Cannot require a meta file')
--- @field buflocal? boolean
--- @field buffer? integer
--- @class vim.api.keyset.create_autocmd.callback_args
--- @field id integer autocommand id
--- @field event string name of the triggered event |autocmd-events|
--- @field group? integer autocommand group id, if any
--- @field match string expanded value of <amatch>
--- @field buf integer expanded value of <abuf>
--- @field file string expanded value of <afile>
--- @field data? any arbitrary data passed from |nvim_exec_autocmds()| *event-data*
--- @class vim.api.keyset.create_user_command.command_args
--- @field name string Command name
---
--- The args passed to the command, if any <args>
--- @field args string
---
--- The args split by unescaped whitespace
--- (when more than one argument is allowed), if any <f-args>
--- @field fargs string[]
---
--- Number of arguments |:command-nargs|
--- @field nargs string
---
--- "true" if the command was executed with a ! modifier <bang>
--- @field bang boolean
---
--- The starting line of the command range <line1>
--- @field line1 integer
---
--- The final line of the command range <line2>
--- @field line2 integer
---
--- The number of items in the command range: 0, 1, or 2 <range>
--- @field range integer
---
--- Any count supplied <count>
--- @field count integer
--- The optional register, if specified <reg>
--- @field reg string
--- Command modifiers, if any <mods>
--- @field mods string
---
--- Command modifiers in a structured format. Has the same structure as the
--- "mods" key of |nvim_parse_cmd()|.
--- @field smods table
--- @class vim.api.keyset.command_info
--- @field name string
--- @field definition string

View File

@ -47,6 +47,18 @@ local LUA_API_RETURN_OVERRIDES = {
nvim_win_get_config = 'vim.api.keyset.win_config',
}
local LUA_API_KEYSET_OVERRIDES = {
create_autocmd = {
callback = 'string|(fun(args: vim.api.keyset.create_autocmd.callback_args): boolean?)',
},
}
local LUA_API_PARAM_OVERRIDES = {
nvim_create_user_command = {
command = 'string|fun(args: vim.api.keyset.create_user_command.command_args)',
},
}
local LUA_META_HEADER = {
'--- @meta _',
'-- THIS FILE IS GENERATED',
@ -118,7 +130,7 @@ local API_TYPES = {
LuaRef = 'function',
Dict = 'table<string,any>',
Float = 'number',
HLGroupID = 'number|string',
HLGroupID = 'integer|string',
void = '',
}
@ -133,6 +145,10 @@ end
--- @param t string
--- @return string
local function api_type(t)
if vim.startswith(t, '*') then
return api_type(t:sub(2)) .. '?'
end
local as0 = t:match('^ArrayOf%((.*)%)')
if as0 then
local as = split(as0, ', ')
@ -149,6 +165,33 @@ local function api_type(t)
return 'table<string,' .. api_type(d0) .. '>'
end
local u = t:match('^Union%((.*)%)')
if u then
local us = vim.split(u, ',%s*')
return table.concat(vim.tbl_map(api_type, us), '|')
end
local l = t:match('^LuaRefOf%((.*)%)')
if l then
--- @type string
l = l:gsub('%s+', ' ')
--- @type string?, string?
local as, r = l:match('%((.*)%),%s*(.*)')
if not as then
--- @type string
as = assert(l:match('%((.*)%)'))
end
local as1 = {} --- @type string[]
for a in vim.gsplit(as, ',%s') do
local a1 = vim.split(a, '%s+', { trimempty = true })
local nm = a1[2]:gsub('%*(.*)$', '%1?')
as1[#as1 + 1] = nm .. ': ' .. api_type(a1[1])
end
return ('fun(%s)%s'):format(table.concat(as1, ', '), r and ': ' .. api_type(r) or '')
end
return API_TYPES[t] or t
end
@ -251,11 +294,13 @@ local function get_api_meta()
sees[#sees + 1] = see.desc
end
local pty_overrides = LUA_API_PARAM_OVERRIDES[fun.name] or {}
local params = {} --- @type [string,string][]
for _, p in ipairs(fun.params) do
params[#params + 1] = {
p.name,
api_type(p.type),
api_type(pty_overrides[p.name] or p.type),
not deprecated and p.desc or nil,
}
end
@ -382,9 +427,11 @@ local function get_api_keysets_meta()
local keysets = metadata.keysets
for _, k in ipairs(keysets) do
local pty_overrides = LUA_API_KEYSET_OVERRIDES[k.name] or {}
local params = {}
for _, key in ipairs(k.keys) do
table.insert(params, { key .. '?', api_type(k.types[key] or 'any') })
local pty = pty_overrides[key] or k.types[key] or 'any'
table.insert(params, { key .. '?', api_type(pty) })
end
ret[k.name] = {
signature = 'NA',

View File

@ -631,7 +631,7 @@ Integer nvim_create_augroup(uint64_t channel_id, String name, Dict(create_augrou
FUNC_API_SINCE(9)
{
char *augroup_name = name.data;
bool clear_autocmds = api_object_to_bool(opts->clear, "clear", true, err);
bool clear_autocmds = GET_BOOL_OR_TRUE(opts, create_augroup, clear);
int augroup = -1;
WITH_SCRIPT_CONTEXT(channel_id, {

View File

@ -226,8 +226,8 @@ Dict(cmd) nvim_parse_cmd(String str, Dict(empty) *opts, Arena *arena, Error *err
addr = "?";
break;
}
PUT_KEY(result, cmd, addr, CSTR_AS_OBJ(addr));
PUT_KEY(result, cmd, nextcmd, CSTR_AS_OBJ(ea.nextcmd));
PUT_KEY(result, cmd, addr, cstr_as_string(addr));
PUT_KEY(result, cmd, nextcmd, cstr_as_string(ea.nextcmd));
// TODO(bfredl): nested keydict would be nice..
Dict mods = arena_dict(arena, 20);

View File

@ -8,18 +8,18 @@ typedef struct {
typedef struct {
OptionalKeys is_set__context_;
Array types;
ArrayOf(String) types;
} Dict(context);
typedef struct {
OptionalKeys is_set__set_decoration_provider_;
LuaRef on_start;
LuaRef on_buf;
LuaRef on_win;
LuaRef on_line;
LuaRef on_end;
LuaRef _on_hl_def;
LuaRef _on_spell_nav;
LuaRefOf(("start" _, Integer tick)) on_start;
LuaRefOf(("buf" _, Integer bufnr, Integer tick)) on_buf;
LuaRefOf(("win" _, Integer winid, Integer bufnr, Integer toprow, Integer botrow)) on_win;
LuaRefOf(("line" _, Integer winid, Integer bufnr, Integer row)) on_line;
LuaRefOf(("end" _, Integer tick)) on_end;
LuaRefOf(("hl_def" _)) _on_hl_def;
LuaRefOf(("spell_nav" _)) _on_spell_nav;
} Dict(set_decoration_provider);
typedef struct {
@ -116,7 +116,7 @@ typedef struct {
String relative;
String split;
Window win;
Array bufpos;
ArrayOf(Integer) bufpos;
Boolean external;
Boolean focusable;
Boolean mouse;
@ -172,17 +172,17 @@ typedef struct {
Boolean altfont;
Boolean nocombine;
Boolean default_ DictKey(default);
Object cterm;
Object foreground;
Object fg;
Object background;
Object bg;
Object ctermfg;
Object ctermbg;
Object special;
Object sp;
Object link;
Object global_link;
Union(Integer, String) cterm;
Union(Integer, String) foreground;
Union(Integer, String) fg;
Union(Integer, String) background;
Union(Integer, String) bg;
Union(Integer, String) ctermfg;
Union(Integer, String) ctermbg;
Union(Integer, String) special;
Union(Integer, String) sp;
HLGroupID link;
HLGroupID global_link;
Boolean fallback;
Integer blend;
Boolean fg_indexed;
@ -230,9 +230,9 @@ typedef struct {
typedef struct {
OptionalKeys is_set__clear_autocmds_;
Buffer buffer;
Object event;
Object group;
Object pattern;
Union(String, ArrayOf(String)) event;
Union(Integer, String) group;
Union(String, ArrayOf(String)) pattern;
} Dict(clear_autocmds);
typedef struct {
@ -241,31 +241,32 @@ typedef struct {
Object callback;
String command;
String desc;
Object group;
Union(Integer, String) group;
Boolean nested;
Boolean once;
Object pattern;
Union(String, ArrayOf(String)) pattern;
} Dict(create_autocmd);
typedef struct {
OptionalKeys is_set__exec_autocmds_;
Buffer buffer;
Object group;
Union(Integer, String) group;
Boolean modeline;
Object pattern;
Union(String, ArrayOf(String)) pattern;
Object data;
} Dict(exec_autocmds);
typedef struct {
OptionalKeys is_set__get_autocmds_;
Object event;
Object group;
Object pattern;
Object buffer;
Union(String, ArrayOf(String)) event;
Union(Integer, String) group;
Union(String, ArrayOf(String)) pattern;
Union(Integer, ArrayOf(Integer)) buffer;
} Dict(get_autocmds);
typedef struct {
Object clear;
OptionalKeys is_set__create_augroup_;
Boolean clear;
} Dict(create_augroup);
typedef struct {
@ -275,12 +276,12 @@ typedef struct {
Integer count;
String reg;
Boolean bang;
Array args;
ArrayOf(String) args;
Dict magic;
Dict mods;
Object nargs;
Object addr;
Object nextcmd;
Union(Integer, String) nargs;
String addr;
String nextcmd;
} Dict(cmd);
typedef struct {
@ -333,11 +334,30 @@ typedef struct {
typedef struct {
OptionalKeys is_set__buf_attach_;
LuaRef on_lines;
LuaRef on_bytes;
LuaRef on_changedtick;
LuaRef on_detach;
LuaRef on_reload;
LuaRefOf(("lines" _,
Integer bufnr,
Integer changedtick,
Integer first,
Integer last_old,
Integer last_new,
Integer byte_count,
Integer *deleted_codepoints,
Integer *deleted_codeunits), *Boolean) on_lines;
LuaRefOf(("bytes" _,
Integer bufnr,
Integer changedtick,
Integer start_row,
Integer start_col,
Integer start_byte,
Integer old_end_row,
Integer old_end_col,
Integer old_end_byte,
Integer new_end_row,
Integer new_end_col,
Integer new_end_byte), *Boolean) on_bytes;
LuaRefOf(("changedtick" _, Integer bufnr, Integer changedtick)) on_changedtick;
LuaRefOf(("detach" _, Integer bufnr)) on_detach;
LuaRefOf(("reload" _, Integer bufnr)) on_reload;
Boolean utf_sizes;
Boolean preview;
} Dict(buf_attach);
@ -350,7 +370,7 @@ typedef struct {
typedef struct {
OptionalKeys is_set__open_term_;
LuaRef on_input;
LuaRefOf(("input" _, Integer term, Integer bufnr, any data)) on_input;
Boolean force_crlf;
} Dict(open_term);
@ -361,12 +381,13 @@ typedef struct {
typedef struct {
OptionalKeys is_set__xdl_diff_;
LuaRef on_hunk;
LuaRefOf((Integer start_a, Integer count_a, Integer start_b, Integer count_b),
*Integer) on_hunk;
String result_type;
String algorithm;
Integer ctxlen;
Integer interhunkctxlen;
Object linematch;
Union(Boolean, Integer) linematch;
Boolean ignore_whitespace;
Boolean ignore_whitespace_change;
Boolean ignore_whitespace_change_at_eol;

View File

@ -21,6 +21,8 @@
# define Dict(name) KeyDict_##name
# define DictHash(name) KeyDict_##name##_get_field
# define DictKey(name)
# define LuaRefOf(...) LuaRef
# define Union(...) Object
# include "api/private/defs.h.inline.generated.h"
#endif

View File

@ -3,7 +3,7 @@ local lpeg = vim.lpeg
-- lpeg grammar for building api metadata from a set of header files. It
-- ignores comments and preprocessor commands and parses a very small subset
-- of C prototypes with a limited set of types
local P, R, S = lpeg.P, lpeg.R, lpeg.S
local P, R, S, V = lpeg.P, lpeg.R, lpeg.S, lpeg.V
local C, Ct, Cc, Cg = lpeg.C, lpeg.Ct, lpeg.Cc, lpeg.Cg
--- @param pat vim.lpeg.Pattern
@ -35,9 +35,39 @@ local cdoc_comment = P('///') * opt(Ct(Cg(rep(space) * rep(not_nl), 'comment')))
local c_preproc = P('#') * rep(not_nl)
local dllexport = P('DLLEXPORT') * rep1(ws)
local typed_container = ((P('ArrayOf(') + P('DictOf(') + P('Dict(')) * rep1(any - P(')')) * P(')'))
--- @param x vim.lpeg.Pattern
local function comma1(x)
return x * rep(fill * P(',') * fill * x)
end
local c_id = (typed_container + (letter * rep(alpha)))
-- Define the grammar
local basic_id = letter * rep(alpha)
local str = P('"') * rep(any - P('"')) * P('"')
--- @param x vim.lpeg.Pattern
local function paren(x)
return P('(') * fill * x * fill * P(')')
end
-- stylua: ignore start
local typed_container = P({
'S',
ID = V('S') + basic_id,
S = (
(P('Union') * paren(comma1(V('ID'))))
+ (P('ArrayOf') * paren(basic_id * opt(P(',') * fill * rep1(num))))
+ (P('DictOf') * paren(basic_id))
+ (P('LuaRefOf') * paren(
paren(comma1((V('ID') + str) * rep1(ws) * opt(P('*')) * basic_id))
* opt(P(',') * fill * opt(P('*')) * V('ID'))
))
+ (P('Dict') * paren(basic_id))),
})
-- stylua: ignore end
local c_id = typed_container + basic_id
local c_void = P('void')
local c_param_type = (

View File

@ -347,12 +347,16 @@ for _, k in ipairs(keysets) do
local function typename(type)
if type == 'HLGroupID' then
return 'kObjectTypeInteger'
elseif not type or vim.startswith(type, 'Union') then
return 'kObjectTypeNil'
elseif vim.startswith(type, 'LuaRefOf') then
return 'kObjectTypeLuaRef'
elseif type == 'StringArray' then
return 'kUnpackTypeStringArray'
elseif type ~= nil then
return 'kObjectType' .. type
elseif vim.startswith(type, 'ArrayOf') then
return 'kObjectTypeArray'
else
return 'kObjectTypeNil'
return 'kObjectType' .. type
end
end

View File

@ -1075,10 +1075,10 @@ HlAttrs dict2hlattrs(Dict(highlight) *dict, bool use_rgb, int *link_id, Error *e
return hlattrs;
}
if (HAS_KEY_X(dict, global_link)) {
*link_id = object_to_hl_id(dict->global_link, "link", err);
*link_id = (int)dict->global_link;
mask |= HL_GLOBAL;
} else {
*link_id = object_to_hl_id(dict->link, "link", err);
*link_id = (int)dict->link;
}
if (ERROR_SET(err)) {