fix(api): allow nvim_buf_set_extmark to accept end_row key (#16686)

nvim_buf_get_extmark uses "end_row" rather than "end_line" in its
'details' dict, which means callers must modify the key names if they
want to re-use the information. Allow nvim_buf_set_extmark to take
"end_row" as an alias to "end_line" to make this more compatible.

See [1].

[1]: https://github.com/neovim/neovim/pull/15011#discussion_r665336968
This commit is contained in:
github-actions[bot] 2021-12-16 11:05:58 -07:00 committed by GitHub
parent 5c8e5432c0
commit 1b54344c11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 11 deletions

View File

@ -2353,7 +2353,7 @@ nvim_buf_set_extmark({buffer}, {ns_id}, {line}, {col}, {*opts})
|api-indexing|
{opts} Optional parameters.
• id : id of the extmark to edit.
• end_line : ending line of the mark, 0-based
• end_row : ending line of the mark, 0-based
inclusive.
• end_col : ending col of the mark, 0-based
exclusive.

View File

@ -339,7 +339,7 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start, Object e
/// @param col Column where to place the mark, 0-based. |api-indexing|
/// @param opts Optional parameters.
/// - id : id of the extmark to edit.
/// - end_line : ending line of the mark, 0-based inclusive.
/// - end_row : ending line of the mark, 0-based inclusive.
/// - end_col : ending col of the mark, 0-based exclusive.
/// - hl_group : name of the highlight group used to highlight
/// this mark.
@ -431,16 +431,26 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
}
int line2 = -1;
if (opts->end_line.type == kObjectTypeInteger) {
Integer val = opts->end_line.data.integer;
// For backward compatibility we support "end_line" as an alias for "end_row"
if (HAS_KEY(opts->end_line)) {
if (HAS_KEY(opts->end_row)) {
api_set_error(err, kErrorTypeValidation, "cannot use both end_row and end_line");
goto error;
}
opts->end_row = opts->end_line;
}
if (opts->end_row.type == kObjectTypeInteger) {
Integer val = opts->end_row.data.integer;
if (val < 0 || val > buf->b_ml.ml_line_count) {
api_set_error(err, kErrorTypeValidation, "end_line value outside range");
api_set_error(err, kErrorTypeValidation, "end_row value outside range");
goto error;
} else {
line2 = (int)val;
}
} else if (HAS_KEY(opts->end_line)) {
api_set_error(err, kErrorTypeValidation, "end_line is not an integer");
} else if (HAS_KEY(opts->end_row)) {
api_set_error(err, kErrorTypeValidation, "end_row is not an integer");
goto error;
}
@ -571,10 +581,10 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
OPTION_TO_BOOL(right_gravity, right_gravity, true);
// Only error out if they try to set end_right_gravity without
// setting end_col or end_line
// setting end_col or end_row
if (line2 == -1 && col2 == -1 && HAS_KEY(opts->end_right_gravity)) {
api_set_error(err, kErrorTypeValidation,
"cannot set end_right_gravity without setting end_line or end_col");
"cannot set end_right_gravity without setting end_row or end_col");
goto error;
}

View File

@ -5,6 +5,7 @@ return {
set_extmark = {
"id";
"end_line";
"end_row";
"end_col";
"hl_group";
"virt_text";

View File

@ -104,10 +104,10 @@ describe('API/extmarks', function()
it("can end extranges past final newline using end_col = 0", function()
set_extmark(ns, marks[1], 0, 0, {
end_col = 0,
end_line = 1
end_row = 1
})
eq("end_col value outside range",
pcall_err(set_extmark, ns, marks[2], 0, 0, { end_col = 1, end_line = 1 }))
pcall_err(set_extmark, ns, marks[2], 0, 0, { end_col = 1, end_row = 1 }))
end)
it('adds, updates and deletes marks', function()
@ -1424,6 +1424,14 @@ describe('API/extmarks', function()
eq({ {1, 0, 0}, {2, 0, 8} },
meths.buf_get_extmarks(0, ns, 0, -1, {}))
end)
it('can accept "end_row" or "end_line" #16548', function()
set_extmark(ns, marks[1], 0, 0, {
end_col = 0,
end_line = 1
})
eq({ {1, 0, 0, { end_col = 0, end_row = 1 }} }, get_extmarks(ns, 0, -1, {details=true}))
end)
end)
describe('Extmarks buffer api with many marks', function()