feat(buffer_updates): allow ignoring when previewing

Also adds a test that we actually subscribe to buffer events when in
CMDPREVIEW.
This commit is contained in:
Thomas Vigouroux 2020-12-15 22:18:35 +01:00
parent d0112693a3
commit 25e20da550
No known key found for this signature in database
GPG Key ID: 16A6001CD57B9100
5 changed files with 33 additions and 7 deletions

View File

@ -50,7 +50,7 @@ function M._create_parser(bufnr, lang, opts)
end end
end end
a.nvim_buf_attach(self.bufnr, false, {on_bytes=bytes_cb, on_detach=detach_cb}) a.nvim_buf_attach(self.bufnr, false, {on_bytes=bytes_cb, on_detach=detach_cb, preview=true})
self:parse() self:parse()

View File

@ -121,6 +121,8 @@ Integer nvim_buf_line_count(Buffer buffer, Error *err)
/// - buffer handle /// - buffer handle
/// - utf_sizes: include UTF-32 and UTF-16 size of the replaced /// - utf_sizes: include UTF-32 and UTF-16 size of the replaced
/// region, as args to `on_lines`. /// region, as args to `on_lines`.
/// - preview: also attach to command preview (i.e. 'inccommand')
/// events.
/// @param[out] err Error details, if any /// @param[out] err Error details, if any
/// @return False if attach failed (invalid parameter, or buffer isn't loaded); /// @return False if attach failed (invalid parameter, or buffer isn't loaded);
/// otherwise True. TODO: LUA_API_NO_EVAL /// otherwise True. TODO: LUA_API_NO_EVAL
@ -176,6 +178,12 @@ Boolean nvim_buf_attach(uint64_t channel_id,
goto error; goto error;
} }
cb.utf_sizes = v->data.boolean; cb.utf_sizes = v->data.boolean;
} else if (is_lua && strequal("preview", k.data)) {
if (v->type != kObjectTypeBoolean) {
api_set_error(err, kErrorTypeValidation, "preview must be boolean");
goto error;
}
cb.preview = v->data.boolean;
} else { } else {
api_set_error(err, kErrorTypeValidation, "unexpected key: %s", k.data); api_set_error(err, kErrorTypeValidation, "unexpected key: %s", k.data);
goto error; goto error;

View File

@ -491,9 +491,10 @@ typedef struct {
LuaRef on_changedtick; LuaRef on_changedtick;
LuaRef on_detach; LuaRef on_detach;
bool utf_sizes; bool utf_sizes;
bool preview;
} BufUpdateCallbacks; } BufUpdateCallbacks;
#define BUF_UPDATE_CALLBACKS_INIT { LUA_NOREF, LUA_NOREF, LUA_NOREF, \ #define BUF_UPDATE_CALLBACKS_INIT { LUA_NOREF, LUA_NOREF, LUA_NOREF, \
LUA_NOREF, false } LUA_NOREF, false, false }
EXTERN int curbuf_splice_pending INIT(= 0); EXTERN int curbuf_splice_pending INIT(= 0);

View File

@ -237,7 +237,7 @@ void buf_updates_send_changes(buf_T *buf,
for (size_t i = 0; i < kv_size(buf->update_callbacks); i++) { for (size_t i = 0; i < kv_size(buf->update_callbacks); i++) {
BufUpdateCallbacks cb = kv_A(buf->update_callbacks, i); BufUpdateCallbacks cb = kv_A(buf->update_callbacks, i);
bool keep = true; bool keep = true;
if (cb.on_lines != LUA_NOREF) { if (cb.on_lines != LUA_NOREF && (cb.preview || !(State & CMDPREVIEW))) {
Array args = ARRAY_DICT_INIT; Array args = ARRAY_DICT_INIT;
Object items[8]; Object items[8];
args.size = 6; // may be increased to 8 below args.size = 6; // may be increased to 8 below
@ -298,7 +298,7 @@ void buf_updates_send_splice(
for (size_t i = 0; i < kv_size(buf->update_callbacks); i++) { for (size_t i = 0; i < kv_size(buf->update_callbacks); i++) {
BufUpdateCallbacks cb = kv_A(buf->update_callbacks, i); BufUpdateCallbacks cb = kv_A(buf->update_callbacks, i);
bool keep = true; bool keep = true;
if (cb.on_bytes != LUA_NOREF) { if (cb.on_bytes != LUA_NOREF && (cb.preview || !(State & CMDPREVIEW))) {
FIXED_TEMP_ARRAY(args, 11); FIXED_TEMP_ARRAY(args, 11);
// the first argument is always the buffer handle // the first argument is always the buffer handle

View File

@ -25,14 +25,14 @@ local function attach_buffer(evname)
local evname = ... local evname = ...
local events = {} local events = {}
function test_register(bufnr, id, changedtick, utf_sizes) function test_register(bufnr, id, changedtick, utf_sizes, preview)
local function callback(...) local function callback(...)
table.insert(events, {id, ...}) table.insert(events, {id, ...})
if test_unreg == id then if test_unreg == id then
return true return true
end end
end end
local opts = {[evname]=callback, on_detach=callback, utf_sizes=utf_sizes} local opts = {[evname]=callback, on_detach=callback, utf_sizes=utf_sizes, preview=preview}
if changedtick then if changedtick then
opts.on_changedtick = callback opts.on_changedtick = callback
end end
@ -290,7 +290,7 @@ describe('lua: nvim_buf_attach on_bytes', function()
if verify then if verify then
meths.buf_get_offset(0, meths.buf_line_count(0)) meths.buf_get_offset(0, meths.buf_line_count(0))
end end
exec_lua("return test_register(...)", 0, "test1",false, nil) exec_lua("return test_register(...)", 0, "test1", false, false, true)
meths.buf_get_changedtick(0) meths.buf_get_changedtick(0)
local verify_name = "test1" local verify_name = "test1"
@ -493,6 +493,23 @@ describe('lua: nvim_buf_attach on_bytes', function()
} }
end) end)
it('inccomand=nosplit and substitute', function()
if verify then pending("Verification can't be done when previewing") end
local check_events = setup_eventcheck(verify, {"abcde"})
meths.set_option('inccommand', 'nosplit')
feed ':%s/bcd/'
check_events {
{ "test1", "bytes", 1, 3, 0, 1, 1, 0, 3, 3, 0, 0, 0 };
}
feed 'a'
check_events {
{ "test1", "bytes", 1, 3, 0, 1, 1, 0, 3, 3, 0, 1, 1 };
}
end)
end end
describe('(with verify) handles', function() describe('(with verify) handles', function()