refactor(api): complete conversion from Dictionary to Dict(opts) (#26365)

This commit is contained in:
Riccardo Mazzarini
2023-12-05 12:33:57 +01:00
committed by GitHub
parent 32c8f951bb
commit 0b74ad0a64
11 changed files with 121 additions and 150 deletions

View File

@@ -152,7 +152,7 @@ Integer nvim_buf_line_count(Buffer buffer, Error *err)
/// @return False if attach failed (invalid parameter, or buffer isn't loaded);
/// otherwise True. TODO: LUA_API_NO_EVAL
Boolean nvim_buf_attach(uint64_t channel_id, Buffer buffer, Boolean send_buffer,
DictionaryOf(LuaRef) opts, Error *err)
Dict(buf_attach) *opts, Error *err)
FUNC_API_SINCE(4)
{
buf_T *buf = find_buffer_by_handle(buffer, err);
@@ -161,64 +161,40 @@ Boolean nvim_buf_attach(uint64_t channel_id, Buffer buffer, Boolean send_buffer,
return false;
}
bool is_lua = (channel_id == LUA_INTERNAL_CALL);
BufUpdateCallbacks cb = BUF_UPDATE_CALLBACKS_INIT;
struct {
const char *name;
LuaRef *dest;
} cbs[] = {
{ "on_lines", &cb.on_lines },
{ "on_bytes", &cb.on_bytes },
{ "on_changedtick", &cb.on_changedtick },
{ "on_detach", &cb.on_detach },
{ "on_reload", &cb.on_reload },
{ NULL, NULL },
};
for (size_t i = 0; i < opts.size; i++) {
String k = opts.items[i].key;
Object *v = &opts.items[i].value;
bool key_used = false;
if (is_lua) {
for (size_t j = 0; cbs[j].name; j++) {
if (strequal(cbs[j].name, k.data)) {
VALIDATE_T(cbs[j].name, kObjectTypeLuaRef, v->type, {
goto error;
});
*(cbs[j].dest) = v->data.luaref;
v->data.luaref = LUA_NOREF;
key_used = true;
break;
}
}
if (key_used) {
continue;
} else if (strequal("utf_sizes", k.data)) {
VALIDATE_T("utf_sizes", kObjectTypeBoolean, v->type, {
goto error;
});
cb.utf_sizes = v->data.boolean;
key_used = true;
} else if (strequal("preview", k.data)) {
VALIDATE_T("preview", kObjectTypeBoolean, v->type, {
goto error;
});
cb.preview = v->data.boolean;
key_used = true;
}
if (channel_id == LUA_INTERNAL_CALL) {
if (HAS_KEY(opts, buf_attach, on_lines)) {
cb.on_lines = opts->on_lines;
opts->on_lines = LUA_NOREF;
}
VALIDATE_S(key_used, "'opts' key", k.data, {
goto error;
});
if (HAS_KEY(opts, buf_attach, on_bytes)) {
cb.on_bytes = opts->on_bytes;
opts->on_bytes = LUA_NOREF;
}
if (HAS_KEY(opts, buf_attach, on_changedtick)) {
cb.on_changedtick = opts->on_changedtick;
opts->on_changedtick = LUA_NOREF;
}
if (HAS_KEY(opts, buf_attach, on_detach)) {
cb.on_detach = opts->on_detach;
opts->on_detach = LUA_NOREF;
}
if (HAS_KEY(opts, buf_attach, on_reload)) {
cb.on_reload = opts->on_reload;
opts->on_reload = LUA_NOREF;
}
cb.utf_sizes = opts->utf_sizes;
cb.preview = opts->preview;
}
return buf_updates_register(buf, channel_id, cb, send_buffer);
error:
buffer_update_callbacks_free(cb);
return false;
}
/// Deactivates buffer-update events on the channel.
@@ -781,16 +757,12 @@ early_end:
ArrayOf(String) nvim_buf_get_text(uint64_t channel_id, Buffer buffer,
Integer start_row, Integer start_col,
Integer end_row, Integer end_col,
Dictionary opts, lua_State *lstate,
Dict(empty) *opts, lua_State *lstate,
Error *err)
FUNC_API_SINCE(9)
{
Array rv = ARRAY_DICT_INIT;
VALIDATE((opts.size == 0), "%s", "opts dict isn't empty", {
return rv;
});
buf_T *buf = find_buffer_by_handle(buffer, err);
if (!buf) {
@@ -1081,7 +1053,7 @@ Boolean nvim_buf_is_loaded(Buffer buffer)
/// @param opts Optional parameters. Keys:
/// - force: Force deletion and ignore unsaved changes.
/// - unload: Unloaded only, do not delete. See |:bunload|
void nvim_buf_delete(Buffer buffer, Dictionary opts, Error *err)
void nvim_buf_delete(Buffer buffer, Dict(buf_delete) *opts, Error *err)
FUNC_API_SINCE(7)
FUNC_API_TEXTLOCK
{
@@ -1091,25 +1063,9 @@ void nvim_buf_delete(Buffer buffer, Dictionary opts, Error *err)
return;
}
bool force = false;
bool unload = false;
for (size_t i = 0; i < opts.size; i++) {
String k = opts.items[i].key;
Object v = opts.items[i].value;
if (strequal("force", k.data)) {
force = api_object_to_bool(v, "force", false, err);
} else if (strequal("unload", k.data)) {
unload = api_object_to_bool(v, "unload", false, err);
} else {
VALIDATE_S(false, "'opts' key", k.data, {
return;
});
}
}
bool force = opts->force;
if (ERROR_SET(err)) {
return;
}
bool unload = opts->unload;
int result = do_buffer(unload ? DOBUF_UNLOAD : DOBUF_WIPE,
DOBUF_FIRST,
@@ -1193,7 +1149,7 @@ Boolean nvim_buf_del_mark(Buffer buffer, String name, Error *err)
/// @return true if the mark was set, else false.
/// @see |nvim_buf_del_mark()|
/// @see |nvim_buf_get_mark()|
Boolean nvim_buf_set_mark(Buffer buffer, String name, Integer line, Integer col, Dictionary opts,
Boolean nvim_buf_set_mark(Buffer buffer, String name, Integer line, Integer col, Dict(empty) *opts,
Error *err)
FUNC_API_SINCE(8)
{

View File

@@ -94,15 +94,11 @@
/// - "belowright": |:belowright|.
/// - "topleft": |:topleft|.
/// - "botright": |:botright|.
Dictionary nvim_parse_cmd(String str, Dictionary opts, Error *err)
Dictionary nvim_parse_cmd(String str, Dict(empty) *opts, Error *err)
FUNC_API_SINCE(10) FUNC_API_FAST
{
Dictionary result = ARRAY_DICT_INIT;
VALIDATE((opts.size == 0), "%s", "opts dict isn't empty", {
return result;
});
// Parse command line
exarg_T ea;
CmdParseInfo cmdinfo;

View File

@@ -126,7 +126,7 @@ void nvim_buf_clear_highlight(Buffer buffer, Integer ns_id, Integer line_start,
/// @param[out] err Error details, if any
/// @return The ns_id that was used
Integer nvim_buf_set_virtual_text(Buffer buffer, Integer src_id, Integer line, Array chunks,
Dictionary opts, Error *err)
Dict(empty) *opts, Error *err)
FUNC_API_SINCE(5)
FUNC_API_DEPRECATED_SINCE(8)
{
@@ -140,11 +140,6 @@ Integer nvim_buf_set_virtual_text(Buffer buffer, Integer src_id, Integer line, A
return 0;
}
if (opts.size > 0) {
api_set_error(err, kErrorTypeValidation, "opts dict isn't empty");
return 0;
}
uint32_t ns_id = src2ns(&src_id);
int width;

View File

@@ -2,6 +2,7 @@
#include <stdint.h> // IWYU pragma: keep
#include "nvim/api/keysets_defs.h" // IWYU pragma: keep
#include "nvim/api/private/defs.h" // IWYU pragma: keep
#ifdef INCLUDE_GENERATED_DECLARATIONS

View File

@@ -187,7 +187,7 @@ static Array extmark_to_array(MTPair extmark, bool id, bool add_dict, bool hl_na
/// @return 0-indexed (row, col) tuple or empty list () if extmark id was
/// absent
ArrayOf(Integer) nvim_buf_get_extmark_by_id(Buffer buffer, Integer ns_id,
Integer id, Dictionary opts,
Integer id, Dict(get_extmark) *opts,
Error *err)
FUNC_API_SINCE(7)
{
@@ -203,27 +203,9 @@ ArrayOf(Integer) nvim_buf_get_extmark_by_id(Buffer buffer, Integer ns_id,
return rv;
});
bool details = false;
bool hl_name = true;
for (size_t i = 0; i < opts.size; i++) {
String k = opts.items[i].key;
Object *v = &opts.items[i].value;
if (strequal("details", k.data)) {
details = api_object_to_bool(*v, "details", false, err);
if (ERROR_SET(err)) {
return rv;
}
} else if (strequal("hl_name", k.data)) {
hl_name = api_object_to_bool(*v, "hl_name", false, err);
if (ERROR_SET(err)) {
return rv;
}
} else {
VALIDATE_S(false, "'opts' key", k.data, {
return rv;
});
}
}
bool details = opts->details;
bool hl_name = GET_BOOL_OR_TRUE(opts, get_extmark, hl_name);
MTPair extmark = extmark_from_id(buf, (uint32_t)ns_id, (uint32_t)id);
if (extmark.start.pos.row < 0) {

View File

@@ -51,6 +51,12 @@ typedef struct {
Boolean undo_restore;
} Dict(set_extmark);
typedef struct {
OptionalKeys is_set__get_extmark_;
Boolean details;
Boolean hl_name;
} Dict(get_extmark);
typedef struct {
OptionalKeys is_set__get_extmarks_;
Integer limit;
@@ -313,3 +319,29 @@ typedef struct {
typedef struct {
Boolean output;
} Dict(exec_opts);
typedef struct {
OptionalKeys is_set__buf_attach_;
LuaRef on_lines;
LuaRef on_bytes;
LuaRef on_changedtick;
LuaRef on_detach;
LuaRef on_reload;
Boolean utf_sizes;
Boolean preview;
} Dict(buf_attach);
typedef struct {
OptionalKeys is_set__buf_delete_;
Boolean force;
Boolean unload;
} Dict(buf_delete);
typedef struct {
OptionalKeys is_set__empty_;
} Dict(empty);
typedef struct {
OptionalKeys is_set__open_term_;
LuaRef on_input;
} Dict(open_term);

View File

@@ -987,7 +987,7 @@ fail:
/// ["input", term, bufnr, data]
/// @param[out] err Error details, if any
/// @return Channel id, or 0 on error
Integer nvim_open_term(Buffer buffer, DictionaryOf(LuaRef) opts, Error *err)
Integer nvim_open_term(Buffer buffer, Dict(open_term) *opts, Error *err)
FUNC_API_SINCE(7)
FUNC_API_TEXTLOCK_ALLOW_CMDWIN
{
@@ -1002,19 +1002,10 @@ Integer nvim_open_term(Buffer buffer, DictionaryOf(LuaRef) opts, Error *err)
}
LuaRef cb = LUA_NOREF;
for (size_t i = 0; i < opts.size; i++) {
String k = opts.items[i].key;
Object *v = &opts.items[i].value;
if (strequal("on_input", k.data)) {
VALIDATE_T("on_input", kObjectTypeLuaRef, v->type, {
return 0;
});
cb = v->data.luaref;
v->data.luaref = LUA_NOREF;
break;
} else {
VALIDATE_S(false, "'opts' key", k.data, {});
}
if (HAS_KEY(opts, open_term, on_input)) {
cb = opts->on_input;
opts->on_input = LUA_NOREF;
}
Channel *chan = channel_alloc(kChannelStreamInternal);
@@ -1941,14 +1932,10 @@ Object nvim_get_proc(Integer pid, Error *err)
/// @param finish Finish the completion and dismiss the popup menu. Implies {insert}.
/// @param opts Optional parameters. Reserved for future use.
/// @param[out] err Error details, if any
void nvim_select_popupmenu_item(Integer item, Boolean insert, Boolean finish, Dictionary opts,
void nvim_select_popupmenu_item(Integer item, Boolean insert, Boolean finish, Dict(empty) *opts,
Error *err)
FUNC_API_SINCE(6)
{
VALIDATE((opts.size == 0), "%s", "opts dict isn't empty", {
return;
});
if (finish) {
insert = true;
}
@@ -2049,7 +2036,7 @@ Boolean nvim_del_mark(String name, Error *err)
/// not set.
/// @see |nvim_buf_set_mark()|
/// @see |nvim_del_mark()|
Array nvim_get_mark(String name, Dictionary opts, Error *err)
Array nvim_get_mark(String name, Dict(empty) *opts, Error *err)
FUNC_API_SINCE(8)
{
Array rv = ARRAY_DICT_INIT;