From 824639c7c17c4870cde83dd28fed7ed6be0ed4c9 Mon Sep 17 00:00:00 2001 From: Brandon Simmons <34775764+simmsbra@users.noreply.github.com> Date: Thu, 6 Apr 2023 11:08:46 -0500 Subject: fix(folds): handle visual blockwise indent insertion correctly (#22898) Previously, the fold information was incorrect because it wasn't being updated during the blockwise insertion. (Solution by zeertzjq) --- test/functional/editor/fold_spec.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test/functional/editor/fold_spec.lua') diff --git a/test/functional/editor/fold_spec.lua b/test/functional/editor/fold_spec.lua index 00e83bedc8..01a30f87bf 100644 --- a/test/functional/editor/fold_spec.lua +++ b/test/functional/editor/fold_spec.lua @@ -359,4 +359,14 @@ a]], '13m7') eq(10, funcs.foldclosedend(7)) eq(14, funcs.foldclosedend(11)) end) + it('updates correctly with indent method and visual blockwise insertion', function() + insert([[ + a + b + ]]) + feed_command('set foldmethod=indent', 'set shiftwidth=2') + feed('gg0jI ') -- indent both lines using visual blockwise mode + eq(1, funcs.foldlevel(1)) + eq(1, funcs.foldlevel(2)) + end) end) -- cgit From 73060f00dd84b2fcfaed74ba061644975707c225 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 7 Apr 2023 09:29:12 +0800 Subject: test: improve editor/fold_spec.lua and editor/put_spec.lua (#22916) - Close and open a new window each time so that window options have their default values in each test. - Change feed_command() to command() as the latter is faster. --- test/functional/editor/fold_spec.lua | 106 +++++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 29 deletions(-) (limited to 'test/functional/editor/fold_spec.lua') diff --git a/test/functional/editor/fold_spec.lua b/test/functional/editor/fold_spec.lua index 01a30f87bf..3115c20410 100644 --- a/test/functional/editor/fold_spec.lua +++ b/test/functional/editor/fold_spec.lua @@ -4,17 +4,17 @@ local clear = helpers.clear local insert = helpers.insert local feed = helpers.feed local expect = helpers.expect -local feed_command = helpers.feed_command +local command = helpers.command local funcs = helpers.funcs -local foldlevel = funcs.foldlevel -local foldclosedend = funcs.foldclosedend local eq = helpers.eq describe('Folds', function() local tempfname = 'Xtest-fold.txt' - clear() - before_each(function() feed_command('enew!') end) + + setup(clear) + before_each(function() command('bwipe! | new') end) after_each(function() os.remove(tempfname) end) + it('manual folding adjusts with filter', function() insert([[ 1 @@ -37,8 +37,11 @@ describe('Folds', function() 18 19 20]]) - feed_command('4,$fold', '%foldopen', '10,$fold', '%foldopen') - feed_command('1,8! cat') + command('4,$fold') + command('%foldopen') + command('10,$fold') + command('%foldopen') + command('1,8! cat') feed('5ggzdzMGdd') expect([[ 1 @@ -51,22 +54,24 @@ describe('Folds', function() 8 9]]) end) + describe('adjusting folds after :move', function() local function manually_fold_indent() -- setting foldmethod twice is a trick to get vim to set the folds for me - feed_command('set foldmethod=indent', 'set foldmethod=manual') + command('set foldmethod=indent') + command('set foldmethod=manual') -- Ensure that all folds will get closed (makes it easier to test the -- length of folds). - feed_command('set foldminlines=0') + command('set foldminlines=0') -- Start with all folds open (so :move ranges aren't affected by closed -- folds). - feed_command('%foldopen!') + command('%foldopen!') end local function get_folds() local rettab = {} for i = 1, funcs.line('$') do - table.insert(rettab, foldlevel(i)) + table.insert(rettab, funcs.foldlevel(i)) end return rettab end @@ -75,16 +80,16 @@ describe('Folds', function() -- This test is easy because we just need to ensure that the resulting -- fold is the same as calculated when creating folds from scratch. insert(insert_string) - feed_command(move_command) + command(move_command) local after_move_folds = get_folds() -- Doesn't change anything, but does call foldUpdateAll() - feed_command('set foldminlines=0') + command('set foldminlines=0') eq(after_move_folds, get_folds()) -- Set up the buffer with insert_string for the manual fold testing. - feed_command('enew!') + command('enew!') insert(insert_string) manually_fold_indent() - feed_command(move_command) + command(move_command) end it('neither closes nor corrupts folds', function() @@ -130,19 +135,20 @@ a for i = 1,funcs.line('$') do eq(-1, funcs.foldclosed(i)) if i == 1 or i == 7 or i == 13 then - eq(0, foldlevel(i)) + eq(0, funcs.foldlevel(i)) elseif i == 4 then - eq(2, foldlevel(i)) + eq(2, funcs.foldlevel(i)) else - eq(1, foldlevel(i)) + eq(1, funcs.foldlevel(i)) end end -- folds are not corrupted feed('zM') - eq(6, foldclosedend(2)) - eq(12, foldclosedend(8)) - eq(18, foldclosedend(14)) + eq(6, funcs.foldclosedend(2)) + eq(12, funcs.foldclosedend(8)) + eq(18, funcs.foldclosedend(14)) end) + it("doesn't split a fold when the move is within it", function() test_move_indent([[ a @@ -157,6 +163,7 @@ a a]], '5m6') eq({0, 1, 1, 2, 2, 2, 2, 1, 1, 0}, get_folds()) end) + it('truncates folds that end in the moved range', function() test_move_indent([[ a @@ -168,6 +175,7 @@ a a]], '4,5m6') eq({0, 1, 2, 0, 0, 0, 0}, get_folds()) end) + it('moves folds that start between moved range and destination', function() test_move_indent([[ a @@ -185,6 +193,7 @@ a a]], '3,4m$') eq({0, 1, 1, 0, 0, 1, 2, 1, 0, 0, 1, 0, 0}, get_folds()) end) + it('does not affect folds outside changed lines', function() test_move_indent([[ a @@ -198,6 +207,7 @@ a a]], '4m5') eq({1, 1, 1, 0, 0, 0, 1, 1, 1}, get_folds()) end) + it('moves and truncates folds that start in moved range', function() test_move_indent([[ a @@ -212,6 +222,7 @@ a a]], '1,3m7') eq({0, 0, 0, 0, 0, 1, 2, 0, 0, 0}, get_folds()) end) + it('breaks a fold when moving text into it', function() test_move_indent([[ a @@ -223,6 +234,7 @@ a a]], '$m4') eq({0, 1, 2, 2, 0, 0, 0}, get_folds()) end) + it('adjusts correctly when moving a range backwards', function() test_move_indent([[ a @@ -232,6 +244,7 @@ a a]], '2,3m0') eq({1, 2, 0, 0, 0}, get_folds()) end) + it('handles shifting all remaining folds', function() test_move_indent([[ a @@ -252,6 +265,7 @@ a]], '13m7') eq({1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 0}, get_folds()) end) end) + it('updates correctly on :read', function() -- luacheck: ignore 621 helpers.write_file(tempfname, [[ @@ -265,8 +279,10 @@ a]], '13m7') a a ]]) - feed_command('set foldmethod=indent', '2', '%foldopen') - feed_command('read ' .. tempfname) + command('set foldmethod=indent') + command('2') + command('%foldopen') + command('read ' .. tempfname) -- Just to check we have the correct file text. expect([[ a @@ -288,6 +304,7 @@ a]], '13m7') eq(1, funcs.foldlevel(i)) end end) + it('combines folds when removing separating space', function() -- luacheck: ignore 621 insert([[ @@ -300,9 +317,11 @@ a]], '13m7') a a ]]) - feed_command('set foldmethod=indent', '3,5d') + command('set foldmethod=indent') + command('3,5d') eq(5, funcs.foldclosedend(1)) end) + it("doesn't combine folds that have a specified end", function() insert([[ {{{ @@ -314,9 +333,12 @@ a]], '13m7') }}} ]]) - feed_command('set foldmethod=marker', '3,5d', '%foldclose') + command('set foldmethod=marker') + command('3,5d') + command('%foldclose') eq(2, funcs.foldclosedend(1)) end) + it('splits folds according to >N and jI ') -- indent both lines using visual blockwise mode eq(1, funcs.foldlevel(1)) eq(1, funcs.foldlevel(2)) -- cgit From d321deb4a9b05e9d81b79ac166274f4a6e7981bf Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 27 Apr 2023 15:51:44 +0800 Subject: test: fix dependencies between test cases (#23343) Discovered using --shuffle argument of busted. --- test/functional/editor/fold_spec.lua | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'test/functional/editor/fold_spec.lua') diff --git a/test/functional/editor/fold_spec.lua b/test/functional/editor/fold_spec.lua index 3115c20410..0ed9d1ba80 100644 --- a/test/functional/editor/fold_spec.lua +++ b/test/functional/editor/fold_spec.lua @@ -58,11 +58,11 @@ describe('Folds', function() describe('adjusting folds after :move', function() local function manually_fold_indent() -- setting foldmethod twice is a trick to get vim to set the folds for me - command('set foldmethod=indent') - command('set foldmethod=manual') + command('setlocal foldmethod=indent') + command('setlocal foldmethod=manual') -- Ensure that all folds will get closed (makes it easier to test the -- length of folds). - command('set foldminlines=0') + command('setlocal foldminlines=0') -- Start with all folds open (so :move ranges aren't affected by closed -- folds). command('%foldopen!') @@ -83,7 +83,7 @@ describe('Folds', function() command(move_command) local after_move_folds = get_folds() -- Doesn't change anything, but does call foldUpdateAll() - command('set foldminlines=0') + command('setlocal foldminlines=0') eq(after_move_folds, get_folds()) -- Set up the buffer with insert_string for the manual fold testing. command('enew!') @@ -279,7 +279,7 @@ a]], '13m7') a a ]]) - command('set foldmethod=indent') + command('setlocal foldmethod=indent') command('2') command('%foldopen') command('read ' .. tempfname) @@ -317,7 +317,7 @@ a]], '13m7') a a ]]) - command('set foldmethod=indent') + command('setlocal foldmethod=indent') command('3,5d') eq(5, funcs.foldclosedend(1)) end) @@ -333,7 +333,7 @@ a]], '13m7') }}} ]]) - command('set foldmethod=marker') + command('setlocal foldmethod=marker') command('3,5d') command('%foldclose') eq(2, funcs.foldclosedend(1)) @@ -372,7 +372,7 @@ a]], '13m7') a a ]]) - command('set foldmethod=expr foldexpr=TestFoldExpr(v:lnum)') + command('setlocal foldmethod=expr foldexpr=TestFoldExpr(v:lnum)') command('2') command('foldopen') command('read ' .. tempfname) @@ -386,7 +386,7 @@ a]], '13m7') end) it('no folds remain if :delete makes buffer empty #19671', function() - command('set foldmethod=manual') + command('setlocal foldmethod=manual') funcs.setline(1, {'foo', 'bar', 'baz'}) command('2,3fold') command('%delete') @@ -394,7 +394,7 @@ a]], '13m7') end) it('multibyte fold markers work #20438', function() - command('set foldmethod=marker foldmarker=«,» commentstring=/*%s*/') + command('setlocal foldmethod=marker foldmarker=«,» commentstring=/*%s*/') insert([[ bbbbb bbbbb @@ -412,7 +412,7 @@ a]], '13m7') a b ]]) - command('set foldmethod=indent shiftwidth=2') + command('setlocal foldmethod=indent shiftwidth=2') feed('gg0jI ') -- indent both lines using visual blockwise mode eq(1, funcs.foldlevel(1)) eq(1, funcs.foldlevel(2)) -- cgit From 58f94861442d182e153ba56b63b5b9845b295d2f Mon Sep 17 00:00:00 2001 From: Brandon Simmons <34775764+simmsbra@users.noreply.github.com> Date: Thu, 20 Jul 2023 19:56:08 -0500 Subject: fix(folds): update folds in Insert mode with fdm=indent (#24402) Previously, when using foldmethod=indent, inserting an unindented line would inadvertently open closed folds below it. As a performance improvement, folds were only updated once, across all lines, after Insert mode was exited. Now, the performance improvement is no longer being used when foldmethod=indent, so folds are updated multiple times during Insert mode, but only across the lines that are changing, which preserves the folds (and their open/close states) instead of recreating them. --- test/functional/editor/fold_spec.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'test/functional/editor/fold_spec.lua') diff --git a/test/functional/editor/fold_spec.lua b/test/functional/editor/fold_spec.lua index 0ed9d1ba80..424ce839a2 100644 --- a/test/functional/editor/fold_spec.lua +++ b/test/functional/editor/fold_spec.lua @@ -7,6 +7,7 @@ local expect = helpers.expect local command = helpers.command local funcs = helpers.funcs local eq = helpers.eq +local neq = helpers.neq describe('Folds', function() local tempfname = 'Xtest-fold.txt' @@ -417,4 +418,31 @@ a]], '13m7') eq(1, funcs.foldlevel(1)) eq(1, funcs.foldlevel(2)) end) + + it("doesn't open folds with indent method when inserting lower foldlevel line", function() + insert([[ + insert an unindented line under this line + keep the lines under this line folded + keep this line folded 1 + keep this line folded 2 + ]]) + command('set foldmethod=indent shiftwidth=2 noautoindent') + eq(1, funcs.foldlevel(1)) + eq(1, funcs.foldlevel(2)) + eq(2, funcs.foldlevel(3)) + eq(2, funcs.foldlevel(4)) + + feed('zo') -- open the outer fold + neq(-1, funcs.foldclosed(3)) -- make sure the inner fold is not open + + feed('gg0oa') -- insert unindented line + + eq(1, funcs.foldlevel(1)) --| insert an unindented line under this line + eq(0, funcs.foldlevel(2)) --|a + eq(1, funcs.foldlevel(3)) --| keep the lines under this line folded + eq(2, funcs.foldlevel(4)) --| keep this line folded 1 + eq(2, funcs.foldlevel(5)) --| keep this line folded 2 + + neq(-1, funcs.foldclosed(4)) -- make sure the inner fold is still not open + end) end) -- cgit