Merge pull request #27398 from bfredl/arena2

refactor(api): use arena for more stuff
This commit is contained in:
bfredl
2024-02-10 12:07:37 +01:00
committed by GitHub
22 changed files with 188 additions and 172 deletions

View File

@@ -96,15 +96,15 @@
/// - "belowright": |:belowright|.
/// - "topleft": |:topleft|.
/// - "botright": |:botright|.
Dictionary nvim_parse_cmd(String str, Dict(empty) *opts, Error *err)
Dict(cmd) nvim_parse_cmd(String str, Dict(empty) *opts, Arena *arena, Error *err)
FUNC_API_SINCE(10) FUNC_API_FAST
{
Dictionary result = ARRAY_DICT_INIT;
Dict(cmd) result = { 0 };
// Parse command line
exarg_T ea;
CmdParseInfo cmdinfo;
char *cmdline = string_to_cstr(str);
char *cmdline = arena_memdupz(arena, str.data, str.size);
const char *errormsg = NULL;
if (!parse_cmdline(cmdline, &ea, &cmdinfo, &errormsg)) {
@@ -124,22 +124,23 @@ Dictionary nvim_parse_cmd(String str, Dict(empty) *opts, Error *err)
// otherwise split arguments by whitespace.
if (ea.argt & EX_NOSPC) {
if (*ea.arg != NUL) {
ADD(args, STRING_OBJ(cstrn_to_string(ea.arg, length)));
args = arena_array(arena, 1);
ADD_C(args, STRING_OBJ(cstrn_as_string(ea.arg, length)));
}
} else {
size_t end = 0;
size_t len = 0;
char *buf = xcalloc(length, sizeof(char));
char *buf = arena_alloc(arena, length + 1, false);
bool done = false;
args = arena_array(arena, uc_nargs_upper_bound(ea.arg, length));
while (!done) {
done = uc_split_args_iter(ea.arg, length, &end, buf, &len);
if (len > 0) {
ADD(args, STRING_OBJ(cstrn_to_string(buf, len)));
ADD_C(args, STRING_OBJ(cstrn_as_string(buf, len)));
buf += len + 1;
}
}
xfree(buf);
}
ucmd_T *cmd = NULL;
@@ -149,40 +150,32 @@ Dictionary nvim_parse_cmd(String str, Dict(empty) *opts, Error *err)
cmd = USER_CMD_GA(&curbuf->b_ucmds, ea.useridx);
}
if (cmd != NULL) {
PUT(result, "cmd", CSTR_TO_OBJ(cmd->uc_name));
} else {
PUT(result, "cmd", CSTR_TO_OBJ(get_command_name(NULL, ea.cmdidx)));
}
char *name = (cmd != NULL ? cmd->uc_name : get_command_name(NULL, ea.cmdidx));
PUT_KEY(result, cmd, cmd, cstr_as_string(name));
if (ea.argt & EX_RANGE) {
Array range = ARRAY_DICT_INIT;
Array range = arena_array(arena, 2);
if (ea.addr_count > 0) {
if (ea.addr_count > 1) {
ADD(range, INTEGER_OBJ(ea.line1));
ADD_C(range, INTEGER_OBJ(ea.line1));
}
ADD(range, INTEGER_OBJ(ea.line2));
ADD_C(range, INTEGER_OBJ(ea.line2));
}
PUT(result, "range", ARRAY_OBJ(range));
PUT_KEY(result, cmd, range, range);
}
if (ea.argt & EX_COUNT) {
if (ea.addr_count > 0) {
PUT(result, "count", INTEGER_OBJ(ea.line2));
} else if (cmd != NULL) {
PUT(result, "count", INTEGER_OBJ(cmd->uc_def));
} else {
PUT(result, "count", INTEGER_OBJ(0));
}
Integer count = ea.addr_count > 0 ? ea.line2 : (cmd != NULL ? cmd->uc_def : 0);
PUT_KEY(result, cmd, count, count);
}
if (ea.argt & EX_REGSTR) {
char reg[2] = { (char)ea.regname, NUL };
PUT(result, "reg", CSTR_TO_OBJ(reg));
PUT_KEY(result, cmd, reg, CSTR_TO_ARENA_STR(arena, reg));
}
PUT(result, "bang", BOOLEAN_OBJ(ea.forceit));
PUT(result, "args", ARRAY_OBJ(args));
PUT_KEY(result, cmd, bang, ea.forceit);
PUT_KEY(result, cmd, args, args);
char nargs[2];
if (ea.argt & EX_EXTRA) {
@@ -201,9 +194,9 @@ Dictionary nvim_parse_cmd(String str, Dict(empty) *opts, Error *err)
nargs[0] = '0';
}
nargs[1] = '\0';
PUT(result, "nargs", CSTR_TO_OBJ(nargs));
PUT_KEY(result, cmd, nargs, CSTR_TO_ARENA_OBJ(arena, nargs));
const char *addr;
char *addr;
switch (ea.addr_type) {
case ADDR_LINES:
addr = "line";
@@ -233,38 +226,37 @@ Dictionary nvim_parse_cmd(String str, Dict(empty) *opts, Error *err)
addr = "?";
break;
}
PUT(result, "addr", CSTR_TO_OBJ(addr));
PUT(result, "nextcmd", CSTR_TO_OBJ(ea.nextcmd));
PUT_KEY(result, cmd, addr, CSTR_AS_OBJ(addr));
PUT_KEY(result, cmd, nextcmd, CSTR_AS_OBJ(ea.nextcmd));
Dictionary mods = ARRAY_DICT_INIT;
// TODO(bfredl): nested keydict would be nice..
Dictionary mods = arena_dict(arena, 20);
Dictionary filter = ARRAY_DICT_INIT;
PUT(filter, "pattern", cmdinfo.cmdmod.cmod_filter_pat
? CSTR_TO_OBJ(cmdinfo.cmdmod.cmod_filter_pat)
: STATIC_CSTR_TO_OBJ(""));
PUT(filter, "force", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_filter_force));
PUT(mods, "filter", DICTIONARY_OBJ(filter));
Dictionary filter = arena_dict(arena, 2);
PUT_C(filter, "pattern", CSTR_TO_ARENA_OBJ(arena, cmdinfo.cmdmod.cmod_filter_pat));
PUT_C(filter, "force", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_filter_force));
PUT_C(mods, "filter", DICTIONARY_OBJ(filter));
PUT(mods, "silent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SILENT));
PUT(mods, "emsg_silent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_ERRSILENT));
PUT(mods, "unsilent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_UNSILENT));
PUT(mods, "sandbox", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SANDBOX));
PUT(mods, "noautocmd", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_NOAUTOCMD));
PUT(mods, "tab", INTEGER_OBJ(cmdinfo.cmdmod.cmod_tab - 1));
PUT(mods, "verbose", INTEGER_OBJ(cmdinfo.cmdmod.cmod_verbose - 1));
PUT(mods, "browse", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_BROWSE));
PUT(mods, "confirm", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_CONFIRM));
PUT(mods, "hide", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_HIDE));
PUT(mods, "keepalt", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_KEEPALT));
PUT(mods, "keepjumps", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_KEEPJUMPS));
PUT(mods, "keepmarks", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_KEEPMARKS));
PUT(mods, "keeppatterns", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_KEEPPATTERNS));
PUT(mods, "lockmarks", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_LOCKMARKS));
PUT(mods, "noswapfile", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_NOSWAPFILE));
PUT(mods, "vertical", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_split & WSP_VERT));
PUT(mods, "horizontal", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_split & WSP_HOR));
PUT_C(mods, "silent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SILENT));
PUT_C(mods, "emsg_silent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_ERRSILENT));
PUT_C(mods, "unsilent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_UNSILENT));
PUT_C(mods, "sandbox", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SANDBOX));
PUT_C(mods, "noautocmd", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_NOAUTOCMD));
PUT_C(mods, "tab", INTEGER_OBJ(cmdinfo.cmdmod.cmod_tab - 1));
PUT_C(mods, "verbose", INTEGER_OBJ(cmdinfo.cmdmod.cmod_verbose - 1));
PUT_C(mods, "browse", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_BROWSE));
PUT_C(mods, "confirm", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_CONFIRM));
PUT_C(mods, "hide", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_HIDE));
PUT_C(mods, "keepalt", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_KEEPALT));
PUT_C(mods, "keepjumps", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_KEEPJUMPS));
PUT_C(mods, "keepmarks", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_KEEPMARKS));
PUT_C(mods, "keeppatterns", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_KEEPPATTERNS));
PUT_C(mods, "lockmarks", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_LOCKMARKS));
PUT_C(mods, "noswapfile", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_NOSWAPFILE));
PUT_C(mods, "vertical", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_split & WSP_VERT));
PUT_C(mods, "horizontal", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_split & WSP_HOR));
const char *split;
char *split;
if (cmdinfo.cmdmod.cmod_split & WSP_BOT) {
split = "botright";
} else if (cmdinfo.cmdmod.cmod_split & WSP_TOP) {
@@ -276,18 +268,17 @@ Dictionary nvim_parse_cmd(String str, Dict(empty) *opts, Error *err)
} else {
split = "";
}
PUT(mods, "split", CSTR_TO_OBJ(split));
PUT_C(mods, "split", CSTR_AS_OBJ(split));
PUT(result, "mods", DICTIONARY_OBJ(mods));
PUT_KEY(result, cmd, mods, mods);
Dictionary magic = ARRAY_DICT_INIT;
PUT(magic, "file", BOOLEAN_OBJ(cmdinfo.magic.file));
PUT(magic, "bar", BOOLEAN_OBJ(cmdinfo.magic.bar));
PUT(result, "magic", DICTIONARY_OBJ(magic));
Dictionary magic = arena_dict(arena, 2);
PUT_C(magic, "file", BOOLEAN_OBJ(cmdinfo.magic.file));
PUT_C(magic, "bar", BOOLEAN_OBJ(cmdinfo.magic.bar));
PUT_KEY(result, cmd, magic, magic);
undo_cmdmod(&cmdinfo.cmdmod);
end:
xfree(cmdline);
return result;
}

