From a4d81a800293f05790025f43033de11c7bd05a7b Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sun, 25 Jul 2021 14:33:28 +0200 Subject: refactor(decorations): merge the two different code paths for virt_text test(bufhl): CHANGE of tested behaviour (inb4 a proper priority mechanism) test(decoration): change of test; previous behavior was buggy (ghost buffer text) --- src/nvim/api/buffer.c | 14 +++++++++----- src/nvim/api/private/helpers.c | 6 +++++- 2 files changed, 14 insertions(+), 6 deletions(-) (limited to 'src/nvim/api') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 47216996b4..56c9cfc517 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -1566,7 +1566,8 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, "virt_text is not an Array"); goto error; } - decor.virt_text = parse_virt_text(v->data.array, err); + decor.virt_text = parse_virt_text(v->data.array, err, + &decor.virt_text_width); if (ERROR_SET(err)) { goto error; } @@ -1949,23 +1950,26 @@ Integer nvim_buf_set_virtual_text(Buffer buffer, } uint64_t ns_id = src2ns(&src_id); + int width; - VirtText virt_text = parse_virt_text(chunks, err); + VirtText virt_text = parse_virt_text(chunks, err, &width); if (ERROR_SET(err)) { return 0; } - VirtText *existing = decor_find_virttext(buf, (int)line, ns_id); + Decoration *existing = decor_find_virttext(buf, (int)line, ns_id); if (existing) { - clear_virttext(existing); - *existing = virt_text; + clear_virttext(&existing->virt_text); + existing->virt_text = virt_text; + existing->virt_text_width = width; return src_id; } Decoration *decor = xcalloc(1, sizeof(*decor)); decor->virt_text = virt_text; + decor->virt_text_width = width; extmark_set(buf, ns_id, 0, (int)line, 0, -1, -1, decor, true, false, kExtmarkNoUndo); diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index e78bd2ea9a..e9a436d3e3 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -1592,9 +1592,10 @@ bool extmark_get_index_from_obj(buf_T *buf, Integer ns_id, Object obj, int } } -VirtText parse_virt_text(Array chunks, Error *err) +VirtText parse_virt_text(Array chunks, Error *err, int *width) { VirtText virt_text = KV_INITIAL_VALUE; + int w = 0; 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"); @@ -1635,9 +1636,12 @@ VirtText parse_virt_text(Array chunks, Error *err) } char *text = transstr(str.size > 0 ? str.data : ""); // allocates + w += (int)mb_string2cells((char_u *)text); + kv_push(virt_text, ((VirtTextChunk){ .text = text, .hl_id = hl_id })); } + *width = w; return virt_text; free_exit: -- cgit