mirror of
https://github.com/neovim/neovim.git
synced 2025-10-04 08:56:29 +00:00
feat(autocmds): retrieve lua callback (#18642)
add a new `callback` field to `nvim_get_autocmds`
This commit is contained in:
@@ -66,7 +66,9 @@ static int64_t next_autocmd_id = 1;
|
||||
/// - group_name (string): the autocommand group name.
|
||||
/// - desc (string): the autocommand description.
|
||||
/// - event (string): the autocommand event.
|
||||
/// - command (string): the autocommand command.
|
||||
/// - command (string): the autocommand command. Note: this will be empty if a callback is set.
|
||||
/// - callback (function|string|nil): Lua function or name of a Vim script function
|
||||
/// which is executed when this autocommand is triggered.
|
||||
/// - once (boolean): whether the autocommand is only run once.
|
||||
/// - pattern (string): the autocommand pattern.
|
||||
/// If the autocommand is buffer local |autocmd-buffer-local|:
|
||||
@@ -280,9 +282,28 @@ Array nvim_get_autocmds(Dict(get_autocmds) *opts, Error *err)
|
||||
PUT(autocmd_info, "desc", CSTR_TO_OBJ(ac->desc));
|
||||
}
|
||||
|
||||
PUT(autocmd_info,
|
||||
"command",
|
||||
STRING_OBJ(cstr_as_string(aucmd_exec_to_string(ac, ac->exec))));
|
||||
if (ac->exec.type == CALLABLE_CB) {
|
||||
PUT(autocmd_info, "command", STRING_OBJ(STRING_INIT));
|
||||
|
||||
Callback *cb = &ac->exec.callable.cb;
|
||||
switch (cb->type) {
|
||||
case kCallbackLua:
|
||||
if (nlua_ref_is_function(cb->data.luaref)) {
|
||||
PUT(autocmd_info, "callback", LUAREF_OBJ(api_new_luaref(cb->data.luaref)));
|
||||
}
|
||||
break;
|
||||
case kCallbackFuncref:
|
||||
case kCallbackPartial:
|
||||
PUT(autocmd_info, "callback", STRING_OBJ(cstr_as_string(callback_to_string(cb))));
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
} else {
|
||||
PUT(autocmd_info,
|
||||
"command",
|
||||
STRING_OBJ(cstr_as_string(xstrdup(ac->exec.callable.cmd))));
|
||||
}
|
||||
|
||||
PUT(autocmd_info,
|
||||
"pattern",
|
||||
@@ -442,7 +463,8 @@ Integer nvim_create_autocmd(uint64_t channel_id, Object event, Dict(create_autoc
|
||||
// not do that.
|
||||
|
||||
Object *callback = &opts->callback;
|
||||
if (callback->type == kObjectTypeLuaRef) {
|
||||
switch (callback->type) {
|
||||
case kObjectTypeLuaRef:
|
||||
if (callback->data.luaref == LUA_NOREF) {
|
||||
api_set_error(err,
|
||||
kErrorTypeValidation,
|
||||
@@ -459,10 +481,12 @@ Integer nvim_create_autocmd(uint64_t channel_id, Object event, Dict(create_autoc
|
||||
|
||||
cb.type = kCallbackLua;
|
||||
cb.data.luaref = api_new_luaref(callback->data.luaref);
|
||||
} else if (callback->type == kObjectTypeString) {
|
||||
break;
|
||||
case kObjectTypeString:
|
||||
cb.type = kCallbackFuncref;
|
||||
cb.data.funcref = string_to_cstr(callback->data.string);
|
||||
} else {
|
||||
break;
|
||||
default:
|
||||
api_set_error(err,
|
||||
kErrorTypeException,
|
||||
"'callback' must be a lua function or name of vim function");
|
||||
|
@@ -2477,32 +2477,6 @@ bool autocmd_delete_id(int64_t id)
|
||||
// AucmdExecutable Functions
|
||||
// ===========================================================================
|
||||
|
||||
/// Generate a string description of a callback
|
||||
static char *aucmd_callback_to_string(Callback cb)
|
||||
{
|
||||
// NOTE: this function probably belongs in a helper
|
||||
|
||||
size_t msglen = 100;
|
||||
char *msg = (char *)xmallocz(msglen);
|
||||
|
||||
switch (cb.type) {
|
||||
case kCallbackLua:
|
||||
snprintf(msg, msglen, "<lua: %d>", cb.data.luaref);
|
||||
break;
|
||||
case kCallbackFuncref:
|
||||
// TODO(tjdevries): Is this enough space for this?
|
||||
snprintf(msg, msglen, "<vim function: %s>", cb.data.funcref);
|
||||
break;
|
||||
case kCallbackPartial:
|
||||
snprintf(msg, msglen, "<vim partial: %s>", cb.data.partial->pt_name);
|
||||
break;
|
||||
default:
|
||||
snprintf(msg, msglen, "%s", "");
|
||||
break;
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
/// Generate a string description for the command/callback of an autocmd
|
||||
char *aucmd_exec_to_string(AutoCmd *ac, AucmdExecutable acc)
|
||||
FUNC_ATTR_PURE
|
||||
@@ -2511,7 +2485,7 @@ char *aucmd_exec_to_string(AutoCmd *ac, AucmdExecutable acc)
|
||||
case CALLABLE_EX:
|
||||
return xstrdup(acc.callable.cmd);
|
||||
case CALLABLE_CB:
|
||||
return aucmd_callback_to_string(acc.callable.cb);
|
||||
return callback_to_string(&acc.callable.cb);
|
||||
case CALLABLE_NONE:
|
||||
return "This is not possible";
|
||||
}
|
||||
|
@@ -1205,6 +1205,30 @@ void callback_copy(Callback *dest, Callback *src)
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate a string description of a callback
|
||||
char *callback_to_string(Callback *cb)
|
||||
{
|
||||
size_t msglen = 100;
|
||||
char *msg = (char *)xmallocz(msglen);
|
||||
|
||||
switch (cb->type) {
|
||||
case kCallbackLua:
|
||||
snprintf(msg, msglen, "<lua: %d>", cb->data.luaref);
|
||||
break;
|
||||
case kCallbackFuncref:
|
||||
// TODO(tjdevries): Is this enough space for this?
|
||||
snprintf(msg, msglen, "<vim function: %s>", cb->data.funcref);
|
||||
break;
|
||||
case kCallbackPartial:
|
||||
snprintf(msg, msglen, "<vim partial: %s>", cb->data.partial->pt_name);
|
||||
break;
|
||||
default:
|
||||
snprintf(msg, msglen, "%s", "");
|
||||
break;
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
/// Remove watcher from a dictionary
|
||||
///
|
||||
/// @param dict Dictionary to remove watcher from.
|
||||
|
Reference in New Issue
Block a user