View File

@@ -514,11 +514,11 @@ static int64_t convert_index(int64_t index)
/// @param name Option name
/// @param[out] err Error details, if any
/// @return Option Information
Dictionary nvim_get_option_info(String name, Error *err)
Dictionary nvim_get_option_info(String name, Arena *arena, Error *err)
FUNC_API_SINCE(7)
FUNC_API_DEPRECATED_SINCE(11)
{
return get_vimoption(name, OPT_GLOBAL, curbuf, curwin, err);
return get_vimoption(name, OPT_GLOBAL, curbuf, curwin, arena, err);
}
/// Sets the global value of an option.

View File

@@ -263,10 +263,10 @@ void nvim_set_option_value(uint64_t channel_id, String name, Object value, Dict(
/// @see |nvim_get_commands()|
///
/// @return dictionary of all options
Dictionary nvim_get_all_options_info(Error *err)
Dictionary nvim_get_all_options_info(Arena *arena, Error *err)
FUNC_API_SINCE(7)
{
return get_all_vimoptions();
return get_all_vimoptions(arena);
}
/// Gets the option information for one option from arbitrary buffer or window
@@ -302,7 +302,7 @@ Dictionary nvim_get_all_options_info(Error *err)
/// Implies {scope} is "local".
/// @param[out] err Error details, if any
/// @return Option Information
Dictionary nvim_get_option_info2(String name, Dict(option) *opts, Error *err)
Dictionary nvim_get_option_info2(String name, Dict(option) *opts, Arena *arena, Error *err)
FUNC_API_SINCE(11)
{
OptIndex opt_idx = 0;
@@ -317,5 +317,5 @@ Dictionary nvim_get_option_info2(String name, Dict(option) *opts, Error *err)
buf_T *buf = (req_scope == kOptReqBuf) ? (buf_T *)from : curbuf;
win_T *win = (req_scope == kOptReqWin) ? (win_T *)from : curwin;
return get_vimoption(name, scope, buf, win, err);
return get_vimoption(name, scope, buf, win, arena, err);
}

View File

@@ -425,12 +425,12 @@ String cstrn_as_string(char *str, size_t maxsize)
/// @param str the C string to use
/// @return The resulting String, or an empty String if
/// str was NULL
String cstr_as_string(char *str) FUNC_ATTR_PURE
String cstr_as_string(const char *str) FUNC_ATTR_PURE
{
if (str == NULL) {
return (String)STRING_INIT;
}
return (String){ .data = str, .size = strlen(str) };
return (String){ .data = (char *)str, .size = strlen(str) };
}
/// Return the owned memory of a ga as a String

View File

@@ -32,7 +32,8 @@
#define CSTR_AS_OBJ(s) STRING_OBJ(cstr_as_string(s))
#define CSTR_TO_OBJ(s) STRING_OBJ(cstr_to_string(s))
#define CSTR_TO_ARENA_OBJ(arena, s) STRING_OBJ(arena_string(arena, cstr_as_string(s)))
#define CSTR_TO_ARENA_STR(arena, s) arena_string(arena, cstr_as_string(s))
#define CSTR_TO_ARENA_OBJ(arena, s) STRING_OBJ(CSTR_TO_ARENA_STR(arena, s))
#define BUFFER_OBJ(s) ((Object) { \
.type = kObjectTypeBuffer, \

View File

@@ -247,10 +247,9 @@ void nvim_ui_attach(uint64_t channel_id, Integer width, Integer height, Dictiona
void ui_attach(uint64_t channel_id, Integer width, Integer height, Boolean enable_rgb, Error *err)
FUNC_API_DEPRECATED_SINCE(1)
{
Dictionary opts = ARRAY_DICT_INIT;
PUT(opts, "rgb", BOOLEAN_OBJ(enable_rgb));
MAXSIZE_TEMP_DICT(opts, 1);
PUT_C(opts, "rgb", BOOLEAN_OBJ(enable_rgb));
nvim_ui_attach(channel_id, width, height, opts, err);
api_free_dictionary(opts);
}
/// Tells the nvim server if focus was gained or lost by the GUI
@@ -789,7 +788,7 @@ void remote_ui_hl_attr_define(UI *ui, Integer id, HlAttrs rgb_attrs, HlAttrs cte
// system. So we add them here.
if (rgb_attrs.url >= 0) {
const char *url = hl_get_url((uint32_t)rgb_attrs.url);
PUT_C(rgb, "url", STRING_OBJ(cstr_as_string((char *)url)));
PUT_C(rgb, "url", CSTR_AS_OBJ(url));
}
ADD_C(args, DICTIONARY_OBJ(rgb));
@@ -857,7 +856,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, CSTR_AS_OBJ((char *)cell));
ADD_C(args, CSTR_AS_OBJ(cell));
push_call(ui, "put", args);
}
@@ -1113,9 +1112,3 @@ void remote_ui_event(UI *ui, char *name, Array args)
free_ret:
arena_mem_free(arena_finish(&arena));
}
void remote_ui_inspect(UI *ui, Dictionary *info)
{
UIData *data = ui->data;
PUT(*info, "chan", INTEGER_OBJ((Integer)data->channel_id));
}

View File

@@ -1866,10 +1866,10 @@ Dictionary nvim__stats(Arena *arena)
/// - "rgb" true if the UI uses RGB colors (false implies |cterm-colors|)
/// - "ext_..." Requested UI extensions, see |ui-option|
/// - "chan" |channel-id| of remote UI
Array nvim_list_uis(void)
Array nvim_list_uis(Arena *arena)
FUNC_API_SINCE(4)
{
return ui_array();
return ui_array(arena);
}
/// Gets the immediate children of process `pid`.
@@ -2005,7 +2005,7 @@ Array nvim__inspect_cell(Integer grid, Integer row, Integer col, Arena *arena, E
ADD_C(ret, DICTIONARY_OBJ(hl_get_attr_by_id(attr, true, arena, err)));
// will not work first time
if (!highlight_use_hlstate()) {
ADD_C(ret, ARRAY_OBJ(hl_inspect(attr)));
ADD_C(ret, ARRAY_OBJ(hl_inspect(attr, arena)));
}
return ret;
}
@@ -2289,7 +2289,7 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Arena *
opts->use_winbar, stc_hl_id);
PUT_C(hl_info, "start", INTEGER_OBJ(0));
PUT_C(hl_info, "group", CSTR_AS_OBJ((char *)grpname));
PUT_C(hl_info, "group", CSTR_AS_OBJ(grpname));
ADD_C(hl_values, DICTIONARY_OBJ(hl_info));
}
@@ -2308,7 +2308,7 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Arena *
snprintf(user_group, sizeof(user_group), "User%d", sp->userhl);
grpname = arena_memdupz(arena, user_group, strlen(user_group));
}
PUT_C(hl_info, "group", CSTR_AS_OBJ((char *)grpname));
PUT_C(hl_info, "group", CSTR_AS_OBJ(grpname));
ADD_C(hl_values, DICTIONARY_OBJ(hl_info));
}
PUT_C(result, "highlights", ARRAY_OBJ(hl_values));

