aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/buffer_updates.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-01-25 18:31:31 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-01-25 18:31:31 +0000
commit9243becbedbb6a1592208051f8fa2b090dcc5e7d (patch)
tree607c2a862ec3f4399b8766383f6f8e04c4aa43b4 /src/nvim/buffer_updates.c
parent9e40b6e9e1bc67f2d856adb837ee64dd0e25b717 (diff)
parent3c48d3c83fc21dbc0841f9210f04bdb073d73cd1 (diff)
downloadrneovim-usermarks.tar.gz
rneovim-usermarks.tar.bz2
rneovim-usermarks.zip
Merge remote-tracking branch 'upstream/master' into usermarksusermarks
Diffstat (limited to 'src/nvim/buffer_updates.c')
-rw-r--r--src/nvim/buffer_updates.c65
1 files changed, 41 insertions, 24 deletions
diff --git a/src/nvim/buffer_updates.c b/src/nvim/buffer_updates.c
index 14973502ab..075ac2adbf 100644
--- a/src/nvim/buffer_updates.c
+++ b/src/nvim/buffer_updates.c
@@ -1,17 +1,31 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stddef.h>
+
+#include "klib/kvec.h"
+#include "lauxlib.h"
+#include "nvim/api/buffer.h"
+#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/assert.h"
#include "nvim/buffer.h"
+#include "nvim/buffer_defs.h"
#include "nvim/buffer_updates.h"
#include "nvim/extmark.h"
+#include "nvim/globals.h"
+#include "nvim/log.h"
#include "nvim/lua/executor.h"
#include "nvim/memline.h"
+#include "nvim/memory.h"
#include "nvim/msgpack_rpc/channel.h"
+#include "nvim/pos.h"
+#include "nvim/types.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
-# include "buffer_updates.c.generated.h"
+# include "buffer_updates.c.generated.h" // IWYU pragma: export
#endif
// Register a channel. Return True if the channel was added, or already added.
@@ -34,12 +48,10 @@ bool buf_updates_register(buf_T *buf, uint64_t channel_id, BufUpdateCallbacks cb
// count how many channels are currently watching the buffer
size_t size = kv_size(buf->update_channels);
- if (size) {
- for (size_t i = 0; i < size; i++) {
- if (kv_A(buf->update_channels, i) == channel_id) {
- // buffer is already registered ... nothing to do
- return true;
- }
+ for (size_t i = 0; i < size; i++) {
+ if (kv_A(buf->update_channels, i) == channel_id) {
+ // buffer is already registered ... nothing to do
+ return true;
}
}
@@ -69,13 +81,14 @@ bool buf_updates_register(buf_T *buf, uint64_t channel_id, BufUpdateCallbacks cb
linedata.size = line_count;
linedata.items = xcalloc(line_count, sizeof(Object));
- buf_collect_lines(buf, line_count, 1, true, &linedata, NULL);
+ buf_collect_lines(buf, line_count, 1, 0, true, &linedata, NULL, NULL);
}
args.items[4] = ARRAY_OBJ(linedata);
args.items[5] = BOOLEAN_OBJ(false);
rpc_send_event(channel_id, "nvim_buf_lines_event", args);
+ api_free_array(args); // TODO(bfredl): no
} else {
buf_updates_changedtick_single(buf, channel_id);
}
@@ -91,10 +104,8 @@ bool buf_updates_active(buf_T *buf)
void buf_updates_send_end(buf_T *buf, uint64_t channelid)
{
- Array args = ARRAY_DICT_INIT;
- args.size = 1;
- args.items = xcalloc(args.size, sizeof(Object));
- args.items[0] = BUFFER_OBJ(buf->handle);
+ MAXSIZE_TEMP_ARRAY(args, 1);
+ ADD_C(args, BUFFER_OBJ(buf->handle));
rpc_send_event(channelid, "nvim_buf_detach_event", args);
}
@@ -135,6 +146,15 @@ void buf_updates_unregister(buf_T *buf, uint64_t channelid)
}
}
+void buf_free_callbacks(buf_T *buf)
+{
+ kv_destroy(buf->update_channels);
+ for (size_t i = 0; i < kv_size(buf->update_callbacks); i++) {
+ buffer_update_callbacks_free(kv_A(buf->update_callbacks, i));
+ }
+ kv_destroy(buf->update_callbacks);
+}
+
void buf_updates_unload(buf_T *buf, bool can_reload)
{
size_t size = kv_size(buf->update_channels);
@@ -230,8 +250,8 @@ void buf_updates_send_changes(buf_T *buf, linenr_T firstline, int64_t num_added,
STATIC_ASSERT(SIZE_MAX >= MAXLNUM, "size_t smaller than MAXLNUM");
linedata.size = (size_t)num_added;
linedata.items = xcalloc((size_t)num_added, sizeof(Object));
- buf_collect_lines(buf, (size_t)num_added, firstline, true, &linedata,
- NULL);
+ buf_collect_lines(buf, (size_t)num_added, firstline, 0, true, &linedata,
+ NULL, NULL);
}
args.items[4] = ARRAY_OBJ(linedata);
args.items[5] = BOOLEAN_OBJ(false);
@@ -241,6 +261,7 @@ void buf_updates_send_changes(buf_T *buf, linenr_T firstline, int64_t num_added,
// the end.
badchannelid = channelid;
}
+ api_free_array(args); // TODO(bfredl): no
}
// We can only ever remove one dead channel at a time. This is OK because the
@@ -285,14 +306,13 @@ void buf_updates_send_changes(buf_T *buf, linenr_T firstline, int64_t num_added,
args.items[7] = INTEGER_OBJ((Integer)deleted_codeunits);
}
textlock++;
- Object res = nlua_call_ref(cb.on_lines, "lines", args, true, NULL);
+ Object res = nlua_call_ref(cb.on_lines, "lines", args, false, NULL);
textlock--;
if (res.type == kObjectTypeBoolean && res.data.boolean == true) {
buffer_update_callbacks_free(cb);
keep = false;
}
- api_free_object(res);
}
if (keep) {
kv_A(buf->update_callbacks, j++) = kv_A(buf->update_callbacks, i);
@@ -335,7 +355,7 @@ void buf_updates_send_splice(buf_T *buf, int start_row, colnr_T start_col, bcoun
ADD_C(args, INTEGER_OBJ(new_byte));
textlock++;
- Object res = nlua_call_ref(cb.on_bytes, "bytes", args, true, NULL);
+ Object res = nlua_call_ref(cb.on_bytes, "bytes", args, false, NULL);
textlock--;
if (res.type == kObjectTypeBoolean && res.data.boolean == true) {
@@ -371,14 +391,13 @@ void buf_updates_changedtick(buf_T *buf)
textlock++;
Object res = nlua_call_ref(cb.on_changedtick, "changedtick",
- args, true, NULL);
+ args, false, NULL);
textlock--;
if (res.type == kObjectTypeBoolean && res.data.boolean == true) {
buffer_update_callbacks_free(cb);
keep = false;
}
- api_free_object(res);
}
if (keep) {
kv_A(buf->update_callbacks, j++) = kv_A(buf->update_callbacks, i);
@@ -389,15 +408,13 @@ void buf_updates_changedtick(buf_T *buf)
void buf_updates_changedtick_single(buf_T *buf, uint64_t channel_id)
{
- Array args = ARRAY_DICT_INIT;
- args.size = 2;
- args.items = xcalloc(args.size, sizeof(Object));
+ MAXSIZE_TEMP_ARRAY(args, 2);
// the first argument is always the buffer handle
- args.items[0] = BUFFER_OBJ(buf->handle);
+ ADD_C(args, BUFFER_OBJ(buf->handle));
// next argument is b:changedtick
- args.items[1] = INTEGER_OBJ(buf_get_changedtick(buf));
+ ADD_C(args, INTEGER_OBJ(buf_get_changedtick(buf)));
// don't try and clean up dead channels here
rpc_send_event(channel_id, "nvim_buf_changedtick_event", args);