mirror of
https://github.com/neovim/neovim.git
synced 2025-10-13 13:26:06 +00:00
Merge pull request #15373 from smolck/lua-notify-dictwatcher
fix(nvim): notify dict watchers on nvim_set_var and vim.g setter
This commit is contained in:
@@ -218,6 +218,8 @@ Object dict_set_var(dict_T *dict, String key, Object value, bool del, bool retva
|
||||
return rv;
|
||||
}
|
||||
|
||||
bool watched = tv_dict_is_watched(dict);
|
||||
|
||||
if (del) {
|
||||
// Delete the key
|
||||
if (di == NULL) {
|
||||
@@ -225,6 +227,10 @@ Object dict_set_var(dict_T *dict, String key, Object value, bool del, bool retva
|
||||
api_set_error(err, kErrorTypeValidation, "Key not found: %s",
|
||||
key.data);
|
||||
} else {
|
||||
// Notify watchers
|
||||
if (watched) {
|
||||
tv_dict_watcher_notify(dict, key.data, NULL, &di->di_tv);
|
||||
}
|
||||
// Return the old value
|
||||
if (retval) {
|
||||
rv = vim_to_object(&di->di_tv);
|
||||
@@ -241,11 +247,16 @@ Object dict_set_var(dict_T *dict, String key, Object value, bool del, bool retva
|
||||
return rv;
|
||||
}
|
||||
|
||||
typval_T oldtv = TV_INITIAL_VALUE;
|
||||
|
||||
if (di == NULL) {
|
||||
// Need to create an entry
|
||||
di = tv_dict_item_alloc_len(key.data, key.size);
|
||||
tv_dict_add(dict, di);
|
||||
} else {
|
||||
if (watched) {
|
||||
tv_copy(&di->di_tv, &oldtv);
|
||||
}
|
||||
// Return the old value
|
||||
if (retval) {
|
||||
rv = vim_to_object(&di->di_tv);
|
||||
@@ -255,6 +266,13 @@ Object dict_set_var(dict_T *dict, String key, Object value, bool del, bool retva
|
||||
|
||||
// Update the value
|
||||
tv_copy(&tv, &di->di_tv);
|
||||
|
||||
// Notify watchers
|
||||
if (watched) {
|
||||
tv_dict_watcher_notify(dict, key.data, &tv, &oldtv);
|
||||
tv_clear(&oldtv);
|
||||
}
|
||||
|
||||
// Clear the temporary variable
|
||||
tv_clear(&tv);
|
||||
}
|
||||
|
@@ -1781,7 +1781,7 @@ void tv_dict_watcher_notify(dict_T *const dict, const char *const key, typval_T
|
||||
tv_dict_add(argv[2].vval.v_dict, v);
|
||||
}
|
||||
|
||||
if (oldtv) {
|
||||
if (oldtv && oldtv->v_type != VAR_UNKNOWN) {
|
||||
dictitem_T *const v = tv_dict_item_alloc_len(S_LEN("old"));
|
||||
tv_copy(oldtv, &v->di_tv);
|
||||
tv_dict_add(argv[2].vval.v_dict, v);
|
||||
|
@@ -1348,12 +1348,8 @@ void set_var_const(const char *name, const size_t name_len, typval_T *const tv,
|
||||
}
|
||||
|
||||
if (watched) {
|
||||
if (oldtv.v_type == VAR_UNKNOWN) {
|
||||
tv_dict_watcher_notify(dict, (char *)v->di_key, &v->di_tv, NULL);
|
||||
} else {
|
||||
tv_dict_watcher_notify(dict, (char *)v->di_key, &v->di_tv, &oldtv);
|
||||
tv_clear(&oldtv);
|
||||
}
|
||||
tv_dict_watcher_notify(dict, (char *)v->di_key, &v->di_tv, &oldtv);
|
||||
tv_clear(&oldtv);
|
||||
}
|
||||
|
||||
if (is_const) {
|
||||
|
@@ -369,12 +369,19 @@ int nlua_setvar(lua_State *lstate)
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool watched = tv_dict_is_watched(dict);
|
||||
|
||||
if (del) {
|
||||
// Delete the key
|
||||
if (di == NULL) {
|
||||
// Doesn't exist, nothing to do
|
||||
return 0;
|
||||
} else {
|
||||
// Notify watchers
|
||||
if (watched) {
|
||||
tv_dict_watcher_notify(dict, key.data, NULL, &di->di_tv);
|
||||
}
|
||||
|
||||
// Delete the entry
|
||||
tv_dict_item_remove(dict, di);
|
||||
}
|
||||
@@ -388,17 +395,29 @@ int nlua_setvar(lua_State *lstate)
|
||||
return luaL_error(lstate, "Couldn't convert lua value");
|
||||
}
|
||||
|
||||
typval_T oldtv = TV_INITIAL_VALUE;
|
||||
|
||||
if (di == NULL) {
|
||||
// Need to create an entry
|
||||
di = tv_dict_item_alloc_len(key.data, key.size);
|
||||
tv_dict_add(dict, di);
|
||||
} else {
|
||||
if (watched) {
|
||||
tv_copy(&di->di_tv, &oldtv);
|
||||
}
|
||||
// Clear the old value
|
||||
tv_clear(&di->di_tv);
|
||||
}
|
||||
|
||||
// Update the value
|
||||
tv_copy(&tv, &di->di_tv);
|
||||
|
||||
// Notify watchers
|
||||
if (watched) {
|
||||
tv_dict_watcher_notify(dict, key.data, &tv, &oldtv);
|
||||
tv_clear(&oldtv);
|
||||
}
|
||||
|
||||
// Clear the temporary variable
|
||||
tv_clear(&tv);
|
||||
}
|
||||
|
Reference in New Issue
Block a user