From e1177be363f84f5f4f34c21b760bc47f70d5fa48 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 19 Aug 2019 23:43:19 +0200 Subject: API: nvim_put #6819 --- test/functional/api/vim_spec.lua | 43 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'test/functional/api/vim_spec.lua') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 0cd81619c1..09c297940c 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -5,6 +5,7 @@ local NIL = helpers.NIL local clear, nvim, eq, neq = helpers.clear, helpers.nvim, helpers.eq, helpers.neq local command = helpers.command local eval = helpers.eval +local expect = helpers.expect local funcs = helpers.funcs local iswin = helpers.iswin local meth_pcall = helpers.meth_pcall @@ -365,6 +366,40 @@ describe('API', function() end) end) + describe('nvim_put', function() + it('inserts text', function() + -- linewise + nvim('put', {'line 1','line 2','line 3'}, 'l', false) + expect([[ + + line 1 + line 2 + line 3]]) + command('%delete _') + -- charwise + nvim('put', {'line 1','line 2','line 3'}, 'c', false) + expect([[ + line 1 + line 2 + line 3]]) + -- blockwise + nvim('put', {'AA','BB'}, 'b', false) + expect([[ + lAAine 1 + lBBine 2 + line 3]]) + command('%delete _') + -- Empty lines list. + nvim('put', {}, 'c', false) + expect([[]]) + -- Single empty line. + nvim('put', {''}, 'c', false) + expect([[ + ]]) + eq('', nvim('eval', 'v:errmsg')) + end) + end) + describe('nvim_strwidth', function() it('works', function() eq(3, nvim('strwidth', 'abc')) @@ -626,12 +661,12 @@ describe('API', function() -- Make any RPC request (can be non-async: op-pending does not block). nvim('get_current_buf') -- Buffer should not change. - helpers.expect([[ + expect([[ FIRST LINE SECOND LINE]]) -- Now send input to complete the operator. nvim('input', 'j') - helpers.expect([[ + expect([[ first line second line]]) end) @@ -664,7 +699,7 @@ describe('API', function() nvim('get_api_info') -- Send input to complete the mapping. nvim('input', 'd') - helpers.expect([[ + expect([[ FIRST LINE SECOND LINE]]) eq('it worked...', helpers.eval('g:foo')) @@ -680,7 +715,7 @@ describe('API', function() nvim('get_api_info') -- Send input to complete the mapping. nvim('input', 'x') - helpers.expect([[ + expect([[ FIRST LINE SECOND LINfooE]]) end) -- cgit From 613296936ba30ae73f3391c2e3c36096f3703c06 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 20 Aug 2019 22:41:21 +0200 Subject: API: nvim_put: always PUT_CURSEND Fixes strange behavior where sometimes the buffer contents of a series of paste chunks (vim._paste) would be out-of-order. Now the tui_spec.lua screen-tests are much more reliable. But they still sometimes fail because of off-by-one cursor (caused by "typeahead race" resulting in wrong mode; fixed later in this patch-series). --- test/functional/api/vim_spec.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'test/functional/api/vim_spec.lua') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 09c297940c..20046147b8 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -382,6 +382,7 @@ describe('API', function() line 1 line 2 line 3]]) + command('1') -- blockwise nvim('put', {'AA','BB'}, 'b', false) expect([[ -- cgit From 93e5f0235b8e85423d0284231661ba4b0d7caa07 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 20 Aug 2019 23:53:13 +0200 Subject: API: nvim_put: "follow" parameter --- test/functional/api/vim_spec.lua | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'test/functional/api/vim_spec.lua') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 20046147b8..3f3d9b74bb 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -369,34 +369,53 @@ describe('API', function() describe('nvim_put', function() it('inserts text', function() -- linewise - nvim('put', {'line 1','line 2','line 3'}, 'l', false) + nvim('put', {'line 1','line 2','line 3'}, 'l', true, true) expect([[ line 1 line 2 line 3]]) + eq({0,4,1,0}, funcs.getpos('.')) command('%delete _') -- charwise - nvim('put', {'line 1','line 2','line 3'}, 'c', false) + nvim('put', {'line 1','line 2','line 3'}, 'c', true, false) expect([[ line 1 line 2 line 3]]) - command('1') + eq({0,1,1,0}, funcs.getpos('.')) -- follow=false -- blockwise - nvim('put', {'AA','BB'}, 'b', false) + nvim('put', {'AA','BB'}, 'b', true, true) expect([[ lAAine 1 lBBine 2 line 3]]) + eq({0,2,4,0}, funcs.getpos('.')) command('%delete _') -- Empty lines list. - nvim('put', {}, 'c', false) + nvim('put', {}, 'c', true, true) + eq({0,1,1,0}, funcs.getpos('.')) expect([[]]) -- Single empty line. - nvim('put', {''}, 'c', false) + nvim('put', {''}, 'c', true, true) + eq({0,1,1,0}, funcs.getpos('.')) expect([[ ]]) + nvim('put', {'AB'}, 'c', true, true) + -- after=false, follow=true + nvim('put', {'line 1','line 2'}, 'c', false, true) + expect([[ + Aline 1 + line 2B]]) + eq({0,2,7,0}, funcs.getpos('.')) + command('%delete _') + nvim('put', {'AB'}, 'c', true, true) + -- after=false, follow=false + nvim('put', {'line 1','line 2'}, 'c', false, false) + expect([[ + Aline 1 + line 2B]]) + eq({0,1,2,0}, funcs.getpos('.')) eq('', nvim('eval', 'v:errmsg')) end) end) -- cgit From eacc70fb3ebae6d76112ab10647a42339f5f223f Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 24 Aug 2019 13:54:27 +0200 Subject: API: nvim_paste --- test/functional/api/vim_spec.lua | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'test/functional/api/vim_spec.lua') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 3f3d9b74bb..212c4f4300 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -366,7 +366,44 @@ describe('API', function() end) end) + describe('nvim_paste', function() + it('validates args', function() + expect_err('Invalid phase: %-2', request, + 'nvim_paste', 'foo', -2) + expect_err('Invalid phase: 4', request, + 'nvim_paste', 'foo', 4) + end) + it('non-streaming', function() + -- With final "\n". + nvim('paste', 'line 1\nline 2\nline 3\n', -1) + expect([[ + line 1 + line 2 + line 3 + ]]) + -- Cursor follows the paste. + eq({0,4,1,0}, funcs.getpos('.')) + eq(false, nvim('get_option', 'paste')) + command('%delete _') + -- Without final "\n". + nvim('paste', 'line 1\nline 2\nline 3', -1) + expect([[ + line 1 + line 2 + line 3]]) + -- Cursor follows the paste. + eq({0,3,6,0}, funcs.getpos('.')) + eq(false, nvim('get_option', 'paste')) + end) + end) + describe('nvim_put', function() + it('validates args', function() + expect_err('Invalid lines %(expected array of strings%)', request, + 'nvim_put', {42}, 'l', false, false) + expect_err("Invalid type: 'x'", request, + 'nvim_put', {'foo'}, 'x', false, false) + end) it('inserts text', function() -- linewise nvim('put', {'line 1','line 2','line 3'}, 'l', true, true) -- cgit From ed60015266356b3c0c42aa34698d9287f22fcba1 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 26 Aug 2019 20:57:57 +0200 Subject: paste: handle vim.paste() failure - Show error only once per "paste stream". - Drain remaining chunks until phase=3. - Lay groundwork for "cancel". - Constrain semantics of "cancel" to mean "client must stop"; it is unrelated to presence of error(s). --- test/functional/api/vim_spec.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'test/functional/api/vim_spec.lua') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 212c4f4300..01e4a3a1a0 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -395,6 +395,11 @@ describe('API', function() eq({0,3,6,0}, funcs.getpos('.')) eq(false, nvim('get_option', 'paste')) end) + it('vim.paste() failure', function() + nvim('execute_lua', 'vim._paste = (function(lines, phase) error("fake fail") end)', {}) + expect_err([[Error executing lua: %[string "%"]:1: fake fail]], + request, 'nvim_paste', 'line 1\nline 2\nline 3', 1) + end) end) describe('nvim_put', function() @@ -455,6 +460,25 @@ describe('API', function() eq({0,1,2,0}, funcs.getpos('.')) eq('', nvim('eval', 'v:errmsg')) end) + + it('detects charwise/linewise text (empty {type})', function() + -- linewise (final item is empty string) + nvim('put', {'line 1','line 2','line 3',''}, '', true, true) + expect([[ + + line 1 + line 2 + line 3]]) + eq({0,4,1,0}, funcs.getpos('.')) + command('%delete _') + -- charwise (final item is non-empty) + nvim('put', {'line 1','line 2','line 3'}, '', true, true) + expect([[ + line 1 + line 2 + line 3]]) + eq({0,3,6,0}, funcs.getpos('.')) + end) end) describe('nvim_strwidth', function() -- cgit From 87389c6a57cf9fa91746503c479cdbea348030b9 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 27 Aug 2019 05:19:25 +0200 Subject: paste: make vim.paste() "public" --- test/functional/api/vim_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/functional/api/vim_spec.lua') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 01e4a3a1a0..47a04795f8 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -396,7 +396,7 @@ describe('API', function() eq(false, nvim('get_option', 'paste')) end) it('vim.paste() failure', function() - nvim('execute_lua', 'vim._paste = (function(lines, phase) error("fake fail") end)', {}) + nvim('execute_lua', 'vim.paste = (function(lines, phase) error("fake fail") end)', {}) expect_err([[Error executing lua: %[string "%"]:1: fake fail]], request, 'nvim_paste', 'line 1\nline 2\nline 3', 1) end) -- cgit From 46aa254bf30d567bd2da4fbfab33bbdcbb111a37 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 27 Aug 2019 05:19:32 +0200 Subject: paste: handle 'nomodifiable' - nvim_paste(): Marshal through luaeval() instead of nvim_execute_lua() because the latter seems to hide some errors. - Handle 'nomodifiable' in `nvim_put()` explicitly. - Require explicit `false` from `vim.paste()` in order to "cancel", otherwise assume true ("continue"). --- test/functional/api/vim_spec.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'test/functional/api/vim_spec.lua') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 47a04795f8..884e07e2c5 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -409,6 +409,11 @@ describe('API', function() expect_err("Invalid type: 'x'", request, 'nvim_put', {'foo'}, 'x', false, false) end) + it("fails if 'nomodifiable'", function() + command('set nomodifiable') + expect_err([[Buffer is not 'modifiable']], request, + 'nvim_put', {'a','b'}, 'l', true, true) + end) it('inserts text', function() -- linewise nvim('put', {'line 1','line 2','line 3'}, 'l', true, true) -- cgit From 3157baed83b7e94f2ff92e6fd97e85dab41a1c94 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 27 Aug 2019 05:19:36 +0200 Subject: API: TRY_WRAP() for "abort-causing non-exception errors" - Introduce TRY_WRAP() until we have an *architectural* solution. - TODO: bfredl idea: prepare error-handling at "top level" (nv_event). - nvim_paste(): Revert luaeval() hack (see parent commit). - With TRY_WRAP() in nvim_put(), 'nomodifiable' error now correctly "bubbles up". --- test/functional/api/vim_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/functional/api/vim_spec.lua') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 884e07e2c5..647fab5c43 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -411,7 +411,7 @@ describe('API', function() end) it("fails if 'nomodifiable'", function() command('set nomodifiable') - expect_err([[Buffer is not 'modifiable']], request, + expect_err([[Vim:E21: Cannot make changes, 'modifiable' is off]], request, 'nvim_put', {'a','b'}, 'l', true, true) end) it('inserts text', function() -- cgit