vim-patch:8.2.3689: ex_let_one() is too long (#23830)

Problem:    ex_let_one() is too long.
Solution:   Split into multiple functions.

3ccb579516

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq 2023-05-30 22:56:27 +08:00 committed by GitHub
parent 70da793c5e
commit f215a2aeaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -701,30 +701,18 @@ static const char *list_arg_vars(exarg_T *eap, const char *arg, int *first)
return arg;
}
/// Set one item of `:let var = expr` or `:let [v1, v2] = list` to its value
///
/// @param[in] arg Start of the variable name.
/// @param[in] tv Value to assign to the variable.
/// @param[in] copy If true, copy value from `tv`.
/// @param[in] endchars Valid characters after variable name or NULL.
/// @param[in] op Operation performed: *op is `+`, `-`, `.` for `+=`, etc.
/// NULL for `=`.
///
/// @return a pointer to the char just after the var name or NULL in case of
/// error.
static char *ex_let_one(char *arg, typval_T *const tv, const bool copy, const bool is_const,
/// Set an environment variable, part of ex_let_one().
static char *ex_let_env(char *arg, typval_T *const tv, const bool is_const,
const char *const endchars, const char *const op)
FUNC_ATTR_NONNULL_ARG(1, 2) FUNC_ATTR_WARN_UNUSED_RESULT
{
char *arg_end = NULL;
// ":let $VAR = expr": Set environment variable.
if (*arg == '$') {
if (is_const) {
emsg(_("E996: Cannot lock an environment variable"));
return NULL;
}
// Find the end of the name.
char *arg_end = NULL;
arg++;
char *name = arg;
int len = get_env_len((const char **)&arg);
@ -757,15 +745,21 @@ static char *ex_let_one(char *arg, typval_T *const tv, const bool copy, const bo
xfree(tofree);
}
}
// ":let &option = expr": Set option value.
// ":let &l:option = expr": Set local option value.
// ":let &g:option = expr": Set global option value.
} else if (*arg == '&') {
return arg_end;
}
/// Set an option, part of ex_let_one().
static char *ex_let_option(char *arg, typval_T *const tv, const bool is_const,
const char *const endchars, const char *const op)
FUNC_ATTR_NONNULL_ARG(1, 2) FUNC_ATTR_WARN_UNUSED_RESULT
{
if (is_const) {
emsg(_("E996: Cannot lock an option"));
return NULL;
}
// Find the end of the name.
char *arg_end = NULL;
int scope;
char *const p = (char *)find_option_end((const char **)&arg, &scope);
if (p == NULL
@ -850,12 +844,20 @@ static char *ex_let_one(char *arg, typval_T *const tv, const bool copy, const bo
xfree(stringval);
xfree(tofree);
}
// ":let @r = expr": Set register contents.
} else if (*arg == '@') {
return arg_end;
}
/// Set a register, part of ex_let_one().
static char *ex_let_register(char *arg, typval_T *const tv, const bool is_const,
const char *const endchars, const char *const op)
FUNC_ATTR_NONNULL_ARG(1, 2) FUNC_ATTR_WARN_UNUSED_RESULT
{
if (is_const) {
emsg(_("E996: Cannot lock a register"));
return NULL;
}
char *arg_end = NULL;
arg++;
if (op != NULL && vim_strchr("+-*/%", (uint8_t)(*op)) != NULL) {
semsg(_(e_letwrong), op);
@ -879,11 +881,41 @@ static char *ex_let_one(char *arg, typval_T *const tv, const bool copy, const bo
}
xfree(ptofree);
}
return arg_end;
}
/// Set one item of `:let var = expr` or `:let [v1, v2] = list` to its value
///
/// @param[in] arg Start of the variable name.
/// @param[in] tv Value to assign to the variable.
/// @param[in] copy If true, copy value from `tv`.
/// @param[in] endchars Valid characters after variable name or NULL.
/// @param[in] op Operation performed: *op is `+`, `-`, `.` for `+=`, etc.
/// NULL for `=`.
///
/// @return a pointer to the char just after the var name or NULL in case of
/// error.
static char *ex_let_one(char *arg, typval_T *const tv, const bool copy, const bool is_const,
const char *const endchars, const char *const op)
FUNC_ATTR_NONNULL_ARG(1, 2) FUNC_ATTR_WARN_UNUSED_RESULT
{
char *arg_end = NULL;
if (*arg == '$') {
// ":let $VAR = expr": Set environment variable.
return ex_let_env(arg, tv, is_const, endchars, op);
} else if (*arg == '&') {
// ":let &option = expr": Set option value.
// ":let &l:option = expr": Set local option value.
// ":let &g:option = expr": Set global option value.
return ex_let_option(arg, tv, is_const, endchars, op);
} else if (*arg == '@') {
// ":let @r = expr": Set register contents.
return ex_let_register(arg, tv, is_const, endchars, op);
} else if (eval_isnamec1(*arg) || *arg == '{') {
// ":let var = expr": Set internal variable.
// ":let {expr} = expr": Idem, name made with curly braces
} else if (eval_isnamec1(*arg) || *arg == '{') {
lval_T lv;
char *const p = get_lval(arg, tv, &lv, false, false, 0, FNE_CHECK_START);
if (p != NULL && lv.ll_name != NULL) {
if (endchars != NULL && vim_strchr(endchars, (uint8_t)(*skipwhite(p))) == NULL) {