fix(remote): make --remote-expr print to stdout (#23980)

This commit is contained in:
zeertzjq 2023-06-11 15:29:51 +08:00 committed by GitHub
parent 4229bbe514
commit cce9460524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 16 deletions

View File

@ -932,7 +932,7 @@ function vim._cs_remote(rcid, server_addr, connect_error, args)
or subcmd == 'tab-wait' or subcmd == 'tab-wait'
or subcmd == 'tab-wait-silent' or subcmd == 'tab-wait-silent'
then then
return { errmsg = 'E5600: Wait commands not yet implemented in nvim' } return { errmsg = 'E5600: Wait commands not yet implemented in Nvim' }
elseif subcmd == 'tab-silent' then elseif subcmd == 'tab-silent' then
f_tab = true f_tab = true
f_silent = true f_silent = true
@ -946,8 +946,9 @@ function vim._cs_remote(rcid, server_addr, connect_error, args)
if rcid == 0 then if rcid == 0 then
return { errmsg = connection_failure_errmsg('Send expression failed.') } return { errmsg = connection_failure_errmsg('Send expression failed.') }
end end
print(vim.fn.rpcrequest(rcid, 'nvim_eval', args[2])) local expr = 'string(' .. args[2] .. ')'
return { should_exit = true, tabbed = false } local res = vim.fn.rpcrequest(rcid, 'nvim_eval', expr)
return { result = res, should_exit = true, tabbed = false }
elseif subcmd ~= '' then elseif subcmd ~= '' then
return { errmsg = 'Unknown option argument: ' .. args[1] } return { errmsg = 'Unknown option argument: ' .. args[1] }
end end

View File

@ -953,7 +953,7 @@ static void remote_request(mparm_T *params, int remote_args, char *server_addr,
TriState tabbed = kNone; TriState tabbed = kNone;
for (size_t i = 0; i < rvobj.data.dictionary.size; i++) { for (size_t i = 0; i < rvobj.data.dictionary.size; i++) {
if (strcmp(rvobj.data.dictionary.items[i].key.data, "errmsg") == 0) { if (strequal(rvobj.data.dictionary.items[i].key.data, "errmsg")) {
if (rvobj.data.dictionary.items[i].value.type != kObjectTypeString) { if (rvobj.data.dictionary.items[i].value.type != kObjectTypeString) {
os_errmsg("vim._cs_remote returned an unexpected type for 'errmsg'\n"); os_errmsg("vim._cs_remote returned an unexpected type for 'errmsg'\n");
os_exit(2); os_exit(2);
@ -961,13 +961,20 @@ static void remote_request(mparm_T *params, int remote_args, char *server_addr,
os_errmsg(rvobj.data.dictionary.items[i].value.data.string.data); os_errmsg(rvobj.data.dictionary.items[i].value.data.string.data);
os_errmsg("\n"); os_errmsg("\n");
os_exit(2); os_exit(2);
} else if (strcmp(rvobj.data.dictionary.items[i].key.data, "tabbed") == 0) { } else if (strequal(rvobj.data.dictionary.items[i].key.data, "result")) {
if (rvobj.data.dictionary.items[i].value.type != kObjectTypeString) {
os_errmsg("vim._cs_remote returned an unexpected type for 'result'\n");
os_exit(2);
}
os_msg(rvobj.data.dictionary.items[i].value.data.string.data);
os_msg("\n");
} else if (strequal(rvobj.data.dictionary.items[i].key.data, "tabbed")) {
if (rvobj.data.dictionary.items[i].value.type != kObjectTypeBoolean) { if (rvobj.data.dictionary.items[i].value.type != kObjectTypeBoolean) {
os_errmsg("vim._cs_remote returned an unexpected type for 'tabbed'\n"); os_errmsg("vim._cs_remote returned an unexpected type for 'tabbed'\n");
os_exit(2); os_exit(2);
} }
tabbed = rvobj.data.dictionary.items[i].value.data.boolean ? kTrue : kFalse; tabbed = rvobj.data.dictionary.items[i].value.data.boolean ? kTrue : kFalse;
} else if (strcmp(rvobj.data.dictionary.items[i].key.data, "should_exit") == 0) { } else if (strequal(rvobj.data.dictionary.items[i].key.data, "should_exit")) {
if (rvobj.data.dictionary.items[i].value.type != kObjectTypeBoolean) { if (rvobj.data.dictionary.items[i].value.type != kObjectTypeBoolean) {
os_errmsg("vim._cs_remote returned an unexpected type for 'should_exit'\n"); os_errmsg("vim._cs_remote returned an unexpected type for 'should_exit'\n");
os_exit(2); os_exit(2);

View File

@ -8,6 +8,8 @@ local exec_lua = helpers.exec_lua
local expect = helpers.expect local expect = helpers.expect
local funcs = helpers.funcs local funcs = helpers.funcs
local insert = helpers.insert local insert = helpers.insert
local is_os = helpers.is_os
local nvim_prog = helpers.nvim_prog
local new_argv = helpers.new_argv local new_argv = helpers.new_argv
local neq = helpers.neq local neq = helpers.neq
local set_session = helpers.set_session local set_session = helpers.set_session
@ -38,10 +40,11 @@ describe('Remote', function()
server:close() server:close()
end) end)
-- Run a `nvim --remote*` command and return { stdout, stderr } of the process
local function run_remote(...) local function run_remote(...)
set_session(server) set_session(server)
local addr = funcs.serverlist()[1] local addr = funcs.serverlist()[1]
local client_argv = new_argv({args={'--server', addr, ...}}) local client_argv = { nvim_prog, '--clean', '--headless', '--server', addr, ... }
-- Create an nvim instance just to run the remote-invoking nvim. We want -- Create an nvim instance just to run the remote-invoking nvim. We want
-- to wait for the remote instance to exit and calling jobwait blocks -- to wait for the remote instance to exit and calling jobwait blocks
@ -50,32 +53,43 @@ describe('Remote', function()
local client_starter = spawn(new_argv(), false, nil, true) local client_starter = spawn(new_argv(), false, nil, true)
set_session(client_starter) set_session(client_starter)
-- Call jobstart() and jobwait() in the same RPC request to reduce flakiness. -- Call jobstart() and jobwait() in the same RPC request to reduce flakiness.
eq({ 0 }, exec_lua([[return vim.fn.jobwait({ vim.fn.jobstart(...) })]], client_argv)) eq({ 0 }, exec_lua([[return vim.fn.jobwait({ vim.fn.jobstart(..., {
stdout_buffered = true,
stderr_buffered = true,
on_stdout = function(_, data, _)
_G.Remote_stdout = table.concat(data, '\n')
end,
on_stderr = function(_, data, _)
_G.Remote_stderr = table.concat(data, '\n')
end,
}) })]], client_argv))
local res = exec_lua([[return { _G.Remote_stdout, _G.Remote_stderr }]])
client_starter:close() client_starter:close()
set_session(server) set_session(server)
return res
end end
it('edit a single file', function() it('edit a single file', function()
run_remote('--remote', fname) eq({ '', '' }, run_remote('--remote', fname))
expect(contents) expect(contents)
eq(2, #funcs.getbufinfo()) eq(2, #funcs.getbufinfo())
end) end)
it('tab edit a single file with a non-changed buffer', function() it('tab edit a single file with a non-changed buffer', function()
run_remote('--remote-tab', fname) eq({ '', '' }, run_remote('--remote-tab', fname))
expect(contents) expect(contents)
eq(1, #funcs.gettabinfo()) eq(1, #funcs.gettabinfo())
end) end)
it('tab edit a single file with a changed buffer', function() it('tab edit a single file with a changed buffer', function()
insert('hello') insert('hello')
run_remote('--remote-tab', fname) eq({ '', '' }, run_remote('--remote-tab', fname))
expect(contents) expect(contents)
eq(2, #funcs.gettabinfo()) eq(2, #funcs.gettabinfo())
end) end)
it('edit multiple files', function() it('edit multiple files', function()
run_remote('--remote', fname, other_fname) eq({ '', '' }, run_remote('--remote', fname, other_fname))
expect(contents) expect(contents)
command('next') command('next')
expect(other_contents) expect(other_contents)
@ -83,7 +97,7 @@ describe('Remote', function()
end) end)
it('send keys', function() it('send keys', function()
run_remote('--remote-send', ':edit '..fname..'<CR><C-W>v') eq({ '', '' }, run_remote('--remote-send', ':edit '..fname..'<CR><C-W>v'))
expect(contents) expect(contents)
eq(2, #funcs.getwininfo()) eq(2, #funcs.getwininfo())
-- Only a single buffer as we're using edit and not drop like --remote does -- Only a single buffer as we're using edit and not drop like --remote does
@ -91,7 +105,9 @@ describe('Remote', function()
end) end)
it('evaluate expressions', function() it('evaluate expressions', function()
run_remote('--remote-expr', 'setline(1, "Yo")') local linesep = is_os('win') and '\r\n' or '\n'
eq({ "function('get')" .. linesep, '' }, run_remote('--remote-expr', 'function("get")'))
eq({ '0' .. linesep, '' }, run_remote('--remote-expr', 'setline(1, "Yo")'))
expect('Yo') expect('Yo')
end) end)
end) end)
@ -113,7 +129,7 @@ describe('Remote', function()
eq(nil, string.find(exec_capture('messages'), 'E247:')) eq(nil, string.find(exec_capture('messages'), 'E247:'))
end) end)
pending('exits with error on', function() describe('exits with error on', function()
local function run_and_check_exit_code(...) local function run_and_check_exit_code(...)
local bogus_argv = new_argv(...) local bogus_argv = new_argv(...)

View File

@ -30,7 +30,7 @@ describe(':help', function()
eq(1002, funcs.win_getid()) eq(1002, funcs.win_getid())
end) end)
it('multibyte help tags work #23978', function() it('multibyte help tags work #23975', function()
mkdir('Xhelptags') mkdir('Xhelptags')
finally(function() finally(function()
rmdir('Xhelptags') rmdir('Xhelptags')