refactor(api): cleanup modify_keymap and parse_keymap_opts

This commit is contained in:
Björn Linse 2021-09-29 16:56:25 +02:00
parent 83778ce567
commit 413bb6b7a4
2 changed files with 16 additions and 45 deletions

View File

@ -818,13 +818,6 @@ Array string_to_array(const String input, bool crlf)
void modify_keymap(Buffer buffer, bool is_unmap, String mode, String lhs, String rhs, void modify_keymap(Buffer buffer, bool is_unmap, String mode, String lhs, String rhs,
Dict(keymap) *opts, Error *err) Dict(keymap) *opts, Error *err)
{ {
char *err_msg = NULL; // the error message to report, if any
char *err_arg = NULL; // argument for the error message format string
ErrorType err_type = kErrorTypeNone;
char_u *lhs_buf = NULL;
char_u *rhs_buf = NULL;
bool global = (buffer == -1); bool global = (buffer == -1);
if (global) { if (global) {
buffer = 0; buffer = 0;
@ -858,17 +851,13 @@ void modify_keymap(Buffer buffer, bool is_unmap, String mode, String lhs, String
CPO_TO_CPO_FLAGS, &parsed_args); CPO_TO_CPO_FLAGS, &parsed_args);
if (parsed_args.lhs_len > MAXMAPLEN) { if (parsed_args.lhs_len > MAXMAPLEN) {
err_msg = "LHS exceeds maximum map length: %s"; api_set_error(err, kErrorTypeValidation, "LHS exceeds maximum map length: %s", lhs.data);
err_arg = lhs.data; goto fail_and_free;
err_type = kErrorTypeValidation;
goto fail_with_message;
} }
if (mode.size > 1) { if (mode.size > 1) {
err_msg = "Shortname is too long: %s"; api_set_error(err, kErrorTypeValidation, "Shortname is too long: %s", mode.data);
err_arg = mode.data; goto fail_and_free;
err_type = kErrorTypeValidation;
goto fail_with_message;
} }
int mode_val; // integer value of the mapping mode, to be passed to do_map() int mode_val; // integer value of the mapping mode, to be passed to do_map()
char_u *p = (char_u *)((mode.size) ? mode.data : "m"); char_u *p = (char_u *)((mode.size) ? mode.data : "m");
@ -880,18 +869,14 @@ void modify_keymap(Buffer buffer, bool is_unmap, String mode, String lhs, String
&& mode.size > 0) { && mode.size > 0) {
// get_map_mode() treats unrecognized mode shortnames as ":map". // get_map_mode() treats unrecognized mode shortnames as ":map".
// This is an error unless the given shortname was empty string "". // This is an error unless the given shortname was empty string "".
err_msg = "Invalid mode shortname: \"%s\""; api_set_error(err, kErrorTypeValidation, "Invalid mode shortname: \"%s\"", (char *)p);
err_arg = (char *)p; goto fail_and_free;
err_type = kErrorTypeValidation;
goto fail_with_message;
} }
} }
if (parsed_args.lhs_len == 0) { if (parsed_args.lhs_len == 0) {
err_msg = "Invalid (empty) LHS"; api_set_error(err, kErrorTypeValidation, "Invalid (empty) LHS");
err_arg = ""; goto fail_and_free;
err_type = kErrorTypeValidation;
goto fail_with_message;
} }
bool is_noremap = parsed_args.noremap; bool is_noremap = parsed_args.noremap;
@ -904,16 +889,13 @@ void modify_keymap(Buffer buffer, bool is_unmap, String mode, String lhs, String
// the given RHS was nonempty and not a <Nop>, but was parsed as if it // the given RHS was nonempty and not a <Nop>, but was parsed as if it
// were empty? // were empty?
assert(false && "Failed to parse nonempty RHS!"); assert(false && "Failed to parse nonempty RHS!");
err_msg = "Parsing of nonempty RHS failed: %s"; api_set_error(err, kErrorTypeValidation, "Parsing of nonempty RHS failed: %s", rhs.data);
err_arg = rhs.data; goto fail_and_free;
err_type = kErrorTypeException;
goto fail_with_message;
} }
} else if (is_unmap && parsed_args.rhs_len) { } else if (is_unmap && parsed_args.rhs_len) {
err_msg = "Gave nonempty RHS in unmap command: %s"; api_set_error(err, kErrorTypeValidation,
err_arg = (char *)parsed_args.rhs; "Gave nonempty RHS in unmap command: %s", parsed_args.rhs);
err_type = kErrorTypeValidation; goto fail_and_free;
goto fail_with_message;
} }
// buf_do_map() reads noremap/unmap as its own argument. // buf_do_map() reads noremap/unmap as its own argument.
@ -942,19 +924,7 @@ void modify_keymap(Buffer buffer, bool is_unmap, String mode, String lhs, String
goto fail_and_free; goto fail_and_free;
} // switch } // switch
xfree(lhs_buf);
xfree(rhs_buf);
xfree(parsed_args.rhs);
xfree(parsed_args.orig_rhs);
return;
fail_with_message:
api_set_error(err, err_type, err_msg, err_arg);
fail_and_free: fail_and_free:
xfree(lhs_buf);
xfree(rhs_buf);
xfree(parsed_args.rhs); xfree(parsed_args.rhs);
xfree(parsed_args.orig_rhs); xfree(parsed_args.orig_rhs);
return; return;

View File

@ -189,7 +189,8 @@ case type: { \
if (0 <= cur.mobj->via.ext.type && cur.mobj->via.ext.type <= EXT_OBJECT_TYPE_MAX) { if (0 <= cur.mobj->via.ext.type && cur.mobj->via.ext.type <= EXT_OBJECT_TYPE_MAX) {
cur.aobj->type = (ObjectType)(cur.mobj->via.ext.type + EXT_OBJECT_TYPE_SHIFT); cur.aobj->type = (ObjectType)(cur.mobj->via.ext.type + EXT_OBJECT_TYPE_SHIFT);
msgpack_object data; msgpack_object data;
msgpack_unpack_return status = msgpack_unpack(cur.mobj->via.ext.ptr, cur.mobj->via.ext.size, NULL, &zone, &data); msgpack_unpack_return status = msgpack_unpack(cur.mobj->via.ext.ptr, cur.mobj->via.ext.size,
NULL, &zone, &data);
if (status != MSGPACK_UNPACK_SUCCESS || data.type != MSGPACK_OBJECT_POSITIVE_INTEGER) { if (status != MSGPACK_UNPACK_SUCCESS || data.type != MSGPACK_OBJECT_POSITIVE_INTEGER) {
ret = false; ret = false;
@ -304,7 +305,7 @@ static void msgpack_rpc_from_handle(ObjectType type, Integer o, msgpack_packer *
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, (int8_t)type - EXT_OBJECT_TYPE_SHIFT); msgpack_pack_ext(res, sbuffer.size, (int8_t)(type - 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);
} }