aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/api/private
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/api/private')
-rw-r--r--src/nvim/api/private/helpers.c48
-rw-r--r--src/nvim/api/private/helpers.h4
2 files changed, 52 insertions, 0 deletions
diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c
index 9ce1786fa0..cc95f46baf 100644
--- a/src/nvim/api/private/helpers.c
+++ b/src/nvim/api/private/helpers.c
@@ -1014,6 +1014,54 @@ bool api_dict_to_keydict(void *retval, FieldHashfn hashy, Dictionary dict, Error
return true;
}
+Dictionary api_keydict_to_dict(void *value, KeySetLink *table, size_t max_size, Arena *arena)
+{
+ Dictionary rv = arena_dict(arena, max_size);
+ for (size_t i = 0; table[i].str; i++) {
+ KeySetLink *field = &table[i];
+ bool is_set = true;
+ if (field->opt_index >= 0) {
+ OptKeySet *ks = (OptKeySet *)value;
+ is_set = ks->is_set_ & (1ULL << field->opt_index);
+ }
+
+ if (!is_set) {
+ continue;
+ }
+
+ char *mem = ((char *)value + field->ptr_off);
+ Object val = NIL;
+
+ if (field->type == kObjectTypeNil) {
+ val = *(Object *)mem;
+ } else if (field->type == kObjectTypeInteger) {
+ val = INTEGER_OBJ(*(Integer *)mem);
+ } else if (field->type == kObjectTypeFloat) {
+ val = FLOAT_OBJ(*(Float *)mem);
+ } else if (field->type == kObjectTypeBoolean) {
+ val = BOOLEAN_OBJ(*(Boolean *)mem);
+ } else if (field->type == kObjectTypeString) {
+ val = STRING_OBJ(*(String *)mem);
+ } else if (field->type == kObjectTypeArray) {
+ val = ARRAY_OBJ(*(Array *)mem);
+ } else if (field->type == kObjectTypeDictionary) {
+ val = DICTIONARY_OBJ(*(Dictionary *)mem);
+ } else if (field->type == kObjectTypeBuffer || field->type == kObjectTypeWindow
+ || field->type == kObjectTypeTabpage) {
+ val.data.integer = *(Integer *)mem;
+ val.type = field->type;
+ } else if (field->type == kObjectTypeLuaRef) {
+ // do nothing
+ } else {
+ abort();
+ }
+
+ PUT_C(rv, field->str, val);
+ }
+
+ return rv;
+}
+
void api_free_keydict(void *dict, KeySetLink *table)
{
for (size_t i = 0; table[i].str; i++) {
diff --git a/src/nvim/api/private/helpers.h b/src/nvim/api/private/helpers.h
index 8a56d1704f..0dbe3d2fb6 100644
--- a/src/nvim/api/private/helpers.h
+++ b/src/nvim/api/private/helpers.h
@@ -32,6 +32,7 @@
#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 BUFFER_OBJ(s) ((Object) { \
.type = kObjectTypeBuffer, \
@@ -70,6 +71,9 @@
#define PUT_C(dict, k, v) \
kv_push_c(dict, ((KeyValuePair) { .key = cstr_as_string(k), .value = v }))
+#define PUT_KEY(d, typ, key, v) \
+ do { (d).is_set__##typ##_ |= (1 << KEYSET_OPTIDX_##typ##__##key); (d).key = v; } while (0)
+
#define ADD(array, item) \
kv_push(array, item)