refactor(tinput_wait_enqueue): use rbuffer_read() when pasting (#17754)

When pasting, all of key buffer can be consumed, and in case of phase 3
the paste event must be put exactly once, so using rbuffer_read() should
be better here.
This commit is contained in:
zeertzjq
2022-03-19 21:56:47 +08:00
committed by GitHub
parent 536dc391f6
commit 2ab52bd988

View File

@@ -115,34 +115,28 @@ static void tinput_done_event(void **argv)
static void tinput_wait_enqueue(void **argv)
{
TermInput *input = argv[0];
if (rbuffer_size(input->key_buffer) == 0 && input->paste == 3) {
// End streamed paste with an empty string.
const String keys = { .data = "", .size = 0 };
String copy = copy_string(keys);
multiqueue_put(main_loop.events, tinput_paste_event, 3,
copy.data, copy.size, (intptr_t)input->paste);
}
RBUFFER_UNTIL_EMPTY(input->key_buffer, buf, len) {
const String keys = { .data = buf, .size = len };
if (input->paste) {
String copy = copy_string(keys);
if (ui_client_channel_id) {
Array args = ARRAY_DICT_INIT;
ADD(args, STRING_OBJ(copy_string(keys))); // 'data'
ADD(args, BOOLEAN_OBJ(true)); // 'crlf'
ADD(args, INTEGER_OBJ(input->paste)); // 'phase'
rpc_send_event(ui_client_channel_id, "nvim_paste", args);
} else {
multiqueue_put(main_loop.events, tinput_paste_event, 3,
copy.data, copy.size, (intptr_t)input->paste);
}
if (input->paste == 1) {
// Paste phase: "continue"
input->paste = 2;
}
rbuffer_consumed(input->key_buffer, len);
rbuffer_reset(input->key_buffer);
if (input->paste) { // produce exactly one paste event
const size_t len = rbuffer_size(input->key_buffer);
String keys = { .data = xmallocz(len), .size = len };
rbuffer_read(input->key_buffer, keys.data, len);
if (ui_client_channel_id) {
Array args = ARRAY_DICT_INIT;
ADD(args, STRING_OBJ(keys)); // 'data'
ADD(args, BOOLEAN_OBJ(true)); // 'crlf'
ADD(args, INTEGER_OBJ(input->paste)); // 'phase'
rpc_send_event(ui_client_channel_id, "nvim_paste", args);
} else {
multiqueue_put(main_loop.events, tinput_paste_event, 3,
keys.data, keys.size, (intptr_t)input->paste);
}
if (input->paste == 1) {
// Paste phase: "continue"
input->paste = 2;
}
rbuffer_reset(input->key_buffer);
} else { // enqueue input for the main thread or Nvim server
RBUFFER_UNTIL_EMPTY(input->key_buffer, buf, len) {
const String keys = { .data = buf, .size = len };
size_t consumed;
if (ui_client_channel_id) {
Array args = ARRAY_DICT_INIT;