mirror of
https://github.com/neovim/neovim.git
synced 2025-09-24 20:18:32 +00:00
refactor(api): VALIDATE macros #22256
- VALIDATE() takes a format string - deduplicate check_string_array - VALIDATE_RANGE - validate UI args
This commit is contained in:
@@ -520,7 +520,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
|
||||
// For backward compatibility we support "end_line" as an alias for "end_row"
|
||||
if (HAS_KEY(opts->end_line)) {
|
||||
VALIDATE(!HAS_KEY(opts->end_row), "cannot use both end_row and end_line", {
|
||||
VALIDATE(!HAS_KEY(opts->end_row), "%s", "cannot use both 'end_row' and 'end_line'", {
|
||||
goto error;
|
||||
});
|
||||
opts->end_row = opts->end_line;
|
||||
@@ -535,30 +535,29 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
bool strict = true;
|
||||
OPTION_TO_BOOL(strict, strict, true);
|
||||
|
||||
if (opts->end_row.type == kObjectTypeInteger) {
|
||||
Integer val = opts->end_row.data.integer;
|
||||
VALIDATE((val >= 0 && !(val > buf->b_ml.ml_line_count && strict)),
|
||||
"end_row value outside range", {
|
||||
goto error;
|
||||
});
|
||||
line2 = (int)val;
|
||||
} else if (HAS_KEY(opts->end_row)) {
|
||||
if (HAS_KEY(opts->end_row)) {
|
||||
VALIDATE_T("end_row", kObjectTypeInteger, opts->end_row.type, {
|
||||
goto error;
|
||||
});
|
||||
|
||||
Integer val = opts->end_row.data.integer;
|
||||
VALIDATE_RANGE((val >= 0 && !(val > buf->b_ml.ml_line_count && strict)), "end_row", {
|
||||
goto error;
|
||||
});
|
||||
line2 = (int)val;
|
||||
}
|
||||
|
||||
colnr_T col2 = -1;
|
||||
if (opts->end_col.type == kObjectTypeInteger) {
|
||||
Integer val = opts->end_col.data.integer;
|
||||
VALIDATE((val >= 0 && val <= MAXCOL), "end_col value outside range", {
|
||||
goto error;
|
||||
});
|
||||
col2 = (int)val;
|
||||
} else if (HAS_KEY(opts->end_col)) {
|
||||
if (HAS_KEY(opts->end_col)) {
|
||||
VALIDATE_T("end_col", kObjectTypeInteger, opts->end_col.type, {
|
||||
goto error;
|
||||
});
|
||||
|
||||
Integer val = opts->end_col.data.integer;
|
||||
VALIDATE_RANGE((val >= 0 && val <= MAXCOL), "end_col", {
|
||||
goto error;
|
||||
});
|
||||
col2 = (int)val;
|
||||
}
|
||||
|
||||
// uncrustify:off
|
||||
@@ -588,33 +587,37 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
}
|
||||
}
|
||||
|
||||
if (opts->conceal.type == kObjectTypeString) {
|
||||
if (HAS_KEY(opts->conceal)) {
|
||||
VALIDATE_T("conceal", kObjectTypeString, opts->conceal.type, {
|
||||
goto error;
|
||||
});
|
||||
|
||||
String c = opts->conceal.data.string;
|
||||
decor.conceal = true;
|
||||
if (c.size) {
|
||||
decor.conceal_char = utf_ptr2char(c.data);
|
||||
}
|
||||
has_decor = true;
|
||||
} else if (HAS_KEY(opts->conceal)) {
|
||||
VALIDATE_T("conceal", kObjectTypeString, opts->conceal.type, {
|
||||
goto error;
|
||||
});
|
||||
}
|
||||
|
||||
if (opts->virt_text.type == kObjectTypeArray) {
|
||||
if (HAS_KEY(opts->virt_text)) {
|
||||
VALIDATE_T("virt_text", kObjectTypeArray, opts->virt_text.type, {
|
||||
goto error;
|
||||
});
|
||||
|
||||
decor.virt_text = parse_virt_text(opts->virt_text.data.array, err,
|
||||
&decor.virt_text_width);
|
||||
has_decor = true;
|
||||
if (ERROR_SET(err)) {
|
||||
goto error;
|
||||
}
|
||||
} else if (HAS_KEY(opts->virt_text)) {
|
||||
VALIDATE_T("virt_text", kObjectTypeArray, opts->virt_text.type, {
|
||||
goto error;
|
||||
});
|
||||
}
|
||||
|
||||
if (opts->virt_text_pos.type == kObjectTypeString) {
|
||||
if (HAS_KEY(opts->virt_text_pos)) {
|
||||
VALIDATE_T("virt_text_pos", kObjectTypeString, opts->virt_text_pos.type, {
|
||||
goto error;
|
||||
});
|
||||
|
||||
String str = opts->virt_text_pos.data.string;
|
||||
if (strequal("eol", str.data)) {
|
||||
decor.virt_text_pos = kVTEndOfLine;
|
||||
@@ -627,19 +630,15 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
goto error;
|
||||
});
|
||||
}
|
||||
} else if (HAS_KEY(opts->virt_text_pos)) {
|
||||
VALIDATE_T("virt_text_pos", kObjectTypeString, opts->virt_text_pos.type, {
|
||||
goto error;
|
||||
});
|
||||
}
|
||||
|
||||
if (opts->virt_text_win_col.type == kObjectTypeInteger) {
|
||||
decor.col = (int)opts->virt_text_win_col.data.integer;
|
||||
decor.virt_text_pos = kVTWinCol;
|
||||
} else if (HAS_KEY(opts->virt_text_win_col)) {
|
||||
if (HAS_KEY(opts->virt_text_win_col)) {
|
||||
VALIDATE_T("virt_text_win_col", kObjectTypeInteger, opts->virt_text_win_col.type, {
|
||||
goto error;
|
||||
});
|
||||
|
||||
decor.col = (int)opts->virt_text_win_col.data.integer;
|
||||
decor.virt_text_pos = kVTWinCol;
|
||||
}
|
||||
|
||||
OPTION_TO_BOOL(decor.virt_text_hide, virt_text_hide, false);
|
||||
@@ -667,7 +666,11 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
bool virt_lines_leftcol = false;
|
||||
OPTION_TO_BOOL(virt_lines_leftcol, virt_lines_leftcol, false);
|
||||
|
||||
if (opts->virt_lines.type == kObjectTypeArray) {
|
||||
if (HAS_KEY(opts->virt_lines)) {
|
||||
VALIDATE_T("virt_lines", kObjectTypeArray, opts->virt_lines.type, {
|
||||
goto error;
|
||||
});
|
||||
|
||||
Array a = opts->virt_lines.data.array;
|
||||
for (size_t j = 0; j < a.size; j++) {
|
||||
VALIDATE_T("virt_text_line", kObjectTypeArray, a.items[j].type, {
|
||||
@@ -681,37 +684,33 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
}
|
||||
has_decor = true;
|
||||
}
|
||||
} else if (HAS_KEY(opts->virt_lines)) {
|
||||
VALIDATE_T("virt_lines", kObjectTypeArray, opts->virt_lines.type, {
|
||||
goto error;
|
||||
});
|
||||
}
|
||||
|
||||
OPTION_TO_BOOL(decor.virt_lines_above, virt_lines_above, false);
|
||||
|
||||
if (opts->priority.type == kObjectTypeInteger) {
|
||||
Integer val = opts->priority.data.integer;
|
||||
|
||||
VALIDATE_S((val >= 0 && val <= UINT16_MAX), "priority", "(out of range)", {
|
||||
goto error;
|
||||
});
|
||||
decor.priority = (DecorPriority)val;
|
||||
} else if (HAS_KEY(opts->priority)) {
|
||||
if (HAS_KEY(opts->priority)) {
|
||||
VALIDATE_T("priority", kObjectTypeInteger, opts->priority.type, {
|
||||
goto error;
|
||||
});
|
||||
|
||||
Integer val = opts->priority.data.integer;
|
||||
|
||||
VALIDATE_RANGE((val >= 0 && val <= UINT16_MAX), "priority", {
|
||||
goto error;
|
||||
});
|
||||
decor.priority = (DecorPriority)val;
|
||||
}
|
||||
|
||||
if (opts->sign_text.type == kObjectTypeString) {
|
||||
if (HAS_KEY(opts->sign_text)) {
|
||||
VALIDATE_T("sign_text", kObjectTypeString, opts->sign_text.type, {
|
||||
goto error;
|
||||
});
|
||||
|
||||
VALIDATE_S(init_sign_text(&decor.sign_text, opts->sign_text.data.string.data),
|
||||
"sign_text", "", {
|
||||
goto error;
|
||||
});
|
||||
has_decor = true;
|
||||
} else if (HAS_KEY(opts->sign_text)) {
|
||||
VALIDATE_T("sign_text", kObjectTypeString, opts->sign_text.type, {
|
||||
goto error;
|
||||
});
|
||||
}
|
||||
|
||||
bool right_gravity = true;
|
||||
@@ -720,7 +719,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
// Only error out if they try to set end_right_gravity without
|
||||
// setting end_col or end_row
|
||||
VALIDATE(!(line2 == -1 && col2 == -1 && HAS_KEY(opts->end_right_gravity)),
|
||||
"cannot set end_right_gravity without setting end_row or end_col", {
|
||||
"%s", "cannot set end_right_gravity without end_row or end_col", {
|
||||
goto error;
|
||||
});
|
||||
|
||||
@@ -732,7 +731,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
bool ephemeral = false;
|
||||
OPTION_TO_BOOL(ephemeral, ephemeral, false);
|
||||
|
||||
if (opts->spell.type == kObjectTypeNil) {
|
||||
if (!HAS_KEY(opts->spell)) {
|
||||
decor.spell = kNone;
|
||||
} else {
|
||||
bool spell = false;
|
||||
@@ -746,12 +745,12 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
has_decor = true;
|
||||
}
|
||||
|
||||
VALIDATE_S((line >= 0), "line", "(out of range)", {
|
||||
VALIDATE_RANGE((line >= 0), "line", {
|
||||
goto error;
|
||||
});
|
||||
|
||||
if (line > buf->b_ml.ml_line_count) {
|
||||
VALIDATE_S(!strict, "line", "(out of range)", {
|
||||
VALIDATE_RANGE(!strict, "line", {
|
||||
goto error;
|
||||
});
|
||||
line = buf->b_ml.ml_line_count;
|
||||
@@ -762,12 +761,12 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
if (col == -1) {
|
||||
col = (Integer)len;
|
||||
} else if (col > (Integer)len) {
|
||||
VALIDATE_S(!strict, "col", "(out of range)", {
|
||||
VALIDATE_RANGE(!strict, "col", {
|
||||
goto error;
|
||||
});
|
||||
col = (Integer)len;
|
||||
} else if (col < -1) {
|
||||
VALIDATE_S(false, "col", "(out of range)", {
|
||||
VALIDATE_RANGE(false, "col", {
|
||||
goto error;
|
||||
});
|
||||
}
|
||||
@@ -783,7 +782,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
|
||||
line2 = (int)line;
|
||||
}
|
||||
if (col2 > (Integer)len) {
|
||||
VALIDATE_S(!strict, "end_col", "(out of range)", {
|
||||
VALIDATE_RANGE(!strict, "end_col", {
|
||||
goto error;
|
||||
});
|
||||
col2 = (int)len;
|
||||
@@ -886,10 +885,10 @@ Integer nvim_buf_add_highlight(Buffer buffer, Integer ns_id, String hl_group, In
|
||||
return 0;
|
||||
}
|
||||
|
||||
VALIDATE_S((line >= 0 && line < MAXLNUM), "line number", "(out of range)", {
|
||||
VALIDATE_RANGE((line >= 0 && line < MAXLNUM), "line number", {
|
||||
return 0;
|
||||
});
|
||||
VALIDATE_S((col_start >= 0 && col_start <= MAXCOL), "column", "(out of range)", {
|
||||
VALIDATE_RANGE((col_start >= 0 && col_start <= MAXCOL), "column", {
|
||||
return 0;
|
||||
});
|
||||
|
||||
@@ -948,7 +947,7 @@ void nvim_buf_clear_namespace(Buffer buffer, Integer ns_id, Integer line_start,
|
||||
return;
|
||||
}
|
||||
|
||||
VALIDATE_S((line_start >= 0 && line_start < MAXLNUM), "line number", "(out of range)", {
|
||||
VALIDATE_RANGE((line_start >= 0 && line_start < MAXLNUM), "line number", {
|
||||
return;
|
||||
});
|
||||
|
||||
@@ -1092,7 +1091,7 @@ static bool extmark_get_index_from_obj(buf_T *buf, Integer ns_id, Object obj, in
|
||||
VALIDATE((pos.size == 2
|
||||
&& pos.items[0].type == kObjectTypeInteger
|
||||
&& pos.items[1].type == kObjectTypeInteger),
|
||||
"Invalid position: expected 2 Integer items", {
|
||||
"%s", "Invalid position: expected 2 Integer items", {
|
||||
return false;
|
||||
});
|
||||
|
||||
@@ -1102,7 +1101,7 @@ static bool extmark_get_index_from_obj(buf_T *buf, Integer ns_id, Object obj, in
|
||||
*col = (colnr_T)(pos_col >= 0 ? pos_col : MAXCOL);
|
||||
return true;
|
||||
} else {
|
||||
VALIDATE(false, "Invalid position: expected mark id Integer or 2-item Array", {
|
||||
VALIDATE(false, "%s", "Invalid position: expected mark id Integer or 2-item Array", {
|
||||
return false;
|
||||
});
|
||||
}
|
||||
@@ -1153,7 +1152,7 @@ VirtText parse_virt_text(Array chunks, Error *err, int *width)
|
||||
});
|
||||
Array chunk = chunks.items[i].data.array;
|
||||
VALIDATE((chunk.size > 0 && chunk.size <= 2 && chunk.items[0].type == kObjectTypeString),
|
||||
"Invalid chunk: expected Array with 1 or 2 Strings", {
|
||||
"%s", "Invalid chunk: expected Array with 1 or 2 Strings", {
|
||||
goto free_exit;
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user