refactor(style): switch-case formatting, "uncrustify:indent-off" #15669

* refactor: disable formatting for attribute in macro
* fixup: disable/enable uncrustify with uncrustify:indent-off/on
* fixup: stop indenting contents inside braces in case
* fixup: remove case brace if no variable declaration
This commit is contained in:
dundargoc
2021-09-17 16:38:16 +02:00
committed by GitHub
parent 1ec3d37192
commit 867e888599
9 changed files with 726 additions and 778 deletions

View File

@@ -44,12 +44,12 @@ disable_processing_nl_cont = false # true/false
# file. # file.
# #
# Default: *INDENT-OFF* # Default: *INDENT-OFF*
disable_processing_cmt = " *INDENT-OFF*" # string disable_processing_cmt = "uncrustify:indent-off" # string
# Specify the marker used in comments to (re)enable processing in a file. # Specify the marker used in comments to (re)enable processing in a file.
# #
# Default: *INDENT-ON* # Default: *INDENT-ON*
enable_processing_cmt = " *INDENT-ON*" # string enable_processing_cmt = "uncrustify:indent-on" # string
# Enable parsing of digraphs. # Enable parsing of digraphs.
enable_digraphs = false # true/false enable_digraphs = false # true/false
@@ -1194,7 +1194,7 @@ indent_switch_case = 0 # unsigned number
# Spaces to indent '{' from 'case'. By default, the brace will appear under # Spaces to indent '{' from 'case'. By default, the brace will appear under
# the 'c' in case. Usually set to 0 or indent_columns. Negative values are OK. # the 'c' in case. Usually set to 0 or indent_columns. Negative values are OK.
# It might wise to choose the same value for the option indent_switch_case. # It might wise to choose the same value for the option indent_switch_case.
indent_case_brace = 2 # number indent_case_brace = 0 # number
# indent 'break' with 'case' from 'switch'. # indent 'break' with 'case' from 'switch'.
indent_switch_break_with_case = false # true/false indent_switch_break_with_case = false # true/false
@@ -2987,7 +2987,7 @@ mod_move_case_break = false # true/false
# Add or remove braces around a fully braced case statement. Will only remove # Add or remove braces around a fully braced case statement. Will only remove
# braces if there are no variable declarations in the block. # braces if there are no variable declarations in the block.
mod_case_brace = ignore # ignore/add/remove/force/not_defined mod_case_brace = remove # ignore/add/remove/force/not_defined
# Whether to remove a void 'return;' that appears as the last statement in a # Whether to remove a void 'return;' that appears as the last statement in a
# function. # function.
@@ -3279,5 +3279,5 @@ set PREPROC REAL_FATTR_CONST
set PREPROC REAL_FATTR_NONNULL_ALL set PREPROC REAL_FATTR_NONNULL_ALL
set PREPROC REAL_FATTR_PURE set PREPROC REAL_FATTR_PURE
set PREPROC REAL_FATTR_WARN_UNUSED_RESULT set PREPROC REAL_FATTR_WARN_UNUSED_RESULT
# option(s) with 'not default' value: 59 # option(s) with 'not default' value: 61
# #

View File

@@ -7653,14 +7653,14 @@ int hkmap(int c)
case ';': case ';':
c = 't'; break; c = 't'; break;
default: { default: {
static char str[] = "zqbcxlsjphmkwonu ydafe rig"; static char str[] = "zqbcxlsjphmkwonu ydafe rig";
if (c < 'a' || c > 'z') { if (c < 'a' || c > 'z') {
return c; return c;
}
c = str[CharOrdLow(c)];
break;
} }
c = str[CharOrdLow(c)];
break;
}
} }
return (int)(CharOrdLow(c) + p_aleph); return (int)(CharOrdLow(c) + p_aleph);

View File

