refactor(api): use FOREACH_ITEM macro in autocmd

This commit is contained in:
Christian Clason 2022-06-26 17:57:45 +02:00
parent da358d4521
commit 5c8025967e

View File

@ -549,9 +549,7 @@ Integer nvim_create_autocmd(uint64_t channel_id, Object event, Dict(create_autoc
int retval; int retval;
for (size_t i = 0; i < patterns.size; i++) { FOREACH_ITEM(patterns, pat, {
Object pat = patterns.items[i];
// See: TODO(sctx) // See: TODO(sctx)
WITH_SCRIPT_CONTEXT(channel_id, { WITH_SCRIPT_CONTEXT(channel_id, {
retval = autocmd_register(autocmd_id, retval = autocmd_register(autocmd_id,
@ -569,7 +567,7 @@ Integer nvim_create_autocmd(uint64_t channel_id, Object event, Dict(create_autoc
api_set_error(err, kErrorTypeException, "Failed to set autocmd"); api_set_error(err, kErrorTypeException, "Failed to set autocmd");
goto cleanup; goto cleanup;
} }
} })
}); });
cleanup: cleanup:
@ -848,12 +846,11 @@ void nvim_exec_autocmds(Object event, Dict(exec_autocmds) *opts, Error *err)
FOREACH_ITEM(event_array, event_str, { FOREACH_ITEM(event_array, event_str, {
GET_ONE_EVENT(event_nr, event_str, cleanup) GET_ONE_EVENT(event_nr, event_str, cleanup)
for (size_t i = 0; i < patterns.size; i++) { FOREACH_ITEM(patterns, pat, {
Object pat = patterns.items[i];
char *fname = opts->buffer.type == kObjectTypeNil ? pat.data.string.data : NULL; char *fname = opts->buffer.type == kObjectTypeNil ? pat.data.string.data : NULL;
did_aucmd |= did_aucmd |=
apply_autocmds_group(event_nr, fname, NULL, true, au_group, buf, NULL, data); apply_autocmds_group(event_nr, fname, NULL, true, au_group, buf, NULL, data);
} })
}) })
if (did_aucmd && modeline) { if (did_aucmd && modeline) {
@ -867,8 +864,8 @@ cleanup:
static bool check_autocmd_string_array(Array arr, char *k, Error *err) static bool check_autocmd_string_array(Array arr, char *k, Error *err)
{ {
for (size_t i = 0; i < arr.size; i++) { FOREACH_ITEM(arr, entry, {
if (arr.items[i].type != kObjectTypeString) { if (entry.type != kObjectTypeString) {
api_set_error(err, api_set_error(err,
kErrorTypeValidation, kErrorTypeValidation,
"All entries in '%s' must be strings", "All entries in '%s' must be strings",
@ -877,13 +874,13 @@ static bool check_autocmd_string_array(Array arr, char *k, Error *err)
} }
// Disallow newlines in the middle of the line. // Disallow newlines in the middle of the line.
const String l = arr.items[i].data.string; const String l = entry.data.string;
if (memchr(l.data, NL, l.size)) { if (memchr(l.data, NL, l.size)) {
api_set_error(err, kErrorTypeValidation, api_set_error(err, kErrorTypeValidation,
"String cannot contain newlines"); "String cannot contain newlines");
return false; return false;
} }
} })
return true; return true;
} }
@ -970,8 +967,8 @@ static bool get_patterns_from_pattern_or_buf(Array *patterns, Object pattern, Ob
} }
Array array = v->data.array; Array array = v->data.array;
for (size_t i = 0; i < array.size; i++) { FOREACH_ITEM(array, entry, {
char *pat = array.items[i].data.string.data; char *pat = entry.data.string.data;
size_t patlen = aucmd_pattern_length(pat); size_t patlen = aucmd_pattern_length(pat);
while (patlen) { while (patlen) {
ADD(*patterns, STRING_OBJ(cbuf_to_string((char *)pat, patlen))); ADD(*patterns, STRING_OBJ(cbuf_to_string((char *)pat, patlen)));
@ -979,7 +976,7 @@ static bool get_patterns_from_pattern_or_buf(Array *patterns, Object pattern, Ob
pat = aucmd_next_pattern(pat, patlen); pat = aucmd_next_pattern(pat, patlen);
patlen = aucmd_pattern_length(pat); patlen = aucmd_pattern_length(pat);
} }
} })
} else { } else {
api_set_error(err, api_set_error(err,
kErrorTypeValidation, kErrorTypeValidation,