feat: make :restart work for remote UI (#34354)

This commit is contained in:
zeertzjq
2025-06-08 06:10:34 +08:00
committed by GitHub
parent faae1e72a2
commit 52c61d9690
11 changed files with 202 additions and 113 deletions

View File

@@ -7,6 +7,7 @@
#include <string.h>
#include "klib/kvec.h"
#include "nvim/api/private/converter.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h"
#include "nvim/api/private/validate.h"
@@ -17,6 +18,7 @@
#include "nvim/channel.h"
#include "nvim/channel_defs.h"
#include "nvim/eval.h"
#include "nvim/eval/typval.h"
#include "nvim/event/defs.h"
#include "nvim/event/loop.h"
#include "nvim/event/multiqueue.h"
@@ -239,6 +241,47 @@ void nvim_ui_detach(uint64_t channel_id, Error *err)
remote_ui_disconnect(channel_id);
}
/// Sends a "restart" UI event to the UI on the given channel.
///
/// @return false if there is no UI on the channel, otherwise true
bool remote_ui_restart(uint64_t channel_id, Error *err)
{
RemoteUI *ui = pmap_get(uint64_t)(&connected_uis, channel_id);
if (!ui) {
api_set_error(err, kErrorTypeException,
"UI not attached to channel: %" PRId64, channel_id);
return false;
}
MAXSIZE_TEMP_ARRAY(args, 2);
ADD_C(args, CSTR_AS_OBJ(get_vim_var_str(VV_PROGPATH)));
Arena arena = ARENA_EMPTY;
const list_T *l = get_vim_var_list(VV_ARGV);
int argc = tv_list_len(l);
assert(argc > 0);
Array argv = arena_array(&arena, (size_t)argc + 1);
bool had_minmin = false;
TV_LIST_ITER_CONST(l, li, {
const char *arg = tv_get_string(TV_LIST_ITEM_TV(li));
if (argv.size > 0 && !had_minmin && strequal(arg, "--")) {
had_minmin = true;
}
// Exclude --embed/--headless from `argv`, as the client may start the server in a
// different way than how the server was originally started.
if (argv.size == 0 || had_minmin
|| (!strequal(arg, "--embed") && !strequal(arg, "--headless"))) {
ADD_C(argv, CSTR_AS_OBJ(arg));
}
});
ADD_C(args, ARRAY_OBJ(argv));
push_call(ui, "restart", args);
arena_mem_free(arena_finish(&arena));
return true;
}
// TODO(bfredl): use me to detach a specific ui from the server
void remote_ui_stop(RemoteUI *ui)
{

View File

@@ -29,8 +29,8 @@ void visual_bell(void)
FUNC_API_SINCE(3);
void flush(void)
FUNC_API_SINCE(3) FUNC_API_REMOTE_IMPL;
void restart(void)
FUNC_API_SINCE(14) FUNC_API_REMOTE_ONLY FUNC_API_CLIENT_IMPL;
void restart(String progpath, Array argv)
FUNC_API_SINCE(14) FUNC_API_REMOTE_ONLY FUNC_API_REMOTE_IMPL FUNC_API_CLIENT_IMPL;
void suspend(void)
FUNC_API_SINCE(3);
void set_title(String title)