From 5862176764c7a86d5fdd2685122810e14a3d5b02 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Wed, 16 Feb 2022 17:11:50 -0500 Subject: feat(remote): add basic --remote support This is starting from @geekodour's work at https://github.com/neovim/neovim/pull/8326 --- runtime/lua/vim/_editor.lua | 72 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 3136e36043..5f3d66e108 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -636,6 +636,78 @@ function vim.pretty_print(...) return ... end +local function __rpcrequest(...) + return vim.api.nvim_call_function("rpcrequest", {...}) +end + +function vim._cs_remote(rcid, args) + + local f_silent = false + local f_wait = false + local f_tab = false + local should_exit = true + local command = 'edit ' + + local subcmd = string.sub(args[1],10) + + if subcmd == '' then + -- no flags to set + elseif subcmd == 'tab' then + f_tab = true + elseif subcmd == 'silent' then + f_silent = true + elseif subcmd == 'wait' then + f_wait = true + elseif subcmd == 'wait-silent' then + f_wait = true + f_silent = true + elseif subcmd == 'tab-wait' then + f_tab = true + f_wait = true + elseif subcmd == 'tab-silent' then + f_tab = true + f_silent = true + elseif subcmd == 'tab-wait-silent' then + f_tab = true + f_wait = true + f_silent = true + elseif subcmd == 'send' then + __rpcrequest(rcid, 'nvim_input', args[2]) + return { should_exit = should_exit, tabbed = f_tab, files = 0 } + -- should we show warning if --server doesn't exist in --send and --expr? + elseif subcmd == 'expr' then + local res = __rpcrequest(rcid, 'vim_eval', args[2]) + print(res) + return { should_exit = should_exit, tabbed = f_tab, files = 0 } + else + print('--remote subcommand not found') + end + + table.remove(args,1) + + if not f_silent and rcid == 0 then + print('Remote server does not exist.') + end + + if f_silent and rcid == 0 then + print('Remote server does not exist. starting new server') + should_exit = false + end + + if f_tab then command = 'tabedit ' end + + if rcid ~= 0 then + for _, key in ipairs(args) do + __rpcrequest(rcid, 'nvim_command', command .. key) + end + end + + return { + should_exit = should_exit, + tabbed = f_tab, + files = table.getn(args) + } +end require('vim._meta') -- cgit From 039e94f491d2f8576cbef1aeacd5ea1f7bc0982a Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Thu, 24 Feb 2022 10:47:41 -0500 Subject: test(remote): add tests for --remote This also fixes a fair number of issues found in running the tests --- runtime/lua/vim/_editor.lua | 57 +++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 31 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 5f3d66e108..869a2706ac 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -636,17 +636,10 @@ function vim.pretty_print(...) return ... end -local function __rpcrequest(...) - return vim.api.nvim_call_function("rpcrequest", {...}) -end - function vim._cs_remote(rcid, args) - local f_silent = false local f_wait = false local f_tab = false - local should_exit = true - local command = 'edit ' local subcmd = string.sub(args[1],10) @@ -672,40 +665,42 @@ function vim._cs_remote(rcid, args) f_wait = true f_silent = true elseif subcmd == 'send' then - __rpcrequest(rcid, 'nvim_input', args[2]) - return { should_exit = should_exit, tabbed = f_tab, files = 0 } - -- should we show warning if --server doesn't exist in --send and --expr? + if rcid == 0 then + vim.cmd('echoerr "E247: Remote server does not exist. Send failed."') + return + end + vim.fn.rpcrequest(rcid, 'nvim_input', args[2]) + return { should_exit = true, tabbed = false } elseif subcmd == 'expr' then - local res = __rpcrequest(rcid, 'vim_eval', args[2]) - print(res) - return { should_exit = should_exit, tabbed = f_tab, files = 0 } + if rcid == 0 then + vim.cmd('echoerr "E247: Remote server does not exist. Send expression failed."') + return + end + vim.fn.rpcrequest(rcid, 'nvim_eval', args[2]) + return { should_exit = true, tabbed = false } else - print('--remote subcommand not found') + vim.cmd('echoerr "Unknown option argument: ' .. args[1] .. '"') + return end - table.remove(args,1) - - if not f_silent and rcid == 0 then - print('Remote server does not exist.') - end - - if f_silent and rcid == 0 then - print('Remote server does not exist. starting new server') + if rcid == 0 then + if not f_silent then + vim.cmd('echohl WarningMsg | echomsg "E247: Remote server does not exist. Editing locally" | echohl None') + end should_exit = false - end - - if f_tab then command = 'tabedit ' end - - if rcid ~= 0 then - for _, key in ipairs(args) do - __rpcrequest(rcid, 'nvim_command', command .. key) + else + local command = {} + if f_tab then table.insert(command, 'tab') end + table.insert(command, 'drop') + for i = 2, #args do + table.insert(command, vim.fn.fnameescape(args[i])) end + vim.fn.rpcrequest(rcid, 'nvim_command', table.concat(command, ' ')) end return { - should_exit = should_exit, + should_exit = rcid ~= 0, tabbed = f_tab, - files = table.getn(args) } end -- cgit From 29c36322857b37263b07eb1301d71ccd8a2ae044 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Thu, 3 Mar 2022 16:33:27 -0500 Subject: fix(remote): report on missing wait commands, typecheck lua results Clean up lint errors, too --- runtime/lua/vim/_editor.lua | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 869a2706ac..030c3b40e8 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -638,56 +638,39 @@ end function vim._cs_remote(rcid, args) local f_silent = false - local f_wait = false local f_tab = false local subcmd = string.sub(args[1],10) - if subcmd == '' then - -- no flags to set - elseif subcmd == 'tab' then + if subcmd == 'tab' then f_tab = true elseif subcmd == 'silent' then f_silent = true - elseif subcmd == 'wait' then - f_wait = true - elseif subcmd == 'wait-silent' then - f_wait = true - f_silent = true - elseif subcmd == 'tab-wait' then - f_tab = true - f_wait = true + elseif subcmd == 'wait' or subcmd == 'wait-silent' or subcmd == 'tab-wait' or subcmd == 'tab-wait-silent' then + return { errmsg = 'E5600: Wait commands not yet implemented in nvim' } elseif subcmd == 'tab-silent' then f_tab = true f_silent = true - elseif subcmd == 'tab-wait-silent' then - f_tab = true - f_wait = true - f_silent = true elseif subcmd == 'send' then if rcid == 0 then - vim.cmd('echoerr "E247: Remote server does not exist. Send failed."') - return + return { errmsg = 'E247: Remote server does not exist. Send failed.' } end vim.fn.rpcrequest(rcid, 'nvim_input', args[2]) return { should_exit = true, tabbed = false } elseif subcmd == 'expr' then if rcid == 0 then - vim.cmd('echoerr "E247: Remote server does not exist. Send expression failed."') - return + return { errmsg = 'E247: Remote server does not exist. Send expression failed.' } end - vim.fn.rpcrequest(rcid, 'nvim_eval', args[2]) + print(vim.fn.rpcrequest(rcid, 'nvim_eval', args[2])) return { should_exit = true, tabbed = false } - else - vim.cmd('echoerr "Unknown option argument: ' .. args[1] .. '"') - return + elseif subcmd ~= '' then + return { errmsg='Unknown option argument: ' .. args[1] } end if rcid == 0 then if not f_silent then vim.cmd('echohl WarningMsg | echomsg "E247: Remote server does not exist. Editing locally" | echohl None') end - should_exit = false else local command = {} if f_tab then table.insert(command, 'tab') end -- cgit From 1dbf8675c71dc500ae7502085161cd56e311ccf6 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Fri, 11 Mar 2022 11:16:34 -0500 Subject: fix(remote): respect silent in error reporting --- runtime/lua/vim/_editor.lua | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 030c3b40e8..a0c60a7dcf 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -636,12 +636,24 @@ function vim.pretty_print(...) return ... end -function vim._cs_remote(rcid, args) +function vim._cs_remote(rcid, server_addr, connect_error, args) + local function connection_failure_errmsg(consequence) + local explanation + if server_addr == '' then + explanation = "No server specified with --server" + else + explanation = "Failed to connect to '" .. server_addr .. "'" + if connect_error ~= "" then + explanation = explanation .. ": " .. connect_error + end + end + return "E247: " .. explanation .. ". " .. consequence + end + local f_silent = false local f_tab = false local subcmd = string.sub(args[1],10) - if subcmd == 'tab' then f_tab = true elseif subcmd == 'silent' then @@ -653,13 +665,13 @@ function vim._cs_remote(rcid, args) f_silent = true elseif subcmd == 'send' then if rcid == 0 then - return { errmsg = 'E247: Remote server does not exist. Send failed.' } + return { errmsg = connection_failure_errmsg('Send failed.') } end vim.fn.rpcrequest(rcid, 'nvim_input', args[2]) return { should_exit = true, tabbed = false } elseif subcmd == 'expr' then if rcid == 0 then - return { errmsg = 'E247: Remote server does not exist. Send expression failed.' } + return { errmsg = connection_failure_errmsg('Send expression failed.') } end print(vim.fn.rpcrequest(rcid, 'nvim_eval', args[2])) return { should_exit = true, tabbed = false } @@ -669,7 +681,7 @@ function vim._cs_remote(rcid, args) if rcid == 0 then if not f_silent then - vim.cmd('echohl WarningMsg | echomsg "E247: Remote server does not exist. Editing locally" | echohl None') + vim.notify(connection_failure_errmsg("Editing locally"), vim.log.levels.WARN) end else local command = {} -- cgit