mirror of
https://github.com/neovim/neovim.git
synced 2025-09-27 13:38:34 +00:00
Merge pull request #23730 from famiu/refactor/api/helpers
refactor(api): new helper macros for cstrings as objects
This commit is contained in:
@@ -293,17 +293,17 @@ Array nvim_get_autocmds(Dict(get_autocmds) *opts, Error *err)
|
||||
break;
|
||||
case kCallbackFuncref:
|
||||
case kCallbackPartial:
|
||||
PUT(autocmd_info, "callback", STRING_OBJ(cstr_as_string(callback_to_string(cb))));
|
||||
PUT(autocmd_info, "callback", CSTR_AS_OBJ(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, "command", CSTR_TO_OBJ(ac->exec.callable.cmd));
|
||||
}
|
||||
|
||||
PUT(autocmd_info, "pattern", STRING_OBJ(cstr_to_string(ap->pat)));
|
||||
PUT(autocmd_info, "event", STRING_OBJ(cstr_to_string(event_nr2name(event))));
|
||||
PUT(autocmd_info, "pattern", CSTR_TO_OBJ(ap->pat));
|
||||
PUT(autocmd_info, "event", CSTR_TO_OBJ(event_nr2name(event)));
|
||||
PUT(autocmd_info, "once", BOOLEAN_OBJ(ac->once));
|
||||
|
||||
if (ap->buflocal_nr) {
|
||||
@@ -475,7 +475,7 @@ Integer nvim_create_autocmd(uint64_t channel_id, Object event, Dict(create_autoc
|
||||
}
|
||||
|
||||
if (patterns.size == 0) {
|
||||
ADD(patterns, STRING_OBJ(STATIC_CSTR_TO_STRING("*")));
|
||||
ADD(patterns, STATIC_CSTR_TO_OBJ("*"));
|
||||
}
|
||||
|
||||
VALIDATE_R((event_array.size > 0), "event", {
|
||||
@@ -587,7 +587,7 @@ void nvim_clear_autocmds(Dict(clear_autocmds) *opts, Error *err)
|
||||
// When we create the autocmds, we want to say that they are all matched, so that's *
|
||||
// but when we clear them, we want to say that we didn't pass a pattern, so that's NUL
|
||||
if (patterns.size == 0) {
|
||||
ADD(patterns, STRING_OBJ(STATIC_CSTR_TO_STRING("")));
|
||||
ADD(patterns, STATIC_CSTR_TO_OBJ(""));
|
||||
}
|
||||
|
||||
// If we didn't pass any events, that means clear all events.
|
||||
@@ -763,7 +763,7 @@ void nvim_exec_autocmds(Object event, Dict(exec_autocmds) *opts, Error *err)
|
||||
}
|
||||
|
||||
if (patterns.size == 0) {
|
||||
ADD(patterns, STRING_OBJ(STATIC_CSTR_TO_STRING("")));
|
||||
ADD(patterns, STATIC_CSTR_TO_OBJ(""));
|
||||
}
|
||||
|
||||
if (HAS_KEY(opts->data)) {
|
||||
@@ -894,7 +894,7 @@ static bool get_patterns_from_pattern_or_buf(Array *patterns, Object pattern, Ob
|
||||
}
|
||||
|
||||
snprintf((char *)pattern_buflocal, BUFLOCAL_PAT_LEN, "<buffer=%d>", (int)buf->handle);
|
||||
ADD(*patterns, STRING_OBJ(cstr_to_string(pattern_buflocal)));
|
||||
ADD(*patterns, CSTR_TO_OBJ(pattern_buflocal));
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@@ -518,7 +518,7 @@ void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, In
|
||||
{
|
||||
MAXSIZE_TEMP_ARRAY(scratch, 1);
|
||||
if (replacement.size == 0) {
|
||||
ADD_C(scratch, STRING_OBJ(STATIC_CSTR_AS_STRING("")));
|
||||
ADD_C(scratch, STATIC_CSTR_AS_OBJ(""));
|
||||
replacement = scratch;
|
||||
}
|
||||
|
||||
|
@@ -245,7 +245,7 @@ Dictionary nvim_parse_cmd(String str, Dictionary opts, Error *err)
|
||||
Dictionary filter = ARRAY_DICT_INIT;
|
||||
PUT(filter, "pattern", cmdinfo.cmdmod.cmod_filter_pat
|
||||
? CSTR_TO_OBJ(cmdinfo.cmdmod.cmod_filter_pat)
|
||||
: STRING_OBJ(STATIC_CSTR_TO_STRING("")));
|
||||
: STATIC_CSTR_TO_OBJ(""));
|
||||
PUT(filter, "force", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_filter_force));
|
||||
PUT(mods, "filter", DICTIONARY_OBJ(filter));
|
||||
|
||||
@@ -438,7 +438,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
|
||||
break;
|
||||
}
|
||||
|
||||
ADD(args, STRING_OBJ(cstr_as_string(data_str)));
|
||||
ADD(args, CSTR_AS_OBJ(data_str));
|
||||
}
|
||||
|
||||
bool argc_valid;
|
||||
|
@@ -106,7 +106,7 @@ bool ns_initialized(uint32_t ns)
|
||||
static Object hl_group_name(int hl_id, bool hl_name)
|
||||
{
|
||||
if (hl_name) {
|
||||
return STRING_OBJ(cstr_to_string(syn_id2name(hl_id)));
|
||||
return CSTR_TO_OBJ(syn_id2name(hl_id));
|
||||
} else {
|
||||
return INTEGER_OBJ(hl_id);
|
||||
}
|
||||
@@ -140,7 +140,7 @@ static Array extmark_to_array(const ExtmarkInfo *extmark, bool id, bool add_dict
|
||||
PUT(dict, "hl_eol", BOOLEAN_OBJ(decor->hl_eol));
|
||||
}
|
||||
if (decor->hl_mode) {
|
||||
PUT(dict, "hl_mode", STRING_OBJ(cstr_to_string(hl_mode_str[decor->hl_mode])));
|
||||
PUT(dict, "hl_mode", CSTR_TO_OBJ(hl_mode_str[decor->hl_mode]));
|
||||
}
|
||||
|
||||
if (kv_size(decor->virt_text)) {
|
||||
@@ -148,7 +148,7 @@ static Array extmark_to_array(const ExtmarkInfo *extmark, bool id, bool add_dict
|
||||
for (size_t i = 0; i < decor->virt_text.size; i++) {
|
||||
Array chunk = ARRAY_DICT_INIT;
|
||||
VirtTextChunk *vtc = &decor->virt_text.items[i];
|
||||
ADD(chunk, STRING_OBJ(cstr_to_string(vtc->text)));
|
||||
ADD(chunk, CSTR_TO_OBJ(vtc->text));
|
||||
if (vtc->hl_id > 0) {
|
||||
ADD(chunk, hl_group_name(vtc->hl_id, hl_name));
|
||||
}
|
||||
@@ -160,7 +160,7 @@ static Array extmark_to_array(const ExtmarkInfo *extmark, bool id, bool add_dict
|
||||
PUT(dict, "virt_text_win_col", INTEGER_OBJ(decor->col));
|
||||
}
|
||||
PUT(dict, "virt_text_pos",
|
||||
STRING_OBJ(cstr_to_string(virt_text_pos_str[decor->virt_text_pos])));
|
||||
CSTR_TO_OBJ(virt_text_pos_str[decor->virt_text_pos]));
|
||||
}
|
||||
|
||||
if (decor->ui_watched) {
|
||||
@@ -177,7 +177,7 @@ static Array extmark_to_array(const ExtmarkInfo *extmark, bool id, bool add_dict
|
||||
for (size_t j = 0; j < vt->size; j++) {
|
||||
Array chunk = ARRAY_DICT_INIT;
|
||||
VirtTextChunk *vtc = &vt->items[j];
|
||||
ADD(chunk, STRING_OBJ(cstr_to_string(vtc->text)));
|
||||
ADD(chunk, CSTR_TO_OBJ(vtc->text));
|
||||
if (vtc->hl_id > 0) {
|
||||
ADD(chunk, hl_group_name(vtc->hl_id, hl_name));
|
||||
}
|
||||
@@ -191,7 +191,7 @@ static Array extmark_to_array(const ExtmarkInfo *extmark, bool id, bool add_dict
|
||||
}
|
||||
|
||||
if (decor->sign_text) {
|
||||
PUT(dict, "sign_text", STRING_OBJ(cstr_to_string(decor->sign_text)));
|
||||
PUT(dict, "sign_text", CSTR_TO_OBJ(decor->sign_text));
|
||||
}
|
||||
|
||||
// uncrustify:off
|
||||
|
@@ -188,7 +188,7 @@ Object nvim_get_option_value(String name, Dict(option) *opts, Error *err)
|
||||
|
||||
switch (result) {
|
||||
case gov_string:
|
||||
rv = STRING_OBJ(cstr_as_string(stringval));
|
||||
rv = CSTR_AS_OBJ(stringval);
|
||||
break;
|
||||
case gov_number:
|
||||
rv = INTEGER_OBJ(numval);
|
||||
|
@@ -660,10 +660,10 @@ static void init_ui_event_metadata(Dictionary *metadata)
|
||||
msgpack_unpacked_destroy(&unpacked);
|
||||
PUT(*metadata, "ui_events", ui_events);
|
||||
Array ui_options = ARRAY_DICT_INIT;
|
||||
ADD(ui_options, STRING_OBJ(cstr_to_string("rgb")));
|
||||
ADD(ui_options, CSTR_TO_OBJ("rgb"));
|
||||
for (UIExtension i = 0; i < kUIExtCount; i++) {
|
||||
if (ui_ext_names[i][0] != '_') {
|
||||
ADD(ui_options, STRING_OBJ(cstr_to_string(ui_ext_names[i])));
|
||||
ADD(ui_options, CSTR_TO_OBJ(ui_ext_names[i]));
|
||||
}
|
||||
}
|
||||
PUT(*metadata, "ui_options", ARRAY_OBJ(ui_options));
|
||||
@@ -692,17 +692,17 @@ static void init_type_metadata(Dictionary *metadata)
|
||||
Dictionary buffer_metadata = ARRAY_DICT_INIT;
|
||||
PUT(buffer_metadata, "id",
|
||||
INTEGER_OBJ(kObjectTypeBuffer - EXT_OBJECT_TYPE_SHIFT));
|
||||
PUT(buffer_metadata, "prefix", STRING_OBJ(cstr_to_string("nvim_buf_")));
|
||||
PUT(buffer_metadata, "prefix", CSTR_TO_OBJ("nvim_buf_"));
|
||||
|
||||
Dictionary window_metadata = ARRAY_DICT_INIT;
|
||||
PUT(window_metadata, "id",
|
||||
INTEGER_OBJ(kObjectTypeWindow - EXT_OBJECT_TYPE_SHIFT));
|
||||
PUT(window_metadata, "prefix", STRING_OBJ(cstr_to_string("nvim_win_")));
|
||||
PUT(window_metadata, "prefix", CSTR_TO_OBJ("nvim_win_"));
|
||||
|
||||
Dictionary tabpage_metadata = ARRAY_DICT_INIT;
|
||||
PUT(tabpage_metadata, "id",
|
||||
INTEGER_OBJ(kObjectTypeTabpage - EXT_OBJECT_TYPE_SHIFT));
|
||||
PUT(tabpage_metadata, "prefix", STRING_OBJ(cstr_to_string("nvim_tabpage_")));
|
||||
PUT(tabpage_metadata, "prefix", CSTR_TO_OBJ("nvim_tabpage_"));
|
||||
|
||||
PUT(types, "Buffer", DICTIONARY_OBJ(buffer_metadata));
|
||||
PUT(types, "Window", DICTIONARY_OBJ(window_metadata));
|
||||
|
@@ -34,6 +34,7 @@
|
||||
.type = kObjectTypeString, \
|
||||
.data.string = s })
|
||||
|
||||
#define CSTR_AS_OBJ(s) STRING_OBJ(cstr_as_string(s))
|
||||
#define CSTR_TO_OBJ(s) STRING_OBJ(cstr_to_string(s))
|
||||
|
||||
#define BUFFER_OBJ(s) ((Object) { \
|
||||
@@ -103,6 +104,9 @@
|
||||
.data = xmemdupz(s, sizeof(s) - 1), \
|
||||
.size = sizeof(s) - 1 })
|
||||
|
||||
#define STATIC_CSTR_AS_OBJ(s) STRING_OBJ(STATIC_CSTR_AS_STRING(s))
|
||||
#define STATIC_CSTR_TO_OBJ(s) STRING_OBJ(STATIC_CSTR_TO_STRING(s))
|
||||
|
||||
// Helpers used by the generated msgpack-rpc api wrappers
|
||||
#define api_init_boolean
|
||||
#define api_init_integer
|
||||
|
@@ -795,7 +795,7 @@ void remote_ui_put(UI *ui, const char *cell)
|
||||
UIData *data = ui->data;
|
||||
data->client_col++;
|
||||
Array args = data->call_buf;
|
||||
ADD_C(args, STRING_OBJ(cstr_as_string((char *)cell)));
|
||||
ADD_C(args, CSTR_AS_OBJ((char *)cell));
|
||||
push_call(ui, "put", args);
|
||||
}
|
||||
|
||||
|
@@ -531,7 +531,7 @@ static void find_runtime_cb(char *fname, void *cookie)
|
||||
{
|
||||
Array *rv = (Array *)cookie;
|
||||
if (fname != NULL) {
|
||||
ADD(*rv, STRING_OBJ(cstr_to_string(fname)));
|
||||
ADD(*rv, CSTR_TO_OBJ(fname));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1383,7 +1383,7 @@ Dictionary nvim_get_mode(void)
|
||||
get_mode(modestr);
|
||||
bool blocked = input_blocking();
|
||||
|
||||
PUT(rv, "mode", STRING_OBJ(cstr_to_string(modestr)));
|
||||
PUT(rv, "mode", CSTR_TO_OBJ(modestr));
|
||||
PUT(rv, "blocking", BOOLEAN_OBJ(blocked));
|
||||
|
||||
return rv;
|
||||
@@ -1926,7 +1926,7 @@ Array nvim__inspect_cell(Integer grid, Integer row, Integer col, Arena *arena, E
|
||||
}
|
||||
ret = arena_array(arena, 3);
|
||||
size_t off = g->line_offset[(size_t)row] + (size_t)col;
|
||||
ADD_C(ret, STRING_OBJ(cstr_as_string((char *)g->chars[off])));
|
||||
ADD_C(ret, CSTR_AS_OBJ((char *)g->chars[off]));
|
||||
int attr = g->attrs[off];
|
||||
ADD_C(ret, DICTIONARY_OBJ(hl_get_attr_by_id(attr, true, arena, err)));
|
||||
// will not work first time
|
||||
@@ -2035,7 +2035,7 @@ Array nvim_get_mark(String name, Dictionary opts, Error *err)
|
||||
ADD(rv, INTEGER_OBJ(row));
|
||||
ADD(rv, INTEGER_OBJ(col));
|
||||
ADD(rv, INTEGER_OBJ(bufnr));
|
||||
ADD(rv, STRING_OBJ(cstr_to_string(filename)));
|
||||
ADD(rv, CSTR_TO_OBJ(filename));
|
||||
|
||||
if (allocated) {
|
||||
xfree(filename);
|
||||
|
@@ -502,7 +502,7 @@ Dictionary nvim_parse_expression(String expr, String flags, Boolean highlight, E
|
||||
};
|
||||
err_dict.items[0] = (KeyValuePair) {
|
||||
.key = STATIC_CSTR_TO_STRING("message"),
|
||||
.value = STRING_OBJ(cstr_to_string(east.err.msg)),
|
||||
.value = CSTR_TO_OBJ(east.err.msg),
|
||||
};
|
||||
if (east.err.arg == NULL) {
|
||||
err_dict.items[1] = (KeyValuePair) {
|
||||
@@ -539,7 +539,7 @@ Dictionary nvim_parse_expression(String expr, String flags, Boolean highlight, E
|
||||
chunk_arr.items[0] = INTEGER_OBJ((Integer)chunk.start.line);
|
||||
chunk_arr.items[1] = INTEGER_OBJ((Integer)chunk.start.col);
|
||||
chunk_arr.items[2] = INTEGER_OBJ((Integer)chunk.end_col);
|
||||
chunk_arr.items[3] = STRING_OBJ(cstr_to_string(chunk.group));
|
||||
chunk_arr.items[3] = CSTR_TO_OBJ(chunk.group);
|
||||
hl.items[i] = ARRAY_OBJ(chunk_arr);
|
||||
}
|
||||
ret.items[ret.size++] = (KeyValuePair) {
|
||||
@@ -616,7 +616,7 @@ Dictionary nvim_parse_expression(String expr, String flags, Boolean highlight, E
|
||||
kv_drop(ast_conv_stack, 1);
|
||||
ret_node->items[ret_node->size++] = (KeyValuePair) {
|
||||
.key = STATIC_CSTR_TO_STRING("type"),
|
||||
.value = STRING_OBJ(cstr_to_string(east_node_type_tab[node->type])),
|
||||
.value = CSTR_TO_OBJ(east_node_type_tab[node->type]),
|
||||
};
|
||||
Array start_array = {
|
||||
.items = xmalloc(2 * sizeof(start_array.items[0])),
|
||||
@@ -701,11 +701,11 @@ Dictionary nvim_parse_expression(String expr, String flags, Boolean highlight, E
|
||||
case kExprNodeComparison:
|
||||
ret_node->items[ret_node->size++] = (KeyValuePair) {
|
||||
.key = STATIC_CSTR_TO_STRING("cmp_type"),
|
||||
.value = STRING_OBJ(cstr_to_string(eltkn_cmp_type_tab[node->data.cmp.type])),
|
||||
.value = CSTR_TO_OBJ(eltkn_cmp_type_tab[node->data.cmp.type]),
|
||||
};
|
||||
ret_node->items[ret_node->size++] = (KeyValuePair) {
|
||||
.key = STATIC_CSTR_TO_STRING("ccs_strategy"),
|
||||
.value = STRING_OBJ(cstr_to_string(ccs_tab[node->data.cmp.ccs])),
|
||||
.value = CSTR_TO_OBJ(ccs_tab[node->data.cmp.ccs]),
|
||||
};
|
||||
ret_node->items[ret_node->size++] = (KeyValuePair) {
|
||||
.key = STATIC_CSTR_TO_STRING("invert"),
|
||||
|
@@ -267,7 +267,7 @@ Dictionary nvim_win_get_config(Window window, Error *err)
|
||||
PUT(rv, "bufpos", ARRAY_OBJ(pos));
|
||||
}
|
||||
}
|
||||
PUT(rv, "anchor", STRING_OBJ(cstr_to_string(float_anchor_str[config->anchor])));
|
||||
PUT(rv, "anchor", CSTR_TO_OBJ(float_anchor_str[config->anchor]));
|
||||
PUT(rv, "row", FLOAT_OBJ(config->row));
|
||||
PUT(rv, "col", FLOAT_OBJ(config->col));
|
||||
PUT(rv, "zindex", INTEGER_OBJ(config->zindex));
|
||||
@@ -283,7 +283,7 @@ Dictionary nvim_win_get_config(Window window, Error *err)
|
||||
char *hi_name = syn_id2name(hi_id);
|
||||
if (hi_name[0]) {
|
||||
ADD(tuple, STRING_OBJ(s));
|
||||
ADD(tuple, STRING_OBJ(cstr_to_string(hi_name)));
|
||||
ADD(tuple, CSTR_TO_OBJ(hi_name));
|
||||
ADD(border, ARRAY_OBJ(tuple));
|
||||
} else {
|
||||
ADD(border, STRING_OBJ(s));
|
||||
@@ -297,7 +297,7 @@ Dictionary nvim_win_get_config(Window window, Error *err)
|
||||
Array tuple = ARRAY_DICT_INIT;
|
||||
ADD(tuple, CSTR_TO_OBJ(title_datas.items[i].text));
|
||||
if (title_datas.items[i].hl_id > 0) {
|
||||
ADD(tuple, STRING_OBJ(cstr_to_string(syn_id2name(title_datas.items[i].hl_id))));
|
||||
ADD(tuple, CSTR_TO_OBJ(syn_id2name(title_datas.items[i].hl_id)));
|
||||
}
|
||||
ADD(titles, ARRAY_OBJ(tuple));
|
||||
}
|
||||
@@ -317,7 +317,7 @@ Dictionary nvim_win_get_config(Window window, Error *err)
|
||||
|
||||
const char *rel = (wp->w_floating && !config->external
|
||||
? float_relative_str[config->relative] : "");
|
||||
PUT(rv, "relative", STRING_OBJ(cstr_to_string(rel)));
|
||||
PUT(rv, "relative", CSTR_TO_OBJ(rel));
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
Reference in New Issue
Block a user