@@ -2144,36 +2144,36 @@ int parse_command_modifiers(exarg_T *eap, char_u **errormsg, bool skip_only)
continue; continue;
case 'f': { // only accept ":filter {pat} cmd" case 'f': { // only accept ":filter {pat} cmd"
char_u *reg_pat; char_u *reg_pat;
if (!checkforcmd(&p, "filter", 4) || *p == NUL || ends_excmd(*p)) { if (!checkforcmd(&p, "filter", 4) || *p == NUL || ends_excmd(*p)) {
break; break;
}
if (*p == '!') {
cmdmod.filter_force = true;
p = skipwhite(p + 1);
if (*p == NUL || ends_excmd(*p)) {
break;
}
}
if (skip_only) {
p = skip_vimgrep_pat(p, NULL, NULL);
} else {
// NOTE: This puts a NUL after the pattern.
p = skip_vimgrep_pat(p, &reg_pat, NULL);
}
if (p == NULL || *p == NUL) {
break;
}
if (!skip_only) {
cmdmod.filter_regmatch.regprog = vim_regcomp(reg_pat, RE_MAGIC);
if (cmdmod.filter_regmatch.regprog == NULL) {
break;
}
}
eap->cmd = p;
continue;
} }
if (*p == '!') {
cmdmod.filter_force = true;
p = skipwhite(p + 1);
if (*p == NUL || ends_excmd(*p)) {
break;
}
}
if (skip_only) {
p = skip_vimgrep_pat(p, NULL, NULL);
} else {
// NOTE: This puts a NUL after the pattern.
p = skip_vimgrep_pat(p, &reg_pat, NULL);
}
if (p == NULL || *p == NUL) {
break;
}
if (!skip_only) {
cmdmod.filter_regmatch.regprog = vim_regcomp(reg_pat, RE_MAGIC);
if (cmdmod.filter_regmatch.regprog == NULL) {
break;
}
}
eap->cmd = p;
continue;
}
// ":hide" and ":hide | cmd" are not modifiers // ":hide" and ":hide | cmd" are not modifiers
case 'h': case 'h':
@@ -2416,19 +2416,19 @@ int parse_cmd_address(exarg_T *eap, char_u **errormsg, bool silent)
eap->line2 = curbuf->b_ml.ml_line_count; eap->line2 = curbuf->b_ml.ml_line_count;
break; break;
case ADDR_LOADED_BUFFERS: { case ADDR_LOADED_BUFFERS: {
buf_T *buf = firstbuf; buf_T *buf = firstbuf;
while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL) { while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL) {
buf = buf->b_next; buf = buf->b_next;
}
eap->line1 = buf->b_fnum;
buf = lastbuf;
while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL) {
buf = buf->b_prev;
}
eap->line2 = buf->b_fnum;
break;
} }
eap->line1 = buf->b_fnum;
buf = lastbuf;
while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL) {
buf = buf->b_prev;
}
eap->line2 = buf->b_fnum;
break;
}
case ADDR_BUFFERS: case ADDR_BUFFERS:
eap->line1 = firstbuf->b_fnum; eap->line1 = firstbuf->b_fnum;
eap->line2 = lastbuf->b_fnum; eap->line2 = lastbuf->b_fnum;
@@ -3387,48 +3387,48 @@ const char * set_one_cmd_context(expand_T *xp, const char *buff)
case CMD_global: case CMD_global:
case CMD_vglobal: { case CMD_vglobal: {
const int delim = (uint8_t)(*arg); // Get the delimiter. const int delim = (uint8_t)(*arg); // Get the delimiter.
if (delim) { if (delim) {
arg++; // Skip delimiter if there is one. arg++; // Skip delimiter if there is one.
} }
while (arg[0] != NUL && (uint8_t)arg[0] != delim) { while (arg[0] != NUL && (uint8_t)arg[0] != delim) {
if (arg[0] == '\\' && arg[1] != NUL) { if (arg[0] == '\\' && arg[1] != NUL) {
arg++;
}
arg++; arg++;
} }
if (arg[0] != NUL) { arg++;
return arg + 1;
}
break;
} }
if (arg[0] != NUL) {
return arg + 1;
}
break;
}
case CMD_and: case CMD_and:
case CMD_substitute: { case CMD_substitute: {
const int delim = (uint8_t)(*arg); const int delim = (uint8_t)(*arg);
if (delim) { if (delim) {
// Skip "from" part. // Skip "from" part.
arg++; arg++;
arg = (const char *)skip_regexp((char_u *)arg, delim, p_magic, NULL); arg = (const char *)skip_regexp((char_u *)arg, delim, p_magic, NULL);
}
// Skip "to" part.
while (arg[0] != NUL && (uint8_t)arg[0] != delim) {
if (arg[0] == '\\' && arg[1] != NUL) {
arg++;
}
arg++;
}
if (arg[0] != NUL) { // Skip delimiter.
arg++;
}
while (arg[0] && strchr("|\"#", arg[0]) == NULL) {
arg++;
}
if (arg[0] != NUL) {
return arg;
}
break;
} }
// Skip "to" part.
while (arg[0] != NUL && (uint8_t)arg[0] != delim) {
if (arg[0] == '\\' && arg[1] != NUL) {
arg++;
}
arg++;
}
if (arg[0] != NUL) { // Skip delimiter.
arg++;
}
while (arg[0] && strchr("|\"#", arg[0]) == NULL) {
arg++;
}
if (arg[0] != NUL) {
return arg;
}
break;
}
case CMD_isearch: case CMD_isearch:
case CMD_dsearch: case CMD_dsearch:
case CMD_ilist: case CMD_ilist:
@@ -5984,115 +5984,115 @@ static size_t uc_check_code(char_u *code, size_t len, char_u *buf, ucmd_T *cmd,
case ct_LINE2: case ct_LINE2:
case ct_RANGE: case ct_RANGE:
case ct_COUNT: { case ct_COUNT: {
char num_buf[20]; char num_buf[20];
long num = (type == ct_LINE1) ? eap->line1 : long num = (type == ct_LINE1) ? eap->line1 :
(type == ct_LINE2) ? eap->line2 : (type == ct_LINE2) ? eap->line2 :
(type == ct_RANGE) ? eap->addr_count : (type == ct_RANGE) ? eap->addr_count :
(eap->addr_count > 0) ? eap->line2 : cmd->uc_def; (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
size_t num_len; size_t num_len;
sprintf(num_buf, "%" PRId64, (int64_t)num); sprintf(num_buf, "%" PRId64, (int64_t)num);
num_len = STRLEN(num_buf); num_len = STRLEN(num_buf);
result = num_len; result = num_len;
if (quote) { if (quote) {
result += 2; result += 2;
}
if (buf != NULL) {
if (quote) {
*buf++ = '"';
}
STRCPY(buf, num_buf);
buf += num_len;
if (quote) {
*buf = '"';
}
}
break;
} }
case ct_MODS: { if (buf != NULL) {
result = quote ? 2 : 0; if (quote) {
if (buf != NULL) { *buf++ = '"';
if (quote) {
*buf++ = '"';
}
*buf = '\0';
} }
STRCPY(buf, num_buf);
bool multi_mods = false; buf += num_len;
if (quote) {
// :aboveleft and :leftabove
if (cmdmod.split & WSP_ABOVE) {
result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
}
// :belowright and :rightbelow
if (cmdmod.split & WSP_BELOW) {
result += add_cmd_modifier(buf, "belowright", &multi_mods);
}
// :botright
if (cmdmod.split & WSP_BOT) {
result += add_cmd_modifier(buf, "botright", &multi_mods);
}
typedef struct {
bool *set;
char *name;
} mod_entry_T;
static mod_entry_T mod_entries[] = {
{ &cmdmod.browse, "browse" },
{ &cmdmod.confirm, "confirm" },
{ &cmdmod.hide, "hide" },
{ &cmdmod.keepalt, "keepalt" },
{ &cmdmod.keepjumps, "keepjumps" },
{ &cmdmod.keepmarks, "keepmarks" },
{ &cmdmod.keeppatterns, "keeppatterns" },
{ &cmdmod.lockmarks, "lockmarks" },
{ &cmdmod.noswapfile, "noswapfile" }
};
// the modifiers that are simple flags
for (size_t i = 0; i < ARRAY_SIZE(mod_entries); i++) {
if (*mod_entries[i].set) {
result += add_cmd_modifier(buf, mod_entries[i].name, &multi_mods);
}
}
// TODO(vim): How to support :noautocmd?
// TODO(vim): How to support :sandbox?
// :silent
if (msg_silent > 0) {
result += add_cmd_modifier(buf, emsg_silent > 0 ? "silent!" : "silent",
&multi_mods);
}
// :tab
if (cmdmod.tab > 0) {
result += add_cmd_modifier(buf, "tab", &multi_mods);
}
// :topleft
if (cmdmod.split & WSP_TOP) {
result += add_cmd_modifier(buf, "topleft", &multi_mods);
}
// TODO(vim): How to support :unsilent?
// :verbose
if (p_verbose > 0) {
result += add_cmd_modifier(buf, "verbose", &multi_mods);
}
// :vertical
if (cmdmod.split & WSP_VERT) {
result += add_cmd_modifier(buf, "vertical", &multi_mods);
}
if (quote && buf != NULL) {
buf += result - 2;
*buf = '"'; *buf = '"';
} }
break;
} }
break;
}
case ct_MODS: {
result = quote ? 2 : 0;
if (buf != NULL) {
if (quote) {
*buf++ = '"';
}
*buf = '\0';
}
bool multi_mods = false;
// :aboveleft and :leftabove
if (cmdmod.split & WSP_ABOVE) {
result += add_cmd_modifier(buf, "aboveleft", &multi_mods);
}
// :belowright and :rightbelow
if (cmdmod.split & WSP_BELOW) {
result += add_cmd_modifier(buf, "belowright", &multi_mods);
}
// :botright
if (cmdmod.split & WSP_BOT) {
result += add_cmd_modifier(buf, "botright", &multi_mods);
}
typedef struct {
bool *set;
char *name;
} mod_entry_T;
static mod_entry_T mod_entries[] = {
{ &cmdmod.browse, "browse" },
{ &cmdmod.confirm, "confirm" },
{ &cmdmod.hide, "hide" },
{ &cmdmod.keepalt, "keepalt" },
{ &cmdmod.keepjumps, "keepjumps" },
{ &cmdmod.keepmarks, "keepmarks" },
{ &cmdmod.keeppatterns, "keeppatterns" },
{ &cmdmod.lockmarks, "lockmarks" },
{ &cmdmod.noswapfile, "noswapfile" }
};
// the modifiers that are simple flags
for (size_t i = 0; i < ARRAY_SIZE(mod_entries); i++) {
if (*mod_entries[i].set) {
result += add_cmd_modifier(buf, mod_entries[i].name, &multi_mods);
}
}
// TODO(vim): How to support :noautocmd?
// TODO(vim): How to support :sandbox?
// :silent
if (msg_silent > 0) {
result += add_cmd_modifier(buf, emsg_silent > 0 ? "silent!" : "silent",
&multi_mods);
}
// :tab
if (cmdmod.tab > 0) {
result += add_cmd_modifier(buf, "tab", &multi_mods);
}
// :topleft
if (cmdmod.split & WSP_TOP) {
result += add_cmd_modifier(buf, "topleft", &multi_mods);
}
// TODO(vim): How to support :unsilent?
// :verbose
if (p_verbose > 0) {
result += add_cmd_modifier(buf, "verbose", &multi_mods);
}
// :vertical
if (cmdmod.split & WSP_VERT) {
result += add_cmd_modifier(buf, "vertical", &multi_mods);
}
if (quote && buf != NULL) {
buf += result - 2;
*buf = '"';
}
break;
}
case ct_REGISTER: case ct_REGISTER:
result = eap->regname ? 1 : 0; result = eap->regname ? 1 : 0;
if (quote) { if (quote) {

View File

@@ -1,37 +1,36 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check // 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 // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include <inttypes.h>
#include <msgpack.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <inttypes.h>
#include <uv.h> #include <uv.h>
#include <msgpack.h>
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
#include "nvim/api/vim.h"
#include "nvim/api/ui.h" #include "nvim/api/ui.h"
#include "nvim/channel.h" #include "nvim/api/vim.h"
#include "nvim/msgpack_rpc/channel.h"
#include "nvim/event/loop.h"
#include "nvim/event/libuv_process.h"
#include "nvim/event/rstream.h"
#include "nvim/event/wstream.h"
#include "nvim/event/socket.h"
#include "nvim/msgpack_rpc/helpers.h"
#include "nvim/vim.h"
#include "nvim/main.h"
#include "nvim/ascii.h" #include "nvim/ascii.h"
#include "nvim/memory.h" #include "nvim/channel.h"
#include "nvim/eval.h" #include "nvim/eval.h"
#include "nvim/os_unix.h" #include "nvim/event/libuv_process.h"
#include "nvim/message.h" #include "nvim/event/loop.h"
#include "nvim/map.h" #include "nvim/event/rstream.h"
#include "nvim/log.h" #include "nvim/event/socket.h"
#include "nvim/misc1.h" #include "nvim/event/wstream.h"
#include "nvim/lib/kvec.h" #include "nvim/lib/kvec.h"
#include "nvim/log.h"
#include "nvim/main.h"
#include "nvim/map.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
#include "nvim/msgpack_rpc/channel.h"
#include "nvim/msgpack_rpc/helpers.h"
#include "nvim/os/input.h" #include "nvim/os/input.h"
#include "nvim/os_unix.h"
#include "nvim/ui.h" #include "nvim/ui.h"
#include "nvim/vim.h"
#if MIN_LOG_LEVEL > DEBUG_LOG_LEVEL #if MIN_LOG_LEVEL > DEBUG_LOG_LEVEL
#define log_client_msg(...) #define log_client_msg(...)
@@ -102,7 +101,7 @@ bool rpc_send_event(uint64_t id, const char *name, Array args)
if (channel) { if (channel) {
send_event(channel, name, args); send_event(channel, name, args);
} else { } else {
broadcast_event(name, args); broadcast_event(name, args);
} }
@@ -116,10 +115,7 @@ bool rpc_send_event(uint64_t id, const char *name, Array args)
/// @param args Array with method arguments /// @param args Array with method arguments
/// @param[out] error True if the return value is an error /// @param[out] error True if the return value is an error
/// @return Whatever the remote method returned /// @return Whatever the remote method returned
Object rpc_send_call(uint64_t id, Object rpc_send_call(uint64_t id, const char *method_name, Array args, Error *err)
const char *method_name,
Array args,
Error *err)
{ {
Channel *channel = NULL; Channel *channel = NULL;
@@ -206,8 +202,7 @@ void rpc_unsubscribe(uint64_t id, char *event)
unsubscribe(channel, event); unsubscribe(channel, event);
} }
static void receive_msgpack(Stream *stream, RBuffer *rbuf, size_t c, static void receive_msgpack(Stream *stream, RBuffer *rbuf, size_t c, void *data, bool eof)
void *data, bool eof)
{ {
Channel *channel = data; Channel *channel = data;
channel_incref(channel); channel_incref(channel);
@@ -463,10 +458,7 @@ static void send_error(Channel *chan, MessageType type, uint32_t id, char *err)
api_clear_error(&e); api_clear_error(&e);
} }
static void send_request(Channel *channel, static void send_request(Channel *channel, uint32_t id, const char *name, Array args)
uint32_t id,
const char *name,
Array args)
{ {
const String method = cstr_as_string((char *)name); const String method = cstr_as_string((char *)name);
channel_write(channel, serialize_request(channel->id, channel_write(channel, serialize_request(channel->id,
@@ -477,9 +469,7 @@ static void send_request(Channel *channel,
1)); 1));
} }
static void send_event(Channel *channel, static void send_event(Channel *channel, const char *name, Array args)
const char *name,
Array args)
{ {
const String method = cstr_as_string((char *)name); const String method = cstr_as_string((char *)name);
channel_write(channel, serialize_request(channel->id, channel_write(channel, serialize_request(channel->id,
@@ -528,9 +518,9 @@ static void unsubscribe(Channel *channel, char *event)
{ {
char *event_string = pmap_get(cstr_t)(&event_strings, event); char *event_string = pmap_get(cstr_t)(&event_strings, event);
if (!event_string) { if (!event_string) {
WLOG("RPC: ch %" PRIu64 ": tried to unsubscribe unknown event '%s'", WLOG("RPC: ch %" PRIu64 ": tried to unsubscribe unknown event '%s'",
channel->id, event); channel->id, event);
return; return;
} }
pmap_del(cstr_t)(channel->rpc.subscribed_events, event_string); pmap_del(cstr_t)(channel->rpc.subscribed_events, event_string);
@@ -589,10 +579,10 @@ void rpc_free(Channel *channel)
static bool is_rpc_response(msgpack_object *obj) static bool is_rpc_response(msgpack_object *obj)
{ {
return obj->type == MSGPACK_OBJECT_ARRAY return obj->type == MSGPACK_OBJECT_ARRAY
&& obj->via.array.size == 4 && obj->via.array.size == 4
&& obj->via.array.ptr[0].type == MSGPACK_OBJECT_POSITIVE_INTEGER && obj->via.array.ptr[0].type == MSGPACK_OBJECT_POSITIVE_INTEGER
&& obj->via.array.ptr[0].via.u64 == 1 && obj->via.array.ptr[0].via.u64 == 1
&& obj->via.array.ptr[1].type == MSGPACK_OBJECT_POSITIVE_INTEGER; && obj->via.array.ptr[1].type == MSGPACK_OBJECT_POSITIVE_INTEGER;
} }
static bool is_valid_rpc_response(msgpack_object *obj, Channel *channel) static bool is_valid_rpc_response(msgpack_object *obj, Channel *channel)
@@ -634,12 +624,8 @@ static void call_set_error(Channel *channel, char *msg, int loglevel)
channel_close(channel->id, kChannelPartRpc, NULL); channel_close(channel->id, kChannelPartRpc, NULL);
} }
static WBuffer *serialize_request(uint64_t channel_id, static WBuffer *serialize_request(uint64_t channel_id, uint32_t request_id, const String method,
uint32_t request_id, Array args, msgpack_sbuffer *sbuffer, size_t refcount)
const String method,
Array args,
msgpack_sbuffer *sbuffer,
size_t refcount)
{ {
msgpack_packer pac; msgpack_packer pac;
msgpack_packer_init(&pac, sbuffer, msgpack_sbuffer_write); msgpack_packer_init(&pac, sbuffer, msgpack_sbuffer_write);
@@ -654,12 +640,8 @@ static WBuffer *serialize_request(uint64_t channel_id,
return rv; return rv;
} }
static WBuffer *serialize_response(uint64_t channel_id, static WBuffer *serialize_response(uint64_t channel_id, MessageType type, uint32_t response_id,
MessageType type, Error *err, Object arg, msgpack_sbuffer *sbuffer)
uint32_t response_id,
Error *err,
Object arg,
msgpack_sbuffer *sbuffer)
{ {
msgpack_packer pac; msgpack_packer pac;
msgpack_packer_init(&pac, sbuffer, msgpack_sbuffer_write); msgpack_packer_init(&pac, sbuffer, msgpack_sbuffer_write);
@@ -733,47 +715,43 @@ static const char *const msgpack_error_messages[] = {
[MSGPACK_UNPACK_NOMEM_ERROR + MUR_OFF] = "not enough memory", [MSGPACK_UNPACK_NOMEM_ERROR + MUR_OFF] = "not enough memory",
}; };
static void log_server_msg(uint64_t channel_id, static void log_server_msg(uint64_t channel_id, msgpack_sbuffer *packed)
msgpack_sbuffer *packed)
{ {
msgpack_unpacked unpacked; msgpack_unpacked unpacked;
msgpack_unpacked_init(&unpacked); msgpack_unpacked_init(&unpacked);
DLOGN("RPC ->ch %" PRIu64 ": ", channel_id); DLOGN("RPC ->ch %" PRIu64 ": ", channel_id);
const msgpack_unpack_return result = const msgpack_unpack_return result =
msgpack_unpack_next(&unpacked, packed->data, packed->size, NULL); msgpack_unpack_next(&unpacked, packed->data, packed->size, NULL);
switch (result) { switch (result) {
case MSGPACK_UNPACK_SUCCESS: { case MSGPACK_UNPACK_SUCCESS: {
uint64_t type = unpacked.data.via.array.ptr[0].via.u64; uint64_t type = unpacked.data.via.array.ptr[0].via.u64;
log_lock(); log_lock();
FILE *f = open_log_file(); FILE *f = open_log_file();
fprintf(f, type ? (type == 1 ? RES : NOT) : REQ); fprintf(f, type ? (type == 1 ? RES : NOT) : REQ);
log_msg_close(f, unpacked.data); log_msg_close(f, unpacked.data);
msgpack_unpacked_destroy(&unpacked); msgpack_unpacked_destroy(&unpacked);
break; break;
} }
case MSGPACK_UNPACK_EXTRA_BYTES: case MSGPACK_UNPACK_EXTRA_BYTES:
case MSGPACK_UNPACK_CONTINUE: case MSGPACK_UNPACK_CONTINUE:
case MSGPACK_UNPACK_PARSE_ERROR: case MSGPACK_UNPACK_PARSE_ERROR:
case MSGPACK_UNPACK_NOMEM_ERROR: { case MSGPACK_UNPACK_NOMEM_ERROR: {
log_lock(); log_lock();
FILE *f = open_log_file(); FILE *f = open_log_file();
fprintf(f, ERR); fprintf(f, ERR);
log_msg_close(f, (msgpack_object) { log_msg_close(f, (msgpack_object) {
.type = MSGPACK_OBJECT_STR, .type = MSGPACK_OBJECT_STR,
.via.str = { .via.str = {
.ptr = (char *)msgpack_error_messages[result + MUR_OFF], .ptr = (char *)msgpack_error_messages[result + MUR_OFF],
.size = (uint32_t)strlen( .size = (uint32_t)strlen(msgpack_error_messages[result + MUR_OFF]),
msgpack_error_messages[result + MUR_OFF]), },
},
}); });
break; break;
} }
} }
} }
static void log_client_msg(uint64_t channel_id, static void log_client_msg(uint64_t channel_id, bool is_request, msgpack_object msg)
bool is_request,
msgpack_object msg)
{ {
DLOGN("RPC <-ch %" PRIu64 ": ", channel_id); DLOGN("RPC <-ch %" PRIu64 ": ", channel_id);
log_lock(); log_lock();

View File

@@ -1,19 +1,18 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check // 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 // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include <stdbool.h>
#include <inttypes.h> #include <inttypes.h>
#include <msgpack.h> #include <msgpack.h>
#include <stdbool.h>
#include "nvim/api/private/dispatch.h" #include "nvim/api/private/dispatch.h"
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
#include "nvim/msgpack_rpc/helpers.h" #include "nvim/assert.h"
#include "nvim/lib/kvec.h" #include "nvim/lib/kvec.h"
#include "nvim/vim.h"
#include "nvim/log.h" #include "nvim/log.h"
#include "nvim/memory.h" #include "nvim/memory.h"
#include "nvim/assert.h" #include "nvim/msgpack_rpc/helpers.h"
#include "nvim/vim.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "msgpack_rpc/helpers.c.generated.h" # include "msgpack_rpc/helpers.c.generated.h"
@@ -48,15 +47,17 @@ static msgpack_sbuffer sbuffer;
} \ } \
\ \
static void msgpack_rpc_from_##lt(Integer o, msgpack_packer *res) \ static void msgpack_rpc_from_##lt(Integer o, msgpack_packer *res) \
/* uncrustify:indent-off */ \
FUNC_ATTR_NONNULL_ARG(2) \ FUNC_ATTR_NONNULL_ARG(2) \
/* uncrustify:indent-on */ \
{ \ { \
msgpack_packer pac; \ msgpack_packer pac; \
msgpack_packer_init(&pac, &sbuffer, msgpack_sbuffer_write); \ msgpack_packer_init(&pac, &sbuffer, msgpack_sbuffer_write); \
msgpack_pack_int64(&pac, (handle_T)o); \ msgpack_pack_int64(&pac, (handle_T)o); \
msgpack_pack_ext(res, sbuffer.size, \ msgpack_pack_ext(res, sbuffer.size, \
kObjectType##t - EXT_OBJECT_TYPE_SHIFT); \ kObjectType##t - EXT_OBJECT_TYPE_SHIFT); \
msgpack_pack_ext_body(res, sbuffer.data, sbuffer.size); \ msgpack_pack_ext_body(res, sbuffer.data, sbuffer.size); \
msgpack_sbuffer_clear(&sbuffer); \ msgpack_sbuffer_clear(&sbuffer); \
} }
void msgpack_rpc_helpers_init(void) void msgpack_rpc_helpers_init(void)
@@ -89,177 +90,167 @@ bool msgpack_rpc_to_object(const msgpack_object *const obj, Object *const arg)
kvec_withinit_t(MPToAPIObjectStackItem, 2) stack = KV_INITIAL_VALUE; kvec_withinit_t(MPToAPIObjectStackItem, 2) stack = KV_INITIAL_VALUE;
kvi_init(stack); kvi_init(stack);
kvi_push(stack, ((MPToAPIObjectStackItem) { kvi_push(stack, ((MPToAPIObjectStackItem) {
.mobj = obj, .mobj = obj,
.aobj = arg, .aobj = arg,
.container = false, .container = false,
.idx = 0, .idx = 0,
})); }));
while (ret && kv_size(stack)) { while (ret && kv_size(stack)) {
MPToAPIObjectStackItem cur = kv_last(stack); MPToAPIObjectStackItem cur = kv_last(stack);
if (!cur.container) { if (!cur.container) {
*cur.aobj = NIL; *cur.aobj = NIL;
} }
switch (cur.mobj->type) { switch (cur.mobj->type) {
case MSGPACK_OBJECT_NIL: { case MSGPACK_OBJECT_NIL:
break; break;
} case MSGPACK_OBJECT_BOOLEAN:
case MSGPACK_OBJECT_BOOLEAN: { *cur.aobj = BOOLEAN_OBJ(cur.mobj->via.boolean);
*cur.aobj = BOOLEAN_OBJ(cur.mobj->via.boolean); break;
break; case MSGPACK_OBJECT_NEGATIVE_INTEGER:
} STATIC_ASSERT(sizeof(Integer) == sizeof(cur.mobj->via.i64),
case MSGPACK_OBJECT_NEGATIVE_INTEGER: { "Msgpack integer size does not match API integer");
STATIC_ASSERT(sizeof(Integer) == sizeof(cur.mobj->via.i64), *cur.aobj = INTEGER_OBJ(cur.mobj->via.i64);
"Msgpack integer size does not match API integer"); break;
*cur.aobj = INTEGER_OBJ(cur.mobj->via.i64); case MSGPACK_OBJECT_POSITIVE_INTEGER:
break; STATIC_ASSERT(sizeof(Integer) == sizeof(cur.mobj->via.u64),
} "Msgpack integer size does not match API integer");
case MSGPACK_OBJECT_POSITIVE_INTEGER: { if (cur.mobj->via.u64 > API_INTEGER_MAX) {
STATIC_ASSERT(sizeof(Integer) == sizeof(cur.mobj->via.u64), ret = false;
"Msgpack integer size does not match API integer"); } else {
if (cur.mobj->via.u64 > API_INTEGER_MAX) { *cur.aobj = INTEGER_OBJ((Integer)cur.mobj->via.u64);
ret = false;
} else {
*cur.aobj = INTEGER_OBJ((Integer)cur.mobj->via.u64);
}
break;
} }
break;
#ifdef NVIM_MSGPACK_HAS_FLOAT32 #ifdef NVIM_MSGPACK_HAS_FLOAT32
case MSGPACK_OBJECT_FLOAT32: case MSGPACK_OBJECT_FLOAT32:
case MSGPACK_OBJECT_FLOAT64: case MSGPACK_OBJECT_FLOAT64:
#else #else
case MSGPACK_OBJECT_FLOAT: case MSGPACK_OBJECT_FLOAT:
#endif #endif
{ {
STATIC_ASSERT(sizeof(Float) == sizeof(cur.mobj->via.f64), STATIC_ASSERT(sizeof(Float) == sizeof(cur.mobj->via.f64),
"Msgpack floating-point size does not match API integer"); "Msgpack floating-point size does not match API integer");
*cur.aobj = FLOAT_OBJ(cur.mobj->via.f64); *cur.aobj = FLOAT_OBJ(cur.mobj->via.f64);
break; break;
} }
#define STR_CASE(type, attr, obj, dest, conv) \ #define STR_CASE(type, attr, obj, dest, conv) \
case type: { \ case type: { \
dest = conv(((String) { \ dest = conv(((String) { \
.size = obj->via.attr.size, \ .size = obj->via.attr.size, \
.data = (obj->via.attr.ptr == NULL || obj->via.attr.size == 0 \ .data = (obj->via.attr.ptr == NULL || obj->via.attr.size == 0 \
? xmemdupz("", 0) \ ? xmemdupz("", 0) \
: xmemdupz(obj->via.attr.ptr, obj->via.attr.size)), \ : xmemdupz(obj->via.attr.ptr, obj->via.attr.size)), \
})); \ })); \
break; \ break; \
} }
STR_CASE(MSGPACK_OBJECT_STR, str, cur.mobj, *cur.aobj, STRING_OBJ) STR_CASE(MSGPACK_OBJECT_STR, str, cur.mobj, *cur.aobj, STRING_OBJ)
STR_CASE(MSGPACK_OBJECT_BIN, bin, cur.mobj, *cur.aobj, STRING_OBJ) STR_CASE(MSGPACK_OBJECT_BIN, bin, cur.mobj, *cur.aobj, STRING_OBJ)
case MSGPACK_OBJECT_ARRAY: { case MSGPACK_OBJECT_ARRAY: {
const size_t size = cur.mobj->via.array.size; const size_t size = cur.mobj->via.array.size;
if (cur.container) { if (cur.container) {
if (cur.idx >= size) { if (cur.idx >= size) {
(void)kv_pop(stack); (void)kv_pop(stack);
} else {
const size_t idx = cur.idx;
cur.idx++;
kv_last(stack) = cur;
kvi_push(stack, ((MPToAPIObjectStackItem) {
.mobj = &cur.mobj->via.array.ptr[idx],
.aobj = &cur.aobj->data.array.items[idx],
.container = false,
}));
}
} else { } else {
*cur.aobj = ARRAY_OBJ(((Array) { const size_t idx = cur.idx;
.size = size, cur.idx++;
.capacity = size,
.items = (size > 0
? xcalloc(size, sizeof(*cur.aobj->data.array.items))
: NULL),
}));
cur.container = true;
kv_last(stack) = cur; kv_last(stack) = cur;
} kvi_push(stack, ((MPToAPIObjectStackItem) {
break; .mobj = &cur.mobj->via.array.ptr[idx],
} .aobj = &cur.aobj->data.array.items[idx],
case MSGPACK_OBJECT_MAP: {
const size_t size = cur.mobj->via.map.size;
if (cur.container) {
if (cur.idx >= size) {
(void)kv_pop(stack);
} else {
const size_t idx = cur.idx;
cur.idx++;
kv_last(stack) = cur;
const msgpack_object *const key = &cur.mobj->via.map.ptr[idx].key;
switch (key->type) {
#define ID(x) x
STR_CASE(MSGPACK_OBJECT_STR, str, key,
cur.aobj->data.dictionary.items[idx].key, ID)
STR_CASE(MSGPACK_OBJECT_BIN, bin, key,
cur.aobj->data.dictionary.items[idx].key, ID)
#undef ID
case MSGPACK_OBJECT_NIL:
case MSGPACK_OBJECT_BOOLEAN:
case MSGPACK_OBJECT_POSITIVE_INTEGER:
case MSGPACK_OBJECT_NEGATIVE_INTEGER:
#ifdef NVIM_MSGPACK_HAS_FLOAT32
case MSGPACK_OBJECT_FLOAT32:
case MSGPACK_OBJECT_FLOAT64:
#else
case MSGPACK_OBJECT_FLOAT:
#endif
case MSGPACK_OBJECT_EXT:
case MSGPACK_OBJECT_MAP:
case MSGPACK_OBJECT_ARRAY: {
ret = false;
break;
}
}
if (ret) {
kvi_push(stack, ((MPToAPIObjectStackItem) {
.mobj = &cur.mobj->via.map.ptr[idx].val,
.aobj = &cur.aobj->data.dictionary.items[idx].value,
.container = false, .container = false,
})); }));
} }
} } else {
*cur.aobj = ARRAY_OBJ(((Array) {
.size = size,
.capacity = size,
.items = (size > 0
? xcalloc(size, sizeof(*cur.aobj->data.array.items))
: NULL),
}));
cur.container = true;
kv_last(stack) = cur;
}
break;
}
case MSGPACK_OBJECT_MAP: {
const size_t size = cur.mobj->via.map.size;
if (cur.container) {
if (cur.idx >= size) {
(void)kv_pop(stack);
} else { } else {
*cur.aobj = DICTIONARY_OBJ(((Dictionary) { const size_t idx = cur.idx;
.size = size, cur.idx++;
.capacity = size, kv_last(stack) = cur;
.items = (size > 0 const msgpack_object *const key = &cur.mobj->via.map.ptr[idx].key;
switch (key->type) {
#define ID(x) x
STR_CASE(MSGPACK_OBJECT_STR, str, key,
cur.aobj->data.dictionary.items[idx].key, ID)
STR_CASE(MSGPACK_OBJECT_BIN, bin, key,
cur.aobj->data.dictionary.items[idx].key, ID)
#undef ID
case MSGPACK_OBJECT_NIL:
case MSGPACK_OBJECT_BOOLEAN:
case MSGPACK_OBJECT_POSITIVE_INTEGER:
case MSGPACK_OBJECT_NEGATIVE_INTEGER:
#ifdef NVIM_MSGPACK_HAS_FLOAT32
case MSGPACK_OBJECT_FLOAT32:
case MSGPACK_OBJECT_FLOAT64:
#else
case MSGPACK_OBJECT_FLOAT:
#endif
case MSGPACK_OBJECT_EXT:
case MSGPACK_OBJECT_MAP:
case MSGPACK_OBJECT_ARRAY:
ret = false;
break;
}
if (ret) {
kvi_push(stack, ((MPToAPIObjectStackItem) {
.mobj = &cur.mobj->via.map.ptr[idx].val,
.aobj = &cur.aobj->data.dictionary.items[idx].value,
.container = false,
}));
}
}
} else {
*cur.aobj = DICTIONARY_OBJ(((Dictionary) {
.size = size,
.capacity = size,
.items = (size > 0
? xcalloc(size, sizeof(*cur.aobj->data.dictionary.items)) ? xcalloc(size, sizeof(*cur.aobj->data.dictionary.items))
: NULL), : NULL),
})); }));
cur.container = true; cur.container = true;
kv_last(stack) = cur; kv_last(stack) = cur;
} }
break; break;
} }
case MSGPACK_OBJECT_EXT: { case MSGPACK_OBJECT_EXT:
switch ((ObjectType)(cur.mobj->via.ext.type + EXT_OBJECT_TYPE_SHIFT)) { switch ((ObjectType)(cur.mobj->via.ext.type + EXT_OBJECT_TYPE_SHIFT)) {
case kObjectTypeBuffer: { case kObjectTypeBuffer:
cur.aobj->type = kObjectTypeBuffer; cur.aobj->type = kObjectTypeBuffer;
ret = msgpack_rpc_to_buffer(cur.mobj, &cur.aobj->data.integer); ret = msgpack_rpc_to_buffer(cur.mobj, &cur.aobj->data.integer);
break; break;
} case kObjectTypeWindow:
case kObjectTypeWindow: { cur.aobj->type = kObjectTypeWindow;
cur.aobj->type = kObjectTypeWindow; ret = msgpack_rpc_to_window(cur.mobj, &cur.aobj->data.integer);
ret = msgpack_rpc_to_window(cur.mobj, &cur.aobj->data.integer); break;
break; case kObjectTypeTabpage:
} cur.aobj->type = kObjectTypeTabpage;
case kObjectTypeTabpage: { ret = msgpack_rpc_to_tabpage(cur.mobj, &cur.aobj->data.integer);
cur.aobj->type = kObjectTypeTabpage; break;
ret = msgpack_rpc_to_tabpage(cur.mobj, &cur.aobj->data.integer); case kObjectTypeNil:
break; case kObjectTypeBoolean:
} case kObjectTypeInteger:
case kObjectTypeNil: case kObjectTypeFloat:
case kObjectTypeBoolean: case kObjectTypeString:
case kObjectTypeInteger: case kObjectTypeArray:
case kObjectTypeFloat: case kObjectTypeDictionary:
case kObjectTypeString: case kObjectTypeLuaRef:
case kObjectTypeArray:
case kObjectTypeDictionary:
case kObjectTypeLuaRef: {
break;
}
}
break; break;
} }
break;
#undef STR_CASE #undef STR_CASE
} }
if (!cur.container) { if (!cur.container) {
@@ -270,8 +261,7 @@ bool msgpack_rpc_to_object(const msgpack_object *const obj, Object *const arg)
return ret; return ret;
} }
static bool msgpack_rpc_to_string(const msgpack_object *const obj, static bool msgpack_rpc_to_string(const msgpack_object *const obj, String *const arg)
String *const arg)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
if (obj->type == MSGPACK_OBJECT_BIN || obj->type == MSGPACK_OBJECT_STR) { if (obj->type == MSGPACK_OBJECT_BIN || obj->type == MSGPACK_OBJECT_STR) {
@@ -303,8 +293,7 @@ bool msgpack_rpc_to_array(const msgpack_object *const obj, Array *const arg)
return true; return true;
} }
bool msgpack_rpc_to_dictionary(const msgpack_object *const obj, bool msgpack_rpc_to_dictionary(const msgpack_object *const obj, Dictionary *const arg)
Dictionary *const arg)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
if (obj->type != MSGPACK_OBJECT_MAP) { if (obj->type != MSGPACK_OBJECT_MAP) {
@@ -317,12 +306,12 @@ bool msgpack_rpc_to_dictionary(const msgpack_object *const obj,
for (uint32_t i = 0; i < obj->via.map.size; i++) { for (uint32_t i = 0; i < obj->via.map.size; i++) {
if (!msgpack_rpc_to_string(&obj->via.map.ptr[i].key, if (!msgpack_rpc_to_string(&obj->via.map.ptr[i].key,
&arg->items[i].key)) { &arg->items[i].key)) {
return false; return false;
} }
if (!msgpack_rpc_to_object(&obj->via.map.ptr[i].val, if (!msgpack_rpc_to_object(&obj->via.map.ptr[i].val,
&arg->items[i].value)) { &arg->items[i].value)) {
return false; return false;
} }
} }
@@ -385,86 +374,78 @@ void msgpack_rpc_from_object(const Object result, msgpack_packer *const res)
&& kObjectTypeTabpage == kObjectTypeWindow + 1, && kObjectTypeTabpage == kObjectTypeWindow + 1,
"Buffer, window and tabpage enum items are in order"); "Buffer, window and tabpage enum items are in order");
switch (cur.aobj->type) { switch (cur.aobj->type) {
case kObjectTypeNil: case kObjectTypeNil:
case kObjectTypeLuaRef: { case kObjectTypeLuaRef:
// TODO(bfredl): could also be an error. Though kObjectTypeLuaRef // TODO(bfredl): could also be an error. Though kObjectTypeLuaRef
// should only appear when the caller has opted in to handle references, // should only appear when the caller has opted in to handle references,
// see nlua_pop_Object. // see nlua_pop_Object.
msgpack_pack_nil(res); msgpack_pack_nil(res);
break; break;
} case kObjectTypeBoolean:
case kObjectTypeBoolean: { msgpack_rpc_from_boolean(cur.aobj->data.boolean, res);
msgpack_rpc_from_boolean(cur.aobj->data.boolean, res); break;
break; case kObjectTypeInteger:
} msgpack_rpc_from_integer(cur.aobj->data.integer, res);
case kObjectTypeInteger: { break;
msgpack_rpc_from_integer(cur.aobj->data.integer, res); case kObjectTypeFloat:
break; msgpack_rpc_from_float(cur.aobj->data.floating, res);
} break;
case kObjectTypeFloat: { case kObjectTypeString:
msgpack_rpc_from_float(cur.aobj->data.floating, res); msgpack_rpc_from_string(cur.aobj->data.string, res);
break; break;
} case kObjectTypeBuffer:
case kObjectTypeString: { msgpack_rpc_from_buffer(cur.aobj->data.integer, res);
msgpack_rpc_from_string(cur.aobj->data.string, res); break;
break; case kObjectTypeWindow:
} msgpack_rpc_from_window(cur.aobj->data.integer, res);
case kObjectTypeBuffer: { break;
msgpack_rpc_from_buffer(cur.aobj->data.integer, res); case kObjectTypeTabpage:
break; msgpack_rpc_from_tabpage(cur.aobj->data.integer, res);
} break;
case kObjectTypeWindow: { case kObjectTypeArray: {
msgpack_rpc_from_window(cur.aobj->data.integer, res); const size_t size = cur.aobj->data.array.size;
break; if (cur.container) {
} if (cur.idx >= size) {
case kObjectTypeTabpage: { (void)kv_pop(stack);
msgpack_rpc_from_tabpage(cur.aobj->data.integer, res);
break;
}
case kObjectTypeArray: {
const size_t size = cur.aobj->data.array.size;
if (cur.container) {
if (cur.idx >= size) {
(void)kv_pop(stack);
} else {
const size_t idx = cur.idx;
cur.idx++;
kv_last(stack) = cur;
kvi_push(stack, ((APIToMPObjectStackItem) {
.aobj = &cur.aobj->data.array.items[idx],
.container = false,
}));
}
} else { } else {
msgpack_pack_array(res, size); const size_t idx = cur.idx;
cur.container = true; cur.idx++;
kv_last(stack) = cur; kv_last(stack) = cur;
kvi_push(stack, ((APIToMPObjectStackItem) {
.aobj = &cur.aobj->data.array.items[idx],
.container = false,
}));
} }
break; } else {
msgpack_pack_array(res, size);
cur.container = true;
kv_last(stack) = cur;
} }
case kObjectTypeDictionary: { break;
const size_t size = cur.aobj->data.dictionary.size; }
if (cur.container) { case kObjectTypeDictionary: {
if (cur.idx >= size) { const size_t size = cur.aobj->data.dictionary.size;
(void)kv_pop(stack); if (cur.container) {
} else { if (cur.idx >= size) {
const size_t idx = cur.idx; (void)kv_pop(stack);
cur.idx++;
kv_last(stack) = cur;
msgpack_rpc_from_string(cur.aobj->data.dictionary.items[idx].key,
res);
kvi_push(stack, ((APIToMPObjectStackItem) {
.aobj = &cur.aobj->data.dictionary.items[idx].value,
.container = false,
}));
}
} else { } else {
msgpack_pack_map(res, size); const size_t idx = cur.idx;
cur.container = true; cur.idx++;
kv_last(stack) = cur; kv_last(stack) = cur;
msgpack_rpc_from_string(cur.aobj->data.dictionary.items[idx].key,
res);
kvi_push(stack, ((APIToMPObjectStackItem) {
.aobj = &cur.aobj->data.dictionary.items[idx].value,
.container = false,
}));
} }
break; } else {
msgpack_pack_map(res, size);
cur.container = true;
kv_last(stack) = cur;
} }
break;
}
} }
if (!cur.container) { if (!cur.container) {
(void)kv_pop(stack); (void)kv_pop(stack);
@@ -495,9 +476,7 @@ void msgpack_rpc_from_dictionary(Dictionary result, msgpack_packer *res)
} }
/// Serializes a msgpack-rpc request or notification(id == 0) /// Serializes a msgpack-rpc request or notification(id == 0)
void msgpack_rpc_serialize_request(uint32_t request_id, void msgpack_rpc_serialize_request(uint32_t request_id, const String method, Array args,
const String method,
Array args,
msgpack_packer *pac) msgpack_packer *pac)
FUNC_ATTR_NONNULL_ARG(4) FUNC_ATTR_NONNULL_ARG(4)
{ {
@@ -513,9 +492,7 @@ void msgpack_rpc_serialize_request(uint32_t request_id,
} }
/// Serializes a msgpack-rpc response /// Serializes a msgpack-rpc response
void msgpack_rpc_serialize_response(uint32_t response_id, void msgpack_rpc_serialize_response(uint32_t response_id, Error *err, Object arg,
Error *err,
Object arg,
msgpack_packer *pac) msgpack_packer *pac)
FUNC_ATTR_NONNULL_ARG(2, 4) FUNC_ATTR_NONNULL_ARG(2, 4)
{ {
@@ -546,15 +523,15 @@ static bool msgpack_rpc_is_notification(msgpack_object *req)
msgpack_object *msgpack_rpc_method(msgpack_object *req) msgpack_object *msgpack_rpc_method(msgpack_object *req)
{ {
msgpack_object *obj = req->via.array.ptr msgpack_object *obj = req->via.array.ptr
+ (msgpack_rpc_is_notification(req) ? 1 : 2); + (msgpack_rpc_is_notification(req) ? 1 : 2);
return obj->type == MSGPACK_OBJECT_STR || obj->type == MSGPACK_OBJECT_BIN ? return obj->type == MSGPACK_OBJECT_STR || obj->type == MSGPACK_OBJECT_BIN ?
obj : NULL; obj : NULL;
} }
msgpack_object *msgpack_rpc_args(msgpack_object *req) msgpack_object *msgpack_rpc_args(msgpack_object *req)
{ {
msgpack_object *obj = req->via.array.ptr msgpack_object *obj = req->via.array.ptr
+ (msgpack_rpc_is_notification(req) ? 2 : 3); + (msgpack_rpc_is_notification(req) ? 2 : 3);
return obj->type == MSGPACK_OBJECT_ARRAY ? obj : NULL; return obj->type == MSGPACK_OBJECT_ARRAY ? obj : NULL;
} }
@@ -567,8 +544,7 @@ static msgpack_object *msgpack_rpc_msg_id(msgpack_object *req)
return obj->type == MSGPACK_OBJECT_POSITIVE_INTEGER ? obj : NULL; return obj->type == MSGPACK_OBJECT_POSITIVE_INTEGER ? obj : NULL;
} }
MessageType msgpack_rpc_validate(uint32_t *response_id, msgpack_object *req, MessageType msgpack_rpc_validate(uint32_t *response_id, msgpack_object *req, Error *err)
Error *err)
{ {
*response_id = 0; *response_id = 0;
// Validate the basic structure of the msgpack-rpc payload // Validate the basic structure of the msgpack-rpc payload

View File

@@ -2,24 +2,24 @@
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include <assert.h> #include <assert.h>
#include <inttypes.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <inttypes.h>
#include "nvim/ascii.h"
#include "nvim/eval.h"
#include "nvim/event/socket.h"
#include "nvim/fileio.h"
#include "nvim/garray.h"
#include "nvim/log.h"
#include "nvim/main.h"
#include "nvim/memory.h"
#include "nvim/msgpack_rpc/channel.h" #include "nvim/msgpack_rpc/channel.h"
#include "nvim/msgpack_rpc/server.h" #include "nvim/msgpack_rpc/server.h"
#include "nvim/os/os.h" #include "nvim/os/os.h"
#include "nvim/event/socket.h"
#include "nvim/ascii.h"
#include "nvim/eval.h"
#include "nvim/garray.h"
#include "nvim/vim.h"
#include "nvim/main.h"
#include "nvim/memory.h"
#include "nvim/log.h"
#include "nvim/fileio.h"
#include "nvim/path.h" #include "nvim/path.h"
#include "nvim/strings.h" #include "nvim/strings.h"
#include "nvim/vim.h"
#define MAX_CONNECTIONS 32 #define MAX_CONNECTIONS 32
#define LISTEN_ADDRESS_ENV_VAR "NVIM_LISTEN_ADDRESS" #define LISTEN_ADDRESS_ENV_VAR "NVIM_LISTEN_ADDRESS"
@@ -90,7 +90,7 @@ char *server_address_new(void)
static uint32_t count = 0; static uint32_t count = 0;
char template[ADDRESS_MAX_SIZE]; char template[ADDRESS_MAX_SIZE];
snprintf(template, ADDRESS_MAX_SIZE, snprintf(template, ADDRESS_MAX_SIZE,
"\\\\.\\pipe\\nvim-%" PRIu64 "-%" PRIu32, os_get_pid(), count++); "\\\\.\\pipe\\nvim-%" PRIu64 "-%" PRIu32, os_get_pid(), count++);
return xstrdup(template); return xstrdup(template);
#else #else
return (char *)vim_tempname(); return (char *)vim_tempname();

View File

@@ -2473,60 +2473,58 @@ bool do_mouse(oparg_T *oap, int c, int dir, long count, bool fixindent)
in_tab_line = true; in_tab_line = true;
c1 = tab_page_click_defs[mouse_col].tabnr; c1 = tab_page_click_defs[mouse_col].tabnr;
switch (tab_page_click_defs[mouse_col].type) { switch (tab_page_click_defs[mouse_col].type) {
case kStlClickDisabled: { case kStlClickDisabled:
break; break;
}
case kStlClickTabClose: { case kStlClickTabClose: {
tabpage_T *tp; tabpage_T *tp;
// Close the current or specified tab page. // Close the current or specified tab page.
if (c1 == 999) { if (c1 == 999) {
tp = curtab; tp = curtab;
} else { } else {
tp = find_tabpage(c1); tp = find_tabpage(c1);
}
if (tp == curtab) {
if (first_tabpage->tp_next != NULL) {
tabpage_close(false);
}
} else if (tp != NULL) {
tabpage_close_other(tp, false);
}
break;
} }
case kStlClickTabSwitch: { if (tp == curtab) {
if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK) { if (first_tabpage->tp_next != NULL) {
// double click opens new page tabpage_close(false);
}
} else if (tp != NULL) {
tabpage_close_other(tp, false);
}
break;
}
case kStlClickTabSwitch:
if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK) {
// double click opens new page
end_visual_mode();
tabpage_new();
tabpage_move(c1 == 0 ? 9999 : c1 - 1);
} else {
// Go to specified tab page, or next one if not clicking
// on a label.
goto_tabpage(c1);
// It's like clicking on the status line of a window.
if (curwin != old_curwin) {
end_visual_mode(); end_visual_mode();
tabpage_new();
tabpage_move(c1 == 0 ? 9999 : c1 - 1);
} else {
// Go to specified tab page, or next one if not clicking
// on a label.
goto_tabpage(c1);
// It's like clicking on the status line of a window.
if (curwin != old_curwin) {
end_visual_mode();
}
} }
break;
} }
break;
case kStlClickFuncRun: { case kStlClickFuncRun: {
typval_T argv[] = { typval_T argv[] = {
{ {
.v_lock = VAR_FIXED, .v_lock = VAR_FIXED,
.v_type = VAR_NUMBER, .v_type = VAR_NUMBER,
.vval = { .vval = {
.v_number = (varnumber_T)tab_page_click_defs[mouse_col].tabnr .v_number = (varnumber_T)tab_page_click_defs[mouse_col].tabnr
},
}, },
{ },
.v_lock = VAR_FIXED, {
.v_type = VAR_NUMBER, .v_lock = VAR_FIXED,
.vval = { .v_type = VAR_NUMBER,
.v_number = (((mod_mask & MOD_MASK_MULTI_CLICK) .vval = {
== MOD_MASK_4CLICK) .v_number = (((mod_mask & MOD_MASK_MULTI_CLICK)
== MOD_MASK_4CLICK)
? 4 ? 4
: ((mod_mask & MOD_MASK_MULTI_CLICK) : ((mod_mask & MOD_MASK_MULTI_CLICK)
== MOD_MASK_3CLICK) == MOD_MASK_3CLICK)
@@ -2535,43 +2533,43 @@ bool do_mouse(oparg_T *oap, int c, int dir, long count, bool fixindent)
== MOD_MASK_2CLICK) == MOD_MASK_2CLICK)
? 2 ? 2
: 1) : 1)
},
}, },
{ },
.v_lock = VAR_FIXED, {
.v_type = VAR_STRING, .v_lock = VAR_FIXED,
.vval = { .v_string = (char_u *)(which_button == MOUSE_LEFT .v_type = VAR_STRING,
.vval = { .v_string = (char_u *)(which_button == MOUSE_LEFT
? "l" ? "l"
: which_button == MOUSE_RIGHT : which_button == MOUSE_RIGHT
? "r" ? "r"
: which_button == MOUSE_MIDDLE : which_button == MOUSE_MIDDLE
? "m" ? "m"
: "?") }, : "?") },
},
{
.v_lock = VAR_FIXED,
.v_type = VAR_STRING,
.vval = {
.v_string = (char_u[]) {
(char_u)(mod_mask & MOD_MASK_SHIFT ? 's' : ' '),
(char_u)(mod_mask & MOD_MASK_CTRL ? 'c' : ' '),
(char_u)(mod_mask & MOD_MASK_ALT ? 'a' : ' '),
(char_u)(mod_mask & MOD_MASK_META ? 'm' : ' '),
NUL
}
}, },
{ }
.v_lock = VAR_FIXED, };
.v_type = VAR_STRING, typval_T rettv;
.vval = { funcexe_T funcexe = FUNCEXE_INIT;
.v_string = (char_u[]) { funcexe.firstline = curwin->w_cursor.lnum;
(char_u)(mod_mask & MOD_MASK_SHIFT ? 's' : ' '), funcexe.lastline = curwin->w_cursor.lnum;
(char_u)(mod_mask & MOD_MASK_CTRL ? 'c' : ' '), funcexe.evaluate = true;
(char_u)(mod_mask & MOD_MASK_ALT ? 'a' : ' '), (void)call_func((char_u *)tab_page_click_defs[mouse_col].func, -1,
(char_u)(mod_mask & MOD_MASK_META ? 'm' : ' '), &rettv, ARRAY_SIZE(argv), argv, &funcexe);
NUL tv_clear(&rettv);
} break;
}, }
}
};
typval_T rettv;
funcexe_T funcexe = FUNCEXE_INIT;
funcexe.firstline = curwin->w_cursor.lnum;
funcexe.lastline = curwin->w_cursor.lnum;
funcexe.evaluate = true;
(void)call_func((char_u *)tab_page_click_defs[mouse_col].func, -1,
&rettv, ARRAY_SIZE(argv), argv, &funcexe);
tv_clear(&rettv);
break;
}
} }
} }
return true; return true;
@@ -4701,42 +4699,42 @@ dozet:
case 'w': // "zw": add wrong word to word list case 'w': // "zw": add wrong word to word list
case 'G': // "zG": add good word to temp word list case 'G': // "zG": add good word to temp word list
case 'W': // "zW": add wrong word to temp word list case 'W': // "zW": add wrong word to temp word list
{ {
char_u *ptr = NULL; char_u *ptr = NULL;
size_t len; size_t len;
if (checkclearop(cap->oap)) { if (checkclearop(cap->oap)) {
break; break;
}
if (VIsual_active && !get_visual_text(cap, &ptr, &len)) {
return;
}
if (ptr == NULL) {
pos_T pos = curwin->w_cursor;
/* Find bad word under the cursor. When 'spell' is
* off this fails and find_ident_under_cursor() is
* used below. */
emsg_off++;
len = spell_move_to(curwin, FORWARD, true, true, NULL);
emsg_off--;
if (len != 0 && curwin->w_cursor.col <= pos.col) {
ptr = ml_get_pos(&curwin->w_cursor);
}
curwin->w_cursor = pos;
}
if (ptr == NULL && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0) {
return;
}
assert(len <= INT_MAX);
spell_add_word(ptr, (int)len,
nchar == 'w' || nchar == 'W'
? SPELL_ADD_BAD : SPELL_ADD_GOOD,
(nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1,
undo);
} }
break; if (VIsual_active && !get_visual_text(cap, &ptr, &len)) {
return;
}
if (ptr == NULL) {
pos_T pos = curwin->w_cursor;
/* Find bad word under the cursor. When 'spell' is
* off this fails and find_ident_under_cursor() is
* used below. */
emsg_off++;
len = spell_move_to(curwin, FORWARD, true, true, NULL);
emsg_off--;
if (len != 0 && curwin->w_cursor.col <= pos.col) {
ptr = ml_get_pos(&curwin->w_cursor);
}
curwin->w_cursor = pos;
}
if (ptr == NULL && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0) {
return;
}
assert(len <= INT_MAX);
spell_add_word(ptr, (int)len,
nchar == 'w' || nchar == 'W'
? SPELL_ADD_BAD : SPELL_ADD_GOOD,
(nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1,
undo);
}
break;
case '=': // "z=": suggestions for a badly spelled word case '=': // "z=": suggestions for a badly spelled word
if (!checkclearop(cap->oap)) { if (!checkclearop(cap->oap)) {
@@ -7055,19 +7053,19 @@ static void nv_g_cmd(cmdarg_T *cap)
break; break;
case 'M': { case 'M': {
const char_u *const ptr = get_cursor_line_ptr(); const char_u *const ptr = get_cursor_line_ptr();
oap->motion_type = kMTCharWise; oap->motion_type = kMTCharWise;
oap->inclusive = false; oap->inclusive = false;
i = (int)mb_string2cells_len(ptr, STRLEN(ptr)); i = (int)mb_string2cells_len(ptr, STRLEN(ptr));
if (cap->count0 > 0 && cap->count0 <= 100) { if (cap->count0 > 0 && cap->count0 <= 100) {
coladvance((colnr_T)(i * cap->count0 / 100)); coladvance((colnr_T)(i * cap->count0 / 100));
} else { } else {
coladvance((colnr_T)(i / 2)); coladvance((colnr_T)(i / 2));
}
curwin->w_set_curswant = true;
} }
break; curwin->w_set_curswant = true;
}
break;
case '_': case '_':
/* "g_": to the last non-blank character in the line or <count> lines /* "g_": to the last non-blank character in the line or <count> lines
@@ -7099,57 +7097,57 @@ static void nv_g_cmd(cmdarg_T *cap)
case '$': case '$':
case K_END: case K_END:
case K_KEND: { case K_KEND: {
int col_off = curwin_col_off(); int col_off = curwin_col_off();
oap->motion_type = kMTCharWise; oap->motion_type = kMTCharWise;
oap->inclusive = true; oap->inclusive = true;
if (curwin->w_p_wrap if (curwin->w_p_wrap
&& curwin->w_width_inner != 0) { && curwin->w_width_inner != 0) {
curwin->w_curswant = MAXCOL; // so we stay at the end curwin->w_curswant = MAXCOL; // so we stay at the end
if (cap->count1 == 1) { if (cap->count1 == 1) {
int width1 = curwin->w_width_inner - col_off; int width1 = curwin->w_width_inner - col_off;
int width2 = width1 + curwin_col_off2(); int width2 = width1 + curwin_col_off2();
validate_virtcol(); validate_virtcol();
i = width1 - 1; i = width1 - 1;
if (curwin->w_virtcol >= (colnr_T)width1) { if (curwin->w_virtcol >= (colnr_T)width1) {
i += ((curwin->w_virtcol - width1) / width2 + 1) i += ((curwin->w_virtcol - width1) / width2 + 1)
* width2; * width2;
}
coladvance((colnr_T)i);
// Make sure we stick in this column.
validate_virtcol();
curwin->w_curswant = curwin->w_virtcol;
curwin->w_set_curswant = false;
if (curwin->w_cursor.col > 0 && curwin->w_p_wrap) {
/*
* Check for landing on a character that got split at
* the end of the line. We do not want to advance to
* the next screen line.
*/
if (curwin->w_virtcol > (colnr_T)i) {
--curwin->w_cursor.col;
}
}
} else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == false) {
clearopbeep(oap);
} }
} else {
if (cap->count1 > 1) {
// if it fails, let the cursor still move to the last char
(void)cursor_down(cap->count1 - 1, false);
}
i = curwin->w_leftcol + curwin->w_width_inner - col_off - 1;
coladvance((colnr_T)i); coladvance((colnr_T)i);
// Make sure we stick in this column. // Make sure we stick in this column.
validate_virtcol(); validate_virtcol();
curwin->w_curswant = curwin->w_virtcol; curwin->w_curswant = curwin->w_virtcol;
curwin->w_set_curswant = false; curwin->w_set_curswant = false;
if (curwin->w_cursor.col > 0 && curwin->w_p_wrap) {
/*
* Check for landing on a character that got split at
* the end of the line. We do not want to advance to
* the next screen line.
*/
if (curwin->w_virtcol > (colnr_T)i) {
--curwin->w_cursor.col;
}
}
} else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == false) {
clearopbeep(oap);
} }
} else {
if (cap->count1 > 1) {
// if it fails, let the cursor still move to the last char
(void)cursor_down(cap->count1 - 1, false);
}
i = curwin->w_leftcol + curwin->w_width_inner - col_off - 1;
coladvance((colnr_T)i);
// Make sure we stick in this column.
validate_virtcol();
curwin->w_curswant = curwin->w_virtcol;
curwin->w_set_curswant = false;
} }
break; }
break;
/* /*
* "g*" and "g#", like "*" and "#" but without using "\<" and "\>" * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"

View File

@@ -2622,64 +2622,64 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append)
break; break;
case kMTCharWise: { case kMTCharWise: {
colnr_T startcol = 0, endcol = MAXCOL; colnr_T startcol = 0, endcol = MAXCOL;
int is_oneChar = false; int is_oneChar = false;
colnr_T cs, ce; colnr_T cs, ce;
p = ml_get(lnum); p = ml_get(lnum);
bd.startspaces = 0; bd.startspaces = 0;
bd.endspaces = 0; bd.endspaces = 0;
if (lnum == oap->start.lnum) { if (lnum == oap->start.lnum) {
startcol = oap->start.col; startcol = oap->start.col;
if (virtual_op) { if (virtual_op) {
getvcol(curwin, &oap->start, &cs, NULL, &ce); getvcol(curwin, &oap->start, &cs, NULL, &ce);
if (ce != cs && oap->start.coladd > 0) { if (ce != cs && oap->start.coladd > 0) {
/* Part of a tab selected -- but don't /* Part of a tab selected -- but don't
* double-count it. */ * double-count it. */
bd.startspaces = (ce - cs + 1) bd.startspaces = (ce - cs + 1)
- oap->start.coladd; - oap->start.coladd;
startcol++; startcol++;
}
} }
} }
if (lnum == oap->end.lnum) {
endcol = oap->end.col;
if (virtual_op) {
getvcol(curwin, &oap->end, &cs, NULL, &ce);
if (p[endcol] == NUL || (cs + oap->end.coladd < ce
// Don't add space for double-wide
// char; endcol will be on last byte
// of multi-byte char.
&& utf_head_off(p, p + endcol) == 0)) {
if (oap->start.lnum == oap->end.lnum
&& oap->start.col == oap->end.col) {
// Special case: inside a single char
is_oneChar = true;
bd.startspaces = oap->end.coladd
- oap->start.coladd + oap->inclusive;
endcol = startcol;
} else {
bd.endspaces = oap->end.coladd
+ oap->inclusive;
endcol -= oap->inclusive;
}
}
}
}
if (endcol == MAXCOL) {
endcol = (colnr_T)STRLEN(p);
}
if (startcol > endcol
|| is_oneChar) {
bd.textlen = 0;
} else {
bd.textlen = endcol - startcol + oap->inclusive;
}
bd.textstart = p + startcol;
yank_copy_line(reg, &bd, y_idx, false);
break;
} }
if (lnum == oap->end.lnum) {
endcol = oap->end.col;
if (virtual_op) {
getvcol(curwin, &oap->end, &cs, NULL, &ce);
if (p[endcol] == NUL || (cs + oap->end.coladd < ce
// Don't add space for double-wide
// char; endcol will be on last byte
// of multi-byte char.
&& utf_head_off(p, p + endcol) == 0)) {
if (oap->start.lnum == oap->end.lnum
&& oap->start.col == oap->end.col) {
// Special case: inside a single char
is_oneChar = true;
bd.startspaces = oap->end.coladd
- oap->start.coladd + oap->inclusive;
endcol = startcol;
} else {
bd.endspaces = oap->end.coladd
+ oap->inclusive;
endcol -= oap->inclusive;
}
}
}
}
if (endcol == MAXCOL) {
endcol = (colnr_T)STRLEN(p);
}
if (startcol > endcol
|| is_oneChar) {
bd.textlen = 0;
} else {
bd.textlen = endcol - startcol + oap->inclusive;
}
bd.textstart = p + startcol;
yank_copy_line(reg, &bd, y_idx, false);
break;
}
// NOTREACHED // NOTREACHED
case kMTUnknown: case kMTUnknown:
abort(); abort();
@@ -5781,15 +5781,15 @@ void cursor_pos_info(dict_T *dict)
len = MAXCOL; len = MAXCOL;
break; break;
case 'v': { case 'v': {
colnr_T start_col = (lnum == min_pos.lnum) colnr_T start_col = (lnum == min_pos.lnum)
? min_pos.col : 0; ? min_pos.col : 0;
colnr_T end_col = (lnum == max_pos.lnum) colnr_T end_col = (lnum == max_pos.lnum)
? max_pos.col - start_col + 1 : MAXCOL; ? max_pos.col - start_col + 1 : MAXCOL;
s = ml_get(lnum) + start_col; s = ml_get(lnum) + start_col;
len = end_col; len = end_col;
} }
break; break;
} }
if (s != NULL) { if (s != NULL) {
byte_count_cursor += line_count_info(s, &word_count_cursor, byte_count_cursor += line_count_info(s, &word_count_cursor,
@@ -6221,23 +6221,19 @@ static void set_clipboard(int name, yankreg_T *reg)
char regtype; char regtype;
switch (reg->y_type) { switch (reg->y_type) {
case kMTLineWise: { case kMTLineWise:
regtype = 'V'; regtype = 'V';
tv_list_append_string(lines, NULL, 0); tv_list_append_string(lines, NULL, 0);
break; break;
} case kMTCharWise:
case kMTCharWise: { regtype = 'v';
regtype = 'v'; break;
break; case kMTBlockWise:
} regtype = 'b';
case kMTBlockWise: { tv_list_append_string(lines, NULL, 0);
regtype = 'b'; break;
tv_list_append_string(lines, NULL, 0); case kMTUnknown:
break; abort();
}
case kMTUnknown: {
abort();
}
} }
list_T *args = tv_list_alloc(3); list_T *args = tv_list_alloc(3);

View File

@@ -5844,21 +5844,21 @@ char_u *get_syntax_name(expand_T *xp, int idx)
case EXP_SUBCMD: case EXP_SUBCMD:
return (char_u *)subcommands[idx].name; return (char_u *)subcommands[idx].name;
case EXP_CASE: { case EXP_CASE: {
static char *case_args[] = { "match", "ignore", NULL }; static char *case_args[] = { "match", "ignore", NULL };
return (char_u *)case_args[idx]; return (char_u *)case_args[idx];
} }
case EXP_SPELL: { case EXP_SPELL: {
static char *spell_args[] = static char *spell_args[] =
{ "toplevel", "notoplevel", "default", NULL }; { "toplevel", "notoplevel", "default", NULL };
return (char_u *)spell_args[idx]; return (char_u *)spell_args[idx];
} }
case EXP_SYNC: { case EXP_SYNC: {
static char *sync_args[] = static char *sync_args[] =
{ "ccomment", "clear", "fromstart", { "ccomment", "clear", "fromstart",
"linebreaks=", "linecont", "lines=", "match", "linebreaks=", "linecont", "lines=", "match",
"maxlines=", "minlines=", "region", NULL }; "maxlines=", "minlines=", "region", NULL };
return (char_u *)sync_args[idx]; return (char_u *)sync_args[idx];
} }
} }
return NULL; return NULL;
} }