Add argument to not send a buffers content when updates are enabled

Add a test.
This commit is contained in:
KillTheMule
2018-01-26 15:49:35 +01:00
parent 9e97f14de2
commit bafae1c427
4 changed files with 46 additions and 35 deletions

View File

@@ -87,6 +87,7 @@ String buffer_get_line(Buffer buffer, Integer index, Error *err)
Boolean nvim_buf_live_updates(uint64_t channel_id, Boolean nvim_buf_live_updates(uint64_t channel_id,
Buffer buffer, Buffer buffer,
Boolean enabled, Boolean enabled,
Boolean send_buffer,
Error *err) Error *err)
FUNC_API_SINCE(3) FUNC_API_REMOTE_ONLY FUNC_API_SINCE(3) FUNC_API_REMOTE_ONLY
{ {
@@ -97,7 +98,7 @@ Boolean nvim_buf_live_updates(uint64_t channel_id,
} }
if (enabled) { if (enabled) {
return liveupdate_register(buf, channel_id); return liveupdate_register(buf, channel_id, send_buffer);
} }
liveupdate_unregister(buf, channel_id); liveupdate_unregister(buf, channel_id);

View File

@@ -6,7 +6,7 @@
// Register a channel. Return True if the channel was added, or already added. // Register a channel. Return True if the channel was added, or already added.
// Return False if the channel couldn't be added because the buffer is // Return False if the channel couldn't be added because the buffer is
// unloaded. // unloaded.
bool liveupdate_register(buf_T *buf, uint64_t channel_id) bool liveupdate_register(buf_T *buf, uint64_t channel_id, bool send_buffer)
{ {
// must fail if the buffer isn't loaded // must fail if the buffer isn't loaded
if (buf->b_ml.ml_mfp == NULL) { if (buf->b_ml.ml_mfp == NULL) {
@@ -27,21 +27,23 @@ bool liveupdate_register(buf_T *buf, uint64_t channel_id)
// append the channelid to the list // append the channelid to the list
kv_push(buf->liveupdate_channels, channel_id); kv_push(buf->liveupdate_channels, channel_id);
// send through the full channel contents now
Array linedata = ARRAY_DICT_INIT; Array linedata = ARRAY_DICT_INIT;
size_t line_count = buf->b_ml.ml_line_count; if (send_buffer) {
linedata.size = line_count; // collect buffer contents
linedata.items = xcalloc(sizeof(Object), line_count); size_t line_count = buf->b_ml.ml_line_count;
for (size_t i = 0; i < line_count; i++) { linedata.size = line_count;
linenr_T lnum = 1 + (linenr_T)i; linedata.items = xcalloc(sizeof(Object), line_count);
for (size_t i = 0; i < line_count; i++) {
linenr_T lnum = 1 + (linenr_T)i;
const char *bufstr = (char *)ml_get_buf(buf, lnum, false); const char *bufstr = (char *)ml_get_buf(buf, lnum, false);
Object str = STRING_OBJ(cstr_to_string(bufstr)); Object str = STRING_OBJ(cstr_to_string(bufstr));
// Vim represents NULs as NLs, but this may confuse clients. // Vim represents NULs as NLs, but this may confuse clients.
strchrsub(str.data.string.data, '\n', '\0'); strchrsub(str.data.string.data, '\n', '\0');
linedata.items[i] = str; linedata.items[i] = str;
}
} }
Array args = ARRAY_DICT_INIT; Array args = ARRAY_DICT_INIT;

View File

@@ -3,7 +3,7 @@
#include "nvim/buffer_defs.h" #include "nvim/buffer_defs.h"
bool liveupdate_register(buf_T *buf, uint64_t channel_id); bool liveupdate_register(buf_T *buf, uint64_t channel_id, bool send_buffer);
void liveupdate_unregister(buf_T *buf, uint64_t channel_id); void liveupdate_unregister(buf_T *buf, uint64_t channel_id);
void liveupdate_unregister_all(buf_T *buf); void liveupdate_unregister_all(buf_T *buf);
void liveupdate_send_changes(buf_T *buf, linenr_T firstline, int64_t num_added, void liveupdate_send_changes(buf_T *buf, linenr_T firstline, int64_t num_added,

View File

@@ -39,7 +39,7 @@ function open(activate, lines)
-- turn on live updates, ensure that the LiveUpdateStart messages -- turn on live updates, ensure that the LiveUpdateStart messages
-- arrive as expectected -- arrive as expectected
if activate then if activate then
ok(buffer('live_updates', b, true)) ok(buffer('live_updates', b, true, true))
expectn('LiveUpdateStart', {b, tick, lines, false}) expectn('LiveUpdateStart', {b, tick, lines, false})
end end
@@ -47,12 +47,12 @@ function open(activate, lines)
end end
function reopen(buf, expectedlines) function reopen(buf, expectedlines)
ok(buffer('live_updates', buf, false)) ok(buffer('live_updates', buf, false, true))
expectn('LiveUpdateEnd', {buf}) expectn('LiveUpdateEnd', {buf})
-- for some reason the :edit! increments tick by 2 -- for some reason the :edit! increments tick by 2
command('edit!') command('edit!')
local tick = eval('b:changedtick') local tick = eval('b:changedtick')
ok(buffer('live_updates', buf, true)) ok(buffer('live_updates', buf, true, true))
expectn('LiveUpdateStart', {buf, tick, origlines, false}) expectn('LiveUpdateStart', {buf, tick, origlines, false})
command('normal! gg') command('normal! gg')
return tick return tick
@@ -161,20 +161,20 @@ describe('liveupdate', function()
command('enew') command('enew')
local tick = eval('b:changedtick') local tick = eval('b:changedtick')
b2 = nvim('get_current_buf') b2 = nvim('get_current_buf')
ok(buffer('live_updates', b2, true)) ok(buffer('live_updates', b2, true, true))
expectn('LiveUpdateStart', {b2, tick, {""}, false}) expectn('LiveUpdateStart', {b2, tick, {""}, false})
eval('append(0, ["new line 1"])') eval('append(0, ["new line 1"])')
tick = tick + 1 tick = tick + 1
expectn('LiveUpdate', {b2, tick, 0, 0, {'new line 1'}}) expectn('LiveUpdate', {b2, tick, 0, 0, {'new line 1'}})
-- turn off live updates manually -- turn off live updates manually
buffer('live_updates', b2, false) buffer('live_updates', b2, false, true)
expectn('LiveUpdateEnd', {b2}) expectn('LiveUpdateEnd', {b2})
-- add multiple lines to a blank file -- add multiple lines to a blank file
command('enew!') command('enew!')
b3 = nvim('get_current_buf') b3 = nvim('get_current_buf')
ok(buffer('live_updates', b3, true)) ok(buffer('live_updates', b3, true, true))
tick = eval('b:changedtick') tick = eval('b:changedtick')
expectn('LiveUpdateStart', {b3, tick, {""}, false}) expectn('LiveUpdateStart', {b3, tick, {""}, false})
eval('append(0, ["new line 1", "new line 2", "new line 3"])') eval('append(0, ["new line 1", "new line 2", "new line 3"])')
@@ -267,7 +267,7 @@ describe('liveupdate', function()
tick = 2 tick = 2
expectn('LiveUpdateEnd', {b}) expectn('LiveUpdateEnd', {b})
bnew = nvim('get_current_buf') bnew = nvim('get_current_buf')
ok(buffer('live_updates', bnew, true)) ok(buffer('live_updates', bnew, true, true))
expectn('LiveUpdateStart', {bnew, tick, {''}, false}) expectn('LiveUpdateStart', {bnew, tick, {''}, false})
sendkeys('i') sendkeys('i')
sendkeys('h') sendkeys('h')
@@ -440,21 +440,21 @@ describe('liveupdate', function()
local b, tick = editoriginal(false) local b, tick = editoriginal(false)
-- turn on live updates many times -- turn on live updates many times
ok(buffer('live_updates', b, true)) ok(buffer('live_updates', b, true, true))
ok(buffer('live_updates', b, true)) ok(buffer('live_updates', b, true, true))
ok(buffer('live_updates', b, true)) ok(buffer('live_updates', b, true, true))
ok(buffer('live_updates', b, true)) ok(buffer('live_updates', b, true, true))
ok(buffer('live_updates', b, true)) ok(buffer('live_updates', b, true, true))
expectn('LiveUpdateStart', {b, tick, origlines, false}) expectn('LiveUpdateStart', {b, tick, origlines, false})
eval('rpcnotify('..channel..', "Hello There")') eval('rpcnotify('..channel..', "Hello There")')
expectn('Hello There', {}) expectn('Hello There', {})
-- turn live updates off many times -- turn live updates off many times
ok(buffer('live_updates', b, false)) ok(buffer('live_updates', b, false, true))
ok(buffer('live_updates', b, false)) ok(buffer('live_updates', b, false, true))
ok(buffer('live_updates', b, false)) ok(buffer('live_updates', b, false, true))
ok(buffer('live_updates', b, false)) ok(buffer('live_updates', b, false, true))
ok(buffer('live_updates', b, false)) ok(buffer('live_updates', b, false, true))
expectn('LiveUpdateEnd', {b}) expectn('LiveUpdateEnd', {b})
eval('rpcnotify('..channel..', "Hello Again")') eval('rpcnotify('..channel..', "Hello Again")')
expectn('Hello Again', {}) expectn('Hello Again', {})
@@ -493,9 +493,9 @@ describe('liveupdate', function()
local b, tick = open(false, lines) local b, tick = open(false, lines)
-- turn on live updates for sessions 1, 2 and 3 -- turn on live updates for sessions 1, 2 and 3
ok(request(1, 'nvim_buf_live_updates', b, true)) ok(request(1, 'nvim_buf_live_updates', b, true, true))
ok(request(2, 'nvim_buf_live_updates', b, true)) ok(request(2, 'nvim_buf_live_updates', b, true, true))
ok(request(3, 'nvim_buf_live_updates', b, true)) ok(request(3, 'nvim_buf_live_updates', b, true, true))
wantn(1, 'LiveUpdateStart', {b, tick, lines, false}) wantn(1, 'LiveUpdateStart', {b, tick, lines, false})
wantn(2, 'LiveUpdateStart', {b, tick, lines, false}) wantn(2, 'LiveUpdateStart', {b, tick, lines, false})
wantn(3, 'LiveUpdateStart', {b, tick, lines, false}) wantn(3, 'LiveUpdateStart', {b, tick, lines, false})
@@ -508,7 +508,7 @@ describe('liveupdate', function()
wantn(3, 'LiveUpdate', {b, tick, 0, 1, {'AA'}}) wantn(3, 'LiveUpdate', {b, tick, 0, 1, {'AA'}})
-- stop watching on channel 1 -- stop watching on channel 1
ok(request(1, 'nvim_buf_live_updates', b, false)) ok(request(1, 'nvim_buf_live_updates', b, false, true))
wantn(1, 'LiveUpdateEnd', {b}) wantn(1, 'LiveUpdateEnd', {b})
-- undo the change to buffer 1 -- undo the change to buffer 1
@@ -731,4 +731,12 @@ describe('liveupdate', function()
expectn('LiveUpdateEnd', {b}) expectn('LiveUpdateEnd', {b})
end end
end) end)
it('doesn\'t send the buffer\'s content when not requested', function()
helpers.clear()
local b, tick = editoriginal(false)
ok(buffer('live_updates', b, true, false))
expectn('LiveUpdateStart', {b, tick, {}, false})
end)
end) end)