Skip to content

Commit 0bc7324

Browse files
committed
(mini.deps) Allow setup to show folds in autocommand/ftplugin.
1 parent de85dec commit 0bc7324

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

lua/mini/deps.lua

+25-20
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ H.clean_confirm = function(paths)
12411241
if #paths_to_delete == 0 then return H.notify('Nothing to delete') end
12421242
H.clean_delete(paths_to_delete)
12431243
end
1244-
H.show_confirm_buf(lines, 'mini-deps://confirm-clean', finish_clean)
1244+
H.show_confirm_buf(lines, { name = 'mini-deps://confirm-clean', exec_on_write = finish_clean })
12451245

12461246
-- Define basic highlighting
12471247
vim.cmd('syntax region MiniDepsHint start="^\\%1l" end="\\%' .. n_header .. 'l$"')
@@ -1364,25 +1364,11 @@ H.update_feedback_confirm = function(lines)
13641364
MiniDeps.update(names, { force = true, offline = true })
13651365
end
13661366

1367-
H.show_confirm_buf(report, 'mini-deps://confirm-update', finish_update)
1367+
H.show_confirm_buf(report, { name = 'mini-deps://confirm-update', exec_on_write = finish_update, setup_folds = true })
13681368

13691369
-- Define basic highlighting
13701370
vim.cmd('syntax region MiniDepsHint start="^\\%1l" end="\\%' .. n_header .. 'l$"')
13711371
H.update_add_syntax()
1372-
1373-
-- Enable folding
1374-
local is_title = function(l) return l:find('^%-%-%-') or l:find('^%+%+%+') or l:find('^%!%!%!') end
1375-
--stylua: ignore
1376-
MiniDeps._confirm_foldexpr = function(lnum)
1377-
if lnum == 1 then return 0 end
1378-
if is_title(vim.fn.getline(lnum - 1)) then return 1 end
1379-
if is_title(vim.fn.getline(lnum + 1)) then return 0 end
1380-
return '='
1381-
end
1382-
-- - Use `:setlocal` for these options to not be inherited if some other
1383-
-- buffer is opened in the same window.
1384-
vim.cmd('setlocal foldenable foldmethod=expr foldlevel=999')
1385-
vim.cmd('setlocal foldexpr=v:lua.MiniDeps._confirm_foldexpr(v:lnum)')
13861372
end
13871373

13881374
H.update_add_syntax = function()
@@ -1411,13 +1397,12 @@ H.update_feedback_log = function(lines)
14111397
end
14121398

14131399
-- Confirm --------------------------------------------------------------------
1414-
H.show_confirm_buf = function(lines, name, exec_on_write)
1400+
H.show_confirm_buf = function(lines, opts)
14151401
-- Show buffer
14161402
local buf_id = vim.api.nvim_create_buf(true, true)
1417-
H.buf_set_name(buf_id, name)
1403+
H.buf_set_name(buf_id, opts.name)
14181404
vim.api.nvim_buf_set_lines(buf_id, 0, -1, false, lines)
14191405
vim.cmd('tab sbuffer ' .. buf_id)
1420-
vim.bo.buftype, vim.bo.filetype, vim.bo.modified = 'acwrite', 'minideps-confirm', false
14211406
local tab_num, win_id = vim.api.nvim_tabpage_get_number(0), vim.api.nvim_get_current_win()
14221407

14231408
local delete_buffer = vim.schedule_wrap(function()
@@ -1426,10 +1411,27 @@ H.show_confirm_buf = function(lines, name, exec_on_write)
14261411
vim.cmd('redraw')
14271412
end)
14281413

1414+
-- Define folding
1415+
local is_title = function(l) return l:find('^%-%-%-') or l:find('^%+%+%+') or l:find('^%!%!%!') end
1416+
--stylua: ignore
1417+
MiniDeps._confirm_foldexpr = function(lnum)
1418+
if lnum == 1 then return 0 end
1419+
if is_title(vim.fn.getline(lnum - 1)) then return 1 end
1420+
if is_title(vim.fn.getline(lnum + 1)) then return 0 end
1421+
return '='
1422+
end
1423+
1424+
-- Possibly set up folding. Use `:setlocal` for these options to not be
1425+
-- inherited if some other buffer is opened in the same window.
1426+
if opts.setup_folds then
1427+
vim.cmd('setlocal foldenable foldmethod=expr foldlevel=999')
1428+
vim.cmd('setlocal foldexpr=v:lua.MiniDeps._confirm_foldexpr(v:lnum)')
1429+
end
1430+
14291431
-- Define action on accepting confirm
14301432
local finish = function()
14311433
MiniDeps._confirm_foldexpr = nil
1432-
exec_on_write(buf_id)
1434+
opts.exec_on_write(buf_id)
14331435
delete_buffer()
14341436
end
14351437
-- - Use `nested` to allow other events (`WinEnter` for 'mini.statusline')
@@ -1444,6 +1446,9 @@ H.show_confirm_buf = function(lines, name, exec_on_write)
14441446
delete_buffer()
14451447
end
14461448
cancel_au_id = vim.api.nvim_create_autocmd('WinClosed', { nested = true, callback = on_cancel })
1449+
1450+
-- Set buffer-local options last (so that user autocmmands could override)
1451+
vim.bo.buftype, vim.bo.filetype, vim.bo.modified = 'acwrite', 'minideps-confirm', false
14471452
end
14481453

14491454
-- CLI ------------------------------------------------------------------------

tests/test_deps.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,9 @@ T['update()']['can fold in cofirm buffer'] = function()
13471347
child.o.foldlevel = 0
13481348
child.o.foldtext = '" >> ".getline(v:foldstart)'
13491349

1350+
-- Should be possible to automatically show folds on start
1351+
child.cmd('au FileType minideps-confirm setlocal foldlevel=0')
1352+
13501353
add('user/plugin_1')
13511354
add('user/plugin_2')
13521355
add('user/plugin_3')
@@ -1382,7 +1385,6 @@ T['update()']['can fold in cofirm buffer'] = function()
13821385
update()
13831386
mock_hide_path(test_dir_absolute)
13841387

1385-
child.type_keys('gg', 'zM')
13861388
child.expect_screenshot()
13871389

13881390
-- Should not preserve fold options in new buffer in same window

0 commit comments

Comments
 (0)