aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/context.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/context.c')
-rw-r--r--src/nvim/context.c140
1 files changed, 32 insertions, 108 deletions
diff --git a/src/nvim/context.c b/src/nvim/context.c
index 95e2618f62..461b10a9d5 100644
--- a/src/nvim/context.c
+++ b/src/nvim/context.c
@@ -66,21 +66,11 @@ Context *ctx_get(size_t index)
void ctx_free(Context *ctx)
FUNC_ATTR_NONNULL_ALL
{
- if (ctx->regs.data) {
- msgpack_sbuffer_destroy(&ctx->regs);
- }
- if (ctx->jumps.data) {
- msgpack_sbuffer_destroy(&ctx->jumps);
- }
- if (ctx->bufs.data) {
- msgpack_sbuffer_destroy(&ctx->bufs);
- }
- if (ctx->gvars.data) {
- msgpack_sbuffer_destroy(&ctx->gvars);
- }
- if (ctx->funcs.items) {
- api_free_array(ctx->funcs);
- }
+ api_free_string(ctx->regs);
+ api_free_string(ctx->jumps);
+ api_free_string(ctx->bufs);
+ api_free_string(ctx->gvars);
+ api_free_array(ctx->funcs);
}
/// Saves the editor state to a context.
@@ -98,19 +88,19 @@ void ctx_save(Context *ctx, const int flags)
}
if (flags & kCtxRegs) {
- ctx_save_regs(ctx);
+ ctx->regs = shada_encode_regs();
}
if (flags & kCtxJumps) {
- ctx_save_jumps(ctx);
+ ctx->jumps = shada_encode_jumps();
}
if (flags & kCtxBufs) {
- ctx_save_bufs(ctx);
+ ctx->bufs = shada_encode_buflist();
}
if (flags & kCtxGVars) {
- ctx_save_gvars(ctx);
+ ctx->gvars = shada_encode_gvars();
}
if (flags & kCtxFuncs) {
@@ -173,33 +163,13 @@ bool ctx_restore(Context *ctx, const int flags)
return true;
}
-/// Saves the global registers to a context.
-///
-/// @param ctx Save to this context.
-static inline void ctx_save_regs(Context *ctx)
- FUNC_ATTR_NONNULL_ALL
-{
- msgpack_sbuffer_init(&ctx->regs);
- shada_encode_regs(&ctx->regs);
-}
-
/// Restores the global registers from a context.
///
/// @param ctx Restore from this context.
static inline void ctx_restore_regs(Context *ctx)
FUNC_ATTR_NONNULL_ALL
{
- shada_read_sbuf(&ctx->regs, kShaDaWantInfo | kShaDaForceit);
-}
-
-/// Saves the jumplist to a context.
-///
-/// @param ctx Save to this context.
-static inline void ctx_save_jumps(Context *ctx)
- FUNC_ATTR_NONNULL_ALL
-{
- msgpack_sbuffer_init(&ctx->jumps);
- shada_encode_jumps(&ctx->jumps);
+ shada_read_string(ctx->regs, kShaDaWantInfo | kShaDaForceit);
}
/// Restores the jumplist from a context.
@@ -208,17 +178,7 @@ static inline void ctx_save_jumps(Context *ctx)
static inline void ctx_restore_jumps(Context *ctx)
FUNC_ATTR_NONNULL_ALL
{
- shada_read_sbuf(&ctx->jumps, kShaDaWantInfo | kShaDaForceit);
-}
-
-/// Saves the buffer list to a context.
-///
-/// @param ctx Save to this context.
-static inline void ctx_save_bufs(Context *ctx)
- FUNC_ATTR_NONNULL_ALL
-{
- msgpack_sbuffer_init(&ctx->bufs);
- shada_encode_buflist(&ctx->bufs);
+ shada_read_string(ctx->jumps, kShaDaWantInfo | kShaDaForceit);
}
/// Restores the buffer list from a context.
@@ -227,17 +187,7 @@ static inline void ctx_save_bufs(Context *ctx)
static inline void ctx_restore_bufs(Context *ctx)
FUNC_ATTR_NONNULL_ALL
{
- shada_read_sbuf(&ctx->bufs, kShaDaWantInfo | kShaDaForceit);
-}
-
-/// Saves global variables to a context.
-///
-/// @param ctx Save to this context.
-static inline void ctx_save_gvars(Context *ctx)
- FUNC_ATTR_NONNULL_ALL
-{
- msgpack_sbuffer_init(&ctx->gvars);
- shada_encode_gvars(&ctx->gvars);
+ shada_read_string(ctx->bufs, kShaDaWantInfo | kShaDaForceit);
}
/// Restores global variables from a context.
@@ -246,7 +196,7 @@ static inline void ctx_save_gvars(Context *ctx)
static inline void ctx_restore_gvars(Context *ctx)
FUNC_ATTR_NONNULL_ALL
{
- shada_read_sbuf(&ctx->gvars, kShaDaWantInfo | kShaDaForceit);
+ shada_read_string(ctx->gvars, kShaDaWantInfo | kShaDaForceit);
}
/// Saves functions to a context.
@@ -291,41 +241,16 @@ static inline void ctx_restore_funcs(Context *ctx)
}
}
-/// Convert msgpack_sbuffer to readfile()-style array.
-///
-/// @param[in] sbuf msgpack_sbuffer to convert.
-///
-/// @return readfile()-style array representation of "sbuf".
-static inline Array sbuf_to_array(msgpack_sbuffer sbuf, Arena *arena)
-{
- list_T *const list = tv_list_alloc(kListLenMayKnow);
- tv_list_append_string(list, "", 0);
- if (sbuf.size > 0) {
- encode_list_write(list, sbuf.data, sbuf.size);
- }
-
- typval_T list_tv = (typval_T) {
- .v_lock = VAR_UNLOCKED,
- .v_type = VAR_LIST,
- .vval.v_list = list
- };
-
- Array array = vim_to_object(&list_tv, arena, false).data.array;
- tv_clear(&list_tv);
- return array;
-}
-
-/// Convert readfile()-style array to msgpack_sbuffer.
+/// Convert readfile()-style array to String
///
/// @param[in] array readfile()-style array to convert.
/// @param[out] err Error object.
///
-/// @return msgpack_sbuffer with conversion result.
-static inline msgpack_sbuffer array_to_sbuf(Array array, Error *err)
+/// @return String with conversion result.
+static inline String array_to_string(Array array, Error *err)
FUNC_ATTR_NONNULL_ALL
{
- msgpack_sbuffer sbuf;
- msgpack_sbuffer_init(&sbuf);
+ String sbuf = STRING_INIT;
typval_T list_tv;
object_to_vim(ARRAY_OBJ(array), &list_tv, err);
@@ -335,41 +260,40 @@ static inline msgpack_sbuffer array_to_sbuf(Array array, Error *err)
api_set_error(err, kErrorTypeException, "%s",
"E474: Failed to convert list to msgpack string buffer");
}
- sbuf.alloc = sbuf.size;
tv_clear(&list_tv);
return sbuf;
}
-/// Converts Context to Dictionary representation.
+/// Converts Context to Dict representation.
///
/// @param[in] ctx Context to convert.
///
-/// @return Dictionary representing "ctx".
-Dictionary ctx_to_dict(Context *ctx, Arena *arena)
+/// @return Dict representing "ctx".
+Dict ctx_to_dict(Context *ctx, Arena *arena)
FUNC_ATTR_NONNULL_ALL
{
assert(ctx != NULL);
- Dictionary rv = arena_dict(arena, 5);
+ Dict rv = arena_dict(arena, 5);
- PUT_C(rv, "regs", ARRAY_OBJ(sbuf_to_array(ctx->regs, arena)));
- PUT_C(rv, "jumps", ARRAY_OBJ(sbuf_to_array(ctx->jumps, arena)));
- PUT_C(rv, "bufs", ARRAY_OBJ(sbuf_to_array(ctx->bufs, arena)));
- PUT_C(rv, "gvars", ARRAY_OBJ(sbuf_to_array(ctx->gvars, arena)));
+ PUT_C(rv, "regs", ARRAY_OBJ(string_to_array(ctx->regs, false, arena)));
+ PUT_C(rv, "jumps", ARRAY_OBJ(string_to_array(ctx->jumps, false, arena)));
+ PUT_C(rv, "bufs", ARRAY_OBJ(string_to_array(ctx->bufs, false, arena)));
+ PUT_C(rv, "gvars", ARRAY_OBJ(string_to_array(ctx->gvars, false, arena)));
PUT_C(rv, "funcs", ARRAY_OBJ(copy_array(ctx->funcs, arena)));
return rv;
}
-/// Converts Dictionary representation of Context back to Context object.
+/// Converts Dict representation of Context back to Context object.
///
-/// @param[in] dict Context Dictionary representation.
+/// @param[in] dict Context Dict representation.
/// @param[out] ctx Context object to store conversion result into.
/// @param[out] err Error object.
///
/// @return types of included context items.
-int ctx_from_dict(Dictionary dict, Context *ctx, Error *err)
+int ctx_from_dict(Dict dict, Context *ctx, Error *err)
FUNC_ATTR_NONNULL_ALL
{
assert(ctx != NULL);
@@ -382,16 +306,16 @@ int ctx_from_dict(Dictionary dict, Context *ctx, Error *err)
}
if (strequal(item.key.data, "regs")) {
types |= kCtxRegs;
- ctx->regs = array_to_sbuf(item.value.data.array, err);
+ ctx->regs = array_to_string(item.value.data.array, err);
} else if (strequal(item.key.data, "jumps")) {
types |= kCtxJumps;
- ctx->jumps = array_to_sbuf(item.value.data.array, err);
+ ctx->jumps = array_to_string(item.value.data.array, err);
} else if (strequal(item.key.data, "bufs")) {
types |= kCtxBufs;
- ctx->bufs = array_to_sbuf(item.value.data.array, err);
+ ctx->bufs = array_to_string(item.value.data.array, err);
} else if (strequal(item.key.data, "gvars")) {
types |= kCtxGVars;
- ctx->gvars = array_to_sbuf(item.value.data.array, err);
+ ctx->gvars = array_to_string(item.value.data.array, err);
} else if (strequal(item.key.data, "funcs")) {
types |= kCtxFuncs;
ctx->funcs = copy_object(item.value, NULL).data.array;