From 5cfa7a72f8c40cdcc0fa93693689915e913806f1 Mon Sep 17 00:00:00 2001 From: Luuk van Baal Date: Tue, 20 Feb 2024 17:25:57 +0100 Subject: refactor(message): propagate highlight id instead of attrs Problem: Highlight group id is not propagated to the end of the message call stack, where ext_messages are emitted. Solution: Refactor message functions to pass along highlight group id instead of attr id. --- src/nvim/api/private/helpers.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/nvim/api/private/helpers.c') diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index e1fb4ed732..d21caf7ed0 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -824,16 +824,15 @@ HlMessage parse_hl_msg(Array chunks, Error *err) String str = copy_string(chunk.items[0].data.string, NULL); - int attr = 0; + int hl_id = 0; if (chunk.size == 2) { String hl = chunk.items[1].data.string; if (hl.size > 0) { // TODO(bfredl): use object_to_hl_id and allow integer - int hl_id = syn_check_group(hl.data, hl.size); - attr = hl_id > 0 ? syn_id2attr(hl_id) : 0; + hl_id = syn_check_group(hl.data, hl.size); } } - kv_push(hl_msg, ((HlMessageChunk){ .text = str, .attr = attr })); + kv_push(hl_msg, ((HlMessageChunk){ .text = str, .hl_id = hl_id })); } return hl_msg; -- cgit From 9c718bc2bce53b5be45061bff940f99e50c8bfcb Mon Sep 17 00:00:00 2001 From: luukvbaal Date: Sun, 17 Nov 2024 00:36:11 +0100 Subject: fix(api): validation, documentation of hl_group #31195 Problem: Documentation for "hl_group" in nvim_buf_set_extmark() is unclear. "hl_group" in nvim_echo() does not accept highlight group id. Solution: Move documentation for highlight group name/id to first mention of hl_group. Update nvim_echo() to accept highlight group id. --- src/nvim/api/private/helpers.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'src/nvim/api/private/helpers.c') diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index d21caf7ed0..88e1664c04 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -771,7 +771,7 @@ int object_to_hl_id(Object obj, const char *what, Error *err) int id = (int)obj.data.integer; return (1 <= id && id <= highlight_num_groups()) ? id : 0; } else { - api_set_error(err, kErrorTypeValidation, "Invalid highlight: %s", what); + api_set_error(err, kErrorTypeValidation, "Invalid hl_group: %s", what); return 0; } } @@ -809,27 +809,22 @@ HlMessage parse_hl_msg(Array chunks, Error *err) { HlMessage hl_msg = KV_INITIAL_VALUE; for (size_t i = 0; i < chunks.size; i++) { - if (chunks.items[i].type != kObjectTypeArray) { - api_set_error(err, kErrorTypeValidation, "Chunk is not an array"); + VALIDATE_T("chunk", kObjectTypeArray, chunks.items[i].type, { goto free_exit; - } + }); Array chunk = chunks.items[i].data.array; - if (chunk.size == 0 || chunk.size > 2 - || chunk.items[0].type != kObjectTypeString - || (chunk.size == 2 && chunk.items[1].type != kObjectTypeString)) { - api_set_error(err, kErrorTypeValidation, - "Chunk is not an array with one or two strings"); + VALIDATE((chunk.size > 0 && chunk.size <= 2 && chunk.items[0].type == kObjectTypeString), + "%s", "Invalid chunk: expected Array with 1 or 2 Strings", { goto free_exit; - } + }); String str = copy_string(chunk.items[0].data.string, NULL); int hl_id = 0; if (chunk.size == 2) { - String hl = chunk.items[1].data.string; - if (hl.size > 0) { - // TODO(bfredl): use object_to_hl_id and allow integer - hl_id = syn_check_group(hl.data, hl.size); + hl_id = object_to_hl_id(chunk.items[1], "text highlight", err); + if (ERROR_SET(err)) { + goto free_exit; } } kv_push(hl_msg, ((HlMessageChunk){ .text = str, .hl_id = hl_id })); -- cgit From 6ea45031d5841d3227c545f213d0903b951e40be Mon Sep 17 00:00:00 2001 From: luukvbaal Date: Sun, 17 Nov 2024 15:06:44 +0100 Subject: fix(api): nvim_echo free text memory with invalid highlight (#31243) Fix regression from #31195 --- src/nvim/api/private/helpers.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/nvim/api/private/helpers.c') diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 88e1664c04..8ddaecc58e 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -823,9 +823,6 @@ HlMessage parse_hl_msg(Array chunks, Error *err) int hl_id = 0; if (chunk.size == 2) { hl_id = object_to_hl_id(chunk.items[1], "text highlight", err); - if (ERROR_SET(err)) { - goto free_exit; - } } kv_push(hl_msg, ((HlMessageChunk){ .text = str, .hl_id = hl_id })); } -- cgit