luaref: free decoration providers

This commit is contained in:
Björn Linse 2021-03-27 11:21:13 +01:00
parent a0b7bb4153
commit 623fe4dc7e
5 changed files with 58 additions and 45 deletions

View File

@ -1708,33 +1708,6 @@ const char *describe_ns(NS ns_id)
return "(UNKNOWN PLUGIN)";
}
DecorProvider *get_provider(NS ns_id, bool force)
{
ssize_t i;
for (i = 0; i < (ssize_t)kv_size(decor_providers); i++) {
DecorProvider *item = &kv_A(decor_providers, i);
if (item->ns_id == ns_id) {
return item;
} else if (item->ns_id > ns_id) {
break;
}
}
if (!force) {
return NULL;
}
for (ssize_t j = (ssize_t)kv_size(decor_providers)-1; j >= i; j++) {
// allocates if needed:
(void)kv_a(decor_providers, (size_t)j+1);
kv_A(decor_providers, (size_t)j+1) = kv_A(decor_providers, j);
}
DecorProvider *item = &kv_a(decor_providers, (size_t)i);
*item = DECORATION_PROVIDER_INIT(ns_id);
return item;
}
static bool parse_float_anchor(String anchor, FloatAnchor *out)
{
if (anchor.size == 0) {

View File

@ -2881,19 +2881,6 @@ void nvim__screenshot(String path)
ui_call_screenshot(path);
}
static void clear_provider(DecorProvider *p)
{
if (p == NULL) {
return;
}
NLUA_CLEAR_REF(p->redraw_start);
NLUA_CLEAR_REF(p->redraw_buf);
NLUA_CLEAR_REF(p->redraw_win);
NLUA_CLEAR_REF(p->redraw_line);
NLUA_CLEAR_REF(p->redraw_end);
p->active = false;
}
/// Set or change decoration provider for a namespace
///
/// This is a very general purpose interface for having lua callbacks
@ -2938,8 +2925,8 @@ void nvim_set_decoration_provider(Integer ns_id, DictionaryOf(LuaRef) opts,
Error *err)
FUNC_API_SINCE(7) FUNC_API_LUA_ONLY
{
DecorProvider *p = get_provider((NS)ns_id, true);
clear_provider(p);
DecorProvider *p = get_decor_provider((NS)ns_id, true);
decor_provider_clear(p);
// regardless of what happens, it seems good idea to redraw
redraw_all_later(NOT_VALID); // TODO(bfredl): too soon?
@ -2982,5 +2969,5 @@ void nvim_set_decoration_provider(Integer ns_id, DictionaryOf(LuaRef) opts,
p->active = true;
return;
error:
clear_provider(p);
decor_provider_clear(p);
}

View File

@ -2,6 +2,7 @@
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "nvim/vim.h"
#include "nvim/lua/executor.h"
#include "nvim/extmark.h"
#include "nvim/decoration.h"
#include "nvim/screen.h"
@ -365,3 +366,52 @@ void decor_add_ephemeral(int start_row, int start_col, int end_row, int end_col,
decor_add(&decor_state, start_row, start_col, end_row, end_col, decor, true,
priority);
}
DecorProvider *get_decor_provider(NS ns_id, bool force)
{
ssize_t i;
for (i = 0; i < (ssize_t)kv_size(decor_providers); i++) {
DecorProvider *item = &kv_A(decor_providers, i);
if (item->ns_id == ns_id) {
return item;
} else if (item->ns_id > ns_id) {
break;
}
}
if (!force) {
return NULL;
}
for (ssize_t j = (ssize_t)kv_size(decor_providers)-1; j >= i; j++) {
// allocates if needed:
(void)kv_a(decor_providers, (size_t)j+1);
kv_A(decor_providers, (size_t)j+1) = kv_A(decor_providers, j);
}
DecorProvider *item = &kv_a(decor_providers, (size_t)i);
*item = DECORATION_PROVIDER_INIT(ns_id);
return item;
}
void decor_provider_clear(DecorProvider *p)
{
if (p == NULL) {
return;
}
NLUA_CLEAR_REF(p->redraw_start);
NLUA_CLEAR_REF(p->redraw_buf);
NLUA_CLEAR_REF(p->redraw_win);
NLUA_CLEAR_REF(p->redraw_line);
NLUA_CLEAR_REF(p->redraw_end);
p->active = false;
}
void decor_free_all_mem(void)
{
for (size_t i = 0; i < kv_size(decor_providers); i++) {
decor_provider_clear(&kv_A(decor_providers, i));
}
kv_destroy(decor_providers);
}

View File

@ -151,7 +151,7 @@ int hl_get_syn_attr(int ns_id, int idx, HlAttrs at_en)
void ns_hl_def(NS ns_id, int hl_id, HlAttrs attrs, int link_id)
{
DecorProvider *p = get_provider(ns_id, true);
DecorProvider *p = get_decor_provider(ns_id, true);
if ((attrs.rgb_ae_attr & HL_DEFAULT)
&& map_has(ColorKey, ColorItem)(ns_hl, ColorKey(ns_id, hl_id))) {
return;
@ -175,7 +175,7 @@ int ns_get_hl(NS ns_id, int hl_id, bool link, bool nodefault)
ns_id = ns_hl_active;
}
DecorProvider *p = get_provider(ns_id, true);
DecorProvider *p = get_decor_provider(ns_id, true);
ColorItem it = map_get(ColorKey, ColorItem)(ns_hl, ColorKey(ns_id, hl_id));
// TODO(bfredl): map_ref true even this?
bool valid_cache = it.version >= p->hl_valid;

View File

@ -20,6 +20,7 @@
#include "nvim/sign.h"
#include "nvim/api/vim.h"
#include "nvim/lua/executor.h"
#include "nvim/decoration.h"
#ifdef UNIT_TESTING
# define malloc(size) mem_malloc(size)
@ -697,6 +698,8 @@ void free_all_mem(void)
check_quickfix_busy();
decor_free_all_mem();
nlua_free_all_mem();
}