Send changedtick as first event if buffer contents weren't requested

This commit is contained in:
KillTheMule 2018-05-10 23:13:58 +02:00
parent ad151847f1
commit 0bee3925ab
3 changed files with 41 additions and 35 deletions

View File

@ -80,7 +80,9 @@ String buffer_get_line(Buffer buffer, Integer index, Error *err)
/// ///
/// @param buffer The buffer handle /// @param buffer The buffer handle
/// @param send_buffer Set to true if the initial notification should contain /// @param send_buffer Set to true if the initial notification should contain
/// the whole buffer /// the whole buffer. If so, the first notification will be a
/// `nvim_buf_lines_event`. Otherwise, the first notification will be
/// a `nvim_buf_changedtick_event`
/// @param[out] err Details of an error that may have occurred /// @param[out] err Details of an error that may have occurred
/// @return False when updates couldn't be enabled because the buffer isn't /// @return False when updates couldn't be enabled because the buffer isn't
/// loaded; otherwise True. /// loaded; otherwise True.

View File

@ -28,20 +28,20 @@ bool buf_updates_register(buf_T *buf, uint64_t channel_id, bool send_buffer)
// append the channelid to the list // append the channelid to the list
kv_push(buf->update_channels, channel_id); kv_push(buf->update_channels, channel_id);
Array args = ARRAY_DICT_INIT;
args.size = 6;
args.items = xcalloc(sizeof(Object), args.size);
// the first argument is always the buffer handle
args.items[0] = BUFFER_OBJ(buf->handle);
args.items[1] = INTEGER_OBJ(buf->b_changedtick);
// the first line that changed (zero-indexed)
args.items[2] = INTEGER_OBJ(-1);
// the last line that was changed
args.items[3] = INTEGER_OBJ(-1);
Array linedata = ARRAY_DICT_INIT;
if (send_buffer) { if (send_buffer) {
Array args = ARRAY_DICT_INIT;
args.size = 6;
args.items = xcalloc(sizeof(Object), args.size);
// the first argument is always the buffer handle
args.items[0] = BUFFER_OBJ(buf->handle);
args.items[1] = INTEGER_OBJ(buf->b_changedtick);
// the first line that changed (zero-indexed)
args.items[2] = INTEGER_OBJ(0);
// the last line that was changed
args.items[3] = INTEGER_OBJ(-1);
Array linedata = ARRAY_DICT_INIT;
// collect buffer contents // collect buffer contents
// True now, but a compile time reminder for future systems we support // True now, but a compile time reminder for future systems we support
@ -50,8 +50,6 @@ bool buf_updates_register(buf_T *buf, uint64_t channel_id, bool send_buffer)
size_t line_count = (size_t)buf->b_ml.ml_line_count; size_t line_count = (size_t)buf->b_ml.ml_line_count;
if (line_count >= 1) { if (line_count >= 1) {
args.items[2] = INTEGER_OBJ(0);
linedata.size = line_count; linedata.size = line_count;
linedata.items = xcalloc(sizeof(Object), line_count); linedata.items = xcalloc(sizeof(Object), line_count);
for (size_t i = 0; i < line_count; i++) { for (size_t i = 0; i < line_count; i++) {
@ -66,12 +64,15 @@ bool buf_updates_register(buf_T *buf, uint64_t channel_id, bool send_buffer)
linedata.items[i] = str; linedata.items[i] = str;
} }
} }
args.items[4] = ARRAY_OBJ(linedata);
args.items[5] = BOOLEAN_OBJ(false);
rpc_send_event(channel_id, "nvim_buf_lines_event", args);
} else {
buf_updates_changedtick_single(buf, channel_id);
} }
args.items[4] = ARRAY_OBJ(linedata);
args.items[5] = BOOLEAN_OBJ(false);
rpc_send_event(channel_id, "nvim_buf_lines_event", args);
return true; return true;
} }
@ -207,9 +208,13 @@ void buf_updates_changedtick(buf_T *buf)
{ {
// notify each of the active channels // notify each of the active channels
for (size_t i = 0; i < kv_size(buf->update_channels); i++) { for (size_t i = 0; i < kv_size(buf->update_channels); i++) {
uint64_t channelid = kv_A(buf->update_channels, i); uint64_t channel_id = kv_A(buf->update_channels, i);
buf_updates_changedtick_single(buf, channel_id);
}
}
// send through the changes now channel contents now void buf_updates_changedtick_single(buf_T *buf, uint64_t channel_id)
{
Array args = ARRAY_DICT_INIT; Array args = ARRAY_DICT_INIT;
args.size = 2; args.size = 2;
args.items = xcalloc(sizeof(Object), args.size); args.items = xcalloc(sizeof(Object), args.size);
@ -221,6 +226,5 @@ void buf_updates_changedtick(buf_T *buf)
args.items[1] = INTEGER_OBJ(buf->b_changedtick); args.items[1] = INTEGER_OBJ(buf->b_changedtick);
// don't try and clean up dead channels here // don't try and clean up dead channels here
rpc_send_event(channelid, "nvim_buf_changedtick", args); rpc_send_event(channel_id, "nvim_buf_changedtick_event", args);
}
} }

View File

