fix(api): don't override Vimscript SID (#32610)

Problem:  When calling an API from Vimscript to set an option, mapping,
          etc., :verbose shows that it's set from an API client.
Solution: Don't override current_sctx.sc_sid when calling an API from
          Vimscript. Also fix the inverse case where API channel id is
          not set when calling an API from RPC. Move channel id into
          sctx_T to make saving and restoring easier.

Related #8329
This commit is contained in:
zeertzjq
2025-02-25 09:17:51 +08:00
committed by GitHub
parent 614c9322d5
commit 095c0876c2
16 changed files with 237 additions and 118 deletions

View File

@@ -1049,32 +1049,18 @@ const char *get_default_stl_hl(win_T *wp, bool use_winbar, int stc_hl_id)
}
}
int find_sid(uint64_t channel_id)
{
switch (channel_id) {
case VIML_INTERNAL_CALL:
// TODO(autocmd): Figure out what this should be
// return SID_API_CLIENT;
case LUA_INTERNAL_CALL:
return SID_LUA;
default:
return SID_API_CLIENT;
}
}
/// Sets sctx for API calls.
///
/// @param channel_id api clients id. Used to determine if it's a internal
/// call or a rpc call.
/// @return returns previous value of current_sctx. To be used
/// to be used for restoring sctx to previous state.
/// @param channel_id api client id to determine if it's a internal or RPC call.
///
/// @return previous value of current_sctx. To be used later for restoring sctx.
sctx_T api_set_sctx(uint64_t channel_id)
{
sctx_T old_current_sctx = current_sctx;
if (channel_id != VIML_INTERNAL_CALL) {
current_sctx.sc_sid =
channel_id == LUA_INTERNAL_CALL ? SID_LUA : SID_API_CLIENT;
current_sctx.sc_sid = channel_id == LUA_INTERNAL_CALL ? SID_LUA : SID_API_CLIENT;
current_sctx.sc_lnum = 0;
current_sctx.sc_chan = channel_id;
}
return old_current_sctx;
}

View File

@@ -186,13 +186,7 @@ typedef struct {
#define WITH_SCRIPT_CONTEXT(channel_id, code) \
do { \
const sctx_T save_current_sctx = current_sctx; \
const uint64_t save_channel_id = current_channel_id; \
current_sctx.sc_sid = \
(channel_id) == LUA_INTERNAL_CALL ? SID_LUA : SID_API_CLIENT; \
current_sctx.sc_lnum = 0; \
current_channel_id = channel_id; \
const sctx_T save_current_sctx = api_set_sctx(channel_id); \
code; \
current_channel_id = save_channel_id; \
current_sctx = save_current_sctx; \
} while (0);