2017-02-28 08:19:45 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
|
|
|
|
|
|
|
local clear = helpers.clear
|
|
|
|
local insert = helpers.insert
|
|
|
|
local feed = helpers.feed
|
|
|
|
local expect = helpers.expect
|
2023-04-06 18:29:12 -07:00
|
|
|
local command = helpers.command
|
2017-02-27 02:46:50 -07:00
|
|
|
local funcs = helpers.funcs
|
|
|
|
local eq = helpers.eq
|
2017-02-28 08:19:45 -07:00
|
|
|
|
|
|
|
describe('Folds', function()
|
2017-03-30 16:21:26 -07:00
|
|
|
local tempfname = 'Xtest-fold.txt'
|
2023-04-06 18:29:12 -07:00
|
|
|
|
|
|
|
setup(clear)
|
|
|
|
before_each(function() command('bwipe! | new') end)
|
2017-03-30 16:21:26 -07:00
|
|
|
after_each(function() os.remove(tempfname) end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-02-28 08:19:45 -07:00
|
|
|
it('manual folding adjusts with filter', function()
|
|
|
|
insert([[
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20]])
|
2023-04-06 18:29:12 -07:00
|
|
|
command('4,$fold')
|
|
|
|
command('%foldopen')
|
|
|
|
command('10,$fold')
|
|
|
|
command('%foldopen')
|
|
|
|
command('1,8! cat')
|
2017-02-28 08:19:45 -07:00
|
|
|
feed('5ggzdzMGdd')
|
|
|
|
expect([[
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9]])
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-02-27 02:46:50 -07:00
|
|
|
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
|
2023-04-27 00:51:44 -07:00
|
|
|
command('setlocal foldmethod=indent')
|
|
|
|
command('setlocal foldmethod=manual')
|
2017-02-27 02:46:50 -07:00
|
|
|
-- Ensure that all folds will get closed (makes it easier to test the
|
|
|
|
-- length of folds).
|
2023-04-27 00:51:44 -07:00
|
|
|
command('setlocal foldminlines=0')
|
2017-02-27 02:46:50 -07:00
|
|
|
-- Start with all folds open (so :move ranges aren't affected by closed
|
|
|
|
-- folds).
|
2023-04-06 18:29:12 -07:00
|
|
|
command('%foldopen!')
|
2017-02-27 02:46:50 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function get_folds()
|
|
|
|
local rettab = {}
|
|
|
|
for i = 1, funcs.line('$') do
|
2023-04-06 18:29:12 -07:00
|
|
|
table.insert(rettab, funcs.foldlevel(i))
|
2017-02-27 02:46:50 -07:00
|
|
|
end
|
|
|
|
return rettab
|
|
|
|
end
|
|
|
|
|
|
|
|
local function test_move_indent(insert_string, move_command)
|
|
|
|
-- 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)
|
2023-04-06 18:29:12 -07:00
|
|
|
command(move_command)
|
2017-02-27 02:46:50 -07:00
|
|
|
local after_move_folds = get_folds()
|
|
|
|
-- Doesn't change anything, but does call foldUpdateAll()
|
2023-04-27 00:51:44 -07:00
|
|
|
command('setlocal foldminlines=0')
|
2017-02-27 02:46:50 -07:00
|
|
|
eq(after_move_folds, get_folds())
|
|
|
|
-- Set up the buffer with insert_string for the manual fold testing.
|
2023-04-06 18:29:12 -07:00
|
|
|
command('enew!')
|
2017-02-27 02:46:50 -07:00
|
|
|
insert(insert_string)
|
|
|
|
manually_fold_indent()
|
2023-04-06 18:29:12 -07:00
|
|
|
command(move_command)
|
2017-02-27 02:46:50 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
it('neither closes nor corrupts folds', function()
|
|
|
|
test_move_indent([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a]], '7,12m0')
|
|
|
|
expect([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a]])
|
|
|
|
-- lines are not closed, folds are correct
|
|
|
|
for i = 1,funcs.line('$') do
|
|
|
|
eq(-1, funcs.foldclosed(i))
|
|
|
|
if i == 1 or i == 7 or i == 13 then
|
2023-04-06 18:29:12 -07:00
|
|
|
eq(0, funcs.foldlevel(i))
|
2017-02-27 02:46:50 -07:00
|
|
|
elseif i == 4 then
|
2023-04-06 18:29:12 -07:00
|
|
|
eq(2, funcs.foldlevel(i))
|
2017-02-27 02:46:50 -07:00
|
|
|
else
|
2023-04-06 18:29:12 -07:00
|
|
|
eq(1, funcs.foldlevel(i))
|
2017-02-27 02:46:50 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- folds are not corrupted
|
|
|
|
feed('zM')
|
2023-04-06 18:29:12 -07:00
|
|
|
eq(6, funcs.foldclosedend(2))
|
|
|
|
eq(12, funcs.foldclosedend(8))
|
|
|
|
eq(18, funcs.foldclosedend(14))
|
2017-02-27 02:46:50 -07:00
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-02-27 02:46:50 -07:00
|
|
|
it("doesn't split a fold when the move is within it", function()
|
|
|
|
test_move_indent([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a]], '5m6')
|
|
|
|
eq({0, 1, 1, 2, 2, 2, 2, 1, 1, 0}, get_folds())
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-02-27 02:46:50 -07:00
|
|
|
it('truncates folds that end in the moved range', function()
|
|
|
|
test_move_indent([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a]], '4,5m6')
|
|
|
|
eq({0, 1, 2, 0, 0, 0, 0}, get_folds())
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-02-27 02:46:50 -07:00
|
|
|
it('moves folds that start between moved range and destination', function()
|
|
|
|
test_move_indent([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a]], '3,4m$')
|
|
|
|
eq({0, 1, 1, 0, 0, 1, 2, 1, 0, 0, 1, 0, 0}, get_folds())
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-02-27 02:46:50 -07:00
|
|
|
it('does not affect folds outside changed lines', function()
|
|
|
|
test_move_indent([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a]], '4m5')
|
|
|
|
eq({1, 1, 1, 0, 0, 0, 1, 1, 1}, get_folds())
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-02-27 02:46:50 -07:00
|
|
|
it('moves and truncates folds that start in moved range', function()
|
|
|
|
test_move_indent([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a]], '1,3m7')
|
|
|
|
eq({0, 0, 0, 0, 0, 1, 2, 0, 0, 0}, get_folds())
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-02-27 02:46:50 -07:00
|
|
|
it('breaks a fold when moving text into it', function()
|
|
|
|
test_move_indent([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a]], '$m4')
|
|
|
|
eq({0, 1, 2, 2, 0, 0, 0}, get_folds())
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-02-27 02:46:50 -07:00
|
|
|
it('adjusts correctly when moving a range backwards', function()
|
|
|
|
test_move_indent([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a]], '2,3m0')
|
|
|
|
eq({1, 2, 0, 0, 0}, get_folds())
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-04-16 12:15:50 -07:00
|
|
|
it('handles shifting all remaining folds', function()
|
|
|
|
test_move_indent([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a]], '13m7')
|
|
|
|
eq({1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 0}, get_folds())
|
|
|
|
end)
|
2017-02-27 02:46:50 -07:00
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-03-30 16:21:26 -07:00
|
|
|
it('updates correctly on :read', function()
|
|
|
|
-- luacheck: ignore 621
|
|
|
|
helpers.write_file(tempfname, [[
|
|
|
|
a
|
|
|
|
|
|
|
|
|
|
|
|
a]])
|
|
|
|
insert([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
]])
|
2023-04-27 00:51:44 -07:00
|
|
|
command('setlocal foldmethod=indent')
|
2023-04-06 18:29:12 -07:00
|
|
|
command('2')
|
|
|
|
command('%foldopen')
|
|
|
|
command('read ' .. tempfname)
|
2017-03-30 16:21:26 -07:00
|
|
|
-- Just to check we have the correct file text.
|
|
|
|
expect([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
|
|
|
|
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
]])
|
|
|
|
for i = 1,2 do
|
|
|
|
eq(1, funcs.foldlevel(i))
|
|
|
|
end
|
|
|
|
for i = 3,5 do
|
|
|
|
eq(0, funcs.foldlevel(i))
|
|
|
|
end
|
|
|
|
for i = 6,8 do
|
|
|
|
eq(1, funcs.foldlevel(i))
|
|
|
|
end
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-03-30 16:21:26 -07:00
|
|
|
it('combines folds when removing separating space', function()
|
|
|
|
-- luacheck: ignore 621
|
|
|
|
insert([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
]])
|
2023-04-27 00:51:44 -07:00
|
|
|
command('setlocal foldmethod=indent')
|
2023-04-06 18:29:12 -07:00
|
|
|
command('3,5d')
|
2017-03-30 16:21:26 -07:00
|
|
|
eq(5, funcs.foldclosedend(1))
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-03-30 16:21:26 -07:00
|
|
|
it("doesn't combine folds that have a specified end", function()
|
|
|
|
insert([[
|
|
|
|
{{{
|
|
|
|
}}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{{{
|
|
|
|
|
|
|
|
}}}
|
|
|
|
]])
|
2023-04-27 00:51:44 -07:00
|
|
|
command('setlocal foldmethod=marker')
|
2023-04-06 18:29:12 -07:00
|
|
|
command('3,5d')
|
|
|
|
command('%foldclose')
|
2017-03-30 16:21:26 -07:00
|
|
|
eq(2, funcs.foldclosedend(1))
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
2017-03-30 16:21:26 -07:00
|
|
|
it('splits folds according to >N and <N with foldexpr', function()
|
|
|
|
helpers.source([[
|
|
|
|
function TestFoldExpr(lnum)
|
|
|
|
let thisline = getline(a:lnum)
|
|
|
|
if thisline == 'a'
|
|
|
|
return 1
|
|
|
|
elseif thisline == 'b'
|
|
|
|
return 0
|
|
|
|
elseif thisline == 'c'
|
|
|
|
return '<1'
|
|
|
|
elseif thisline == 'd'
|
|
|
|
return '>1'
|
|
|
|
endif
|
|
|
|
return 0
|
|
|
|
endfunction
|
|
|
|
]])
|
|
|
|
helpers.write_file(tempfname, [[
|
|
|
|
b
|
|
|
|
b
|
|
|
|
a
|
|
|
|
a
|
|
|
|
d
|
|
|
|
a
|
|
|
|
a
|
|
|
|
c]])
|
|
|
|
insert([[
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
a
|
|
|
|
]])
|
2023-04-27 00:51:44 -07:00
|
|
|
command('setlocal foldmethod=expr foldexpr=TestFoldExpr(v:lnum)')
|
2023-04-06 18:29:12 -07:00
|
|
|
command('2')
|
|
|
|
command('foldopen')
|
|
|
|
command('read ' .. tempfname)
|
|
|
|
command('%foldclose')
|
2017-03-30 16:21:26 -07:00
|
|
|
eq(2, funcs.foldclosedend(1))
|
|
|
|
eq(0, funcs.foldlevel(3))
|
|
|
|
eq(0, funcs.foldlevel(4))
|
|
|
|
eq(6, funcs.foldclosedend(5))
|
|
|
|
eq(10, funcs.foldclosedend(7))
|
|
|
|
eq(14, funcs.foldclosedend(11))
|
|
|
|
end)
|
2023-04-06 18:29:12 -07:00
|
|
|
|
|
|
|
it('no folds remain if :delete makes buffer empty #19671', function()
|
2023-04-27 00:51:44 -07:00
|
|
|
command('setlocal foldmethod=manual')
|
2023-04-06 18:29:12 -07:00
|
|
|
funcs.setline(1, {'foo', 'bar', 'baz'})
|
|
|
|
command('2,3fold')
|
|
|
|
command('%delete')
|
|
|
|
eq(0, funcs.foldlevel(1))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('multibyte fold markers work #20438', function()
|
2023-04-27 00:51:44 -07:00
|
|
|
command('setlocal foldmethod=marker foldmarker=«,» commentstring=/*%s*/')
|
2023-04-06 18:29:12 -07:00
|
|
|
insert([[
|
|
|
|
bbbbb
|
|
|
|
bbbbb
|
|
|
|
bbbbb]])
|
|
|
|
feed('zfgg')
|
|
|
|
expect([[
|
|
|
|
bbbbb/*«*/
|
|
|
|
bbbbb
|
|
|
|
bbbbb/*»*/]])
|
|
|
|
eq(1, funcs.foldlevel(1))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('updates correctly with indent method and visual blockwise insertion #22898', function()
|
2023-04-06 09:08:46 -07:00
|
|
|
insert([[
|
|
|
|
a
|
|
|
|
b
|
|
|
|
]])
|
2023-04-27 00:51:44 -07:00
|
|
|
command('setlocal foldmethod=indent shiftwidth=2')
|
2023-04-06 09:08:46 -07:00
|
|
|
feed('gg0<C-v>jI <Esc>') -- indent both lines using visual blockwise mode
|
|
|
|
eq(1, funcs.foldlevel(1))
|
|
|
|
eq(1, funcs.foldlevel(2))
|
|
|
|
end)
|
2017-02-28 08:19:45 -07:00
|
|
|
end)
|