@ -338,7 +338,7 @@ describe('buffer events', function()
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_lines_event', {b, tick, 1, 2, {}, false}) expectn('nvim_buf_lines_event', {b, tick, 1, 2, {}, false})
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_changedtick', {b, tick}) expectn('nvim_buf_changedtick_event', {b, tick})
command('set autoindent') command('set autoindent')
command('normal! >>') command('normal! >>')
tick = tick + 1 tick = tick + 1
@ -356,7 +356,7 @@ describe('buffer events', function()
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_lines_event', {b, tick, 1, 2, {}, false}) expectn('nvim_buf_lines_event', {b, tick, 1, 2, {}, false})
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_changedtick', {b, tick}) expectn('nvim_buf_changedtick_event', {b, tick})
command('normal! ggOmmm') command('normal! ggOmmm')
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_lines_event', {b, tick, 0, 0, {"\t"}, false}) expectn('nvim_buf_lines_event', {b, tick, 0, 0, {"\t"}, false})
@ -376,7 +376,7 @@ describe('buffer events', function()
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_lines_event', {b, tick, 0, 1, {'Line 1'}, false}) expectn('nvim_buf_lines_event', {b, tick, 0, 1, {'Line 1'}, false})
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_changedtick', {b, tick}) expectn('nvim_buf_changedtick_event', {b, tick})
-- change the file directly -- change the file directly
local f = io.open(filename, 'a') local f = io.open(filename, 'a')
@ -412,7 +412,7 @@ describe('buffer events', function()
tick1 = tick1 + 1 tick1 = tick1 + 1
expectn('nvim_buf_lines_event', {b1, tick1, 0, 1, {'A1'}, false}) expectn('nvim_buf_lines_event', {b1, tick1, 0, 1, {'A1'}, false})
tick1 = tick1 + 1 tick1 = tick1 + 1
expectn('nvim_buf_changedtick', {b1, tick1}) expectn('nvim_buf_changedtick_event', {b1, tick1})
command('b'..b2nr) command('b'..b2nr)
command('normal! x') command('normal! x')
@ -422,7 +422,7 @@ describe('buffer events', function()
tick2 = tick2 + 1 tick2 = tick2 + 1
expectn('nvim_buf_lines_event', {b2, tick2, 0, 1, {'B1'}, false}) expectn('nvim_buf_lines_event', {b2, tick2, 0, 1, {'B1'}, false})
tick2 = tick2 + 1 tick2 = tick2 + 1
expectn('nvim_buf_changedtick', {b2, tick2}) expectn('nvim_buf_changedtick_event', {b2, tick2})
command('b'..b3nr) command('b'..b3nr)
command('normal! x') command('normal! x')
@ -432,7 +432,7 @@ describe('buffer events', function()
tick3 = tick3 + 1 tick3 = tick3 + 1
expectn('nvim_buf_lines_event', {b3, tick3, 0, 1, {'C1'}, false}) expectn('nvim_buf_lines_event', {b3, tick3, 0, 1, {'C1'}, false})
tick3 = tick3 + 1 tick3 = tick3 + 1
expectn('nvim_buf_changedtick', {b3, tick3}) expectn('nvim_buf_changedtick_event', {b3, tick3})
end) end)
it('doesn\'t get confused when you turn watching on/off many times', it('doesn\'t get confused when you turn watching on/off many times',
@ -514,8 +514,8 @@ describe('buffer events', function()
wantn(2, 'nvim_buf_lines_event', {b, tick, 0, 1, {'AAA'}, false}) wantn(2, 'nvim_buf_lines_event', {b, tick, 0, 1, {'AAA'}, false})
wantn(3, 'nvim_buf_lines_event', {b, tick, 0, 1, {'AAA'}, false}) wantn(3, 'nvim_buf_lines_event', {b, tick, 0, 1, {'AAA'}, false})
tick = tick + 1 tick = tick + 1
wantn(2, 'nvim_buf_changedtick', {b, tick}) wantn(2, 'nvim_buf_changedtick_event', {b, tick})
wantn(3, 'nvim_buf_changedtick', {b, tick}) wantn(3, 'nvim_buf_changedtick_event', {b, tick})
-- make sure there are no other pending nvim_buf_lines_event messages going to -- make sure there are no other pending nvim_buf_lines_event messages going to
-- channel 1 -- channel 1
@ -667,7 +667,7 @@ describe('buffer events', function()
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_lines_event', {b, tick, 0, 1, {'AAA'}, false}) expectn('nvim_buf_lines_event', {b, tick, 0, 1, {'AAA'}, false})
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_changedtick', {b, tick}) expectn('nvim_buf_changedtick_event', {b, tick})
-- close our buffer by creating a new one -- close our buffer by creating a new one
command('enew') command('enew')
@ -694,7 +694,7 @@ describe('buffer events', function()
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_lines_event', {b, tick, 0, 1, {'AAA'}, false}) expectn('nvim_buf_lines_event', {b, tick, 0, 1, {'AAA'}, false})
tick = tick + 1 tick = tick + 1
expectn('nvim_buf_changedtick', {b, tick}) expectn('nvim_buf_changedtick_event', {b, tick})
-- close our buffer by creating a new one -- close our buffer by creating a new one
command('set hidden') command('set hidden')
@ -733,7 +733,7 @@ describe('buffer events', function()
helpers.clear() helpers.clear()
local b, tick = editoriginal(false) local b, tick = editoriginal(false)
ok(buffer('attach', b, false)) ok(buffer('attach', b, false))
expectn('nvim_buf_lines_event', {b, tick, -1, -1, {}, false}) expectn('nvim_buf_changedtick_event', {b, tick})
end) end)
end) end)