From 1fe1bb084d0099fc4f9bfdc11189485d0f74b75a Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Mon, 19 Dec 2022 16:37:45 +0000 Subject: refactor(options): deprecate nvim[_buf|_win]_[gs]et_option Co-authored-by: zeertzjq Co-authored-by: famiu --- test/functional/ui/inccommand_user_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/functional/ui/inccommand_user_spec.lua') diff --git a/test/functional/ui/inccommand_user_spec.lua b/test/functional/ui/inccommand_user_spec.lua index 43e9b94feb..6329ece40a 100644 --- a/test/functional/ui/inccommand_user_spec.lua +++ b/test/functional/ui/inccommand_user_spec.lua @@ -391,7 +391,7 @@ describe("'inccommand' for user commands", function() vim.api.nvim_create_user_command('Replace', function() end, { nargs = '*', preview = function() - vim.api.nvim_set_option('inccommand', 'split') + vim.api.nvim_set_option_value('inccommand', 'split', {}) return 2 end, }) -- cgit From 9dd48f7832f4656af4a2579368641268bb6399e7 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 29 May 2023 08:44:52 +0800 Subject: fix(substitute): properly check if preview is needed (#23809) --- test/functional/ui/inccommand_user_spec.lua | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'test/functional/ui/inccommand_user_spec.lua') diff --git a/test/functional/ui/inccommand_user_spec.lua b/test/functional/ui/inccommand_user_spec.lua index 6329ece40a..9cc6e095c5 100644 --- a/test/functional/ui/inccommand_user_spec.lua +++ b/test/functional/ui/inccommand_user_spec.lua @@ -401,6 +401,40 @@ describe("'inccommand' for user commands", function() feed('e') assert_alive() end) + + it('no crash when adding highlight after :substitute #21495', function() + command('set inccommand=nosplit') + exec_lua([[ + vim.api.nvim_create_user_command("Crash", function() end, { + preview = function(_, preview_ns, _) + vim.cmd("%s/text/cats/g") + vim.api.nvim_buf_add_highlight(0, preview_ns, "Search", 0, 0, -1) + return 1 + end, + }) + ]]) + feed(':C') + screen:expect([[ + {1: cats on line 1} | + more cats on line 2 | + oh no, even more cats | + will the cats ever stop | + oh well | + did the cats stop | + why won't it stop | + make the cats stop | + | + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + :C^ | + ]]) + assert_alive() + end) end) describe("'inccommand' with multiple buffers", function() -- cgit From 643bea31b8673f5db70816ecac61f76087019fb5 Mon Sep 17 00:00:00 2001 From: Alexandre Teoi Date: Wed, 26 Jul 2023 00:22:57 -0300 Subject: fix(inccommand): restrict cmdpreview undo calls (#24289) Problem: The cmdpreview saved undo nodes on cmdpreview_prepare() from ex_getln.c may become invalid (free) if the preview function makes undo operations, causing heap-use-after-free errors. Solution: Save the buffer undo list on cmdpreview_prepare)_ and start a new empty one. On cmdpreview_restore_state(), undo all the entries in the new undo list and restore the original one. With this approach, the preview function will be allowed to undo only its own changes. Fix #20036 Fix #20248 --- test/functional/ui/inccommand_user_spec.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'test/functional/ui/inccommand_user_spec.lua') diff --git a/test/functional/ui/inccommand_user_spec.lua b/test/functional/ui/inccommand_user_spec.lua index 9cc6e095c5..8a1030fa25 100644 --- a/test/functional/ui/inccommand_user_spec.lua +++ b/test/functional/ui/inccommand_user_spec.lua @@ -435,6 +435,28 @@ describe("'inccommand' for user commands", function() ]]) assert_alive() end) + + it("no crash if preview callback executes undo", function() + command('set inccommand=nosplit') + exec_lua([[ + vim.api.nvim_create_user_command('Foo', function() end, { + nargs = '?', + preview = function(_, _, _) + vim.cmd.undo() + end, + }) + ]]) + + -- Clear undo history + command('set undolevels=-1') + feed('ggyyp') + command('set undolevels=1000') + + feed('yypp:Fo') + assert_alive() + feed(':Fo') + assert_alive() + end) end) describe("'inccommand' with multiple buffers", function() -- cgit From 14d047ad2f448885de39966d1963f15d3fa21089 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 26 Jul 2023 11:58:26 +0800 Subject: test(inccommand): add a test for #20248 (#24489) --- test/functional/ui/inccommand_user_spec.lua | 56 ++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'test/functional/ui/inccommand_user_spec.lua') diff --git a/test/functional/ui/inccommand_user_spec.lua b/test/functional/ui/inccommand_user_spec.lua index 8a1030fa25..62c4cac709 100644 --- a/test/functional/ui/inccommand_user_spec.lua +++ b/test/functional/ui/inccommand_user_spec.lua @@ -436,7 +436,7 @@ describe("'inccommand' for user commands", function() assert_alive() end) - it("no crash if preview callback executes undo", function() + it('no crash if preview callback executes undo #20036', function() command('set inccommand=nosplit') exec_lua([[ vim.api.nvim_create_user_command('Foo', function() end, { @@ -457,6 +457,60 @@ describe("'inccommand' for user commands", function() feed(':Fo') assert_alive() end) + + it('breaking undo chain in Insert mode works properly #20248', function() + command('set inccommand=nosplit') + command('inoremap . .u') + exec_lua([[ + vim.api.nvim_create_user_command('Test', function() end, { + nargs = 1, + preview = function(opts, _, _) + vim.cmd('norm i' .. opts.args) + return 1 + end + }) + ]]) + feed(':Test a.a.a.a.') + screen:expect([[ + text on line 1 | + more text on line 2 | + oh no, even more text | + will the text ever stop | + oh well | + did the text stop | + why won't it stop | + make the text stop | + a.a.a.a. | + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + :Test a.a.a.a.^ | + ]]) + feed('') + screen:expect([[ + text on line 1 | + more text on line 2 | + oh no, even more text | + will the text ever stop | + oh well | + did the text stop | + why won't it stop | + make the text stop | + ^ | + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + | + ]]) + end) end) describe("'inccommand' with multiple buffers", function() -- cgit From ef44e597294e4d0d9128ef69b6aa7481a54e17cb Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 5 Aug 2023 22:42:34 +0800 Subject: fix(inccommand): don't set an invalid 'undolevels' value (#24575) Problem: Cannot break undo by setting 'undolevels' to itself in 'inccommand' preview callback. Solution: Don't set an invalid 'undolevels' value. Co-authored-by: Michael Henry --- test/functional/ui/inccommand_user_spec.lua | 38 ++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'test/functional/ui/inccommand_user_spec.lua') diff --git a/test/functional/ui/inccommand_user_spec.lua b/test/functional/ui/inccommand_user_spec.lua index 62c4cac709..da7508fad1 100644 --- a/test/functional/ui/inccommand_user_spec.lua +++ b/test/functional/ui/inccommand_user_spec.lua @@ -239,7 +239,8 @@ describe("'inccommand' for user commands", function() [1] = {background = Screen.colors.Yellow1}, [2] = {foreground = Screen.colors.Blue1, bold = true}, [3] = {reverse = true}, - [4] = {reverse = true, bold = true} + [4] = {reverse = true, bold = true}, + [5] = {foreground = Screen.colors.Blue}, }) screen:attach() exec_lua(setup_replace_cmd) @@ -458,9 +459,8 @@ describe("'inccommand' for user commands", function() assert_alive() end) - it('breaking undo chain in Insert mode works properly #20248', function() + local function test_preview_break_undo() command('set inccommand=nosplit') - command('inoremap . .u') exec_lua([[ vim.api.nvim_create_user_command('Test', function() end, { nargs = 1, @@ -490,6 +490,26 @@ describe("'inccommand' for user commands", function() {2:~ }| :Test a.a.a.a.^ | ]]) + feed('u') + screen:expect([[ + text on line 1 | + more text on line 2 | + oh no, even more text | + will the text ever stop | + oh well | + did the text stop | + why won't it stop | + make the text stop | + a.a.a. | + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + :Test a.a.a.a.{5:^[}u^ | + ]]) feed('') screen:expect([[ text on line 1 | @@ -510,6 +530,18 @@ describe("'inccommand' for user commands", function() {2:~ }| | ]]) + end + + describe('breaking undo chain in Insert mode works properly', function() + it('when using i_CTRL-G_u #20248', function() + command('inoremap . .u') + test_preview_break_undo() + end) + + it('when setting &l:undolevels to itself #24575', function() + command('inoremap . .let &l:undolevels = &l:undolevels') + test_preview_break_undo() + end) end) end) -- cgit