Merge pull request #17001 from mjlbach/feat/non-strict-extmarks

feat(extmarks): add strict option
This commit is contained in:
bfredl
2022-01-15 21:06:04 +01:00
committed by GitHub
3 changed files with 53 additions and 11 deletions

View File

@@ -404,6 +404,10 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start, Object e
/// for left). Defaults to false. /// for left). Defaults to false.
/// - priority: a priority value for the highlight group. For /// - priority: a priority value for the highlight group. For
/// example treesitter highlighting uses a value of 100. /// example treesitter highlighting uses a value of 100.
/// - strict: boolean that indicates extmark should not be placed
/// if the line or column value is past the end of the
/// buffer or end of the line respectively. Defaults to true.
///
/// @param[out] err Error details, if any /// @param[out] err Error details, if any
/// @return Id of the created/updated extmark /// @return Id of the created/updated extmark
Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer col, Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer col,
@@ -441,9 +445,18 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
opts->end_row = opts->end_line; opts->end_row = opts->end_line;
} }
#define OPTION_TO_BOOL(target, name, val) \
target = api_object_to_bool(opts->name, #name, val, err); \
if (ERROR_SET(err)) { \
goto error; \
}
bool strict = true;
OPTION_TO_BOOL(strict, strict, true);
if (opts->end_row.type == kObjectTypeInteger) { if (opts->end_row.type == kObjectTypeInteger) {
Integer val = opts->end_row.data.integer; Integer val = opts->end_row.data.integer;
if (val < 0 || val > buf->b_ml.ml_line_count) { if (val < 0 || (val > buf->b_ml.ml_line_count && strict)) {
api_set_error(err, kErrorTypeValidation, "end_row value outside range"); api_set_error(err, kErrorTypeValidation, "end_row value outside range");
goto error; goto error;
} else { } else {
@@ -512,12 +525,6 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
goto error; goto error;
} }
#define OPTION_TO_BOOL(target, name, val) \
target = api_object_to_bool(opts->name, #name, val, err); \
if (ERROR_SET(err)) { \
goto error; \
}
OPTION_TO_BOOL(decor.virt_text_hide, virt_text_hide, false); OPTION_TO_BOOL(decor.virt_text_hide, virt_text_hide, false);
OPTION_TO_BOOL(decor.hl_eol, hl_eol, false); OPTION_TO_BOOL(decor.hl_eol, hl_eol, false);
@@ -596,16 +603,30 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
bool ephemeral = false; bool ephemeral = false;
OPTION_TO_BOOL(ephemeral, ephemeral, false); OPTION_TO_BOOL(ephemeral, ephemeral, false);
if (line < 0 || line > buf->b_ml.ml_line_count) { if (line < 0) {
api_set_error(err, kErrorTypeValidation, "line value outside range"); api_set_error(err, kErrorTypeValidation, "line value outside range");
goto error; goto error;
} else if (line > buf->b_ml.ml_line_count) {
if (strict) {
api_set_error(err, kErrorTypeValidation, "line value outside range");
goto error;
} else {
line = buf->b_ml.ml_line_count;
}
} else if (line < buf->b_ml.ml_line_count) { } else if (line < buf->b_ml.ml_line_count) {
len = ephemeral ? MAXCOL : STRLEN(ml_get_buf(buf, (linenr_T)line+1, false)); len = ephemeral ? MAXCOL : STRLEN(ml_get_buf(buf, (linenr_T)line+1, false));
} }
if (col == -1) { if (col == -1) {
col = (Integer)len; col = (Integer)len;
} else if (col < -1 || col > (Integer)len) { } else if (col > (Integer)len) {
if (strict) {
api_set_error(err, kErrorTypeValidation, "col value outside range");
goto error;
} else {
col = (Integer)len;
}
} else if (col < -1) {
api_set_error(err, kErrorTypeValidation, "col value outside range"); api_set_error(err, kErrorTypeValidation, "col value outside range");
goto error; goto error;
} }
@@ -621,8 +642,12 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer
line2 = (int)line; line2 = (int)line;
} }
if (col2 > (Integer)len) { if (col2 > (Integer)len) {
if (strict) {
api_set_error(err, kErrorTypeValidation, "end_col value outside range"); api_set_error(err, kErrorTypeValidation, "end_col value outside range");
goto error; goto error;
} else {
col2 = (int)len;
}
} }
} else if (line2 >= 0) { } else if (line2 >= 0) {
col2 = 0; col2 = 0;

View File

@@ -21,6 +21,7 @@ return {
"virt_lines"; "virt_lines";
"virt_lines_above"; "virt_lines_above";
"virt_lines_leftcol"; "virt_lines_leftcol";
"strict";
}; };
keymap = { keymap = {
"noremap"; "noremap";

View File

@@ -110,6 +110,22 @@ describe('API/extmarks', function()
pcall_err(set_extmark, ns, marks[2], 0, 0, { end_col = 1, end_row = 1 })) pcall_err(set_extmark, ns, marks[2], 0, 0, { end_col = 1, end_row = 1 }))
end) end)
it("can end extranges past final newline when strict mode is false", function()
set_extmark(ns, marks[1], 0, 0, {
end_col = 1,
end_row = 1,
strict = false,
})
end)
it("can end extranges past final column when strict mode is false", function()
set_extmark(ns, marks[1], 0, 0, {
end_col = 6,
end_row = 0,
strict = false,
})
end)
it('adds, updates and deletes marks', function() it('adds, updates and deletes marks', function()
local rv = set_extmark(ns, marks[1], positions[1][1], positions[1][2]) local rv = set_extmark(ns, marks[1], positions[1][1], positions[1][2])
eq(marks[1], rv) eq(marks[1], rv)