View File

@@ -626,7 +626,7 @@ Dict(win_config) nvim_win_get_config(Window window, Arena *arena, Error *err)
PUT_KEY_X(rv, bufpos, pos);
}
}
PUT_KEY_X(rv, anchor, cstr_as_string((char *)float_anchor_str[config->anchor]));
PUT_KEY_X(rv, anchor, cstr_as_string(float_anchor_str[config->anchor]));
PUT_KEY_X(rv, row, config->row);
PUT_KEY_X(rv, col, config->col);
PUT_KEY_X(rv, zindex, config->zindex);
@@ -659,12 +659,12 @@ Dict(win_config) nvim_win_get_config(Window window, Arena *arena, Error *err)
PUT_KEY_X(rv, width, wp->w_width);
PUT_KEY_X(rv, height, wp->w_height);
WinSplit split = win_split_dir(wp);
PUT_KEY_X(rv, split, cstr_as_string((char *)win_split_str[split]));
PUT_KEY_X(rv, split, cstr_as_string(win_split_str[split]));
}
const char *rel = (wp->w_floating && !config->external
? float_relative_str[config->relative] : "");
PUT_KEY_X(rv, relative, cstr_as_string((char *)rel));
PUT_KEY_X(rv, relative, cstr_as_string(rel));
return rv;
}

