From 11142f6ffe46da1f20c570333a2c05b6e3015f56 Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Sat, 8 Jan 2022 08:14:24 -0800 Subject: feat(extmarks): add strict option The strict option, when set to false, allows placing extmarks on invalid row and column values greater than the maximum buffer row or line column respectively. This allows for nvim_buf_set_extmark to be a drop-in replacement for nvim_buf_set_highlight. --- src/nvim/api/extmark.c | 13 ++++++++++--- src/nvim/api/keysets.lua | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nvim/api/extmark.c b/src/nvim/api/extmark.c index 742b953c2a..04456eee01 100644 --- a/src/nvim/api/extmark.c +++ b/src/nvim/api/extmark.c @@ -404,6 +404,10 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start, Object e /// for left). Defaults to false. /// - priority: a priority value for the highlight group. For /// example treesitter highlighting uses a value of 100. +/// - strict: boolean that indicates extmark should be placed +/// even 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 /// @return Id of the created/updated extmark Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer col, @@ -596,7 +600,10 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer bool ephemeral = false; OPTION_TO_BOOL(ephemeral, ephemeral, false); - if (line < 0 || line > buf->b_ml.ml_line_count) { + bool strict = true; + OPTION_TO_BOOL(strict, strict, true); + + if (line < 0 || (line > buf->b_ml.ml_line_count && strict)) { api_set_error(err, kErrorTypeValidation, "line value outside range"); goto error; } else if (line < buf->b_ml.ml_line_count) { @@ -605,7 +612,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer if (col == -1) { col = (Integer)len; - } else if (col < -1 || col > (Integer)len) { + } else if (col < -1 || (col > (Integer)len && strict)) { api_set_error(err, kErrorTypeValidation, "col value outside range"); goto error; } @@ -620,7 +627,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer // reuse len from before line2 = (int)line; } - if (col2 > (Integer)len) { + if (col2 > (Integer)len && strict) { api_set_error(err, kErrorTypeValidation, "end_col value outside range"); goto error; } diff --git a/src/nvim/api/keysets.lua b/src/nvim/api/keysets.lua index 97ee885ff6..a385cfc64f 100644 --- a/src/nvim/api/keysets.lua +++ b/src/nvim/api/keysets.lua @@ -21,6 +21,7 @@ return { "virt_lines"; "virt_lines_above"; "virt_lines_leftcol"; + "strict"; }; keymap = { "noremap"; -- cgit From d0d4fb792f2b3ac40a00b7929c8653968821e5ae Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Wed, 12 Jan 2022 12:01:29 -0800 Subject: Address 'review' --- src/nvim/api/extmark.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/nvim/api/extmark.c b/src/nvim/api/extmark.c index 04456eee01..ab97ff114c 100644 --- a/src/nvim/api/extmark.c +++ b/src/nvim/api/extmark.c @@ -404,8 +404,8 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start, Object e /// for left). Defaults to false. /// - priority: a priority value for the highlight group. For /// example treesitter highlighting uses a value of 100. -/// - strict: boolean that indicates extmark should be placed -/// even if the line or column value is past the end of the +/// - 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 @@ -603,16 +603,31 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer bool strict = true; OPTION_TO_BOOL(strict, strict, true); - if (line < 0 || (line > buf->b_ml.ml_line_count && strict)) { + if (line < 0 ) { api_set_error(err, kErrorTypeValidation, "line value outside range"); 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) { len = ephemeral ? MAXCOL : STRLEN(ml_get_buf(buf, (linenr_T)line+1, false)); } if (col == -1) { col = (Integer)len; - } else if (col < -1 || (col > (Integer)len && strict)) { + } 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"); goto error; } @@ -627,9 +642,13 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer // reuse len from before line2 = (int)line; } - if (col2 > (Integer)len && strict) { - api_set_error(err, kErrorTypeValidation, "end_col value outside range"); - goto error; + if (col2 > (Integer)len) { + if (strict) { + api_set_error(err, kErrorTypeValidation, "end_col value outside range"); + goto error; + } else { + col2 = (int)len; + } } } else if (line2 >= 0) { col2 = 0; -- cgit From 2fd8330628e007704c28043089a47ec75093bcb8 Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Wed, 12 Jan 2022 12:46:31 -0800 Subject: Address review r2 --- src/nvim/api/extmark.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/nvim/api/extmark.c b/src/nvim/api/extmark.c index ab97ff114c..5624ae83dd 100644 --- a/src/nvim/api/extmark.c +++ b/src/nvim/api/extmark.c @@ -603,11 +603,10 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer bool strict = true; OPTION_TO_BOOL(strict, strict, true); - if (line < 0 ) { + if (line < 0) { api_set_error(err, kErrorTypeValidation, "line value outside range"); goto error; - } - else if (line > buf->b_ml.ml_line_count) { + } else if (line > buf->b_ml.ml_line_count) { if (strict) { api_set_error(err, kErrorTypeValidation, "line value outside range"); goto error; @@ -627,7 +626,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer } else { col = (Integer)len; } - } else if (col < -1 ) { + } else if (col < -1) { api_set_error(err, kErrorTypeValidation, "col value outside range"); goto error; } -- cgit From facd07bcf76b3c0b41eb046fce6b9d5bad62b89e Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Sat, 15 Jan 2022 08:37:44 -0800 Subject: Address review r3 --- src/nvim/api/extmark.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/nvim/api/extmark.c b/src/nvim/api/extmark.c index 5624ae83dd..58be8f2807 100644 --- a/src/nvim/api/extmark.c +++ b/src/nvim/api/extmark.c @@ -445,9 +445,18 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer 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) { 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"); goto error; } else { @@ -516,12 +525,6 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer 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.hl_eol, hl_eol, false); @@ -600,9 +603,6 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer bool ephemeral = false; OPTION_TO_BOOL(ephemeral, ephemeral, false); - bool strict = true; - OPTION_TO_BOOL(strict, strict, true); - if (line < 0) { api_set_error(err, kErrorTypeValidation, "line value outside range"); goto error; -- cgit