mirror of
https://github.com/neovim/neovim.git
synced 2025-10-03 00:18:33 +00:00
refactor(api): consistent VALIDATE messages #22262
Problem: Validation messages are not consistently formatted. - Parameter names sometimes are NOT quoted. - Descriptive names (non-parameters) sometimes ARE quoted. Solution: Always quote the `name` value passed to a VALIDATE macro _unless_ the value has whitespace.
This commit is contained in:
@@ -9,6 +9,55 @@
|
||||
# include "api/private/validate.c.generated.h"
|
||||
#endif
|
||||
|
||||
/// Creates "Invalid …" message and sets it on `err`.
|
||||
void api_err_invalid(Error *err, const char *name, const char *val_s, int64_t val_n, bool quote_val)
|
||||
{
|
||||
ErrorType errtype = kErrorTypeValidation;
|
||||
// Treat `name` without whitespace as a parameter (surround in quotes).
|
||||
// Treat `name` with whitespace as a description (no quotes).
|
||||
char *has_space = strchr(name, ' ');
|
||||
|
||||
// No value.
|
||||
if (val_s && val_s[0] == '\0') {
|
||||
api_set_error(err, errtype, has_space ? "Invalid %s" : "Invalid '%s'", name);
|
||||
return;
|
||||
}
|
||||
|
||||
// Number value.
|
||||
if (val_s == NULL) {
|
||||
api_set_error(err, errtype, has_space ? "Invalid %s: %" PRId64 : "Invalid '%s': %" PRId64,
|
||||
name, val_n);
|
||||
return;
|
||||
}
|
||||
|
||||
// String value.
|
||||
if (has_space) {
|
||||
api_set_error(err, errtype, quote_val ? "Invalid %s: '%s'" : "Invalid %s: %s", name, val_s);
|
||||
} else {
|
||||
api_set_error(err, errtype, quote_val ? "Invalid '%s': '%s'" : "Invalid '%s': %s", name, val_s);
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates "Invalid …: expected …" message and sets it on `err`.
|
||||
void api_err_exp(Error *err, const char *name, const char *expected, const char *actual)
|
||||
{
|
||||
ErrorType errtype = kErrorTypeValidation;
|
||||
// Treat `name` without whitespace as a parameter (surround in quotes).
|
||||
// Treat `name` with whitespace as a description (no quotes).
|
||||
char *has_space = strchr(name, ' ');
|
||||
|
||||
if (!actual) {
|
||||
api_set_error(err, errtype,
|
||||
has_space ? "Invalid %s: expected %s" : "Invalid '%s': expected %s",
|
||||
name, expected);
|
||||
return;
|
||||
}
|
||||
|
||||
api_set_error(err, errtype,
|
||||
has_space ? "Invalid %s: expected %s, got %s" : "Invalid '%s': expected %s, got %s",
|
||||
name, expected, actual);
|
||||
}
|
||||
|
||||
bool check_string_array(Array arr, char *name, bool disallow_nl, Error *err)
|
||||
{
|
||||
snprintf(IObuff, sizeof(IObuff), "'%s' item", name);
|
||||
|
Reference in New Issue
Block a user