View File

@@ -121,7 +121,7 @@ static void augroup_map_del(int id, const char *name)
{
if (name != NULL) {
String key;
map_del(String, int)(&map_augroup_name_to_id, cstr_as_string((char *)name), &key);
map_del(String, int)(&map_augroup_name_to_id, cstr_as_string(name), &key);
api_free_string(key);
}
if (id > 0) {
@@ -476,7 +476,7 @@ void augroup_del(char *name, bool stupid_legacy_mode)
int augroup_find(const char *name)
FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
{
int existing_id = map_get(String, int)(&map_augroup_name_to_id, cstr_as_string((char *)name));
int existing_id = map_get(String, int)(&map_augroup_name_to_id, cstr_as_string(name));
if (existing_id == AUGROUP_DELETED) {
return existing_id;
}

View File

@@ -977,12 +977,12 @@ void decor_to_dict_legacy(Dictionary *dict, DecorInline decor, bool hl_name, Are
}
if (sh_hl.url != NULL) {
PUT_C(*dict, "url", STRING_OBJ(cstr_as_string((char *)sh_hl.url)));
PUT_C(*dict, "url", STRING_OBJ(cstr_as_string(sh_hl.url)));
}
if (virt_text) {
if (virt_text->hl_mode) {
PUT_C(*dict, "hl_mode", CSTR_AS_OBJ((char *)hl_mode_str[virt_text->hl_mode]));
PUT_C(*dict, "hl_mode", CSTR_AS_OBJ(hl_mode_str[virt_text->hl_mode]));
}
Array chunks = virt_text_to_array(virt_text->data.virt_text, hl_name, arena);
@@ -992,7 +992,7 @@ void decor_to_dict_legacy(Dictionary *dict, DecorInline decor, bool hl_name, Are
if (virt_text->pos == kVPosWinCol) {
PUT_C(*dict, "virt_text_win_col", INTEGER_OBJ(virt_text->col));
}
PUT_C(*dict, "virt_text_pos", CSTR_AS_OBJ((char *)virt_text_pos_str[virt_text->pos]));
PUT_C(*dict, "virt_text_pos", CSTR_AS_OBJ(virt_text_pos_str[virt_text->pos]));
priority = virt_text->priority;
}

View File

@@ -2093,8 +2093,8 @@ static void f_feedkeys(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
flags = tv_get_string_buf(&argvars[1], nbuf);
}
nvim_feedkeys(cstr_as_string((char *)keys),
cstr_as_string((char *)flags), true);
nvim_feedkeys(cstr_as_string(keys),
cstr_as_string(flags), true);
}
/// "filereadable()" function
@@ -4528,7 +4528,7 @@ static void f_luaeval(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
return;
}
nlua_typval_eval(cstr_as_string((char *)str), &argvars[1], rettv);
nlua_typval_eval(cstr_as_string(str), &argvars[1], rettv);
}
static void find_some_match(typval_T *const argvars, typval_T *const rettv,

View File

@@ -116,12 +116,13 @@ retry: {}
// new attr id, send event to remote ui:s
int id = (int)k;
Array inspect = hl_inspect(id);
Arena arena = ARENA_EMPTY;
Array inspect = hl_inspect(id, &arena);
// Note: internally we don't distinguish between cterm and rgb attributes,
// remote_ui_hl_attr_define will however.
ui_call_hl_attr_define(id, entry.attr, entry.attr, inspect);
api_free_array(inspect);
arena_mem_free(arena_finish(&arena));
return id;
}
@@ -129,13 +130,14 @@ retry: {}
void ui_send_all_hls(UI *ui)
{
for (size_t i = 1; i < set_size(&attr_entries); i++) {
Array inspect = hl_inspect((int)i);
Arena arena = ARENA_EMPTY;
Array inspect = hl_inspect((int)i, &arena);
HlAttrs attr = attr_entry(i).attr;
remote_ui_hl_attr_define(ui, (Integer)i, attr, attr, inspect);
api_free_array(inspect);
arena_mem_free(arena_finish(&arena));
}
for (size_t hlf = 0; hlf < HLF_COUNT; hlf++) {
remote_ui_hl_group_set(ui, cstr_as_string((char *)hlf_names[hlf]),
remote_ui_hl_group_set(ui, cstr_as_string(hlf_names[hlf]),
highlight_attr[hlf]);
}
}
@@ -204,7 +206,7 @@ int ns_get_hl(NS *ns_hl, int hl_id, bool link, bool nodefault)
if (!valid_item && p->hl_def != LUA_NOREF && !recursive) {
MAXSIZE_TEMP_ARRAY(args, 3);
ADD_C(args, INTEGER_OBJ((Integer)ns_id));
ADD_C(args, CSTR_TO_OBJ(syn_id2name(hl_id)));
ADD_C(args, CSTR_AS_OBJ(syn_id2name(hl_id)));
ADD_C(args, BOOLEAN_OBJ(link));
// TODO(bfredl): preload the "global" attr dict?
@@ -1176,17 +1178,30 @@ int object_to_color(Object val, char *key, bool rgb, Error *err)
}
}
Array hl_inspect(int attr)
Array hl_inspect(int attr, Arena *arena)
{
// TODO(bfredl): use arena allocation
Array ret = ARRAY_DICT_INIT;
if (hlstate_active) {
hl_inspect_impl(&ret, attr);
if (!hlstate_active) {
return (Array)ARRAY_DICT_INIT;
}
Array ret = arena_array(arena, hl_inspect_size(attr));
hl_inspect_impl(&ret, attr, arena);
return ret;
}
static void hl_inspect_impl(Array *arr, int attr)
static size_t hl_inspect_size(int attr)
{
if (attr <= 0 || attr >= (int)set_size(&attr_entries)) {
return 0;
}
HlEntry e = attr_entry(attr);
if (e.kind == kHlCombine || e.kind == kHlBlend || e.kind == kHlBlendThrough) {
return hl_inspect_size(e.id1) + hl_inspect_size(e.id2);
}
return 1;
}
static void hl_inspect_impl(Array *arr, int attr, Arena *arena)
{
Dictionary item = ARRAY_DICT_INIT;
if (attr <= 0 || attr >= (int)set_size(&attr_entries)) {
@@ -1196,35 +1211,36 @@ static void hl_inspect_impl(Array *arr, int attr)
HlEntry e = attr_entry(attr);
switch (e.kind) {
case kHlSyntax:
PUT(item, "kind", CSTR_TO_OBJ("syntax"));
PUT(item, "hi_name",
CSTR_TO_OBJ(syn_id2name(e.id1)));
item = arena_dict(arena, 3);
PUT_C(item, "kind", CSTR_AS_OBJ("syntax"));
PUT_C(item, "hi_name", CSTR_AS_OBJ(syn_id2name(e.id1)));
break;
case kHlUI:
PUT(item, "kind", CSTR_TO_OBJ("ui"));
item = arena_dict(arena, 4);
PUT_C(item, "kind", CSTR_AS_OBJ("ui"));
const char *ui_name = (e.id1 == -1) ? "Normal" : hlf_names[e.id1];
PUT(item, "ui_name", CSTR_TO_OBJ(ui_name));
PUT(item, "hi_name",
CSTR_TO_OBJ(syn_id2name(e.id2)));
PUT_C(item, "ui_name", CSTR_AS_OBJ(ui_name));
PUT_C(item, "hi_name", CSTR_AS_OBJ(syn_id2name(e.id2)));
break;
case kHlTerminal:
PUT(item, "kind", CSTR_TO_OBJ("term"));
item = arena_dict(arena, 2);
PUT_C(item, "kind", CSTR_AS_OBJ("term"));
break;
case kHlCombine:
case kHlBlend:
case kHlBlendThrough:
// attribute combination is associative, so flatten to an array
hl_inspect_impl(arr, e.id1);
hl_inspect_impl(arr, e.id2);
hl_inspect_impl(arr, e.id1, arena);
hl_inspect_impl(arr, e.id2, arena);
return;
case kHlUnknown:
case kHlInvalid:
return;
}
PUT(item, "id", INTEGER_OBJ(attr));
ADD(*arr, DICTIONARY_OBJ(item));
PUT_C(item, "id", INTEGER_OBJ(attr));
ADD_C(*arr, DICTIONARY_OBJ(item));
}

View File

@@ -2241,7 +2241,7 @@ void highlight_changed(void)
HlAttrs attrs = syn_attr2entry(highlight_attr[hlf]);
msg_grid.blending = attrs.hl_blend > -1;
}
ui_call_hl_group_set(cstr_as_string((char *)hlf_names[hlf]),
ui_call_hl_group_set(cstr_as_string(hlf_names[hlf]),
highlight_attr[hlf]);
highlight_attr_last[hlf] = highlight_attr[hlf];
}

View File

@@ -3017,7 +3017,7 @@ void msg_ext_ui_flush(void)
msg_ext_emit_chunk();
if (msg_ext_chunks.size > 0) {
ui_call_msg_show(cstr_as_string((char *)msg_ext_kind),
ui_call_msg_show(cstr_as_string(msg_ext_kind),
msg_ext_chunks, msg_ext_overwrite);
if (!msg_ext_overwrite) {
msg_ext_visible++;

View File

@@ -547,7 +547,7 @@ static void send_error(Channel *chan, MsgpackRpcRequestHandler handler, MessageT
static void send_request(Channel *channel, uint32_t id, const char *name, Array args)
{
const String method = cstr_as_string((char *)name);
const String method = cstr_as_string(name);
channel_write(channel, serialize_request(channel->id,
id,
method,
@@ -558,7 +558,7 @@ static void send_request(Channel *channel, uint32_t id, const char *name, Array
static void send_event(Channel *channel, const char *name, Array args)
{
const String method = cstr_as_string((char *)name);
const String method = cstr_as_string(name);
channel_write(channel, serialize_request(channel->id,
0,
method,
@@ -583,7 +583,7 @@ static void broadcast_event(const char *name, Array args)
goto end;
}
const String method = cstr_as_string((char *)name);
const String method = cstr_as_string(name);
WBuffer *buffer = serialize_request(0,
0,
method,

View File

@@ -6314,32 +6314,33 @@ int get_sidescrolloff_value(win_T *wp)
return (int)(wp->w_p_siso < 0 ? p_siso : wp->w_p_siso);
}
Dictionary get_vimoption(String name, int scope, buf_T *buf, win_T *win, Error *err)
Dictionary get_vimoption(String name, int scope, buf_T *buf, win_T *win, Arena *arena, Error *err)
{
OptIndex opt_idx = find_option_len(name.data, name.size);
VALIDATE_S(opt_idx != kOptInvalid, "option (not found)", name.data, {
return (Dictionary)ARRAY_DICT_INIT;
});
return vimoption2dict(&options[opt_idx], scope, buf, win);
return vimoption2dict(&options[opt_idx], scope, buf, win, arena);
}
Dictionary get_all_vimoptions(void)
Dictionary get_all_vimoptions(Arena *arena)
{
Dictionary retval = ARRAY_DICT_INIT;
Dictionary retval = arena_dict(arena, kOptIndexCount);
for (OptIndex opt_idx = 0; opt_idx < kOptIndexCount; opt_idx++) {
Dictionary opt_dict = vimoption2dict(&options[opt_idx], OPT_GLOBAL, curbuf, curwin);
PUT(retval, options[opt_idx].fullname, DICTIONARY_OBJ(opt_dict));
Dictionary opt_dict = vimoption2dict(&options[opt_idx], OPT_GLOBAL, curbuf, curwin, arena);
PUT_C(retval, options[opt_idx].fullname, DICTIONARY_OBJ(opt_dict));
}
return retval;
}
static Dictionary vimoption2dict(vimoption_T *opt, int req_scope, buf_T *buf, win_T *win)
static Dictionary vimoption2dict(vimoption_T *opt, int req_scope, buf_T *buf, win_T *win,
Arena *arena)
{
Dictionary dict = ARRAY_DICT_INIT;
Dictionary dict = arena_dict(arena, 13);
PUT(dict, "name", CSTR_TO_OBJ(opt->fullname));
PUT(dict, "shortname", CSTR_TO_OBJ(opt->shortname));
PUT_C(dict, "name", CSTR_AS_OBJ(opt->fullname));
PUT_C(dict, "shortname", CSTR_AS_OBJ(opt->shortname));
const char *scope;
if (opt->indir & PV_BUF) {
@@ -6350,14 +6351,14 @@ static Dictionary vimoption2dict(vimoption_T *opt, int req_scope, buf_T *buf, wi
scope = "global";
}
PUT(dict, "scope", CSTR_TO_OBJ(scope));
PUT_C(dict, "scope", CSTR_AS_OBJ(scope));
// welcome to the jungle
PUT(dict, "global_local", BOOLEAN_OBJ(opt->indir & PV_BOTH));
PUT(dict, "commalist", BOOLEAN_OBJ(opt->flags & P_COMMA));
PUT(dict, "flaglist", BOOLEAN_OBJ(opt->flags & P_FLAGLIST));
PUT_C(dict, "global_local", BOOLEAN_OBJ(opt->indir & PV_BOTH));
PUT_C(dict, "commalist", BOOLEAN_OBJ(opt->flags & P_COMMA));
PUT_C(dict, "flaglist", BOOLEAN_OBJ(opt->flags & P_FLAGLIST));
PUT(dict, "was_set", BOOLEAN_OBJ(opt->flags & P_WAS_SET));
PUT_C(dict, "was_set", BOOLEAN_OBJ(opt->flags & P_WAS_SET));
LastSet last_set = { .channel_id = 0 };
if (req_scope == OPT_GLOBAL) {
@@ -6375,16 +6376,16 @@ static Dictionary vimoption2dict(vimoption_T *opt, int req_scope, buf_T *buf, wi
}
}
PUT(dict, "last_set_sid", INTEGER_OBJ(last_set.script_ctx.sc_sid));
PUT(dict, "last_set_linenr", INTEGER_OBJ(last_set.script_ctx.sc_lnum));
PUT(dict, "last_set_chan", INTEGER_OBJ((int64_t)last_set.channel_id));
PUT_C(dict, "last_set_sid", INTEGER_OBJ(last_set.script_ctx.sc_sid));
PUT_C(dict, "last_set_linenr", INTEGER_OBJ(last_set.script_ctx.sc_lnum));
PUT_C(dict, "last_set_chan", INTEGER_OBJ((int64_t)last_set.channel_id));
// TODO(bfredl): do you even nocp?
OptVal def = optval_from_varp(get_opt_idx(opt), &opt->def_val);
PUT(dict, "type", CSTR_TO_OBJ(optval_type_get_name(def.type)));
PUT(dict, "default", optval_as_object(optval_copy(def)));
PUT(dict, "allows_duplicates", BOOLEAN_OBJ(!(opt->flags & P_NODUP)));
PUT_C(dict, "type", CSTR_AS_OBJ(optval_type_get_name(def.type)));
PUT_C(dict, "default", optval_as_object(def));
PUT_C(dict, "allows_duplicates", BOOLEAN_OBJ(!(opt->flags & P_NODUP)));
return dict;
}

View File

@@ -95,7 +95,7 @@ int os_chdir(const char *path)
}
int err = uv_chdir(path);
if (err == 0) {
ui_call_chdir(cstr_as_string((char *)path));
ui_call_chdir(cstr_as_string(path));
}
return err;
}

View File

@@ -488,7 +488,7 @@ void pum_redraw(void)
if (ui_has(kUIMultigrid)) {
const char *anchor = pum_above ? "SW" : "NW";
int row_off = pum_above ? -pum_height : 0;
ui_call_win_float_pos(pum_grid.handle, -1, cstr_as_string((char *)anchor), pum_anchor_grid,
ui_call_win_float_pos(pum_grid.handle, -1, cstr_as_string(anchor), pum_anchor_grid,
pum_row - row_off, pum_left_col, false, pum_grid.zindex);
}

View File

@@ -84,7 +84,7 @@ static int64_t group_get_ns(const char *group)
return UINT32_MAX; // All namespaces
}
// Specific or non-existing namespace
int ns = map_get(String, int)(&namespace_ids, cstr_as_string((char *)group));
int ns = map_get(String, int)(&namespace_ids, cstr_as_string(group));
return ns ? ns : -1;
}

View File

@@ -229,7 +229,7 @@ void ui_refresh(void)
}
ui_ext[i] = ext_widgets[i];
if (i < kUIGlobalCount) {
ui_call_option_set(cstr_as_string((char *)ui_ext_names[i]),
ui_call_option_set(cstr_as_string(ui_ext_names[i]),
BOOLEAN_OBJ(ext_widgets[i]));
}
}
@@ -451,8 +451,7 @@ void ui_set_ext_option(UI *ui, UIExtension ext, bool active)
return;
}
if (ui_ext_names[ext][0] != '_' || active) {
remote_ui_option_set(ui, cstr_as_string((char *)ui_ext_names[ext]),
BOOLEAN_OBJ(active));
remote_ui_option_set(ui, cstr_as_string(ui_ext_names[ext]), BOOLEAN_OBJ(active));
}
if (ext == kUITermColors) {
ui_default_colors_set();
@@ -646,34 +645,35 @@ bool ui_has(UIExtension ext)
return ui_ext[ext];
}
Array ui_array(void)
Array ui_array(Arena *arena)
{
Array all_uis = ARRAY_DICT_INIT;
Array all_uis = arena_array(arena, ui_count);
for (size_t i = 0; i < ui_count; i++) {
UI *ui = uis[i];
Dictionary info = ARRAY_DICT_INIT;
PUT(info, "width", INTEGER_OBJ(ui->width));
PUT(info, "height", INTEGER_OBJ(ui->height));
PUT(info, "rgb", BOOLEAN_OBJ(ui->rgb));
PUT(info, "override", BOOLEAN_OBJ(ui->override));
Dictionary info = arena_dict(arena, 10 + kUIExtCount);
PUT_C(info, "width", INTEGER_OBJ(ui->width));
PUT_C(info, "height", INTEGER_OBJ(ui->height));
PUT_C(info, "rgb", BOOLEAN_OBJ(ui->rgb));
PUT_C(info, "override", BOOLEAN_OBJ(ui->override));
// TUI fields. (`stdin_fd` is intentionally omitted.)
PUT(info, "term_name", CSTR_TO_OBJ(ui->term_name));
PUT_C(info, "term_name", CSTR_AS_OBJ(ui->term_name));
// term_background is deprecated. Populate with an empty string
PUT(info, "term_background", CSTR_TO_OBJ(""));
PUT_C(info, "term_background", STATIC_CSTR_AS_OBJ(""));
PUT(info, "term_colors", INTEGER_OBJ(ui->term_colors));
PUT(info, "stdin_tty", BOOLEAN_OBJ(ui->stdin_tty));
PUT(info, "stdout_tty", BOOLEAN_OBJ(ui->stdout_tty));
PUT_C(info, "term_colors", INTEGER_OBJ(ui->term_colors));
PUT_C(info, "stdin_tty", BOOLEAN_OBJ(ui->stdin_tty));
PUT_C(info, "stdout_tty", BOOLEAN_OBJ(ui->stdout_tty));
for (UIExtension j = 0; j < kUIExtCount; j++) {
if (ui_ext_names[j][0] != '_' || ui->ui_ext[j]) {
PUT(info, ui_ext_names[j], BOOLEAN_OBJ(ui->ui_ext[j]));
PUT_C(info, (char *)ui_ext_names[j], BOOLEAN_OBJ(ui->ui_ext[j]));
}
}
remote_ui_inspect(ui, &info);
ADD(all_uis, DICTIONARY_OBJ(info));
PUT_C(info, "chan", INTEGER_OBJ((Integer)ui->data->channel_id));
ADD_C(all_uis, DICTIONARY_OBJ(info));
}
return all_uis;
}

View File

@@ -1136,6 +1136,20 @@ bool uc_split_args_iter(const char *arg, size_t arglen, size_t *end, char *buf,
return true;
}
size_t uc_nargs_upper_bound(const char *arg, size_t arglen)
{
bool was_white = true; // space before first arg
size_t nargs = 0;
for (size_t i = 0; i < arglen; i++) {
bool is_white = ascii_iswhite(arg[i]);
if (was_white && !is_white) {
nargs++;
}
was_white = is_white;
}
return nargs;
}
/// split and quote args for <f-args>
static char *uc_split_args(const char *arg, char **args, const size_t *arglens, size_t argc,
size_t *lenp)

View File

@@ -796,7 +796,7 @@ void ui_ext_win_position(win_T *wp, bool validate)
wp->w_grid_alloc.zindex = wp->w_float_config.zindex;
if (ui_has(kUIMultigrid)) {
String anchor = cstr_as_string((char *)float_anchor_str[c.anchor]);
String anchor = cstr_as_string(float_anchor_str[c.anchor]);
if (!c.hide) {
ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor,
grid->handle, row, col, c.focusable,