From cce9460524aa17bcd4daa095f4706220b81f8845 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 11 Jun 2023 15:29:51 +0800 Subject: [PATCH] fix(remote): make --remote-expr print to stdout (#23980) --- runtime/lua/vim/_editor.lua | 7 +++--- src/nvim/main.c | 13 +++++++--- test/functional/core/remote_spec.lua | 34 ++++++++++++++++++++------- test/functional/ex_cmds/help_spec.lua | 2 +- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index d46b0fbf32..dc396b8f65 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -932,7 +932,7 @@ function vim._cs_remote(rcid, server_addr, connect_error, args) or subcmd == 'tab-wait' or subcmd == 'tab-wait-silent' 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 f_tab = true f_silent = true @@ -946,8 +946,9 @@ function vim._cs_remote(rcid, server_addr, connect_error, args) if rcid == 0 then return { errmsg = connection_failure_errmsg('Send expression failed.') } end - print(vim.fn.rpcrequest(rcid, 'nvim_eval', args[2])) - return { should_exit = true, tabbed = false } + local expr = 'string(' .. args[2] .. ')' + local res = vim.fn.rpcrequest(rcid, 'nvim_eval', expr) + return { result = res, should_exit = true, tabbed = false } elseif subcmd ~= '' then return { errmsg = 'Unknown option argument: ' .. args[1] } end diff --git a/src/nvim/main.c b/src/nvim/main.c index 88eedf80d3..6a02aa3dae 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -953,7 +953,7 @@ static void remote_request(mparm_T *params, int remote_args, char *server_addr, TriState tabbed = kNone; 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) { os_errmsg("vim._cs_remote returned an unexpected type for 'errmsg'\n"); 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("\n"); 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) { os_errmsg("vim._cs_remote returned an unexpected type for 'tabbed'\n"); os_exit(2); } 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) { os_errmsg("vim._cs_remote returned an unexpected type for 'should_exit'\n"); os_exit(2); diff --git a/test/functional/core/remote_spec.lua b/test/functional/core/remote_spec.lua index f74bb65553..23aab400df 100644 --- a/test/functional/core/remote_spec.lua +++ b/test/functional/core/remote_spec.lua @@ -8,6 +8,8 @@ local exec_lua = helpers.exec_lua local expect = helpers.expect local funcs = helpers.funcs local insert = helpers.insert +local is_os = helpers.is_os +local nvim_prog = helpers.nvim_prog local new_argv = helpers.new_argv local neq = helpers.neq local set_session = helpers.set_session @@ -38,10 +40,11 @@ describe('Remote', function() server:close() end) + -- Run a `nvim --remote*` command and return { stdout, stderr } of the process local function run_remote(...) set_session(server) 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 -- 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) set_session(client_starter) -- 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() set_session(server) + return res end it('edit a single file', function() - run_remote('--remote', fname) + eq({ '', '' }, run_remote('--remote', fname)) expect(contents) eq(2, #funcs.getbufinfo()) end) 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) eq(1, #funcs.gettabinfo()) end) it('tab edit a single file with a changed buffer', function() insert('hello') - run_remote('--remote-tab', fname) + eq({ '', '' }, run_remote('--remote-tab', fname)) expect(contents) eq(2, #funcs.gettabinfo()) end) it('edit multiple files', function() - run_remote('--remote', fname, other_fname) + eq({ '', '' }, run_remote('--remote', fname, other_fname)) expect(contents) command('next') expect(other_contents) @@ -83,7 +97,7 @@ describe('Remote', function() end) it('send keys', function() - run_remote('--remote-send', ':edit '..fname..'v') + eq({ '', '' }, run_remote('--remote-send', ':edit '..fname..'v')) expect(contents) eq(2, #funcs.getwininfo()) -- Only a single buffer as we're using edit and not drop like --remote does @@ -91,7 +105,9 @@ describe('Remote', function() end) 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') end) end) @@ -113,7 +129,7 @@ describe('Remote', function() eq(nil, string.find(exec_capture('messages'), 'E247:')) end) - pending('exits with error on', function() + describe('exits with error on', function() local function run_and_check_exit_code(...) local bogus_argv = new_argv(...) diff --git a/test/functional/ex_cmds/help_spec.lua b/test/functional/ex_cmds/help_spec.lua index 7fc318d813..aca0cbbaa6 100644 --- a/test/functional/ex_cmds/help_spec.lua +++ b/test/functional/ex_cmds/help_spec.lua @@ -30,7 +30,7 @@ describe(':help', function() eq(1002, funcs.win_getid()) end) - it('multibyte help tags work #23978', function() + it('multibyte help tags work #23975', function() mkdir('Xhelptags') finally(function() rmdir('Xhelptags')